Advertisement
pivotraze

Player Script

Dec 2nd, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BOO 9.59 KB | None | 0 0
  1. /* This script is licensed under the BSD license.
  2. Author: Cody Dostal
  3. Author Email: allysman21@gmail.com
  4. The License: http://seafiresoftware.org/wordpress/our-bsd/
  5. Date Written: 4/22/2012
  6. Engine Version: 3.5.1f2
  7. Script Version: v1.1
  8. */
  9.  
  10. /* NOTE: With good chance, you may have to change any hard-coded values in these scripts.
  11. These values work with my version, and may not with your version. */
  12.  
  13.  
  14. /* In Boo, // == one line comment, # = one line comment, and "/(star)" to "(star)/"
  15. is a multiline comment.*/
  16.  
  17. // Import is the C# equivalent to using, I don't think UnityScript requires any for of imports.
  18. import UnityEngine
  19. import System.Collections
  20.  
  21. class player (MonoBehaviour):
  22.    
  23.     /* When setting up variables in Boo, it's alters slightly from C# and from UnityScript. Here are examples:
  24.        In C# you would type: public single variableName
  25.        In UnityScript, you would type: public var variableName
  26.        In Boo, there are actually two ways:
  27.           Way 1: public variableName as single = value
  28.           Way 2: public variableName = value
  29.        If you use way two, it will automatically detect the type. I used Way 1 purely to look similar to 3DBuzz's C# tutorial
  30.     */
  31.    
  32.     // Setting *GENERAL* Floats. x.xF makes a float
  33.     public flySpeed as single = 0.0F
  34.     private projectileOffset as single = 1.3F
  35.     public slowFlySpeed as single = 0.0F
  36.     public fastFlySpeed as single = 0.0F
  37.  
  38.     // Setting *GENERAL* Game Objects
  39.     public projPrefab as GameObject
  40.     public explosionPrefab as GameObject
  41.  
  42.     // Sertting *GENERAL* Integers
  43.     public static score as int = 0
  44.     public static lives as int = 3
  45.     public static missed as int = 0
  46.     private static scoreSub as int = 100
  47.  
  48.     // Special Spawn. Invincible + Slide on Screen
  49.     private maxBlinkAmt as int = 10
  50.     private curBlinkAmt as int = 0
  51.     private shipInvTime as single = 1.5f
  52.     private shipMoveOnScreenSpeed as single = 3f
  53.     private blinkRate as single = .1f
  54.    
  55.     private gamePaused = false
  56.    
  57.     /* Setting up an enum. Kindof like a variable type made by you.
  58.        You set up the type (state in this instance). Kindof like
  59.        int and single. */
  60.     enum state:
  61.         Playing
  62.         Explosion
  63.         Invincible
  64.     /* Setting default state to Playing */ 
  65.     PlayerState as state = state.Playing
  66.    
  67.    
  68.     // Setting up the Menu
  69.     private buttonLocH as single = 35
  70.     private buttonLocV as single = 5
  71.  
  72.     private labelLocH as int = 30
  73.     private labelLocV as int = 85
  74.  
  75.     def Start():
  76.         /* StartCoroutine makes the SlideUpInvicible run as an enumerator. An enumerator type (for sections in THIS case)
  77.            allows a portion of the script to be pause while waiting for something. For example, yield WaitForSeconds(time)
  78.            will let the script keep running until the specified time has passed. VERY useful in games especially, moreso than
  79.            many other kinds of scripting/coding.
  80.         */
  81.         StartCoroutine(SlideUpInvincible())
  82.        
  83.     def SlideUpInvincible() as IEnumerator:
  84.         /* For as long as the ship position is below -5.65 on the Y-axis  (remember, the "f" (or "F") designates float,
  85.         I'm finicky on capitalization.)*/
  86.         while transform.position.y < -5.65f:
  87.             // Slide the ship up slowly
  88.             // Info on time.deltaTime: http://unity3d.com/support/documentation/ScriptReference/Time-deltaTime.html
  89.             amtToMove as single = shipMoveOnScreenSpeed * Time.deltaTime
  90.             transform.position = Vector3(0f, transform.position.y + amtToMove, transform.position.z)
  91.             // Return 0, or false. Just makes it keep running.
  92.             yield 0
  93.            
  94.         // Makes the player INVINCIBLE!!!!!1!!!!one!!
  95.         PlayerState = state.Invincible
  96.         // If the variable curBlinkAmt is below maxBlinkAmt
  97.         while curBlinkAmt < maxBlinkAmt:
  98.             // Alternate between viewable and non-viewable
  99.             gameObject.renderer.enabled = not gameObject.renderer.enabled
  100.            
  101.            
  102.             // For each time the ship is viewable.
  103.             if gameObject.renderer.enabled == true:
  104.                 // Increase curBlinkAmt by 1
  105.                 curBlinkAmt++
  106.                
  107.             // Wait for (blinkrate)
  108.             yield WaitForSeconds(blinkRate)
  109.  
  110.         // Set player to Play mode
  111.         PlayerState = state.Playing
  112.         // Set curBlinkAmt to 0, so it will work the next time we use this script.
  113.         curBlinkAmt = 0
  114.    
  115.     // def is the c# equivalent of void (for the *MOST* part. Functionally, in Unity, it's the same.)
  116.     def Update ():
  117.         /* If (somehow) the score variable/amount gets below 0
  118.                Reset to 0.*/
  119.         if score < 0:
  120.             score = 0
  121.                
  122.         if PlayerState != state.Explosion:
  123.    
  124.             // Getting the Axis. "GetAxisRaw" get an old-arcade feel, abrupt end, not smooth
  125.             // Time.deltaTime = 1s (in basis. Remove it, and you *FLY* FAST!)
  126.             amtToMove = Input.GetAxisRaw("Horizontal") * flySpeed * Time.deltaTime
  127.             amtToMoveV = Input.GetAxisRaw("Vertical") * flySpeed * Time.deltaTime
  128.             transform.Translate(Vector3.right * amtToMove)
  129.             transform.Translate(Vector3.up * amtToMoveV)
  130.  
  131.             // Faster Speed. Hold Shift calls it. It ADDS to flySpeed
  132.             if Input.GetKey(KeyCode.LeftShift):
  133.                 fAmtToMove = Input.GetAxisRaw("Horizontal") * fastFlySpeed * Time.deltaTime
  134.                 fAmtToMoveV = Input.GetAxisRaw("Vertical") * fastFlySpeed * Time.deltaTime
  135.                 transform.Translate(Vector3.right * fAmtToMove)
  136.                 transform.Translate(Vector3.up * fAmtToMoveV)
  137.            
  138.             // Slower Speed. Hold LEFT Control calls it. It ADDS to flySpeed
  139.             if Input.GetKey(KeyCode.LeftControl):
  140.                 sAmtToMove = Input.GetAxisRaw("Horizontal") * slowFlySpeed * Time.deltaTime
  141.                 sAmtToMoveV = Input.GetAxisRaw("Vertical") * slowFlySpeed * Time.deltaTime
  142.                 transform.Translate(Vector3.right * sAmtToMove)
  143.                 transform.Translate(Vector3.up * sAmtToMoveV)
  144.                 // Setting position for the projectile
  145.             position as Vector3 = Vector3(transform.position.x, projectileOffset + transform.position.y, transform.position.z)
  146.  
  147.             // Setting the keys you can push/click to actually shoot. Instantiate = Create
  148.             if Input.GetKeyDown(KeyCode.Space):
  149.                 Instantiate(projPrefab, position, Quaternion.identity)
  150.             elif Input.GetKeyDown(KeyCode.RightControl):
  151.                 Instantiate(projPrefab, position, Quaternion.identity)
  152.             // The 0 in GetMouseButtonDown, in this case, does NOT mean false. It means Left Mouse Button.
  153.             if Input.GetMouseButtonDown(0):
  154.                 Instantiate(projPrefab, position, Quaternion.identity)
  155.            
  156.             if Input.GetKeyDown(KeyCode.Escape):
  157.                 if gamePaused == false:
  158.                     Time.timeScale = 0
  159.                     gamePaused = true
  160.                 elif gamePaused == true:
  161.                     Time.timeScale = 1
  162.                     gamePaused = false
  163.                
  164.             /* Screen Wrap. Without this, you will fly FOREVER to the Left or the right. Will not wrap to other side.
  165.                Also handles stopping from flying up/down out of screen. ORTHOGRAPHIC VIEWPOINT ONLY (since, techincally, we don't use the Z-axis)
  166.             */
  167.             if transform.position.x <= -7.3:
  168.                 transform.position = Vector3(7.3, transform.position.y, transform.position.z)
  169.  
  170.             elif transform.position.x >= 7.3:
  171.                 transform.position = Vector3(-7.3, transform.position.y, transform.position.z)
  172.  
  173.             elif transform.position.y >= 1.5:
  174.                 transform.position = Vector3(transform.position.x, 1.5, transform.position.z)
  175.  
  176.             elif transform.position.y <= -5.65:
  177.                 transform.position = Vector3(transform.position.x, -5.65, transform.position.z)
  178.            
  179.             // End of Screen Wrap
  180.  
  181.     // Setting up the GUI, or "HUD" in the Game.
  182.     def OnGUI():
  183.         GUI.Label(Rect(10, 10, 120, 20), "Score: " + player.score.ToString())
  184.         GUI.Label(Rect(10, 30, 60, 20), "Lives: " + player.lives.ToString())
  185.         GUI.Label(Rect(10, 50, 60, 20), "Missed: " + player.missed.ToString())
  186.    
  187.         if gamePaused == false:
  188.             pass
  189.         elif gamePaused == true:
  190.             GUI.Label(Rect(Screen.width / 2 - labelLocH, Screen.width / 2 - labelLocV, 60, 30), "PAUSED.")
  191.             if GUI.Button(Rect(Screen.width / 2 - buttonLocH, Screen.height / 2 - buttonLocV, 70, 35), "Resume"):
  192.                 Time.timeScale = 1
  193.                 gamePaused = false
  194.                    
  195.     def OnTriggerEnter(thisObject as Collider):
  196.         if thisObject.tag != "bullet":
  197.             if PlayerState == state.Playing:
  198.                 player.lives--
  199.                 StartCoroutine(DestroyShip())
  200.        
  201.     def DestroyShip() as IEnumerator:
  202.         // Change the player state to Explosion
  203.         PlayerState = state.Explosion
  204.        
  205.         /* Set up the explosion position to last player location
  206.            and Instantiate(Create) the explosion */
  207.         exploPos = Vector3(transform.position.x, transform.position.y, transform.position.z)
  208.         Instantiate(explosionPrefab, exploPos, Quaternion.identity)
  209.  
  210.         /* Hide the ship
  211.            Move to just off screen
  212.            Wait for a short amount of time */
  213.         gameObject.renderer.enabled = false
  214.         transform.position = Vector3(0f, -7.68, transform.position.z)
  215.         yield WaitForSeconds(shipInvTime)
  216.        
  217.         if player.lives > 0:
  218.             // Turn the ship to visible again
  219.             gameObject.renderer.enabled = true
  220.            
  221.             // Decrement score by 300, only if score is greater than 0
  222.             if score > 0:
  223.                 score = score - 300
  224.             elif score < 0:
  225.                 score = 0
  226.                
  227.             while transform.position.y < -5.65:
  228.                 // Slide the ship up slowly
  229.                 amtToMove as single = shipMoveOnScreenSpeed * Time.deltaTime
  230.                 transform.position = Vector3(0f, transform.position.y + amtToMove, transform.position.z)
  231.                 yield 0
  232.            
  233.             // Change the player to Invincible
  234.             PlayerState = state.Invincible
  235.             while curBlinkAmt < maxBlinkAmt:
  236.                 /* Each time the script returns to begining,
  237.                  it will turn it on and off */
  238.                 gameObject.renderer.enabled = not gameObject.renderer.enabled
  239.                
  240.                 // Each time the ship is visible, turn curBlinkAmt up
  241.                 if gameObject.renderer.enabled == true:
  242.                     curBlinkAmt++
  243.                    
  244.                 yield WaitForSeconds(blinkRate)
  245.  
  246.  
  247.             PlayerState = state.Playing
  248.             // Set curBlinkAmt back to 0 so it will work next time around.
  249.             curBlinkAmt = 0
  250.         else:
  251.             // If lives = 0, destory ship and load lose screen.
  252.             Destroy(gameObject)
  253.             Application.LoadLevel(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement