Пример #1
0
        // 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);
            }
        }
Пример #2
0
        /// <summary>
        /// Create the graph and fills it with the tilemap information
        /// Only "walkable" nodes are considered
        /// </summary>
        private void MountLevel()
        {
            Collectables = new List <Collectable>();

            //iterates through all tiles and create nodes in the graph
            //and elements int he level based on the tile's poisition
            //in the grid and type. Walls are not considered for the graph
            foreach (var tilePosition in _tilemap.cellBounds.allPositionsWithin)
            {
                var tile = _tilemap.GetTile(tilePosition);

                if (tile != null)
                {
                    bool           clearColor = false;
                    Pacman.NavNode navNode    = new Pacman.NavNode();
                    if (tile.name.Equals("pelletTile"))
                    {
                        clearColor = true;

                        navNode.NodeType = Pacman.NodeType.Normal;
                        navNode.Indexes  = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);

                        var pellet = Instantiate(_pelletPrefab).GetComponent <Pellet>();
                        pellet.PelletType         = PelletTypes.Small;
                        pellet.transform.position = _tilemap.GetCellCenterWorld(tilePosition);
                        pellet.Index = new Vector2(tilePosition.x, tilePosition.y);
                        Collectables.Add(pellet);
                    }
                    else if (tile.name.Equals("bigPelletTile"))
                    {
                        clearColor = true;

                        navNode.NodeType = Pacman.NodeType.PowerUp;
                        navNode.Indexes  = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);

                        var bigPellet = Instantiate(_bigPelletPrefab).GetComponent <Pellet>();
                        bigPellet.PelletType         = PelletTypes.Big;
                        bigPellet.transform.position = _tilemap.GetCellCenterWorld(tilePosition);
                        bigPellet.Index = new Vector2(tilePosition.x, tilePosition.y);
                        Collectables.Add(bigPellet);
                    }
                    else if (tile.name.Equals("emptyTile"))
                    {
                        clearColor = true;

                        navNode.NodeType = Pacman.NodeType.Normal;
                        navNode.Indexes  = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("bonusPointTile"))
                    {
                        clearColor = true;

                        navNode.NodeType        = Pacman.NodeType.BonusPoint;
                        NavGraph.BonusPointNode = navNode;
                        navNode.Indexes         = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);

                        var bonusPellet = Instantiate(_bonusPelletPrefab).GetComponent <Pellet>();
                        bonusPellet.PelletType         = PelletTypes.Bonus;
                        bonusPellet.transform.position = _tilemap.GetCellCenterWorld(tilePosition);
                        bonusPellet.Index = new Vector2(tilePosition.x, tilePosition.y);
                        Collectables.Add(bonusPellet);
                    }
                    else if (tile.name.Equals("playerTile"))
                    {
                        clearColor = true;

                        navNode.NodeType         = Pacman.NodeType.PlayerSpawn;
                        NavGraph.PlayerSpawnNode = navNode;
                        navNode.Indexes          = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("enemy1"))
                    {
                        clearColor = true;

                        navNode.NodeType            = Pacman.NodeType.EnemyPosition1;
                        NavGraph.EnemyPosition1Node = navNode;
                        navNode.Indexes             = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("enemy2"))
                    {
                        clearColor = true;

                        navNode.NodeType            = Pacman.NodeType.EnemyPosition2;
                        NavGraph.EnemyPosition2Node = navNode;
                        navNode.Indexes             = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("enemy3"))
                    {
                        clearColor = true;

                        navNode.NodeType            = Pacman.NodeType.EnemyPosition3;
                        NavGraph.EnemyPosition3Node = navNode;
                        navNode.Indexes             = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("enemy4"))
                    {
                        clearColor = true;

                        navNode.NodeType            = Pacman.NodeType.EnemyPosition4;
                        NavGraph.EnemyPosition4Node = navNode;
                        navNode.Indexes             = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("warperDown"))
                    {
                        //clearColor = true;

                        navNode.NodeType    = Pacman.NodeType.WarperDown;
                        NavGraph.WarperDown = navNode;
                        navNode.Indexes     = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("warperUp"))
                    {
                        //clearColor = true;

                        navNode.NodeType  = Pacman.NodeType.WarperUp;
                        NavGraph.WarperUp = navNode;
                        navNode.Indexes   = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("warperLeft"))
                    {
                        //clearColor = true;

                        navNode.NodeType    = Pacman.NodeType.WarperLeft;
                        NavGraph.WarperLeft = navNode;
                        navNode.Indexes     = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("warperRight"))
                    {
                        //clearColor = true;

                        navNode.NodeType     = Pacman.NodeType.WarperRight;
                        NavGraph.WarperRight = navNode;
                        navNode.Indexes      = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("nonPlayableTile"))
                    {
                        clearColor = true;

                        navNode.NodeType = Pacman.NodeType.NonWalkable;
                        navNode.Indexes  = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }
                    else if (tile.name.Equals("EnemyExitTile"))
                    {
                        clearColor = true;

                        navNode.NodeType             = Pacman.NodeType.Normal;
                        NavGraph.EnemyExitTargetNode = navNode;
                        navNode.Indexes = new System.Tuple <int, int>(tilePosition.x, tilePosition.y);
                        NavGraph.AddNavNode(navNode);
                    }

                    if (clearColor)
                    {
                        _tilemap.SetTileFlags(tilePosition, TileFlags.None);
                        _tilemap.SetColor(tilePosition, new Color(0, 0, 0, 0));
                    }
                }
            }


            //link warp nodes
            NavGraph.WarperLeft.Left   = NavGraph.WarperRight;
            NavGraph.WarperRight.Right = NavGraph.WarperLeft;
            NavGraph.WarperUp.Up       = NavGraph.WarperDown;
            NavGraph.WarperDown.Down   = NavGraph.WarperUp;
        }