Advertisement
Guest User

Player Class

a guest
Aug 27th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player : MonoBehaviour {
  5.  
  6.    [SerializeField] private int health;
  7.     private float moveSpeed;
  8.     private int sheildIntegrity;
  9.     private bool isAlive;
  10.     private bool isGamePaused;
  11.     private Rigidbody2D rBody;
  12.     Bullet bulletsReference;
  13.  
  14.     SpriteRenderer[] renderers;
  15.  
  16.     void Awake()
  17.     {
  18.         bulletsReference = GetComponent<Bullet>();
  19.     }
  20.  
  21.     /// <summary>
  22.     public delegate void Dead();
  23.     public static event Dead PlayerDeath;
  24.     /// </summary>
  25.  
  26.     // Use this for initialization
  27.     void Start ()
  28.     {
  29.  
  30.         Reset();
  31.         rBody = GetComponent<Rigidbody2D>();
  32.         renderers = GetComponents<SpriteRenderer>();
  33.        
  34.     }
  35.    
  36.     void Reset()
  37.     {
  38.         health = 100;
  39.         sheildIntegrity = 100;
  40.         isAlive = true;
  41.         isGamePaused = false;
  42.         moveSpeed = 0f;
  43.         transform.position.Set(0f, 0f, 0f);
  44.        
  45.     }
  46.     void OnEnable()
  47.     {
  48.         InputManager.ApplySpeed += ApplySpeed;
  49.         InputManager.MoveShip += Movement;
  50.         InputManager.RotateShip += Rotate;
  51.  
  52.         GameManager.GamePauseEvent += Pause;
  53.         GameManager.GamePlayingEvent += Playing;
  54.         GameManager.GameStartEvent += Playing;        
  55.        
  56.     }
  57.  
  58.     void OnDisable()
  59.     {
  60.         InputManager.ApplySpeed -= ApplySpeed;
  61.         InputManager.MoveShip -= Movement;
  62.         InputManager.RotateShip -= Rotate;
  63.  
  64.         GameManager.GamePauseEvent -= Pause;
  65.         GameManager.GamePlayingEvent -= Playing;
  66.         GameManager.GameStartEvent -= Playing;
  67.  
  68.  
  69.  
  70.     }
  71.  
  72.     void Movement(Vector2 axisInput)         //TODO maybe change this to a Vec2 if needed
  73.     {
  74.        
  75.         Vector2 moveDirection = new Vector2(axisInput.x * moveSpeed, axisInput.y * moveSpeed);
  76.         rBody.AddForce(moveDirection);
  77.         //transform.position += moveDirection * moveSpeed * Time.deltaTime;
  78.  
  79.  
  80.     }
  81.  
  82.     void Pause()
  83.     {
  84.         //TODO implement pausing feature which stops input affecting the player
  85.         isGamePaused = true;
  86.     }
  87.    
  88.     void Playing()
  89.     {
  90.         //TODO implment an unpausing feature which will allow the input to affect the player
  91.         isGamePaused = false;
  92.     }
  93.  
  94.  
  95.     void Rotate(Vector2 axisInput)
  96.     {
  97.         if(!isGamePaused)
  98.         {
  99.             float angle = Mathf.Atan2(axisInput.x, axisInput.y) * Mathf.Rad2Deg;
  100.             transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
  101.  
  102.         }
  103.  
  104.  
  105.     }
  106.  
  107.     void ApplySpeed(float axisInput)
  108.     {
  109.  
  110.         if(!isGamePaused)
  111.              moveSpeed = 5 * axisInput;          //TODO get rid of the magic number
  112.        
  113.    
  114.     }
  115.  
  116.     void MoveXPosition(float _xEntry)
  117.     {
  118.        transform.position.Set(0, rBody.transform.position.y, 0);
  119.  
  120.        
  121.     }
  122.  
  123.     void MoveYPosition(float _yEntry)
  124.     {
  125.         rBody.transform.position.Set(rBody.transform.position.x, _yEntry, 0);
  126.        
  127.     }
  128.  
  129.  
  130.     void OnCollisionEnter2D(Collision2D col)
  131.     {
  132.        
  133.         if(col.gameObject.tag == "Enemy")
  134.             TakeDamage();
  135.        
  136.     }
  137.  
  138.     void TakeDamage()
  139.     {
  140.        
  141.         health -= 20;
  142.  
  143.         if (health == 0)
  144.         {
  145.             Reset();
  146.             Pause();
  147.             PlayerDeath();
  148.            
  149.         }
  150.            
  151.     }
  152.     void LateUpdate()
  153.     {
  154.         // NOTE: this works only if the cam center is at 0,0
  155.        
  156.  
  157.         float HalfScreenSize = 6.5f;
  158.         float HalfScreenHeight = 5.5f;
  159.  
  160.         // Get the actual position of the original
  161.         Vector3 pos = transform.position;
  162.  
  163.         // Add the screen size
  164.         //pos.x += HalfScreenSize * 2;
  165.  
  166.         // wrap around if necessary
  167.         if (pos.x > HalfScreenSize )
  168.         {
  169.             pos.x -= HalfScreenSize * 2;
  170.  
  171.             transform.position = pos;
  172.         }
  173.         else if (pos.x < -HalfScreenSize)
  174.         {
  175.             pos.x += HalfScreenSize * 2;
  176.             transform.position = pos;
  177.         }
  178.  
  179.         if (pos.y > HalfScreenHeight)
  180.         {
  181.             pos.y -= HalfScreenHeight * 2;
  182.             transform.position = pos;
  183.         }
  184.  
  185.         else if (pos.y < -HalfScreenHeight )
  186.         {
  187.             pos.y += HalfScreenHeight * 2;
  188.             transform.position = pos;
  189.         }
  190.  
  191.  
  192.  
  193.     }
  194.  
  195.    
  196.  
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement