| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class Block : Character
- {
- public enum State
- {
- outGround = 0, //出土
- charge = 1, //蓄力
- sprint = 2, //冲刺
- bomb = 3, //爆炸
- none = 4,
- back = 5, //被击飞后退
- warning = 6, //预警
- drop = 7, //坠落
- }
- [Header("属性")]
- public YuMenGuan ymg;
- public bool isSpecial;
- public bool isSpecialDie;
- [Header("掉落魂")]
- public GameObject soulPrefab;
- public float soulStartSpeed = 1f;
- public int dropSoul;
- public float dropSoulAngle = 60f;
- [Header("砖头怪攻击状态")]
- public State curState;
- public float chargeTime; //空中蓄力时长
- private float hasChargeTime;
- [Header("冲出地面")]
- public float outSpeed;
- public float maxHeight, minHeight; //随即高度
- public float height;
- public AttackInfo outDamage;
- [Header("向前冲刺")]
- public float sprintAccSpeed; //冲刺加速度
- private float curSpeed; //当前速度
- [Header("警告")]
- public GameObject warning;
- public Vector3 posYZ;
- private GameObject curWarning;
- public float warningTime;
- private float hasWarningTime;
- [Header("爆炸")]
- public GameObject bombPrefab;
- private bool isTowerBomb;
- private GameObject curBomb;
- [Header("攻击来源")]
- public BeHitTrigger hitTrigger;
- public int attackerID;
- [Header("击飞轨迹")]
- public float backForceRate; //击飞的力的倍数
- public void Ready()
- {
- Vector3 ps = transform.position;
- ps.y = posYZ.y;
- ps.z = posYZ.z;
- curWarning = PoolManager.Instantiate(warning, ps, new Quaternion(0, 0, 0, 0), null);
- hp = totalHp;
- if (!isSpecial)
- {
- height = Random.Range(minHeight, maxHeight);
- if (height > ymg.mostHeight)
- {
- ymg.mostHeight = height;
- }
- ChangeAttackState(State.warning);
- }
- else
- {
- ChangeAttackState(State.outGround);
- }
- }
- public override void ChangeState(CharacterState newState)
- {
- if (state == newState)
- {
- return;
- }
- switch (state)
- {
- default:
- break;
- }
- state = newState;
- switch (newState)
- {
- case CharacterState.Die:
- if (isSpecial)
- {
- ymg.AllBlocksDrop();
- }
- if (!isTowerBomb)
- {
- ChangeAttackState(State.none);
- }
- curWarning.SetActive(false);
- dieKeepTime = totalDieKeepTime;
- break;
- default:
- break;
- }
- }
- public override void OnState()
- {
- switch (state)
- {
- case CharacterState.Die:
- dieKeepTime -= Time.deltaTime;
- if (!isDie && dieKeepTime <= 0)
- {
- if (curBomb != null || isSpecialDie)
- {
- DropSouls();
- }
- isDie = true;
- bodyTrans.gameObject.SetActive(false);
- }
- if (curBomb != null && !curBomb.activeSelf ||isDie && curBomb == null)
- {
- gameObject.SetActive(false);
- }
- break;
- default:
- break;
- }
- }
- public void DropSouls()
- {
- if (dropSoul > 1)
- {
- for (int i = 0; i < dropSoul; i++)
- {
- float angleInterval = dropSoulAngle / (float)(dropSoul - 1);
- float angle = 90 + ((float)i - (float)(dropSoul - 1) / 2) * angleInterval;
- angle = angle / 180 * Mathf.PI;
- GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
- Vector3 dir = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);
- soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
- }
- }
- else
- {
- GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
- Vector3 dir = Vector3.up;
- soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
- }
- }
- public override void BeHit(int damage)
- {
- if (hitTrigger.attackerID == 2)
- {
- rb.velocity = Vector3.zero;
- ChangeAttackState(State.back);
- return;
- }
- else
- {
- hp -= damage;
- //伤害跳字
- if (showInjuryNum)
- {
- GameObject injuryNum = PoolManager.Instantiate(injuryNumText);
- injuryNum.transform.position = new Vector3(transform.position.x + Random.Range(-1f, 1f), transform.position.y + 1, transform.position.z);
- TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
- text.text = damage.ToString();
- if (gameObject.CompareTag("Player"))
- {
- text.color = Color.red;
- }
- }
- if (hp <= 0)
- {
- ChangeState(CharacterState.Die);
- return;
- }
- }
- }
- public void ChangeAttackState(State newState)
- {
- switch (curState)
- {
- case State.outGround:
- rb.velocity = Vector3.zero;
- break;
- case State.charge:
- break;
- case State.sprint:
- break;
- case State.bomb:
- break;
- default:
- break;
- }
- curState = newState;
- switch (newState)
- {
- case State.warning:
- curWarning.SetActive(true);
- hasWarningTime = warningTime;
- break;
- case State.outGround:
- if (!isSpecial && height == ymg.mostHeight)
- {
- ymg.SpecialBlockOut(this);
- gameObject.SetActive(false);
- }
- ani.Play("show", 0, 0);
- break;
- case State.charge:
- ani.Play("idle", 0, 0);
- hasChargeTime = 0;
- break;
- case State.sprint:
- //ani.Play("move_start", 0, 0);
- break;
- case State.bomb:
- ani.Play("charge", 0, 0);
- if (isTowerBomb)
- {
- rb.velocity = Vector3.zero;
- }
- curBomb = PoolManager.Instantiate(bombPrefab, transform.position, new Quaternion(0, 0, 0, 0), transform);
- ChangeState(CharacterState.Die);
- break;
- case State.none:
- rb.velocity = Vector3.zero;
- break;
- case State.back:
- DropSouls();
- rb.useGravity = true;
- break;
- case State.drop:
- GetComponentInChildren<Collider>().enabled = false;
- rb.velocity = Vector3.zero;
- rb.useGravity = true;
- break;
- default:
- break;
- }
- }
- public void OnAttackState()
- {
- switch (curState)
- {
- case State.warning:
- hasWarningTime -= Time.deltaTime;
- if (hasWarningTime <= 0)
- {
- ChangeAttackState(State.outGround);
- }
- break;
- case State.outGround:
- Vector3 ve = rb.velocity;
- ve.y = outSpeed;
- rb.velocity = ve;
- if (transform.position.y >= height)
- {
- curWarning.SetActive(false);
- ChangeAttackState(State.charge);
- }
- break;
- case State.charge:
- hasChargeTime += Time.deltaTime;
- if (hasChargeTime >= chargeTime)
- {
- ChangeAttackState(State.sprint);
- }
- break;
- case State.sprint:
- if (targetCharacter == null || targetCharacter.isDie)
- {
- CheckTarget();
- }
- Vector3 dir;
- if (targetCharacter.transform.position.x >= transform.position.x)
- {
- dir = Vector3.right;
- }
- else
- {
- dir = Vector3.left;
- }
- curSpeed += sprintAccSpeed * Time.deltaTime;
- rb.velocity = dir * curSpeed;
- if (transform.position.x >= targetCharacter.transform.position.x)
- {
- isTowerBomb = true;
- ChangeAttackState(State.bomb);
- }
- break;
- case State.back:
- if (rb.useGravity && transform.position.y <= 1f)
- {
- rb.useGravity = false;
- Vector3 vee = rb.velocity;
- vee.y = 0;
- rb.velocity = vee;
- Vector3 poss = transform.position;
- poss.y = 1;
- transform.position = poss;
- ChangeState(CharacterState.Die);
- }
- break;
- case State.drop:
- if(rb.useGravity && transform.position.y <= 1f)
- {
- rb.useGravity = false;
- Vector3 vee = rb.velocity;
- vee.y = 0;
- rb.velocity = vee;
- Vector3 poss = transform.position;
- poss.y = 1;
- transform.position = poss;
- ChangeState(CharacterState.Die);
- }
- break;
- default:
- break;
- }
- }
- private void CheckTarget()
- {
- float minDis = -1;
- foreach (GameObject g in TowerMap.myTowers)
- {
- float dis = Vector2.Distance(g.transform.position, transform.position);
- if (minDis == -1 || dis < minDis)
- {
- minDis = dis;
- targetCharacter = g.GetComponent<Character>();
- }
- }
- }
- public override void FixedUpdate()
- {
- base.FixedUpdate();
- OnAttackState();
- }
- }
|