Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Player : MonoBehaviour {
- [SerializeField] private int health;
- private float moveSpeed;
- private int sheildIntegrity;
- private bool isAlive;
- private bool isGamePaused;
- private Rigidbody2D rBody;
- Bullet bulletsReference;
- SpriteRenderer[] renderers;
- void Awake()
- {
- bulletsReference = GetComponent<Bullet>();
- }
- /// <summary>
- public delegate void Dead();
- public static event Dead PlayerDeath;
- /// </summary>
- // Use this for initialization
- void Start ()
- {
- Reset();
- rBody = GetComponent<Rigidbody2D>();
- renderers = GetComponents<SpriteRenderer>();
- }
- void Reset()
- {
- health = 100;
- sheildIntegrity = 100;
- isAlive = true;
- isGamePaused = false;
- moveSpeed = 0f;
- transform.position.Set(0f, 0f, 0f);
- }
- void OnEnable()
- {
- InputManager.ApplySpeed += ApplySpeed;
- InputManager.MoveShip += Movement;
- InputManager.RotateShip += Rotate;
- GameManager.GamePauseEvent += Pause;
- GameManager.GamePlayingEvent += Playing;
- GameManager.GameStartEvent += Playing;
- }
- void OnDisable()
- {
- InputManager.ApplySpeed -= ApplySpeed;
- InputManager.MoveShip -= Movement;
- InputManager.RotateShip -= Rotate;
- GameManager.GamePauseEvent -= Pause;
- GameManager.GamePlayingEvent -= Playing;
- GameManager.GameStartEvent -= Playing;
- }
- void Movement(Vector2 axisInput) //TODO maybe change this to a Vec2 if needed
- {
- Vector2 moveDirection = new Vector2(axisInput.x * moveSpeed, axisInput.y * moveSpeed);
- rBody.AddForce(moveDirection);
- //transform.position += moveDirection * moveSpeed * Time.deltaTime;
- }
- void Pause()
- {
- //TODO implement pausing feature which stops input affecting the player
- isGamePaused = true;
- }
- void Playing()
- {
- //TODO implment an unpausing feature which will allow the input to affect the player
- isGamePaused = false;
- }
- void Rotate(Vector2 axisInput)
- {
- if(!isGamePaused)
- {
- float angle = Mathf.Atan2(axisInput.x, axisInput.y) * Mathf.Rad2Deg;
- transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
- }
- }
- void ApplySpeed(float axisInput)
- {
- if(!isGamePaused)
- moveSpeed = 5 * axisInput; //TODO get rid of the magic number
- }
- void MoveXPosition(float _xEntry)
- {
- transform.position.Set(0, rBody.transform.position.y, 0);
- }
- void MoveYPosition(float _yEntry)
- {
- rBody.transform.position.Set(rBody.transform.position.x, _yEntry, 0);
- }
- void OnCollisionEnter2D(Collision2D col)
- {
- if(col.gameObject.tag == "Enemy")
- TakeDamage();
- }
- void TakeDamage()
- {
- health -= 20;
- if (health == 0)
- {
- Reset();
- Pause();
- PlayerDeath();
- }
- }
- void LateUpdate()
- {
- // NOTE: this works only if the cam center is at 0,0
- float HalfScreenSize = 6.5f;
- float HalfScreenHeight = 5.5f;
- // Get the actual position of the original
- Vector3 pos = transform.position;
- // Add the screen size
- //pos.x += HalfScreenSize * 2;
- // wrap around if necessary
- if (pos.x > HalfScreenSize )
- {
- pos.x -= HalfScreenSize * 2;
- transform.position = pos;
- }
- else if (pos.x < -HalfScreenSize)
- {
- pos.x += HalfScreenSize * 2;
- transform.position = pos;
- }
- if (pos.y > HalfScreenHeight)
- {
- pos.y -= HalfScreenHeight * 2;
- transform.position = pos;
- }
- else if (pos.y < -HalfScreenHeight )
- {
- pos.y += HalfScreenHeight * 2;
- transform.position = pos;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement