private void InitStateMachine() { blackboard = new StateBlackboard(); blackboard.Add <bool>(BlackboardKey.DamageFlag, false); blackboard.Add <bool>(BlackboardKey.RepeatDamageFlag, false); blackboard.Add <Transform>(BlackboardKey.Transform, transform); blackboard.Add <float>(BlackboardKey.RunRange, 3f); EnemyIdleState idleState = EnemyIdleState.Create(blackboard, animator, "Idle", 0); StateNode idleNode = new StateNode(idleState, new Condition(() => { return(true); })); SetAnimTriggerAction idleAction = new SetAnimTriggerAction(blackboard, animator, "Idle"); SetAnimTriggerAction runAction = new SetAnimTriggerAction(blackboard, animator, "Run"); TimerAction timerAction = new TimerAction(blackboard, 0.5f, 1.5f); SetRandomDestinationAction destinationAction = new SetRandomDestinationAction(blackboard, transform.position, 6f); MoveToDestinationAction moveToAction = new MoveToDestinationAction(blackboard, navAgent); SetSkillAction setSkillAction = new SetSkillAction(blackboard, skillController); LookAtTargetAction lookAtAction = new LookAtTargetAction(blackboard, transform); SetAnimTriggerAction attackAction = new SetAnimTriggerAction(blackboard, animator, BlackboardKey.AnimTrigger, true); RangedAttackState attackState = new RangedAttackState(blackboard, idleAction, timerAction, destinationAction, runAction, moveToAction, setSkillAction, lookAtAction, attackAction, 300); StateNode attackNode = new StateNode(attackState, new Condition(() => target != null)); DamageState damageState = DamageState.Create(blackboard, animator, "Damage", 500); StateNode damageNode = new StateNode(damageState, new Condition(() => { return(blackboard.Get <bool>(BlackboardKey.DamageFlag)); })); DeathState deathState = DeathState.Create(blackboard, animator, "Death", int.MaxValue); StateNode deathNode = new StateNode(deathState, new Condition(() => { return(!self.IsAlive); })); List <StateNode> nodes = new List <StateNode>(); nodes.Add(idleNode); nodes.Add(attackNode); nodes.Add(damageNode); nodes.Add(deathNode); stateMachine = new StateMachine(blackboard, nodes, idleState); }
//Start overrides the virtual Start function of the base class. void Start() { //Register this enemy with our instance of GameManager by adding it to a list of Enemy objects. //This allows the GameManager to issue movement commands. GameManager.instance.AddEnemyToList(this); if (currentHealth == 0) { currentHealth = startingHealth; } nextRangeTime = rangedAttackCD; nextMeleeTime = meleeAttackCD; if (!canMove) { AttackRange = 3f; } baseSpeed = speed; if (isBoss) { if (chaseSpeed == 0) { chaseSpeed = speed + 2f; } } else { if (chaseSpeed == 0) { chaseSpeed = speed + 1f; } } if (cannotChase) { speed = Random.Range(2, 7); chaseSpeed = speed; baseSpeed = speed; } patrolState = new PatrolState(); attackState = new AttackState(); chaseState = new ChaseState(); rangedAttackState = new RangedAttackState(); animator = GetComponent <Animator>(); if (canMove) { changeState(patrolState); } else { changeState(chaseState); } if (animator.GetBool("Moving")) { animator.SetBool("Moving", true); } healthbar = transform.FindChild("EnemyCanvas").FindChild("Healthbar").FindChild("Health").GetComponent <Image>(); //if (isGhost) startingLoc = transform.position; roomId = RoomManager.Instance.findRoomId(transform.position.x, transform.position.y); currentRoomId = roomId; }
private void Start() { attackState = new RangedAttackState(this); }