Guest User

Game Code

a guest
Feb 9th, 2018
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////
  2. // Title    : Space Invaders - playerController.cs
  3. // Author   :
  4. // Date     :
  5. //////////////////////////////////////////////////////////////////////////
  6.  
  7. //////////////////////////////////////////////////////////////////////////
  8. // Pre-Processor Directives
  9. //////////////////////////////////////////////////////////////////////////
  10. using UnityEngine;
  11.  
  12. public class playerController : MonoBehaviour
  13. {
  14.  
  15.     /*          CONSTS          */
  16.     const float CONST_PLAYER_Y = 3.0f;
  17.     const float PLAYER_X_SPEED = 0.030F;
  18.     const float MISSILE_Y_MAX = 14f;
  19.     const float MISSILE_Y_SPAWN = 3.1F;
  20.     const float MISSILE_X_MAX = -0.11f;
  21.     const float MISSILE_VELOCITY = 0.025f;
  22.  
  23.  
  24.     /*          PLAYER VARS             */
  25.     public GameObject Player;
  26.     public int Player_Lives;
  27.     Vector2 Player_Spawn;
  28.     Vector3 Player_Transform;
  29.     float Player_X;
  30.     bool HashHitEnemy;
  31.     public bool HasFired;
  32.  
  33.  
  34.     /*          MISSILE VARS            */
  35.     public GameObject Missile;
  36.     Vector2 Missile_Dynamic_Spawn;
  37.     Vector2 MISSILE_FIXED_SPAWN = new Vector2(0f, 3.1f);
  38.     float Missile_X_Position;
  39.     float Missile_Y_Position;
  40.     Vector3 Missile_Transform;
  41.  
  42.     /// <summary>
  43.     /// Global variables
  44.     ///
  45.     /// * default y position of the player (to prevent movement up the screen)
  46.     /// * default x position of the missile (to snap its coordinates to the player)
  47.     /// * new x position of the missile (as the player updates)
  48.     /// * transform component of the missile
  49.     /// * how far up the missile fires to
  50.     /// * true/false for if the player has shot anything
  51.     /// * lives of the player
  52.     /// </summary>
  53.  
  54.     // Use this for initialization
  55.     void Start ()
  56.     {
  57.         Player_Spawn = new Vector2(-0.11F, CONST_PLAYER_Y);
  58.         Missile_Dynamic_Spawn = new Vector2(0f, 3.1f);
  59.         Missile_Y_Position = Missile_Dynamic_Spawn.y;
  60.         Player_Lives = 3;
  61.         HashHitEnemy = false;
  62.         HasFired = false;
  63.  
  64.         Set_Player_Spawn();
  65.     }
  66.    
  67.     // Update is called once per frame
  68.     // Controls for player movement and ability to fire a missile
  69.     void Update ()
  70.     {
  71.         #region User Input
  72.         if (Input.GetKey(KeyCode.A))
  73.         {
  74.             Player_X -= PLAYER_X_SPEED;
  75.         }
  76.         else if (Input.GetKey(KeyCode.D))
  77.         {
  78.             Player_X += PLAYER_X_SPEED;
  79.         }
  80.         else if (Input.GetKeyDown(KeyCode.Space))
  81.         {
  82.             if (HasFired == false)
  83.             {
  84.                 HasFired = true;
  85.                 Create_New_Missile_Spawn();
  86.                 Apply_New_Missile_Spawn();
  87.             }
  88.         }
  89.         #endregion
  90.  
  91.         if (HasFired == true && Missile.transform.position.y < MISSILE_Y_MAX)
  92.         {
  93.             Missile_Y_Position += MISSILE_VELOCITY;
  94.             Missile.transform.position = new Vector3(Missile.transform.position.x, Missile_Y_Position, 0F);
  95.         }
  96.         else
  97.         {
  98.             HasFired = false;
  99.             Missile.transform.position = Missile_Dynamic_Spawn;
  100.         }
  101.  
  102.         Player.transform.position = new Vector3(Player_X, CONST_PLAYER_Y, 0F);
  103.     }
  104.  
  105.     private void Create_New_Missile_Spawn()
  106.     {
  107.         Missile_Dynamic_Spawn = new Vector2(Player_X, MISSILE_Y_SPAWN);
  108.     }
  109.  
  110.     private void Apply_New_Missile_Spawn()
  111.     {
  112.  
  113.         Missile.transform.position = Missile_Dynamic_Spawn;
  114.     }
  115.  
  116.     private void Set_Player_Spawn()
  117.     {
  118.         Player.transform.position = Player_Spawn;
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment