Advertisement
Guest User

jetpack-chan.cs

a guest
Nov 23rd, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PC : MonoBehaviour {
  5.  
  6.     // attributes
  7.     public float moveSpeed;
  8.     public GameObject bullet;
  9.     public GameObject jetpackSmoke;
  10.     public bool hit = false;
  11.     public bool falling = false;
  12.     public float specialTimer = 4.0f;
  13.     public bool specialOK;
  14.     public float invincibleTime = 0.0f;
  15.  
  16.     private int extraLives = 2;
  17.     private float bulletCooldown = 0.0f;
  18.     private bool usingSpecial = false;
  19.     private float jetpackSmokeTime = 0.25f;
  20.    
  21.     private float screenHeight;
  22.     private float screenWidth;
  23.     public Animator animator;
  24.     // private BoxCollider2D boxCollider;
  25.     private SpriteRenderer spriteRenderer;
  26.  
  27.     bool facingRight;
  28.  
  29.     void Awake() {
  30.  
  31.         if (Application.loadedLevel == 1) // mode 1
  32.             specialOK = false;
  33.         else if (Application.loadedLevel == 2) // mode 2 (special attack variant)
  34.             specialOK = true;
  35.  
  36.         moveSpeed = 3.0f;
  37.         facingRight = true;
  38.         screenHeight = Screen.height;
  39.         screenWidth = Screen.width;
  40.  
  41.         animator = GetComponent<Animator>();
  42.         // boxCollider = GetComponent<BoxCollider2D>();
  43.         spriteRenderer = GetComponent<SpriteRenderer>();
  44.     }
  45.  
  46.     void Start () {
  47.     }
  48.    
  49.     void Update () {
  50.  
  51.         if (Input.GetKeyDown ("escape")) {
  52.             Application.LoadLevel ("menu");
  53.         }
  54.  
  55.         // map key presses to movement
  56.         // use WASD to move
  57.         float horizontal = Input.GetAxis ("Horizontal");
  58.         float vertical = Input.GetAxis ("Vertical");
  59.  
  60.         if (horizontal != 0)
  61.             horizontal *= moveSpeed;
  62.         if (vertical != 0)
  63.             vertical *= moveSpeed;
  64.  
  65.         // check for movement
  66.         if (horizontal != 0 || vertical != 0) animator.SetBool("moving", true);
  67.         else animator.SetBool ("moving", false);
  68.  
  69.         if (!falling)
  70.             rigidbody2D.velocity = new Vector2(horizontal, vertical);
  71.         if ((horizontal > 0 && !facingRight) || (horizontal < 0 && facingRight))
  72.             flip();
  73.  
  74.         // check if activated special: can only use once
  75.         // activate with spacebar
  76.         if (Input.GetKeyDown ("space") && specialOK) {
  77.             specialOK = false;
  78.             usingSpecial = true;
  79.         }
  80.  
  81.         // turn off special attack after a few seconds
  82.         if (usingSpecial) {
  83.             specialTimer = Mathf.Max (0, specialTimer - Time.deltaTime);
  84.             if (specialTimer == 0) {
  85.                 usingSpecial = false;
  86.             }
  87.         }
  88.  
  89.         // check if shooting
  90.         // use arrow keys to shoot
  91.         if (!falling)
  92.             updateShooting ();
  93.  
  94.         if (falling) {
  95.             // fall to bottom of screen
  96.             rigidbody2D.velocity += new Vector2(0, -0.4f);
  97.             animator.SetBool ("hit", true);
  98.         }
  99.         else animator.SetBool("hit", false);
  100.  
  101.         updateInvincibility();
  102.  
  103.         // smoke trail
  104.         if (!falling)
  105.             updateSmoke();
  106.     }
  107.  
  108.     void updateSmoke() {
  109.         jetpackSmokeTime = Mathf.Max (0, jetpackSmokeTime - Time.deltaTime);
  110.  
  111.         if (jetpackSmokeTime > 0) return;
  112.  
  113.         Vector3 pos = new Vector3(transform.position.x-(Orient(0.25f)), transform.position.y-0.15f, transform.position.z+2.0f);
  114.         Instantiate(jetpackSmoke, pos, Quaternion.identity);
  115.         jetpackSmokeTime = 0.025f;
  116.     }
  117.  
  118.     // world wrap-around if not hit:
  119.     // respawn at center of screen if hit
  120.     void OnBecameInvisible() {
  121.  
  122.         if (!hit) {
  123.             Vector3 pos = Camera.main.WorldToScreenPoint (transform.position);
  124.             if (pos.x < 0 || pos.x > screenWidth) {
  125.                 transform.position = new Vector2 (-transform.position.x, transform.position.y);
  126.             }
  127.             if (pos.y < 0 || pos.y > screenHeight) {
  128.                 transform.position = new Vector2 (transform.position.x, -transform.position.y);
  129.             }
  130.         }
  131.  
  132.         else if (falling && transform.position.y > 0) return;
  133.  
  134.         // if falling off-screen (at bottom only) due to damage and still have lives left
  135.         else if (extraLives >= 0) {
  136.  
  137.             rigidbody2D.velocity = new Vector2(0, 0);
  138.             transform.position = new Vector2(0, 0);
  139.             invincibleTime = 2.0f;
  140.             falling = false;
  141.         }
  142.         else {
  143.             Application.LoadLevel ("gameover");
  144.         }
  145.     }
  146.  
  147.     void flip() {
  148.         facingRight = !facingRight;
  149.         Vector3 nextScale = transform.localScale;
  150.         nextScale.x *= -1f;
  151.         transform.localScale = nextScale;
  152.     }
  153.  
  154.     // each key press checks if usingSpecial is turned ON:
  155.     // if it is, fire an extra bullet in opposite direction.
  156.     // usingSpecial turns off after a while (see Update() for details)
  157.     void updateShooting() {
  158.         float speed = 300f; // force to apply to bullet
  159.         bool shoot = false; // only shoot if keys are pressed
  160.         Vector3 bulletpos = new Vector3(transform.position.x, transform.position.y+0.02f, transform.position.z+1.0f); // place behind player
  161.         Vector3 bulletforce = new Vector3(speed, speed);
  162.         Vector3 directionVector = new Vector3 (0, 0); // used to pick shooting direction
  163.        
  164.         bulletCooldown = Mathf.Max (0, bulletCooldown - Time.deltaTime);
  165.  
  166.         if (bulletCooldown > 0) return;
  167.        
  168.         if (Input.GetKey ("up")) {
  169.             directionVector = new Vector3(0, 1);
  170.             shoot = true;
  171.             bulletCooldown = 0.35f;
  172.         }
  173.         else if (Input.GetKey ("down")) {
  174.             directionVector = new Vector3(0, -1);
  175.             shoot = true;
  176.             bulletCooldown = 0.35f;
  177.         }
  178.         else if (Input.GetKey ("left")) {
  179.             directionVector = new Vector3(-1, 0);
  180.             shoot = true;
  181.             bulletCooldown = 0.35f;
  182.         }
  183.         else if (Input.GetKey ("right")) {
  184.             directionVector = new Vector3(1, 0);
  185.             shoot = true;
  186.             bulletCooldown = 0.35f;
  187.         }
  188.  
  189.         // bulletforce changes direction depending on which keys are pressed
  190.         bulletforce.x *= directionVector.x;
  191.         bulletforce.y *= directionVector.y;
  192.  
  193.         if (shoot) {
  194.             GameObject bulletObject = (GameObject)Instantiate (bullet, bulletpos, Quaternion.identity);
  195.             bulletObject.rigidbody2D.AddForce (bulletforce);
  196.  
  197.             // if in mode2 and special attack is active, shoot another bullet in the opposite direction
  198.             if (usingSpecial) {
  199.                 GameObject bulletObject2 = (GameObject)Instantiate (bullet, bulletpos, Quaternion.identity);
  200.                 bulletObject2.rigidbody2D.AddForce (-bulletforce);
  201.             }
  202.         }
  203.     }
  204.  
  205.     void updateInvincibility() {
  206.         invincibleTime = Mathf.Max (0, invincibleTime - Time.deltaTime);
  207.         Color nextColor = spriteRenderer.color;
  208.  
  209.         if (invincibleTime == 0) {
  210.             nextColor.a = 1.0f;
  211.             spriteRenderer.color = nextColor;
  212.             hit = false;
  213.         }
  214.         else {
  215.             nextColor.a = ((int)(invincibleTime*15)) % 2 == 0 ? 0.75f : 0.25f;
  216.             spriteRenderer.color = nextColor;
  217.         }
  218.     }
  219.  
  220.     void getHit() {
  221.         hit = true;
  222.         falling = true;
  223.         extraLives--;
  224.         invincibleTime = 2.0f;
  225.  
  226.         rigidbody2D.velocity = new Vector2(0, 0);
  227.         rigidbody2D.AddForce(new Vector2(0, 400));
  228.     }
  229.  
  230.     float Orient(float f) {
  231.         return facingRight ? f : -f;
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement