1 using UnityEngine; 2 using System.Collections; 3 4 public enum WolfState 5 { 6 Idle, 7 Walk, 8 Attack, 9 Death 10 } 11 12 public class WolfBaby : MonoBehaviour { 13 public WolfState state = WolfState.Idle; 14 public string aniname_death; 15 //public string aniname_now; 16 public string aniname_walk; 17 public string aniname_idle; 18 public float time = 1; 19 public float speed=1; 20 public int hp = 100; 21 public int attack = 10; 22 public GameObject body; 23 public AudioClip miss_sound; 24 25 private int exp = 20;//杀死一只小狼获得的经验值 26 private float timer = 0; 27 public string aniname_now;//当前的状态 28 private CharacterController cc; 29 public float missRate = 0.2f; 30 public PlayerStates ps; 31 private Color normal; 32 33 private GameObject hudtextFollow; 34 private GameObject hudtextGo; 35 public GameObject hudtextPrefab; 36 private HUDText hudtext; 37 private UIFollowTarget followTarget; 38 39 public string aniname_normalattack;//普通攻击 40 public float time_normalattack;//普通攻击计时器 41 42 public string aniname_crazyattack;//疯狂攻击 43 public float time_crazyattack;//疯狂攻击计时器 44 public float crazyattack_rate;//疯狂攻击的几率 45 46 public string aniname_attack_now;//当前的攻击 47 public int attack_rate = 1;//攻击速率 每秒 48 private float attack_timer = 0; 49 50 51 private Transform target;//攻击的目标 52 public float minDistance = 2;//攻击的最小距离 53 public float maxDistance = 5;//攻击的最大距离 54 55 public WolfSpawn spawn; 56 57 58 void Awake() { 59 ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStates>(); 60 aniname_now = aniname_idle; 61 cc = transform.GetComponent<CharacterController>(); 62 normal = body.renderer.material.color; 63 hudtextFollow = transform.Find("HUDText").gameObject; 64 } 65 66 void Start() { 67 //hudtextGo = GameObject.Instantiate(hudtextPrefab, transform.position, Quaternion.identity) as GameObject; 68 hudtextGo = NGUITools.AddChild(HUDTextParent._instance.gameObject, hudtextPrefab); 69 //hudtextGo.transform.parent = HUDTextParent._instance.gameObject.transform; 70 hudtext = hudtextGo.GetComponent<HUDText>(); 71 followTarget = hudtextGo.GetComponent<UIFollowTarget>(); 72 //print(hudtextFollow.transform); 73 followTarget.target = hudtextFollow.transform; 74 //print(followTarget.target); 75 followTarget.gameCamera = Camera.main; 76 //followTarget.uiCamera = UICamera.current.GetComponent<Camera>(); 77 } 78 79 void Update() { 80 if (state == WolfState.Death) { 81 animation.CrossFade(aniname_death); 82 } 83 else if (state == WolfState.Attack) { 84 AutoAttack(); 85 } 86 else{ //巡逻 87 animation.CrossFade(aniname_now); 88 if (aniname_now == aniname_walk) { 89 cc.SimpleMove(transform.forward * speed); 90 } 91 timer += Time.deltaTime; 92 if (timer > time) { 93 RandomState(); 94 timer = 0; 95 } 96 97 } 98 99 if (Input.GetMouseButtonDown(0)) { 100 TakeDamage(2); 101 } 102 103 } 104 105 //随机转换Idle和Walk的状态 106 void RandomState() { 107 int value = Random.Range(0, 2); 108 if (value == 0) 109 { 110 aniname_now = aniname_idle; 111 } 112 else { 113 if(aniname_now!=aniname_walk){ 114 cc.transform.Rotate(transform.up * Random.Range(0, 360)); 115 } 116 aniname_now = aniname_walk; 117 } 118 119 } 120 121 //伤害效果 122 public void TakeDamage(int attack ) { 123 if (state == WolfState.Death) 124 return; 125 126 //如果小狼受到伤害的话,让小狼攻击主角 127 target = GameObject.FindGameObjectWithTag(Tags.player).transform; 128 state = WolfState.Attack; 129 130 float value = Random.Range(0f, 1f); 131 if (value < missRate) 132 { //miss效果 133 AudioSource.PlayClipAtPoint(miss_sound, transform.position); 134 hudtext.Add("Miss", Color.gray, 1); 135 } 136 else {//击中效果 137 hudtext.Add("--" + attack, Color.red, 1); 138 this.hp -= attack; 139 StartCoroutine(ShowBodyRed()); 140 if (hp < 0) { 141 state = WolfState.Death; 142 Destroy(this.gameObject, 2); 143 } 144 } 145 } 146 147 IEnumerator ShowBodyRed() { 148 body.renderer.material.color = Color.red; 149 yield return new WaitForSeconds(1f); 150 body.renderer.material.color = normal; 151 } 152 153 void AutoAttack() { 154 if (target != null) 155 { 156 float distance = Vector3.Distance(target.position, transform.position); 157 if (distance > maxDistance) 158 { 159 target = null; 160 state = WolfState.Idle; 161 } 162 else if (distance <= minDistance)//攻击 163 { 164 attack_timer += Time.deltaTime; 165 animation .CrossFade (aniname_attack_now); 166 if (aniname_attack_now == aniname_normalattack) { 167 if (attack_timer > time_normalattack) { 168 //伤害TODO 169 aniname_attack_now = aniname_idle; 170 } 171 } 172 else if (aniname_attack_now == aniname_crazyattack) { 173 if (attack_timer > time_crazyattack) { 174 //产生伤害TODO 175 aniname_attack_now = aniname_idle; 176 } 177 } 178 //连续攻击 179 if (attack_timer > 1 / attack_rate) { 180 RandomAttack(); 181 attack_timer = 0; 182 } 183 184 } 185 else 186 { //朝着角色行走 187 transform.LookAt(target.transform); 188 cc.SimpleMove(transform.forward * speed); 189 animation.CrossFade(aniname_walk); 190 } 191 } 192 else { 193 state = WolfState.Idle;//进行巡逻 194 } 195 } 196 197 //当MonoBehaviour将被销毁时,这个函数被调用 198 //此处就是当wolfbaby被销毁时,调用该函数 199 void OnDestory() { 200 spawn.minusNumber(); 201 ps.GetExp(exp); 202 BarNPC._instance.OnKillWolf(); 203 Destroy(hudtextGo); 204 } 205 206 //随机攻击状态 207 void RandomAttack() { 208 float value = Random.Range(0, 1); 209 if (value < crazyattack_rate) 210 { 211 aniname_attack_now = aniname_crazyattack; 212 } 213 else { 214 aniname_attack_now = aniname_normalattack; 215 } 216 } 217 218 //当鼠标放上时,修改鼠标显示样式 219 void OnMouseEnter() { 220 CursorManager._instance.SetAttack(); 221 } 222 }
时间: 2024-10-13 16:16:03