Created
April 6, 2014 01:53
-
-
Save jquave/10000495 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class Enemy : MonoBehaviour { | |
| public int hp = 5; | |
| public GameObject dropPrefab; | |
| public GameObject heartPrefab; | |
| int maxWalkSpeed = 3; | |
| int direction = 3; | |
| bool freeze = false; | |
| SpriteRenderer sr; | |
| float secsToFreeze = 1; | |
| public Player player; | |
| public bool isBoss = false; | |
| public tk2dSpriteAnimator tkanim; | |
| void Start () { | |
| sr = GetComponent<SpriteRenderer>() as SpriteRenderer; | |
| if(player==null) { | |
| player = FindObjectOfType(typeof(Player)) as Player; | |
| } | |
| isBoss = ((Random.Range(0,100)<1.0f) /*&&(Time.timeSinceLevelLoad>15)*/ ); | |
| if(isBoss) { | |
| transform.localScale = 2.0f*Vector3.one; | |
| hp = 150; | |
| maxWalkSpeed = 1; | |
| //tkanim.transform.localScale = 2.0f*Vector3.one; | |
| } | |
| else { | |
| if(Random.Range(0,100)<20) { | |
| // 20% of enemies are armored | |
| hp *= 2; | |
| } | |
| } | |
| direction = maxWalkSpeed; | |
| } | |
| //float lasty = 0; | |
| void Update () { | |
| if(freeze) return; | |
| //transform.position = new Vector3(transform.position.x+direction*Time.deltaTime, transform.position.y, transform.position.z); | |
| rigidbody2D.velocity = new Vector2(direction, rigidbody2D.velocity.y); | |
| if(direction>0) { | |
| transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x),transform.localScale.y,transform.localScale.z); | |
| } | |
| if(direction<0) { | |
| transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x),transform.localScale.y,transform.localScale.z); | |
| } | |
| //float yd = Mathf.Abs(transform.position.y - lasty); | |
| //lasty = transform.position.y; | |
| //if(rigidbody2D.velocity.magnitude>0.1f) { | |
| //if(yd>0.0000001f) { | |
| if(Mathf.Abs(rigidbody2D.velocity.y)>0.1f) { | |
| // Falling | |
| if(hp>5) | |
| tkanim.Play("ArmoredFalling"); | |
| else | |
| tkanim.Play("Falling"); | |
| } | |
| else { | |
| if(hp>5) | |
| tkanim.Play("ArmoredWalk"); | |
| else | |
| tkanim.Play("Walk"); | |
| } | |
| } | |
| void OnTriggerEnter2D(Collider2D c) { | |
| if(c.CompareTag("Bullet")) { | |
| //Debug.Log("bullet hit enenmy in OnTriggerEnter2D"); | |
| // Hit by bullet | |
| Bullet b = c.GetComponent<Bullet>() as Bullet; | |
| b.Hit(this); | |
| } | |
| } | |
| void OnCollisionEnter2D(Collision2D c) { | |
| if(c.collider.CompareTag("Enemy")) return; | |
| ContactPoint2D []contacts = c.contacts; | |
| foreach(ContactPoint2D contactPoint in contacts) { | |
| //Debug.Log("p: "+contactPoint.point.y+" - py: "+transform.position.y); | |
| float yd = (transform.position.y-contactPoint.point.y); | |
| //Debug.Log(yd); | |
| float spriteHeight = 0.38f * Mathf.Abs(transform.localScale.x); | |
| if((yd)<spriteHeight) { | |
| direction = -direction; | |
| transform.position -= Mathf.Abs(transform.localScale.x) * direction * Time.deltaTime * Vector3.left; | |
| //Debug.Log("TURN AROUND"); | |
| } | |
| //if(contactPoint.point.y>transform.position.y) | |
| // direction = -direction; | |
| } | |
| //direction = -direction; | |
| //if(c.collider.CompareTag("Wall")) direction = -direction; | |
| if(c.collider.CompareTag("Player")) { | |
| // Hit player, makes him drop metal | |
| //player.HurtMetal(5); | |
| //player.Hurt(35); | |
| } | |
| if(c.collider.CompareTag("Bullet")) { | |
| // Hit by bullet | |
| Bullet b = c.collider.GetComponent<Bullet>() as Bullet; | |
| b.Hit(this); | |
| Debug.Log("bullet hit enenmy in OnCollisionEnter2D"); | |
| } | |
| } | |
| public void FreezeForSecs(float secs) { | |
| secsToFreeze=secs; | |
| StartCoroutine(FreezeForTime()); | |
| } | |
| IEnumerator FreezeForTime() { | |
| StopCoroutine("FreezeForTime"); | |
| freeze = true; | |
| sr.color = new Color(0,0,255); | |
| yield return new WaitForSeconds(secsToFreeze); | |
| freeze = false; | |
| sr.color = new Color(255,255,255); | |
| yield return null; | |
| } | |
| IEnumerator Decolor() { | |
| yield return new WaitForSeconds(0.1f); | |
| sr.color = Color.white; | |
| tkanim.Sprite.color = Color.white; | |
| yield return null; | |
| } | |
| public void Deactivate() { | |
| freeze = true; | |
| StopAllCoroutines(); | |
| } | |
| public void Activate() { | |
| freeze = false; | |
| } | |
| public void Hurt(int amount) { | |
| sr.color = Color.red; | |
| tkanim.Sprite.color = Color.red; | |
| StartCoroutine(Decolor()); | |
| hp -= amount; | |
| if(hp<=0) { | |
| hp = 0; | |
| if(Random.Range(0,100)>=70) { | |
| // 30% chance of dropping anything at all | |
| // 20% chance of that drop being health | |
| if(Random.Range(0,100)<=20) { | |
| GameObject.Instantiate(heartPrefab, transform.position, Quaternion.identity); | |
| } | |
| else { | |
| // 80% chance of the drop being scra[ | |
| GameObject.Instantiate(dropPrefab, transform.position, Quaternion.identity); | |
| } | |
| } | |
| player.DidKillEnemy(); | |
| Destroy(gameObject); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment