protected override void Update() { base.Update(); if (Spawned) { switch (ActionState) { case EnemyActionState.Following: if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead) { //follow player var currentNode = GameController.Instance.LevelManager.NavGraph.GetNode( NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2); var path = GameController.Instance.LevelManager.NavGraph.ShortestPath( currentNode, GameController.Instance.GetPlayerCurrentNode(), new System.Collections.Generic.List <Pacman.NodeType>() { Pacman.NodeType.NonWalkable }); //moves a max of 3 nodes per time NavEntity.SetPath(path.GetRange(0, Mathf.Min(5, path.Count))); } break; case EnemyActionState.Fleeing: if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead) { //avoid player var currentNode = GameController.Instance.LevelManager.NavGraph.GetNode( NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2); var path = GameController.Instance.LevelManager.NavGraph.FarthestPath( currentNode, GameController.Instance.GetPlayerCurrentNode(), new System.Collections.Generic.List <Pacman.NodeType>() { Pacman.NodeType.NonWalkable }); NavEntity.SetPath(path); } break; } } }
private void Flee() { if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead) { //avoid player var currentNode = GameController.Instance.LevelManager.NavGraph.GetNode( NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2); var path = GameController.Instance.LevelManager.NavGraph.FarthestPath( currentNode, GameController.Instance.GetPlayerCurrentNode(), new System.Collections.Generic.List <Pacman.NodeType>() { Pacman.NodeType.NonWalkable }); //moves a max of 2 nodes per time NavEntity.SetPath(path); } }
// Update is called once per frame protected override void Update() { base.Update(); //player moves on tile per time if (!Initialized) { Debug.Log("Initialize player first"); return; } //read player's input var inputDirection = MovementDirection; if (Input.GetKey(KeyCode.UpArrow)) { inputDirection = MovementDirections.Up; } else if (Input.GetKey(KeyCode.DownArrow)) { inputDirection = MovementDirections.Down; } else if (Input.GetKey(KeyCode.LeftArrow)) { inputDirection = MovementDirections.Left; } else if (Input.GetKey(KeyCode.RightArrow)) { inputDirection = MovementDirections.Right; } //it means the entity has stopped if (NavEntity.CanMove && NavEntity.ReachedDestination) { GameEvents.Instance.OnPlayerWalkedTile( new GameEvents.PlayerWalkedTileEventArgs() { indexX = NavEntity.LastIndexes.Item1, indexY = NavEntity.LastIndexes.Item2 }); bool changedDirection = false; if (inputDirection != MovementDirection) { int xIndexShiftNewDirection = 0; int yIndexShiftNewDirection = 0; //calc the direction on grid coordinates for new input switch (inputDirection) { case MovementDirections.Up: xIndexShiftNewDirection = 0; yIndexShiftNewDirection = 1; break; case MovementDirections.Down: xIndexShiftNewDirection = 0; yIndexShiftNewDirection = -1; break; case MovementDirections.Left: xIndexShiftNewDirection = -1; yIndexShiftNewDirection = 0; break; case MovementDirections.Right: xIndexShiftNewDirection = 1; yIndexShiftNewDirection = 0; break; } //try finding a walkable node in the new direction Pacman.NavNode nodeInDirection = GameController.Instance.LevelManager.NavGraph.GetNode( (int)Math.Round(NavEntity.Position.Item1) + xIndexShiftNewDirection, (int)Math.Round(NavEntity.Position.Item2) + yIndexShiftNewDirection); //found a node, change player's path and direction if (nodeInDirection != null && nodeInDirection.NodeType != Pacman.NodeType.NonWalkable) { changedDirection = true; MovementDirection = inputDirection; NavEntity.SetPath( new System.Collections.Generic.List <Pacman.NavNode>() { nodeInDirection }); } } //try to move in same direction if (!changedDirection) { int xIndexShiftOldDirection = 0; int yIndexShiftOldDirection = 0; //calc the direction on grid coordinates for new input switch (MovementDirection) { case MovementDirections.Up: xIndexShiftOldDirection = 0; yIndexShiftOldDirection = 1; break; case MovementDirections.Down: xIndexShiftOldDirection = 0; yIndexShiftOldDirection = -1; break; case MovementDirections.Left: xIndexShiftOldDirection = -1; yIndexShiftOldDirection = 0; break; case MovementDirections.Right: xIndexShiftOldDirection = 1; yIndexShiftOldDirection = 0; break; } //try finding a walkable node in the old direction Pacman.NavNode nodeInDirection = GameController.Instance.LevelManager.NavGraph.GetNode( (int)Math.Round(NavEntity.Position.Item1) + xIndexShiftOldDirection, (int)Math.Round(NavEntity.Position.Item2) + yIndexShiftOldDirection); //found a node, change player's target if (nodeInDirection != null && nodeInDirection.NodeType != Pacman.NodeType.NonWalkable) { NavEntity.SetPath( new System.Collections.Generic.List <Pacman.NavNode>() { nodeInDirection }); } } } if (MovementDirection == MovementDirections.Left) { transform.localScale = new Vector3(-1, 1, 1); } else if (MovementDirection == MovementDirections.Right) { transform.localScale = new Vector3(1, 1, 1); } }
protected override void Update() { base.Update(); if (Spawned) { switch (ActionState) { case EnemyActionState.Following: //test condictions for firing the magi ball if (CanDamage) { if (_magicTimer > GameController.Instance.NECRO_MAGIC_COOLDOWN) { _magicTimer = 0; //fires the magic var directions = System.Enum.GetValues(typeof(MovementDirections)); foreach (MovementDirections direction in directions) { if (direction != MovementDirections.None) { var fireball = Instantiate(_magicPrefab).GetComponent <Fireball>(); fireball.Initialize(transform.position, direction, GameController.Instance.NECRO_MAGIC_SPEED); } } } else { _magicTimer += Time.deltaTime; } } if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead) { //follow player var currentNode = GameController.Instance.LevelManager.NavGraph.GetNode( NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2); var path = GameController.Instance.LevelManager.NavGraph.ShortestPath( currentNode, GameController.Instance.GetPlayerCurrentNode(), new System.Collections.Generic.List <Pacman.NodeType>() { Pacman.NodeType.NonWalkable }); //moves a max of 3 nodes per time NavEntity.SetPath(path.GetRange(0, Mathf.Min(3, path.Count))); } break; case EnemyActionState.Fleeing: if (NavEntity.CanMove && NavEntity.ReachedDestination && !IsDead) { //avoid player var currentNode = GameController.Instance.LevelManager.NavGraph.GetNode( NavEntity.LastIndexes.Item1, NavEntity.LastIndexes.Item2); var path = GameController.Instance.LevelManager.NavGraph.FarthestPath( currentNode, GameController.Instance.GetPlayerCurrentNode(), new System.Collections.Generic.List <Pacman.NodeType>() { Pacman.NodeType.NonWalkable }); NavEntity.SetPath(path); } break; } } }
public void WalkOutsideSpawnZone(List <Pacman.NavNode> path) { NavEntity.SetPath(path); NavEntity.EnableMoving(); StartCoroutine(WaitWalkingOutsideSpawnZone()); }