Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////////////////////
- // Title : Space Invaders - playerController.cs
- // Author :
- // Date :
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- // Pre-Processor Directives
- //////////////////////////////////////////////////////////////////////////
- using UnityEngine;
- public class playerController : MonoBehaviour
- {
- /* CONSTS */
- const float CONST_PLAYER_Y = 3.0f;
- const float PLAYER_X_SPEED = 0.030F;
- const float MISSILE_Y_MAX = 14f;
- const float MISSILE_Y_SPAWN = 3.1F;
- const float MISSILE_X_MAX = -0.11f;
- const float MISSILE_VELOCITY = 0.025f;
- /* PLAYER VARS */
- public GameObject Player;
- public int Player_Lives;
- Vector2 Player_Spawn;
- Vector3 Player_Transform;
- float Player_X;
- bool HashHitEnemy;
- public bool HasFired;
- /* MISSILE VARS */
- public GameObject Missile;
- Vector2 Missile_Dynamic_Spawn;
- Vector2 MISSILE_FIXED_SPAWN = new Vector2(0f, 3.1f);
- float Missile_X_Position;
- float Missile_Y_Position;
- Vector3 Missile_Transform;
- /// <summary>
- /// Global variables
- ///
- /// * default y position of the player (to prevent movement up the screen)
- /// * default x position of the missile (to snap its coordinates to the player)
- /// * new x position of the missile (as the player updates)
- /// * transform component of the missile
- /// * how far up the missile fires to
- /// * true/false for if the player has shot anything
- /// * lives of the player
- /// </summary>
- // Use this for initialization
- void Start ()
- {
- Player_Spawn = new Vector2(-0.11F, CONST_PLAYER_Y);
- Missile_Dynamic_Spawn = new Vector2(0f, 3.1f);
- Missile_Y_Position = Missile_Dynamic_Spawn.y;
- Player_Lives = 3;
- HashHitEnemy = false;
- HasFired = false;
- Set_Player_Spawn();
- }
- // Update is called once per frame
- // Controls for player movement and ability to fire a missile
- void Update ()
- {
- #region User Input
- if (Input.GetKey(KeyCode.A))
- {
- Player_X -= PLAYER_X_SPEED;
- }
- else if (Input.GetKey(KeyCode.D))
- {
- Player_X += PLAYER_X_SPEED;
- }
- else if (Input.GetKeyDown(KeyCode.Space))
- {
- if (HasFired == false)
- {
- HasFired = true;
- Create_New_Missile_Spawn();
- Apply_New_Missile_Spawn();
- }
- }
- #endregion
- if (HasFired == true && Missile.transform.position.y < MISSILE_Y_MAX)
- {
- Missile_Y_Position += MISSILE_VELOCITY;
- Missile.transform.position = new Vector3(Missile.transform.position.x, Missile_Y_Position, 0F);
- }
- else
- {
- HasFired = false;
- Missile.transform.position = Missile_Dynamic_Spawn;
- }
- Player.transform.position = new Vector3(Player_X, CONST_PLAYER_Y, 0F);
- }
- private void Create_New_Missile_Spawn()
- {
- Missile_Dynamic_Spawn = new Vector2(Player_X, MISSILE_Y_SPAWN);
- }
- private void Apply_New_Missile_Spawn()
- {
- Missile.transform.position = Missile_Dynamic_Spawn;
- }
- private void Set_Player_Spawn()
- {
- Player.transform.position = Player_Spawn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment