Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameManager : MonoBehaviour {
- // reference to other scripts
- public Controller2D controller2D;
- public float screenPositionX;
- public float screenPositionY;
- int sw = Screen.width;
- int sh = Screen.height;
- public float iconSizeX = 25;
- public float iconSizeY = 25;
- static public int bulletDamage = 1;
- static public int curHealth;
- public int maxHealth;
- public int curExp = 0;
- int maxExp = 50;
- int level = 1;
- public int curCoins = 0;
- bool pauseMenu;
- private void Awake() {
- print("AWAKE1() bulletDamage: " + bulletDamage); // 1
- curExp = PlayerPrefs.GetInt("Player Exp");
- level = PlayerPrefs.GetInt("Player Level");
- curCoins = PlayerPrefs.GetInt("Player Coins");
- bulletDamage = PlayerPrefs.GetInt("Player Damage");
- print("AWAKE2() bulletDamage: " + bulletDamage); // 0
- // ...
- }
- private void Update() {
- // ...
- }
- void OnGUI() {
- if(pauseMenu) {
- if(GUI.Button(new Rect(sw * 0.25f, sh * 0.3f, sw * 0.5f, sh * 0.1f), "Save")) {
- SaveGame();
- }
- // ...
- }
- }
- public void SaveGame() {
- PlayerPrefs.SetInt("Player Level", level);
- PlayerPrefs.SetInt("Player Exp", curExp);
- PlayerPrefs.SetInt("Player Coins", curCoins);
- print("SAVEGAME() bd: " + bulletDamage); // 0
- PlayerPrefs.SetInt("Player Damage", bulletDamage);
- }
- void PlayerDamaged(int damage) {
- if(curHealth > 0) {
- curHealth -= damage;
- }
- if(curHealth <= 0) {
- curHealth = 0;
- RestartScene();
- }
- }
- }
- public class Bullet : MonoBehaviour {
- private void OnTriggerEnter(Collider other) {
- if(other.gameObject.tag == "Enemy") {
- Destroy(gameObject);
- other.gameObject.SendMessage("EnemyDamaged", GameManager.bulletDamage, SendMessageOptions.DontRequireReceiver);
- other.gameObject.SendMessage("TakenDamage", SendMessageOptions.DontRequireReceiver);
- }
- if(other.gameObject.tag == "Wall") {
- Destroy(gameObject);
- }
- }
- private void FixedUpdate() {
- Destroy(gameObject, 1.25f);
- }
- }
- public class Controller2D : MonoBehaviour {
- // references to other scripts
- CharacterController characterController;
- public GameManager gameManager;
- Renderer rend;
- public float gravity = 10;
- public float walkSpeed = 5;
- public float jumpHeight = 5;
- float takenDamage = 0.2f;
- Vector3 moveDirection = Vector3.zero;
- float horizontal = 0;
- public Rigidbody bulletPrefab;
- float attackRate = 0.2f;
- float coolDown;
- bool lookRight = true;
- void Start() {
- characterController = GetComponent<CharacterController>();
- rend = GetComponent<Renderer>();
- }
- void Update() {
- characterController.Move(moveDirection * Time.deltaTime);
- horizontal = Input.GetAxis("Horizontal");
- moveDirection.y -= gravity * Time.deltaTime;
- if(horizontal == 0) {
- moveDirection.x = horizontal;
- }
- if(horizontal > 0.01f) {
- lookRight = true;
- moveDirection.x = horizontal * walkSpeed;
- }
- if(horizontal < -0.01f) {
- lookRight = false;
- moveDirection.x = horizontal * walkSpeed;
- }
- if(characterController.isGrounded) {
- if(Input.GetKeyDown(KeyCode.Space)) {
- moveDirection.y = jumpHeight;
- }
- }
- if(Time.time >= coolDown) {
- if(Input.GetKeyDown(KeyCode.F)) {
- BulletAttack();
- }
- }
- }
- void BulletAttack() {
- if(lookRight) {
- Rigidbody bPrefab = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as Rigidbody;
- bPrefab.AddForce(Vector3.right * 500);
- coolDown = Time.time + attackRate;
- }
- else {
- Rigidbody bPrefab = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as Rigidbody;
- bPrefab.AddForce(-Vector3.right * 500);
- coolDown = Time.time + attackRate;
- }
- }
- public IEnumerator TakenDamage() {
- rend.enabled = false;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = true;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = false;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = true;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = false;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = true;
- yield return new WaitForSeconds(takenDamage);
- }
- }
- public class Enemy2D : MonoBehaviour {
- // references to other scripts
- public GameManager gameManager;
- Renderer rend;
- Rigidbody rb;
- float startingPos;
- float endPos;
- public int unitsToMove = 5;
- public int moveSpeed = 2;
- bool moveRight = true;
- int damageValue = 1;
- int enemyHealth = 1;
- public bool basicEnemy = true;
- public bool advancedEnemy;
- float takenDamage = 0.2f;
- private void Start() {
- rb = GetComponent<Rigidbody>();
- rend = GetComponent<Renderer>();
- }
- private void Update() {
- if(moveRight) {
- rb.position += Vector3.right * moveSpeed * Time.deltaTime;
- }
- if(rb.position.x >= endPos) {
- moveRight = false;
- }
- if(!moveRight) {
- rb.position -= Vector3.right * moveSpeed * Time.deltaTime;
- }
- if(rb.position.x <= startingPos) {
- moveRight = true;
- }
- }
- private void Awake() {
- startingPos = transform.position.x;
- endPos = startingPos + unitsToMove;
- if(basicEnemy) {
- enemyHealth = 3;
- }
- if(advancedEnemy) {
- enemyHealth = 6;
- }
- }
- void OnTriggerEnter(Collider col) {
- if(col.gameObject.tag == "Player") {
- gameManager.SendMessage("PlayerDamaged", damageValue, SendMessageOptions.DontRequireReceiver);
- gameManager.controller2D.SendMessage("TakenDamage", SendMessageOptions.DontRequireReceiver);
- }
- }
- void EnemyDamaged(int damage) {
- if(enemyHealth > 0) {
- enemyHealth -= damage;
- }
- if(enemyHealth <= 0) {
- enemyHealth = 0;
- Destroy(gameObject);
- if(basicEnemy) {
- gameManager.curExp += 5;
- gameManager.curCoins += 5;
- }
- if(advancedEnemy) {
- gameManager.curExp += 10;
- gameManager.curCoins += 10;
- }
- }
- }
- public IEnumerator TakenDamage() {
- rend.enabled = false;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = true;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = false;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = true;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = false;
- yield return new WaitForSeconds(takenDamage);
- rend.enabled = true;
- yield return new WaitForSeconds(takenDamage);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement