|
|
- using UnityEngine;
- using System.Collections;
-
- [RequireComponent(typeof(Rigidbody2D))]
- public class PlayerController : InteractableObject {
-
- public GameObject bullet;
-
- public float speed;
- public int health;
- public Renderer healthMat;
- public Color[] healthColor;
-
- public GameObject Explosion;
- public ParticleSystem boostEffect;
- private int bulletDirection = -1;
- private Rigidbody2D rigid;
- private float lastHit = 0.0f;
- private bool isDead = false;
-
- // Use this for initialization
- void Start() {
- rigid = GetComponent<Rigidbody2D>();
- rigid.AddForce(Vector2.right * 200);
-
- }
-
- // Update is called once per frame
- void Update() {
-
- }
-
-
-
-
- public void rotatePlayer() {
- if (isDead)
- return;
-
- rigid.velocity = Vector3.zero;
- transform.Rotate(new Vector3(0, 0, 5f));
-
-
- }
-
- public void movePlayer() {
- if (isDead)
- return;
-
- //Debug.Log(transform.rotation.eulerAngles.z * Mathf.Deg2Rad);
- boostEffect.startRotation = -transform.rotation.eulerAngles.z * Mathf.Deg2Rad;
- boostEffect.Play();
- rigid.AddForce(transform.right * speed,ForceMode2D.Impulse);
-
-
- }
-
-
-
- GameObject findClosestTarget() {
-
- GameObject[] targets = GameObject.FindGameObjectsWithTag("Enemy");
-
- GameObject tMin = null;
- float minDist = Mathf.Infinity;
- Vector3 currentPos = transform.position;
- foreach (GameObject t in targets) {
- float dist = Vector3.Distance(t.transform.position, currentPos);
- if (dist < minDist) {
- tMin = t;
- minDist = dist;
- }
- }
- return tMin;
- }
-
- public void shoot() {
- if (isDead)
- return;
-
- GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position + (transform.up * bulletDirection * 0.1f), transform.rotation);
- MissileController bulletScript = bulletClone.GetComponent<MissileController>();
- bulletScript.startVelocity = transform.up * bulletDirection;
- bulletDirection = -bulletDirection;
-
- bulletScript.ignoreList = GetComponents<Collider2D>();
- bulletScript.target = findClosestTarget();
- bulletScript.enabled = true;
-
- }
-
- private void setDead() {
- rigid.angularDrag = 0;
- rigid.drag = 1;
- rigid.constraints = RigidbodyConstraints2D.None;
- }
-
- public override void shot() {
-
- if (Time.time - lastHit < 1) {
- return;
- }
-
- health--;
-
- if (isDead) {
- if (Explosion != null)
- Instantiate(Explosion, transform.position, transform.rotation);
-
- Destroy(this.gameObject, 0.1f);
- }
-
-
- if (health <= 0) {
- isDead = true;
- setDead();
- health = 0;
- }
- healthMat.material.color = healthColor[3-health];
- lastHit = Time.time;
-
-
-
-
- }
- }
|