Advertisement
dantepw

Untitled

Jul 23rd, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour {
  5.    
  6.     private float health = 300f;
  7.    
  8.    
  9.     public GameObject laserPrefab;
  10.     public ParticleSystem[] particlePrefabs;
  11.    
  12.     public float speed;
  13.     public float laserSpeed; //velocidade do tiro
  14.     public float laserRate; //a cada X segundos sai 1 tiro
  15.    
  16.     public float xmin;
  17.     public float xmax;
  18.     public float ymin;
  19.     public float ymax;
  20.    
  21.     //permite 1 tiro a cada X tempo
  22.     public float interval;
  23.     float nextShot = 0.1f;
  24.    
  25.    
  26.     void Start()
  27.     {
  28.         Clamping();
  29.     }
  30.    
  31.    
  32.     // Update is called once per frame
  33.     void Update ()
  34.     {
  35.         MoveKeyboard();
  36.         MoveMobile();
  37.     }
  38.    
  39.     //evita que o player saia do campo de visao da tela. Aqui estamos atribuindo os valores
  40.     void Clamping()
  41.     {
  42.         //descobre qual eh o tamanho da tela e assim me diz quais sao os limites
  43.         float distance = transform.position.z - Camera.main.transform.position.z;
  44.        
  45.         //padding da posição X e Y
  46.         float paddingX = transform.GetComponent<Renderer>().bounds.max.x;
  47.         float paddingY = 0.4f;
  48.        
  49.        
  50.         Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance)); //left edge
  51.         Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance)); //right edge  
  52.        
  53.         Vector3 bottommost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance));
  54.         Vector3 topmost = Camera.main.ViewportToWorldPoint(new Vector3(0,1,distance));
  55.        
  56.        
  57.         xmin = leftmost.x + paddingX;
  58.         xmax = rightmost.x - paddingX;
  59.         ymin = bottommost.y + paddingY;
  60.         ymax = topmost.y - paddingY;
  61.     }
  62.    
  63.     void MoveKeyboard()
  64.     {
  65.         float vertical = Input.GetAxis("Vertical") * speed;
  66.         float horizontal = Input.GetAxis("Horizontal") * speed;
  67.        
  68.         vertical *= Time.deltaTime;
  69.         horizontal *= Time.deltaTime;
  70.         transform.Translate (horizontal, vertical, 0f);
  71.        
  72.         //impede que o jogador saia da tela
  73.         float newX = Mathf.Clamp (transform.position.x, xmin, xmax);
  74.         float newY = Mathf.Clamp (transform.position.y, ymin, ymax);
  75.        
  76.         transform.position = new Vector3(newX, newY, transform.position.z);
  77.        
  78.         if (Input.GetKeyDown(KeyCode.Space) || Input.GetKey(KeyCode.Space))
  79.         {
  80.             // only shoot after interval
  81.             if (Time.time >= nextShot)
  82.             {
  83.                 nextShot = Time.time + interval; // update nextShot
  84.                 ShootLaser();
  85.             }
  86.             //InvokeRepeating("ShootLaser", 0.000001f, laserRate);
  87.         }
  88.     }
  89.    
  90.     void MoveMobile()
  91.     {
  92.         if (Input.touchCount > 0)
  93.         {
  94.             // The screen has been touched so store the touch
  95.             Touch touch = Input.GetTouch(0);
  96.            
  97.             // If the finger is on the screen, move the object smoothly to the touch position
  98.             if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
  99.             {
  100.                 // only shoot after interval
  101.                 if (Time.time >= nextShot)
  102.                 {
  103.                     nextShot = Time.time + interval; // update nextShot
  104.                     ShootLaser();
  105.                 }
  106.                
  107.                 Vector3 offset = new Vector3 (0, 0.5f, 0);
  108.                 Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));                
  109.                 transform.position = Vector3.Lerp(transform.position, touchPosition + offset, speed * Time.deltaTime);
  110.             }
  111.         }
  112.     }
  113.    
  114.     void ShootLaser()
  115.     {
  116.         print ("Laser shot");
  117.        
  118.         Vector3 offset = new Vector3 (0, 0.3f, 0);
  119.         GameObject laser = Instantiate (laserPrefab, transform.position + offset, Quaternion.identity) as GameObject;
  120.         laser.GetComponent<Rigidbody2D>().velocity = new Vector3 (0, laserSpeed, 0);
  121.     }
  122.    
  123.     void OnTriggerEnter2D(Collider2D col)
  124.     {
  125.         LaserEnemy laser = col.gameObject.GetComponent<LaserEnemy>();
  126.        
  127.         if (laser)
  128.         {
  129.             health -= laser.GetDamage();
  130.             laser.Hit ();
  131.             print ("Player hit by laser! Health: " + health);
  132.            
  133.             if (health == 100f)
  134.             {
  135.                 print ("VIDA ACABANDO!");
  136.                 //GameObject fire = (GameObject) Instantiate (particlePrefabs[0], this.transform.position, Quaternion.identity);
  137.                 //fire.transform.parent = this.transform;
  138.             }
  139.            
  140.             if (health <= 0)
  141.             {
  142.                 print ("Player morreu!");
  143.                 Destroy (this.gameObject);
  144.                 Instantiate (particlePrefabs[1], this.transform.position, Quaternion.identity);
  145.             }
  146.         }
  147.     }
  148.  
  149.     bool TurnFireOn()
  150.     {
  151.         if (health == 100f)
  152.             return true;
  153.         else
  154.             return false;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement