Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Silverkeep/Guardian of Silverkeep/Assets/Scripts/Enemy_StandardWeak.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
144 lines (127 sloc)
3.94 KB
This file contains 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_StandardWeak : MonoBehaviour | |
{ | |
#region Field | |
public float enemyMoveSpeed; | |
public int enemyHealth; | |
public int enemyDamage; | |
private Vector3 selfPosition; | |
public GameObject JoyStone; | |
private Vector3 moveDirection = Vector3.zero; | |
private CharacterController controller; | |
//this is a test for child toggle | |
public GameObject testChild001; | |
public GameObject water_baddie_front; | |
public GameObject water_baddie_back; | |
public GameObject water_baddie_left; | |
public GameObject water_baddie_right; | |
enum EnemyMentalState | |
{ | |
idle, | |
moving | |
//aggressive, | |
} | |
enum EnemyDirection | |
{ | |
up, | |
down, | |
left, | |
right | |
} | |
private EnemyMentalState mentalstate = EnemyMentalState.moving; | |
private EnemyDirection direction = EnemyDirection.right; | |
#endregion | |
//helper function that choose randomly from a enum | |
static T GetRandomEnum<T>() | |
{ | |
System.Array A = System.Enum.GetValues(typeof(T)); | |
T V = (T)A.GetValue(UnityEngine.Random.Range(0,A.Length)); | |
return V; | |
} | |
// Use this for initialization | |
void Start () | |
{ | |
StartCoroutine(EnemyBehavior()); | |
water_baddie_back = GameObject.Find("water_baddie_back"); | |
water_baddie_front = GameObject.Find("water_baddie_front"); | |
water_baddie_left = GameObject.Find("water_baddie_left"); | |
water_baddie_right = GameObject.Find("water_baddie_right"); | |
water_baddie_back.SetActive(false); | |
water_baddie_left.SetActive(false); | |
water_baddie_right.SetActive(false); | |
water_baddie_front.SetActive(true); | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
CharacterController controller = this.gameObject.GetComponent<CharacterController>(); | |
controller.isTrigger = true; | |
if(transform.position.y != 0) | |
transform.Translate(0, -transform.position.y, 0); | |
//updating selfPosition | |
selfPosition = new Vector3(transform.position.x, 0, transform.position.z); | |
//initiallize and updates self direction | |
Vector3 CurrentDirection = Vector3.right; | |
#region CurrentDirection choose(need improvement) | |
if(direction == EnemyDirection.up) | |
CurrentDirection = Vector3.forward; | |
else if(direction == EnemyDirection.down) | |
CurrentDirection = Vector3.back; | |
else if(direction == EnemyDirection.right) | |
CurrentDirection = Vector3.right; | |
else if(direction == EnemyDirection.left) | |
CurrentDirection = Vector3.left; | |
moveDirection = CurrentDirection * enemyMoveSpeed; | |
#endregion | |
//behavior implements based on mental state | |
if (mentalstate == EnemyMentalState.idle) | |
transform.Translate(0,0,0); | |
else if (mentalstate == EnemyMentalState.moving) | |
{ | |
//float amtToMove = enemyMoveSpeed * Time.deltaTime; | |
//transform.Translate (CurrentDirection * amtToMove); | |
controller.Move (moveDirection * Time.deltaTime); | |
} | |
//boundary check | |
if(transform.position.x <= -35 || transform.position.x >= 35 || transform.position.z >= 20 ||transform.position.z <= -20) | |
Destroy(this.gameObject); | |
} | |
#region EnemyBehavior | |
IEnumerator EnemyBehavior() | |
{ | |
while(true) | |
{ | |
yield return new WaitForSeconds(1f); | |
mentalstate = GetRandomEnum<EnemyMentalState>(); | |
direction = GetRandomEnum<EnemyDirection>(); | |
} | |
} | |
#endregion | |
//Func lose health, usually called by ammo unit | |
void gotHit(int dmg) | |
{ | |
/* | |
GameObject testchild = GameObject.Find("water_baddie_right"); | |
testchild.gameObject.SetActive(false); | |
testChild001.SetActive(true);*/ //hidden for done testing | |
//GameObject testchild2 = GameObject.Find ("water_baddie_left"); | |
//testchild2.gameObject.SetActive(false); | |
enemyHealth = enemyHealth - dmg; | |
//death and leave coin behind | |
if(enemyHealth <= 0) | |
{ | |
Instantiate(JoyStone, selfPosition, Quaternion.identity); | |
Destroy(this.gameObject); | |
} | |
} | |
//hit player by collide, call player gothit function | |
void OnTriggerEnter(Collider otherObject) | |
{ | |
//Debug.Log ("got hit"); | |
if (otherObject.tag == "Player") | |
{ | |
otherObject.gameObject.SendMessage("heroGotHit",enemyDamage); | |
} | |
} | |
} |