| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- using Base.Common;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using cfg;
- using TMPro;
- public class RockRoller : Enemy
- {
- public int hitTime = 1;
- public int curHitTime = 0;
- public RockAttackTrigger attackTrigger0;
- public AttackTrigger attackEnemyTrigger0;
- public List<TargetType> initialTargetTypes = new List<TargetType>();
- public GameObject stone;
- public string smallStone;
- public float baseRotateSpeed = 90f;
- public float rotateMul = 1f;
- private float rotateDirection = -1f;
- private float dieTime = 0.1f;
- private float deathTimer = 0f;
- private bool isDying;
- private bool isFalled = false;
- public Enemy owner;
- private bool isGather = false;
- public float gatherTime = 1f;
- private float gatherTimer = 0f;
- private bool isWeak = false;
- public float weakTime = 1f;
- private float weakTimer = 0f;
- public float addForce = 1f;
- public SpriteRenderer RockSpriterRenderer;
- public List<Sprite> spriteList = new List<Sprite>();
- public List<float> scaleList = new List<float>();
- public bool isBig;
- private string enemyStr;
- public GameObject stoneBreakEff;
- public Transform effectSpawnPoint;
- public float effectDestroyDelay = 2f;
- public float smallStoneFoece = 20f;
- public override void Init()
- {
- base.Init();
- }
- private void Awake()
- {
- base.Awake();
- InitializeComponents();
- InitializeAttackMethods();
- InitializeTargetTypes();
- InitializeStoneReference();
- InitializeSpriter();
- InitializeScale();
- InitializeEffectSpawnPoint();
- }
- private void InitializeComponents()
- {
- TryGetComponent<Rigidbody>(out rb);
- }
- private void InitializeAttackMethods()
- {
- if (attackTrigger0 != null && attackTrigger0.attackMethod.attackInfo.damage == 0 &&
- attackEnemyTrigger0 != null && attackEnemyTrigger0.attackMethod.attackInfo.damage == 0)
- {
- attackTrigger0.attackMethod = attackController.attackMethod_march[0];
- attackEnemyTrigger0.attackMethod = attackController.attackMethod_summon[0];
- }
- }
- private void InitializeTargetTypes()
- {
- if (attackController != null && attackController.targetTypes != null)
- {
- initialTargetTypes.Clear();
- initialTargetTypes.AddRange(attackController.targetTypes);
- }
- }
- private void InitializeStoneReference()
- {
- if (stone == null)
- {
- stone = gameObject;
- }
- enemyStr = $"Prefab/Enemy/{smallStone}";
- }
- private void InitializeSpriter()
- {
- if (RockSpriterRenderer != null)
- {
- changeSpriter(0);
- }
-
- }
- private void InitializeScale()
- {
- changeScale(0);
- }
- public void changeSpriter(int id)
- {
- RockSpriterRenderer.sprite = spriteList[id];
- }
- public void changeScale(int id)
- {
- transform.localScale = new Vector3(1f, 1f, 1f)*scaleList[id];
- }
- private void InitializeEffectSpawnPoint()
- {
- if (effectSpawnPoint == null && stone != null)
- {
- effectSpawnPoint = stone.transform;
- }
- }
- protected override void OnEnable()
- {
- base.OnEnable();
- ResetState();
- }
- private void ResetState()
- {
- hitFeedbackSystem.canBeHitStun = false;
- hitFeedbackSystem.canFreeze = false;
- isDying = false;
- isFalled = false;
- deathTimer = 0f;
- gatherTimer = 0f;
- isGather = false;
- weakTimer = 0f;
- isWeak = false;
- rotateDirection = -1f;
- curHitTime = 0;
- stone.SetActive(true);
- if (state == CharacterState.None)
- {
- ChangeState(CharacterState.Idle);
- }
- if (attackEnemyTrigger0 != null)
- attackEnemyTrigger0.gameObject.SetActive(false);
- if (attackTrigger0 != null)
- attackTrigger0.gameObject.SetActive(true);
- changeSpriter(0);
- changeScale(0);
- ResetTargetTypes();
- }
- private void ResetTargetTypes()
- {
- if (attackController != null && attackController.targetTypes != null)
- {
- attackController.targetTypes.Clear();
- attackController.targetTypes.AddRange(initialTargetTypes);
- }
- }
- public void Update()
- {
- UpdateHitFeedback();
- if (owner == null) return;
- HandleOwnerSpecialState();
- Rotate();
- if (GameManager.instance.gameType == GameType.GameEnd)
- {
- ChangeState(CharacterState.Die);
- }
- }
- public void Rotate()
- {
- if (stone == null) return;
- float currentSpeed = baseRotateSpeed;
- if (rb != null)
- {
- currentSpeed = rb.velocity.magnitude * rotateMul * baseRotateSpeed;
- currentSpeed = Mathf.Clamp(currentSpeed, 0, baseRotateSpeed * 5);
- }
- stone.transform.Rotate(Vector3.forward, rotateDirection * currentSpeed * Time.deltaTime);
- }
- private void PlayBreakEff()
- {
- Vector3 spawnPos = effectSpawnPoint.position;
- GameObject effectObj;
- if (PoolManager.instance != null)
- {
- effectObj = PoolManager.Instantiate(stoneBreakEff, spawnPos);
- }
- else
- {
- effectObj = Instantiate(stoneBreakEff, effectSpawnPoint);
- }
- effectObj.transform.localScale = transform.localScale;
- // 自动销毁特效,防止内存泄漏
- if (effectDestroyDelay > 0)
- {
- Destroy(effectObj, effectDestroyDelay);
- }
- }
- private void UpdateHitFeedback()
- {
- hitFeedbackSystem.canBeHitStun = false;
- hitFeedbackSystem.canFreeze = false;
- }
- private void HandleOwnerSpecialState()
- {
- if (owner.state == CharacterState.SpecialStatus_BlowUp)
- {
- if (attackTrigger0 != null)
- attackTrigger0.gameObject.SetActive(false);
- UpdateTargetTypesForAttack();
- }
- }
- private void UpdateTargetTypesForAttack()
- {
- if (attackController != null)
- {
- attackController.targetTypes.Clear();
- attackController.targetTypes.Add(TargetType.Enemy);
- attackController.targetTypes.Add(TargetType.EnemyTower);
- }
- }
- public override void FixedUpdate()
- {
- OnState();
- HandleDyingState();
- }
- private void HandleDyingState()
- {
- if (owner != null && owner.state == CharacterState.SpecialStatus_BlowUp &&
- owner.attributeStatus.hitState == 1 && !isDying)
- {
- stone.SetActive(false);
- StartDying();
- }
- if (isDying)
- {
- deathTimer += Time.deltaTime;
- if (deathTimer >= dieTime)
- {
- HandleDeath();
- }
- }
- }
- private void StartDying()
- {
- if (attackEnemyTrigger0 != null)
- attackEnemyTrigger0.gameObject.SetActive(true);
- isDying = true;
- isFalled = true;
- deathTimer = 0f;
- }
- private void HandleDeath()
- {
- if (owner != null && curHitTime >= hitTime)
- {
- PlayBreakEff();
- owner.gameObject.SetActive(false);
- if (isBig)
- {
- SpawnSmallStones();
- }
- curHitTime = 0;
- }
- isDying = false;
- }
- private void SpawnSmallStones()
- {
- attackEnemyTrigger0.gameObject.SetActive(false);
- ResetTargetTypes();
- SingleEnemyConfig cfgEnemy = GameManager.instance.allCfgData.CfgEnemy.Get("小滚石怪");
- // 生成第一个小石头
- GameObject stone1 = Util.Instantiate(enemyStr, transform.position - new Vector3(0, 0, 0), active: true);
- ConfigureSmallStone(stone1, cfgEnemy);
- SmallStoneForce(stone1, new Vector3(-1, 1.2f, 0), smallStoneFoece);
- // 生成第二个小石头
- GameObject stone2 = Util.Instantiate(enemyStr, transform.position - new Vector3(0, 0, 0), active: true);
- ConfigureSmallStone(stone2, cfgEnemy);
- SmallStoneForce(stone2, new Vector3(1, 1.2f, 0), smallStoneFoece);
- isFalled = false;
- }
- private void SmallStoneForce(GameObject stone, Vector3 forceDirection, float forceMul)
- {
- Rigidbody rockRb = stone.GetComponent<Rigidbody>();
- RockRoller Roller = stone.GetComponent<RockRoller>();
- Roller.ChangeState(CharacterState.Fall);
- rockRb.AddForce(forceDirection * forceMul, ForceMode.Impulse);
- }
- private void ConfigureSmallStone(GameObject stoneObj, SingleEnemyConfig cfgEnemy)
- {
- RockRoller rock = stoneObj.GetComponent<RockRoller>();
- if (rock != null)
- {
- Debug.Log(cfgEnemy.AttackMarch[0]);
- Debug.Log(cfgEnemy.AttackSummon);
- Debug.Log(cfgEnemy.MinMoveSpeed);
- Debug.Log(cfgEnemy.MaxMoveSpeed);
- rock.attackTrigger0.attackMethod.attackInfo.damage = cfgEnemy.AttackMarch[0];
- rock.attackEnemyTrigger0.attackMethod.attackInfo.damage = cfgEnemy.AttackSummon;
- rock.owner.moveSpeed = Random.Range(cfgEnemy.MinMoveSpeed, cfgEnemy.MaxMoveSpeed);
- }
- }
- public override void ChangeState(CharacterState newState)
- {
- if (state == newState || newState == CharacterState.FramePause || state == CharacterState.Die)
- {
- return;
- }
- ExitCurrentState();
- EnterNewState(newState);
- }
- private void ExitCurrentState()
- {
- switch (state)
- {
- case CharacterState.Idle:
- rb.velocity = Vector3.zero;
- break;
- case CharacterState.Die:
- isDie = false;
- break;
- }
- }
- private void EnterNewState(CharacterState newState)
- {
- CharacterState oldState = state;
- state = newState;
- switch (newState)
- {
- case CharacterState.Idle:
- EnterIdleState();
- break;
- case CharacterState.Rush:
- EnterRushState();
- break;
- case CharacterState.Rise:
- EnterRiseState();
- break;
- case CharacterState.Fall:
- EnterFallState();
- break;
- case CharacterState.Die:
- EnterDieState();
- break;
- case CharacterState.SpecialStatus_BlowUp:
- EnterBlowUpState();
- break;
- }
- }
- private void EnterIdleState()
- {
- rotateDirection = -1f;
- rb.velocity = Vector3.zero;
- if (attackEnemyTrigger0 != null) attackEnemyTrigger0.gameObject.SetActive(false);
- if (attackTrigger0 != null) attackTrigger0.gameObject.SetActive(false);
- }
- private void EnterRushState()
- {
- rb.velocity = Vector3.zero;
- if (attackEnemyTrigger0 != null) attackEnemyTrigger0.gameObject.SetActive(false);
- if (attackTrigger0 != null) attackTrigger0.gameObject.SetActive(true);
- }
- private void EnterRiseState()
- {
- isGather = true;
- gatherTimer = 0f;
- }
- private void EnterFallState()
- {
- rotateDirection = 1f;
- weakTimer = 0f;
- }
- private void EnterDieState()
- {
- isDie = true;
- dieKeepTime = totalDieKeepTime;
- if (GameManager.instance.gameType != GameType.GameEnd)
- {
- DropSouls();
- }
- }
- private void EnterBlowUpState()
- {
- rotateDirection = 1f;
- curHitTime += 1;
- isDying = false;
- isFalled = true;
- deathTimer = 0f;
- if (owner != null) owner.attributeStatus.hitState = 0;
- }
- public override void OnState()
- {
- switch (state)
- {
- case CharacterState.Idle:
- ChangeState(CharacterState.Rise);
- break;
- case CharacterState.Rise:
- HandleRiseState();
- break;
- case CharacterState.Rush:
- HandleRushState();
- break;
- case CharacterState.Fall:
- HandleFallState();
- break;
- case CharacterState.SpecialStatus_BlowUp:
- attributeStatus.SpecialStateEffect(SpecialState.BlownUp);
- break;
- case CharacterState.Die:
- dieKeepTime -= Time.deltaTime;
- if (dieKeepTime <= 0)
- {
- if (killer != null && killer.GetComponent<Demonic>())
- {
- SoldierType st = killer.GetComponent<Demonic>().soldierType;
- SoldierEXP.expInstance.AddEXP(st, (int)(exp * LevelSelect.EXPRatio + 0.5f));
- }
- if (dieEffect)
- {
- PoolManager.Instantiate(dieEffect, transform.position);
- }
- GameManager gameManager = GameManager.instance;
- if (gameManager.gameType == GameType.GameEnd)
- {
- GameObject fx = PoolManager.Instantiate(gameManager.dropGoldFX);
- fx.transform.position = transform.position;
- GameObject injuryNum = PoolManager.Instantiate(gameManager.dropGoldText);
- injuryNum.transform.position = new Vector3(
- transform.position.x + injuryNumPos_march.x + Random.Range(-injuryNumRandom_march.x / 2f, injuryNumRandom_march.x / 2f),
- transform.position.y + injuryNumPos_march.y + Random.Range(-injuryNumRandom_march.y / 2f, injuryNumRandom_march.y / 2f),
- transform.position.z);
- TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
- text.text = $"+{gameManager.enemyGoldDrop}";
- }
- gameObject.SetActive(false);
- break;
- }
- break;
- break;
- }
- }
- private void HandleRiseState()
- {
- if (isGather && gatherTimer < gatherTime)
- {
- gatherTimer += Time.deltaTime;
- }
- else
- {
- Debug.Log("gatherFinsh");
- isGather = false;
- ChangeState(CharacterState.Rush);
- }
- }
- private void HandleRushState()
- {
- Vector3 velocity = rb.velocity;
- if (Mathf.Abs(velocity.x) < moveSpeed)
- {
- velocity.x += addForce;
- }
- rb.velocity = velocity;
- }
- private void HandleFallState()
- {
- if (!foot.TrigGround)
- {
- isWeak = true;
- }
- if (isWeak && weakTimer < weakTime && foot.TrigGround)
- {
- rb.velocity = Vector3.zero;
- weakTimer += Time.deltaTime;
- }
- else if (weakTimer >= weakTime)
- {
- isWeak = false;
- ChangeState(CharacterState.Idle);
- }
- }
- public void OnDisable()
- {
- EnemyCreater.instance.OnEnemyRecycle(this);
- ResetState();
- }
- }
|