Advertisement
vjanomolee

Player script

Apr 13th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // PLAYER SCRIPT -This script handles most of the gameplay along with ScriptSceneManager
  2. var playerSpeedVertical             = 8.0;
  3. var playerSpeedHorizontal       = 8.0;
  4. var horMin              = -6.0;
  5. var horMax              = 6.0;
  6. var verMin              = -8.0;
  7. var verMax              = 8.0;
  8. var laser               : Transform;
  9. var laserCanonM             : Transform;
  10. var laserSound              : AudioClip;
  11. var redLaser                : Transform;
  12. var laserCanonL             : Transform;
  13. var laserCanonR             : Transform;
  14. var redLaserSound           : AudioClip;
  15. var playerOriginY           = -5.882426;
  16. var playerOriginX           = 0;
  17. var coolDownTime                = 3;  
  18. var missleTurret                : Transform;
  19. var missle              : Transform;
  20. var missleCount             = 10;
  21. var mCountA                 =50;
  22. var mCountB                 =25;
  23. var mCountC                 =100;
  24. var mCountD             =35;
  25. var missleSound             : AudioClip;
  26. var shieldMesh              : Transform;
  27. var shieldKeyInput          : KeyCode;
  28. var shieldActivationSound       : AudioClip;
  29. var numberOfShields         = 1;
  30. var sCountA                 =50;
  31. var sCountB                 =10;
  32. var sCountC                 =100;
  33. var sCountD             =20;
  34. var shieldOn                = false;
  35. var invincible              = false;
  36. var instructions                =false;
  37. var instructionTime         =5;
  38. var w                   = 300;  // used for the MISSLE MODE ENGAGED GUI label
  39. var h                   = 300;
  40.  
  41. function Update ()
  42. {
  43.    var transV : float = Input.GetAxis ("Vertical") *playerSpeedVertical *Time.deltaTime;//Stores variable for vertical movement
  44.    var transH : float = Input.GetAxis ("Horizontal") *playerSpeedHorizontal *Time.deltaTime;//Stores variable for horizontal movement
  45.    transform.Translate (transH, transV, 0); //Move the player based on transV + transH variables above                                                 
  46.    transform.position.x = Mathf.Clamp(transform.position.x, horMin, horMax); //Clamp player movement to horizontal limits
  47.    transform.position.y = Mathf.Clamp(transform.position.y, verMin, verMax); //Clamp player movement to vertical limits
  48.    if(Input.GetKeyDown("space"))    //check to see if spacebar key is pushed, if it is run code inside the if brackets
  49.    {
  50.         if(ScriptSceneManager.doubleShotMode == true) //Check to see if doubleShotMode is activated
  51.         {   // if it's activated fire a doubleLaser blast when user presses the space key
  52.             Instantiate (redLaser, laserCanonL.position, laserCanonL.rotation);//create a redLaser prefab at laserCanonL pos.&rot.
  53.             Instantiate (redLaser, laserCanonR.position, laserCanonR.rotation); //create a redLaser prefab at laserCanonR pos.&rot.
  54.             audio.PlayClipAtPoint(redLaserSound, transform.position); //play redLaserSound audio
  55.         }
  56.         else
  57.         {
  58.             Instantiate (laser, laserCanonM.position, laserCanonM.rotation); //create a laser prefab at laserCanon(middle) pos.&rot.
  59.             audio.PlayClipAtPoint(laserSound, transform.position);  // play laser blast sound
  60.         }
  61.     }
  62.        
  63.     if(Input.GetKeyDown("m")&&ScriptSceneManager.missleMode==true)//fire a pair of missles when user presses the m key if in misslemode
  64.     {
  65.         if(missleCount >=1)
  66.         {
  67.             Instantiate (missle, missleTurret.position,missleTurret.rotation); //create the missle prefab  
  68.             audio.PlayClipAtPoint(missleSound, transform.position);  // play missle launch sound
  69.             missleCount -=1;
  70.         }  
  71.     }
  72.    
  73.     if(Input.GetKeyDown(shieldKeyInput))   // If user pushes inspector chosen keyboard key
  74.     {
  75.         if(!shieldOn) //if the Shield is not on run the code inside
  76.         {
  77.         var clone = Instantiate (shieldMesh, transform.position, transform.rotation);   //create the shield prefab
  78.         clone.transform.parent = gameObject.transform;  // place the shield mesh at the same point(transform) as the game object(player)
  79.         audio.PlayClipAtPoint(shieldActivationSound, transform.position); //play shield activation sound
  80.         shieldOn = true;
  81.         numberOfShields -=1;
  82.         }
  83.     }
  84. }
  85.  
  86. function ResetPlayer () //moves the player back to the inspector set origin + play a blinking animation for feedback
  87. {
  88.     invincible = true; // Make the player invincible
  89.     transform.position.y = playerOriginY; //Reset the position of the player to Y origin
  90.     transform.position.x = playerOriginX; //Reset the position of the player to X origin
  91.     ScriptSceneManager.missleMode =false;
  92.     ScriptSceneManager.doubleShotMode =false;
  93.     animation.Play("Blink", PlayMode.StopAll); // Play the blink animation clip+ stop all other animations
  94.     yield WaitForSeconds(coolDownTime);  // wait for inspector set seconds
  95.     animation.Stop("Blink");  // Stop playing the blink animation clip
  96.     invincible = false; // Set the player to not be invincible
  97.    
  98.    
  99. }
  100.  
  101. function OnGUI ()  //Create and Display the HUD information for number of shields and missles left
  102. { GUI.Label (Rect (sCountA,sCountB,sCountC,sCountD), "Shields left: " +numberOfShields);
  103.     if (ScriptSceneManager.missleMode == true)
  104.     {
  105.          GUI.Label (Rect (mCountA,mCountB,mCountC,mCountD), "Missles left: " +missleCount);
  106.          if(instructions == true)
  107.          {
  108.             ShowInstructions();
  109.          }
  110.     }
  111. }
  112.  
  113. function ShowInstructions()
  114. {
  115.     var rect = Rect((Screen.width-w)/2, (Screen.height-h)/2, w, h);
  116.     if(instructions == true)
  117.     {
  118.         GUI.Label (rect, "MISSLE MODE ENGAGED!                Press M to fire missles. You only have 10, so use them wisely!");
  119.     }
  120.     yield WaitForSeconds(instructionTime);
  121.     instructions = false;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement