void Fly() { Transform target; animator.SetBool("Fly", true); if (m_RigidBody2D.position.x >= middlePoint.position.x) { target = enterPoint; } else { target = exitPoint; } AttackTimer -= Time.deltaTime; if (AttackTimer <= 0) { nextAttackState = WizardAttackState.STATE_RECOVERY; RecoverTimer = FLYRECOVER; animator.SetBool("Fly", false); recoveryDone = false; } else if (Vector2.Distance(m_RigidBody2D.position, target.position) <= 0.2f) { m_RigidBody2D.position = Vector2.MoveTowards(m_RigidBody2D.position, new Vector2(target.position.x, m_RigidBody2D.position.y), movSpeed * Time.deltaTime); } }
protected override void InitializeStateMachine() { WizardIdleState wizardIdleState = new WizardIdleState(wizardAnimator); WizardRunState wizardRunState = new WizardRunState(wizardAnimator, enemyMovement); WizardChaseState wizardChaseState = new WizardChaseState(wizardAnimator, enemyMovement, this); WizardAttackState wizardAttackState = new WizardAttackState(wizardAnimator, wizardAttackController); WizardDeathState wizardDeathState = new WizardDeathState(wizardAnimator); stateMachine.AddTransition(wizardIdleState, wizardRunState, SelectRandomTimeForIdleState()); //Idle To Run stateMachine.AddTransition(wizardRunState, wizardIdleState, SelectRandomTimeForRunState()); // Run To Idle stateMachine.AddTransition(wizardIdleState, wizardChaseState, HasTarget()); //Idle To Chase stateMachine.AddTransition(wizardRunState, wizardChaseState, HasTarget()); //Run To Chase stateMachine.AddTransition(wizardChaseState, wizardIdleState, HasNoTarget()); //Chase To Idle stateMachine.AddTransition(wizardAttackState, wizardIdleState, HasNoTarget()); // Attack To Idle stateMachine.AddTransition(wizardChaseState, wizardAttackState, CanAttack()); //Chase To Attack stateMachine.AddTransition(wizardAttackState, wizardChaseState, ReturnChase()); // Attack To Chase stateMachine.AddAnyTransition(wizardDeathState, IsDead()); stateMachine.SetState(wizardIdleState); Func <bool> SelectRandomTimeForIdleState() => () => wizardIdleState.Timer > wizardIdleState.IdleTime; Func <bool> SelectRandomTimeForRunState() => () => wizardRunState.Timer > wizardRunState.RunTime; Func <bool> HasTarget() => () => Target != null; Func <bool> HasNoTarget() => () => Target == null; Func <bool> CanAttack() => () => Target != null && Mathf.Abs(Target.position.x - transform.position.x) < WIZARD_ATTACK_RANGE; Func <bool> ReturnChase() => () => Target != null && (Mathf.Abs(Target.position.x - transform.position.x) > WIZARD_RETURN_CHASE_RANGE); Func <bool> IsDead() => () => health <= 0; }
void GroundPound() { GameObject newGameObject; SandWall sandWall1; SandWall sandWall2; Debug.Log("GroundPounds:" + GroundPounds); animator.SetBool("GroundPound", true); AttackTimer -= Time.deltaTime; if (AttackTimer > CLIMBTIME) { m_RigidBody2D.position = Vector2.MoveTowards(m_RigidBody2D.position, new Vector2(Player.position.x, initHeight + height), fallSpeed * Time.deltaTime); } else if (AttackTimer <= CLIMBTIME && AttackTimer > FALLTIME) { m_RigidBody2D.position = Vector2.MoveTowards(m_RigidBody2D.position, new Vector2(Player.position.x, m_RigidBody2D.position.y), movSpeed * Time.deltaTime); } else if (AttackTimer <= FALLTIME && AttackTimer > IMPACTMOMENT) { m_RigidBody2D.position = Vector2.MoveTowards(m_RigidBody2D.position, new Vector2(m_RigidBody2D.position.x, initHeight), fallSpeed * Time.deltaTime); } else if (AttackTimer <= IMPACTMOMENT && AttackTimer > 0) { if (!doOnce) { doOnce = true; newGameObject = (GameObject)Instantiate(sandWall, new Vector3(m_RigidBody2D.position.x, initHeight, 0), Quaternion.identity); sandWall1 = (SandWall)newGameObject.GetComponent <SandWall>(); newGameObject = (GameObject)Instantiate(sandWall, new Vector3(m_RigidBody2D.position.x, initHeight, 0), Quaternion.identity); sandWall2 = (SandWall)newGameObject.GetComponent <SandWall>(); sandWall1.destroyPoint = enterPoint; sandWall1.movesRight = false; sandWall2.destroyPoint = exitPoint; sandWall2.movesRight = true; } } else if (AttackTimer < 0 && GroundPounds > 0) { AttackTimer = GROUNDPOUNDTIMER; doOnce = false; GroundPounds--; } else if (AttackTimer < 0 && GroundPounds <= 0) { nextAttackState = WizardAttackState.STATE_RECOVERY; animator.SetBool("GroundPound", false); animator.SetBool("Recover", true); RecoverTimer = GROUNDPOUNDRECOVER; recoveryDone = false; doOnce = false; } }
void Awake() { calledOnce = true; doOnce = false; animator = (Animator)gameObject.GetComponent <Animator>(); m_RigidBody2D = (Rigidbody2D)gameObject.GetComponent <Rigidbody2D>(); pouch = (GoldPouch)gameObject.GetComponent <GoldPouch>(); RecoverTimer = 0; recoveryDone = true; AttackTimer = 0; ShootCoolDownInterval = 0; fireBalls = 0; GroundPounds = 0; facingRight = false; prevFace = false; initPosition = gameObject.transform; currentState = WizardState.STATE_CALM; nextState = currentState; currentAttackState = WizardAttackState.STATE_RECOVERY; nextAttackState = WizardAttackState.STATE_RECOVERY; }
void ShootFireBall() { GameObject newGameObject; FireBall currentFireBall; float speed = 5; AttackTimer -= Time.deltaTime; ShootCoolDownInterval -= Time.deltaTime; animator.SetBool("FireBall", true); facePlayer(); if (currentState == WizardState.STATE_ANGRY) { speed = 5; } else if (currentState == WizardState.STATE_CALM) { speed = 7; } if (AttackTimer <= STARTSHOOTING && AttackTimer >= 0) { if (ShootCoolDownInterval < 0 && fireBalls > 0) { ShootCoolDownInterval = SHOOTCOOLDOWN; newGameObject = (GameObject)Instantiate(fireBall, m_RigidBody2D.position, Quaternion.identity); currentFireBall = (FireBall)newGameObject.GetComponent <FireBall>(); currentFireBall.Target = Player.transform; currentFireBall.speed = speed; fireBalls--; } } else if (AttackTimer <= 0) { nextAttackState = WizardAttackState.STATE_RECOVERY; RecoverTimer = FIREBALLRECOVER; animator.SetBool("FireBall", false); recoveryDone = false; } }
void SwitchAttack() { System.Random random = new System.Random(); int nextAttack = random.Next(0, 3); switch (currentAttackState) { case WizardAttackState.STATE_RECOVERY: Recover(); if (recoveryDone) { facePlayer(); switch (nextAttack) { case 0: nextAttackState = WizardAttackState.STATE_FIREBALL; AttackTimer = FIREBALLTIMER; ShootCoolDownInterval = SHOOTCOOLDOWN; if (currentState == WizardState.STATE_CALM) { fireBalls = 1; } else { fireBalls = 3; } break; case 1: nextAttackState = WizardAttackState.STATE_FLY; AttackTimer = FLYTIMER; if (currentState == WizardState.STATE_CALM) { movSpeed = 4; } else { fireBalls = 6; } break; case 2: nextAttackState = WizardAttackState.STATE_GROUNDPOUND; AttackTimer = GROUNDPOUNDTIMER; if (currentState == WizardState.STATE_CALM) { GroundPounds = 0; } else { GroundPounds = 2; } break; } } break; case WizardAttackState.STATE_FIREBALL: ShootFireBall(); break; case WizardAttackState.STATE_FLY: Fly(); break; case WizardAttackState.STATE_GROUNDPOUND: GroundPound(); break; } currentAttackState = nextAttackState; }