Added project
This commit is contained in:
33
Project1/Assets/Scripts/CoinCollect.cs
Normal file
33
Project1/Assets/Scripts/CoinCollect.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
/*
|
||||
Author: Alexandrov Alexander Alexandrovich
|
||||
Date: 25/10/2019
|
||||
|
||||
Script that implement collect of coins by player's character
|
||||
*/
|
||||
|
||||
public class CoinCollect : MonoBehaviour {
|
||||
|
||||
private GameController gameController;
|
||||
|
||||
void Start() {
|
||||
GameObject gameControllerObject = GameObject.FindGameObjectWithTag("game_controller");
|
||||
if (gameControllerObject != null) {
|
||||
gameController = gameControllerObject.GetComponent<GameController>();
|
||||
} else {
|
||||
Debug.Log("Cannot find 'GameController' script!");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (other.gameObject.CompareTag("Player")) {
|
||||
gameController.AddScore(1);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Project1/Assets/Scripts/CoinCollect.cs.meta
Normal file
11
Project1/Assets/Scripts/CoinCollect.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 63095f8fc32f26caa9c224dfb1dfc0b9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
31
Project1/Assets/Scripts/DeathTrigger.cs
Normal file
31
Project1/Assets/Scripts/DeathTrigger.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/*
|
||||
Author: Alexandrov Alexander Alexandrovich
|
||||
Date: 25/10/2019
|
||||
|
||||
Script that implement death of player character
|
||||
*/
|
||||
public class DeathTrigger : MonoBehaviour {
|
||||
|
||||
private GameController gameController;
|
||||
|
||||
void Start() {
|
||||
GameObject gameControllerObject = GameObject.FindGameObjectWithTag("game_controller");
|
||||
if (gameControllerObject != null) {
|
||||
gameController = gameControllerObject.GetComponent<GameController>();
|
||||
} else {
|
||||
Debug.Log("Cannot find 'GameController' script!");
|
||||
}
|
||||
}
|
||||
|
||||
void OnTriggerEnter2D(Collider2D other) {
|
||||
if (other.gameObject.CompareTag("Player")) {
|
||||
Destroy(other.gameObject);
|
||||
gameController.GameOver();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Project1/Assets/Scripts/DeathTrigger.cs.meta
Normal file
11
Project1/Assets/Scripts/DeathTrigger.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3942b8c9650a182dbcee46239b92e91
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
64
Project1/Assets/Scripts/GameController.cs
Normal file
64
Project1/Assets/Scripts/GameController.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
/*
|
||||
Author: Alexandrov Alexander Alexandrovich
|
||||
Date: 25/10/2019
|
||||
|
||||
Script that implement game controller
|
||||
*/
|
||||
public class GameController : MonoBehaviour {
|
||||
|
||||
public int coinsCount = 35;
|
||||
|
||||
public Text scoreText;
|
||||
public Text gameOverText;
|
||||
public Text restartText;
|
||||
|
||||
private int score;
|
||||
private bool gameOver;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start() {
|
||||
score = 0;
|
||||
gameOver = false;
|
||||
restartText.text = "";
|
||||
gameOverText.text = "";
|
||||
UpdateScore();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update() {
|
||||
if (gameOver) {
|
||||
if (Input.GetKeyDown(KeyCode.R)) {
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddScore(int newScoreValue) {
|
||||
score += newScoreValue;
|
||||
UpdateScore();
|
||||
if (score >= coinsCount) {
|
||||
GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
|
||||
if (playerObject != null) {
|
||||
Destroy(playerObject);
|
||||
GameOver();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateScore() {
|
||||
scoreText.text = "Coins: " + score;
|
||||
}
|
||||
|
||||
public void GameOver() {
|
||||
gameOverText.text = "Game Over!";
|
||||
restartText.text = "Press 'R' for Restart";
|
||||
gameOver = true;
|
||||
}
|
||||
}
|
||||
11
Project1/Assets/Scripts/GameController.cs.meta
Normal file
11
Project1/Assets/Scripts/GameController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1198df6c34ca18189b09a025c67b7ff9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
36
Project1/Assets/Scripts/MovingPlatform.cs
Normal file
36
Project1/Assets/Scripts/MovingPlatform.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/*
|
||||
Author: Alexandrov Alexander Alexandrovich
|
||||
Date: 25/10/2019
|
||||
|
||||
Script that implement moving platform
|
||||
*/
|
||||
public class MovingPlatform : MonoBehaviour {
|
||||
|
||||
public Vector3 finishPos = Vector3.zero;
|
||||
public float speed = 0.5f;
|
||||
|
||||
private Vector3 _startPos;
|
||||
private float _trackPercent = 0;
|
||||
private int _direction = 1;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start() {
|
||||
_startPos = transform.position;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update() {
|
||||
_trackPercent += _direction * speed * Time.deltaTime;
|
||||
float x = (finishPos.x - _startPos.x) * _trackPercent + _startPos.x;
|
||||
float y = (finishPos.y - _startPos.y) * _trackPercent + _startPos.y;
|
||||
transform.position = new Vector3(x, y, _startPos.z);
|
||||
|
||||
if ((_direction == 1 && _trackPercent > 1f) || (_direction == -1 && _trackPercent < 0f)) {
|
||||
_direction *= -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Project1/Assets/Scripts/MovingPlatform.cs.meta
Normal file
11
Project1/Assets/Scripts/MovingPlatform.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c62bdd469e711fc7ab1a01d86ee4b03e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
79
Project1/Assets/Scripts/PlayerController.cs
Normal file
79
Project1/Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/*
|
||||
Author: Alexandrov Alexander Alexandrovich
|
||||
Date: 25/10/2019
|
||||
|
||||
Script that implement player character controller
|
||||
*/
|
||||
public class PlayerController : MonoBehaviour {
|
||||
|
||||
public Transform playerCamera;
|
||||
public float speed = 250.0f;
|
||||
public float jumpForce = 12.0f;
|
||||
private Rigidbody2D _body;
|
||||
private BoxCollider2D _box;
|
||||
private Vector3 _camPos;
|
||||
|
||||
void Start() {
|
||||
_body = GetComponent<Rigidbody2D>();
|
||||
_box = GetComponent<BoxCollider2D>();
|
||||
_camPos = playerCamera.transform.position;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
float deltaX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
|
||||
Vector2 movement = new Vector2(deltaX, _body.velocity.y);
|
||||
_body.velocity = movement;
|
||||
|
||||
Vector3 max = _box.bounds.max;
|
||||
Vector3 min = _box.bounds.min;
|
||||
|
||||
Vector2 corner1 = new Vector2(max.x, min.y - .1f);
|
||||
Vector2 corner2 = new Vector2(min.x, min.y - .2f);
|
||||
Collider2D hit = Physics2D.OverlapArea(corner1, corner2);
|
||||
|
||||
bool grounded = false;
|
||||
if (hit != null) {
|
||||
grounded = true;
|
||||
}
|
||||
|
||||
_body.gravityScale = grounded && deltaX == 0 ? 0 : 1;
|
||||
|
||||
if (grounded && Input.GetKeyDown(KeyCode.Space)) {
|
||||
_body.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
|
||||
}
|
||||
|
||||
MovingPlatform platform = null;
|
||||
|
||||
if (hit != null) {
|
||||
platform = hit.GetComponent<MovingPlatform>();
|
||||
}
|
||||
|
||||
//Checking if player character stay out on moving platform
|
||||
if (platform != null) {
|
||||
transform.parent = platform.transform;
|
||||
} else {
|
||||
transform.parent = null;
|
||||
}
|
||||
|
||||
Vector3 pScale = Vector3.one;
|
||||
|
||||
if (deltaX != 0) {
|
||||
transform.localScale = new Vector3(Mathf.Sign(deltaX)/pScale.x, 1/pScale.y, 1);
|
||||
}
|
||||
|
||||
//Checking for player character moving out of screen edge.
|
||||
if (transform.position.x > playerCamera.transform.position.x + 9) {
|
||||
_camPos.x = _camPos.x + 18;
|
||||
playerCamera.transform.position = _camPos;
|
||||
playerCamera.GetComponentInChildren<SpriteRenderer>().flipX = !playerCamera.GetComponentInChildren<SpriteRenderer>().flipX;
|
||||
} else if (transform.position.x < playerCamera.transform.position.x - 9) {
|
||||
_camPos.x = _camPos.x - 18;
|
||||
playerCamera.transform.position = _camPos;
|
||||
playerCamera.GetComponentInChildren<SpriteRenderer>().flipX = !playerCamera.GetComponentInChildren<SpriteRenderer>().flipX;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Project1/Assets/Scripts/PlayerController.cs.meta
Normal file
11
Project1/Assets/Scripts/PlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 881be70d4f6d194e68a431107ba6ef2e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user