Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour {
- private float health = 300f;
- public GameObject laserPrefab;
- public ParticleSystem[] particlePrefabs;
- public float speed;
- public float laserSpeed; //velocidade do tiro
- public float laserRate; //a cada X segundos sai 1 tiro
- public float xmin;
- public float xmax;
- public float ymin;
- public float ymax;
- //permite 1 tiro a cada X tempo
- public float interval;
- float nextShot = 0.1f;
- void Start()
- {
- Clamping();
- }
- // Update is called once per frame
- void Update ()
- {
- MoveKeyboard();
- MoveMobile();
- }
- //evita que o player saia do campo de visao da tela. Aqui estamos atribuindo os valores
- void Clamping()
- {
- //descobre qual eh o tamanho da tela e assim me diz quais sao os limites
- float distance = transform.position.z - Camera.main.transform.position.z;
- //padding da posição X e Y
- float paddingX = transform.GetComponent<Renderer>().bounds.max.x;
- float paddingY = 0.4f;
- Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance)); //left edge
- Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance)); //right edge
- Vector3 bottommost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance));
- Vector3 topmost = Camera.main.ViewportToWorldPoint(new Vector3(0,1,distance));
- xmin = leftmost.x + paddingX;
- xmax = rightmost.x - paddingX;
- ymin = bottommost.y + paddingY;
- ymax = topmost.y - paddingY;
- }
- void MoveKeyboard()
- {
- float vertical = Input.GetAxis("Vertical") * speed;
- float horizontal = Input.GetAxis("Horizontal") * speed;
- vertical *= Time.deltaTime;
- horizontal *= Time.deltaTime;
- transform.Translate (horizontal, vertical, 0f);
- //impede que o jogador saia da tela
- float newX = Mathf.Clamp (transform.position.x, xmin, xmax);
- float newY = Mathf.Clamp (transform.position.y, ymin, ymax);
- transform.position = new Vector3(newX, newY, transform.position.z);
- if (Input.GetKeyDown(KeyCode.Space) || Input.GetKey(KeyCode.Space))
- {
- // only shoot after interval
- if (Time.time >= nextShot)
- {
- nextShot = Time.time + interval; // update nextShot
- ShootLaser();
- }
- //InvokeRepeating("ShootLaser", 0.000001f, laserRate);
- }
- }
- void MoveMobile()
- {
- if (Input.touchCount > 0)
- {
- // The screen has been touched so store the touch
- Touch touch = Input.GetTouch(0);
- // If the finger is on the screen, move the object smoothly to the touch position
- if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
- {
- // only shoot after interval
- if (Time.time >= nextShot)
- {
- nextShot = Time.time + interval; // update nextShot
- ShootLaser();
- }
- Vector3 offset = new Vector3 (0, 0.5f, 0);
- Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
- transform.position = Vector3.Lerp(transform.position, touchPosition + offset, speed * Time.deltaTime);
- }
- }
- }
- void ShootLaser()
- {
- print ("Laser shot");
- Vector3 offset = new Vector3 (0, 0.3f, 0);
- GameObject laser = Instantiate (laserPrefab, transform.position + offset, Quaternion.identity) as GameObject;
- laser.GetComponent<Rigidbody2D>().velocity = new Vector3 (0, laserSpeed, 0);
- }
- void OnTriggerEnter2D(Collider2D col)
- {
- LaserEnemy laser = col.gameObject.GetComponent<LaserEnemy>();
- if (laser)
- {
- health -= laser.GetDamage();
- laser.Hit ();
- print ("Player hit by laser! Health: " + health);
- if (health == 100f)
- {
- print ("VIDA ACABANDO!");
- //GameObject fire = (GameObject) Instantiate (particlePrefabs[0], this.transform.position, Quaternion.identity);
- //fire.transform.parent = this.transform;
- }
- if (health <= 0)
- {
- print ("Player morreu!");
- Destroy (this.gameObject);
- Instantiate (particlePrefabs[1], this.transform.position, Quaternion.identity);
- }
- }
- }
- bool TurnFireOn()
- {
- if (health == 100f)
- return true;
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement