Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PC : MonoBehaviour {
- // attributes
- public float moveSpeed;
- public GameObject bullet;
- public GameObject jetpackSmoke;
- public bool hit = false;
- public bool falling = false;
- public float specialTimer = 4.0f;
- public bool specialOK;
- public float invincibleTime = 0.0f;
- private int extraLives = 2;
- private float bulletCooldown = 0.0f;
- private bool usingSpecial = false;
- private float jetpackSmokeTime = 0.25f;
- private float screenHeight;
- private float screenWidth;
- public Animator animator;
- // private BoxCollider2D boxCollider;
- private SpriteRenderer spriteRenderer;
- bool facingRight;
- void Awake() {
- if (Application.loadedLevel == 1) // mode 1
- specialOK = false;
- else if (Application.loadedLevel == 2) // mode 2 (special attack variant)
- specialOK = true;
- moveSpeed = 3.0f;
- facingRight = true;
- screenHeight = Screen.height;
- screenWidth = Screen.width;
- animator = GetComponent<Animator>();
- // boxCollider = GetComponent<BoxCollider2D>();
- spriteRenderer = GetComponent<SpriteRenderer>();
- }
- void Start () {
- }
- void Update () {
- if (Input.GetKeyDown ("escape")) {
- Application.LoadLevel ("menu");
- }
- // map key presses to movement
- // use WASD to move
- float horizontal = Input.GetAxis ("Horizontal");
- float vertical = Input.GetAxis ("Vertical");
- if (horizontal != 0)
- horizontal *= moveSpeed;
- if (vertical != 0)
- vertical *= moveSpeed;
- // check for movement
- if (horizontal != 0 || vertical != 0) animator.SetBool("moving", true);
- else animator.SetBool ("moving", false);
- if (!falling)
- rigidbody2D.velocity = new Vector2(horizontal, vertical);
- if ((horizontal > 0 && !facingRight) || (horizontal < 0 && facingRight))
- flip();
- // check if activated special: can only use once
- // activate with spacebar
- if (Input.GetKeyDown ("space") && specialOK) {
- specialOK = false;
- usingSpecial = true;
- }
- // turn off special attack after a few seconds
- if (usingSpecial) {
- specialTimer = Mathf.Max (0, specialTimer - Time.deltaTime);
- if (specialTimer == 0) {
- usingSpecial = false;
- }
- }
- // check if shooting
- // use arrow keys to shoot
- if (!falling)
- updateShooting ();
- if (falling) {
- // fall to bottom of screen
- rigidbody2D.velocity += new Vector2(0, -0.4f);
- animator.SetBool ("hit", true);
- }
- else animator.SetBool("hit", false);
- updateInvincibility();
- // smoke trail
- if (!falling)
- updateSmoke();
- }
- void updateSmoke() {
- jetpackSmokeTime = Mathf.Max (0, jetpackSmokeTime - Time.deltaTime);
- if (jetpackSmokeTime > 0) return;
- Vector3 pos = new Vector3(transform.position.x-(Orient(0.25f)), transform.position.y-0.15f, transform.position.z+2.0f);
- Instantiate(jetpackSmoke, pos, Quaternion.identity);
- jetpackSmokeTime = 0.025f;
- }
- // world wrap-around if not hit:
- // respawn at center of screen if hit
- void OnBecameInvisible() {
- if (!hit) {
- Vector3 pos = Camera.main.WorldToScreenPoint (transform.position);
- if (pos.x < 0 || pos.x > screenWidth) {
- transform.position = new Vector2 (-transform.position.x, transform.position.y);
- }
- if (pos.y < 0 || pos.y > screenHeight) {
- transform.position = new Vector2 (transform.position.x, -transform.position.y);
- }
- }
- else if (falling && transform.position.y > 0) return;
- // if falling off-screen (at bottom only) due to damage and still have lives left
- else if (extraLives >= 0) {
- rigidbody2D.velocity = new Vector2(0, 0);
- transform.position = new Vector2(0, 0);
- invincibleTime = 2.0f;
- falling = false;
- }
- else {
- Application.LoadLevel ("gameover");
- }
- }
- void flip() {
- facingRight = !facingRight;
- Vector3 nextScale = transform.localScale;
- nextScale.x *= -1f;
- transform.localScale = nextScale;
- }
- // each key press checks if usingSpecial is turned ON:
- // if it is, fire an extra bullet in opposite direction.
- // usingSpecial turns off after a while (see Update() for details)
- void updateShooting() {
- float speed = 300f; // force to apply to bullet
- bool shoot = false; // only shoot if keys are pressed
- Vector3 bulletpos = new Vector3(transform.position.x, transform.position.y+0.02f, transform.position.z+1.0f); // place behind player
- Vector3 bulletforce = new Vector3(speed, speed);
- Vector3 directionVector = new Vector3 (0, 0); // used to pick shooting direction
- bulletCooldown = Mathf.Max (0, bulletCooldown - Time.deltaTime);
- if (bulletCooldown > 0) return;
- if (Input.GetKey ("up")) {
- directionVector = new Vector3(0, 1);
- shoot = true;
- bulletCooldown = 0.35f;
- }
- else if (Input.GetKey ("down")) {
- directionVector = new Vector3(0, -1);
- shoot = true;
- bulletCooldown = 0.35f;
- }
- else if (Input.GetKey ("left")) {
- directionVector = new Vector3(-1, 0);
- shoot = true;
- bulletCooldown = 0.35f;
- }
- else if (Input.GetKey ("right")) {
- directionVector = new Vector3(1, 0);
- shoot = true;
- bulletCooldown = 0.35f;
- }
- // bulletforce changes direction depending on which keys are pressed
- bulletforce.x *= directionVector.x;
- bulletforce.y *= directionVector.y;
- if (shoot) {
- GameObject bulletObject = (GameObject)Instantiate (bullet, bulletpos, Quaternion.identity);
- bulletObject.rigidbody2D.AddForce (bulletforce);
- // if in mode2 and special attack is active, shoot another bullet in the opposite direction
- if (usingSpecial) {
- GameObject bulletObject2 = (GameObject)Instantiate (bullet, bulletpos, Quaternion.identity);
- bulletObject2.rigidbody2D.AddForce (-bulletforce);
- }
- }
- }
- void updateInvincibility() {
- invincibleTime = Mathf.Max (0, invincibleTime - Time.deltaTime);
- Color nextColor = spriteRenderer.color;
- if (invincibleTime == 0) {
- nextColor.a = 1.0f;
- spriteRenderer.color = nextColor;
- hit = false;
- }
- else {
- nextColor.a = ((int)(invincibleTime*15)) % 2 == 0 ? 0.75f : 0.25f;
- spriteRenderer.color = nextColor;
- }
- }
- void getHit() {
- hit = true;
- falling = true;
- extraLives--;
- invincibleTime = 2.0f;
- rigidbody2D.velocity = new Vector2(0, 0);
- rigidbody2D.AddForce(new Vector2(0, 400));
- }
- float Orient(float f) {
- return facingRight ? f : -f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement