Ejemplo n.º 1
0
        IEnumerator Coroutine_Spawn_NPC(float duration = 10.0f)
        {
            while (mFsm.GetCurrentState().ID == (int)GameState.StateID.PLAYING)
            {
                int rx = Random.Range(2, mCurrentGenerator.cols - 2);
                int ry = Random.Range(2, mCurrentGenerator.rows - 2);

                int sx = rx + mCurrentGenerator.START_X;
                int sy = ry + mCurrentGenerator.START_Y;

                Maze.Cell startCell = mCurrentGenerator.maze.GetCell(rx, ry);

                GameObject     npc = Instantiate(mNpcPrefab, new Vector3(sx, sy, 0.0f), Quaternion.identity);
                MazePathFinder mpf = npc.AddComponent <MazePathFinder>();
                mpf.mGenerator = mCurrentGenerator;
                mpf.mNpc       = npc;
                mpf.mSpeed     = 0.5f;

                // player position.
                int dx = (int)mPlayerMovement.mPlayer.transform.position.x - mCurrentGenerator.START_X;
                int dy = (int)mPlayerMovement.mPlayer.transform.position.y - mCurrentGenerator.START_Y;
                mpf.FindPath(startCell, mCurrentGenerator.maze.GetCell(dx, dy));
                mpf.onReachGoal += NPCOnReachGoal;

                mNPCs.Add(mpf);
                yield return(new WaitForSeconds(duration));
            }
        }
Ejemplo n.º 2
0
        bool GenerateStep()
        {
            if (_stack.Count == 0)
            {
                return(true);
            }

            Maze.Cell c = _stack.Peek();

            var neighbours = maze.GetNeighboursNotVisited(c.x, c.y);

            if (neighbours.Count != 0)
            {
                var index = 0;
                if (neighbours.Count > 1)
                {
                    index = UnityEngine.Random.Range(0, neighbours.Count);
                }
                var       item      = neighbours[index];
                Maze.Cell neighbour = item.Item2;
                neighbour.visited = true;
                maze.RemoveCellWall(c.x, c.y, item.Item1);

                _stack.Push(neighbour);
            }
            else
            {
                _stack.Pop();
            }
            return(false);
        }
Ejemplo n.º 3
0
        private static bool IsCellInList(Maze.Cell state, List <Node <Maze.Cell> > li)
        {
            int i = 0;

            for (; i < li.Count; ++i)
            {
                if (state.x == li[i].Data.x && state.y == li[i].Data.y)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        // Start is called before the first frame update
        void Start()
        {
            //UnityEngine.Random.InitState(100);

            START_Y       = -rows / 2;
            START_X       = -cols / 2;
            maze          = new Maze(rows, cols);
            mCellGameObjs = new GameObject[cols, rows];
            for (int i = 0; i < cols; ++i)
            {
                for (int j = 0; j < rows; ++j)
                {
                    GameObject obj = Instantiate(mCellPrefab);
                    obj.transform.parent = transform;
                    Maze.Cell cell = maze.GetCell(i, j);
                    cell.OnSetDirFlag      = OnCellSetDirFlag;
                    obj.transform.position = new Vector3(START_X + cell.x, START_Y + cell.y, 1.0f);
                    mCellGameObjs[i, j]    = obj;
                }
            }
            CreateNewMaze();
        }
Ejemplo n.º 5
0
        void HandleMouseClick()
        {
            if (mMenuHandler.mShowingExitPopup)
            {
                return;
            }

            if (Input.GetMouseButtonDown(0))
            {
                Vector2 rayPos = new Vector2(
                    Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
                    Camera.main.ScreenToWorldPoint(Input.mousePosition).y
                    );

                //Debug.Log("POSR: " + rayPos.x + ", " + rayPos.y);
                int x = (int)rayPos.x - mCurrentGenerator.START_X;
                int y = (int)rayPos.y - mCurrentGenerator.START_Y;
                //Debug.Log("POS : " + x + ", " + y);

                if (x < 0 || x >= mCurrentGenerator.cols || y < 0 || y >= mCurrentGenerator.rows)
                {
                    return;
                }
                Maze.Cell cell = mCurrentGenerator.maze.GetCell(x, y);

                GameObject     npc = Instantiate(mNpcPrefab, new Vector3((int)rayPos.x, (int)rayPos.y, 0.0f), Quaternion.identity);
                MazePathFinder mpf = npc.AddComponent <MazePathFinder>();
                mpf.mGenerator = mCurrentGenerator;
                mpf.mNpc       = npc;
                mpf.mSpeed     = 1.0f;

                // player position.
                int dx = (int)mPlayerMovement.mPlayer.transform.position.x - mCurrentGenerator.START_X;
                int dy = (int)mPlayerMovement.mPlayer.transform.position.y - mCurrentGenerator.START_Y;
                mpf.FindPath(cell, mCurrentGenerator.maze.GetCell(dx, dy));

                mNPCs.Add(mpf);
            }
        }
Ejemplo n.º 6
0
        IEnumerator Coroutine_Print(float t)
        {
            while (true)
            {
                mGenerator.RemoveAllHightlights();

                float x = mPlayer.transform.position.x + 0.5f;
                float y = mPlayer.transform.position.y + 0.5f;
                int   i = (int)Mathf.Floor(x) - mGenerator.START_X;
                int   j = (int)Mathf.Floor(y) - mGenerator.START_Y;


                if (!(i < 0 || j < 0 || i > mGenerator.cols - 1 || j > mGenerator.rows - 1))
                {
                    Maze.Cell cell = mGenerator.maze.GetCell(i, j);
                    //Debug.Log(i + ", " + j + " | " + cell.flag[0] + ", " + cell.flag[1] + ", " + cell.flag[2] + ", " + cell.flag[3]);
                    //Debug.Log(i + ", " + j);

                    mGenerator.HighlightCell(i, j, true);
                }

                yield return(new WaitForSeconds(t));
            }
        }
Ejemplo n.º 7
0
        IEnumerator Coroutine_Spawn_Ammo(int count = 5)
        {
            int i = 0;

            while (i < count)
            {
                int rx = Random.Range(2, mCurrentGenerator.cols - 2);
                int ry = Random.Range(2, mCurrentGenerator.rows - 2);

                Maze.Cell cell = mCurrentGenerator.maze.GetCell(rx, ry);
                while (!cell.containsGold && !cell.containsAmmo)
                {
                    int sx = rx + mCurrentGenerator.START_X;
                    int sy = ry + mCurrentGenerator.START_Y;

                    GameObject ammo = Instantiate(mAmmoPrefab, new Vector3(sx, sy, 0.0f), Quaternion.identity);
                    mAmmos.Add(ammo);

                    mCurrentGenerator.maze.GetCell(rx, ry).containsAmmo = true;
                    i++;
                    yield return(null);
                }
            }
        }
Ejemplo n.º 8
0
        public void Tick()
        {
            UpdateRotation();
            float x = mPlayer.transform.position.x;
            float y = mPlayer.transform.position.y;

            int i = (int)Mathf.Floor(x + 0.5f) - mGenerator.START_X;
            int j = (int)Mathf.Floor(y + 0.5f) - mGenerator.START_Y;

            if (i < 0 || j < 0 || i > mGenerator.cols - 1 || j > mGenerator.rows - 1)
            {
                return;
            }

            Maze.Cell cell = mGenerator.maze.GetCell(i, j);

            if (player_moving)
            {
                return;
            }

            //mPlayer.transform.rotation = Quaternion.Lerp(
            //    mPlayer.transform.rotation, Quaternion.Euler(0.0f, 0.0f, mCurrentAngle),
            //    Time.deltaTime * 10.0f);

            if (!cell.flag[0])
            {
                if (j < mGenerator.rows - 1 && my > 0.0f /* && Mathf.Abs(my) > Mathf.Abs(mx)*/)
                {
                    StartCoroutine(Coroutine_MoveOverSeconds(mPlayer,
                                                             new Vector3(
                                                                 i + mGenerator.START_X,
                                                                 j + mGenerator.START_Y +
                                                                 1, 0.0f),
                                                             1.0f / mSpeed));
                }
            }
            if (!cell.flag[1])
            {
                // can go right.
                if (i < mGenerator.cols - 1 && mx > 0.0f /* && Mathf.Abs(mx) > Mathf.Abs(my)*/)
                {
                    StartCoroutine(Coroutine_MoveOverSeconds(mPlayer,
                                                             new Vector3(
                                                                 i + mGenerator.START_X + 1,
                                                                 j + mGenerator.START_Y,
                                                                 0.0f),
                                                             1.0f / mSpeed));
                }
            }
            if (!cell.flag[2])
            {
                if (j > 0 && my < 0.0f /* && Mathf.Abs(my) > Mathf.Abs(mx)*/)
                {
                    Vector3 a = mPlayer.transform.position;
                    Vector3 b = new Vector3(i + mGenerator.START_X, j + mGenerator.START_Y - 1, 0.0f);

                    StartCoroutine(Coroutine_MoveOverSeconds(mPlayer,
                                                             new Vector3(
                                                                 i + mGenerator.START_X,
                                                                 j + mGenerator.START_Y - 1,
                                                                 0.0f),
                                                             1.0f / mSpeed));
                }
            }
            if (!cell.flag[3])
            {
                // can go left.
                if (i > 0 && mx < 0.0f /* && Mathf.Abs(mx) > Mathf.Abs(my)*/)
                {
                    StartCoroutine(Coroutine_MoveOverSeconds(mPlayer,
                                                             new Vector3(
                                                                 i + mGenerator.START_X - 1,
                                                                 j + mGenerator.START_Y,
                                                                 0.0f),
                                                             1.0f / mSpeed));
                }
            }

            if (cell.x == mGenerator.cols - 1 && cell.y == mGenerator.rows - 1)
            {
                Debug.Log("Win");

                StartCoroutine(Coroutine_MoveOverSeconds(mPlayer, new Vector3(i + mGenerator.START_X + 4,
                                                                              j + mGenerator.START_Y, 0.0f), 1.0f / mSpeed));
                mOnReachDestination?.Invoke();
            }
        }
Ejemplo n.º 9
0
        private IEnumerator Coroutine_FindPath(Maze.Cell start, Maze.Cell goal)
        {
            Maze.Cell c = start;

            PriorityQueue <Maze.Cell> openlist   = new PriorityQueue <Maze.Cell>();
            List <Node <Maze.Cell> >  closedlist = new List <Node <Maze.Cell> >();

            Node <Maze.Cell> root = new Node <Maze.Cell>(start, 0, null);

            root.Parent = null;

            openlist.Add(root);
            closedlist.Add(root);

            List <Node <Maze.Cell> > astarSolution = new List <Node <Maze.Cell> >();

            mStatus = StatusType.RUNNING;
            while (openlist.Count > 0 && mStatus != StatusType.SUCCESS)
            {
                if (openlist.Count == 0)
                {
                    mStatus = StatusType.FAILURE;
                    break;
                }
                // sort the openlist and get the node with the least cost.
                // in our case the PriorityQueue data structure does the work for us.
                Node <Maze.Cell> current = openlist.GetAndRemoveTop();


                if (current.Data.x == goal.x && current.Data.y == goal.y)
                {
                    // fil the solution.
                    Node <Maze.Cell> s = current;
                    do
                    {
                        astarSolution.Add(s);
                        s = s.Parent;
                    } while (s != null);

                    mStatus = StatusType.SUCCESS;
                }

                List <Tuple <Maze.Directions, Maze.Cell> > neighbors = mGenerator.maze.GetNeighbours(current.Data.x, current.Data.y);

                foreach (Tuple <Maze.Directions, Maze.Cell> next in neighbors)
                {
                    if (!IsCellInList(next.Item2, closedlist))
                    {
                        int cost = 0;
                        if (current.Data.flag[(int)next.Item1])
                        {
                            // no go region.
                            cost = 1000;
                        }
                        else
                        {
                            int G = cost + current.Cost;
                            int H = Mathf.Abs(goal.x - next.Item2.x) + Mathf.Abs(goal.y - next.Item2.y);
                            int F = G + H;
                            Node <Maze.Cell> n = new Node <Maze.Cell>(next.Item2, F, current);

                            openlist.Add(n);
                            closedlist.Add(n);
                        }
                    }
                }
                yield return(null);
            }
            yield return(StartCoroutine(MoveToGoal(astarSolution, mNpc)));
        }
Ejemplo n.º 10
0
 public void FindPath(Maze.Cell start, Maze.Cell goal)
 {
     StartCoroutine(Coroutine_FindPath(start, goal));
 }