Exemple #1
0
 public void Reset()
 {
     this.GetComponent <Renderer> ().material.color = this.GetComponent <GhostSpecialization> ().GetBaseColor();
     lastDirection        = GameController.direction.kLeft;
     currentMode          = lastMode = this.GetComponent <GhostSpecialization> ().GetStartMode();
     transform.localScale = Vector3.one;
     timeElapsed          = 0;
 }
Exemple #2
0
 public GameController.direction GetAction(GameMaze m, GameController c, xyLoc yourLoc)
 {
     if (c.CanMove(yourLoc, currentDirection))
     {
         lastDirection = currentDirection;
         return(currentDirection);
     }
     return(lastDirection);
 }
Exemple #3
0
    GameController.direction MoveToTarget(int targetx, int targety, xyLoc myLoc,
                                          GameController.direction forbiddenDirection,
                                          GameController c)
    {
        GameController.direction best = GameController.direction.kUp;
        float distance = 1000f;

        if (c.CanMove(myLoc, GameController.direction.kUp) && forbiddenDirection != GameController.direction.kUp)
        {
            float thisDist = Dist(targetx, targety, myLoc.x / 8, myLoc.y / 8 + 1);
            if (thisDist < distance)
            {
                best     = GameController.direction.kUp;
                distance = thisDist;
            }
        }
        if (c.CanMove(myLoc, GameController.direction.kLeft) && forbiddenDirection != GameController.direction.kLeft)
        {
            float thisDist = Dist(targetx, targety, myLoc.x / 8 - 1, myLoc.y / 8);
            if (thisDist < distance)
            {
                best     = GameController.direction.kLeft;
                distance = thisDist;
            }
        }
        if (c.CanMove(myLoc, GameController.direction.kDown) && forbiddenDirection != GameController.direction.kDown)
        {
            float thisDist = Dist(targetx, targety, myLoc.x / 8, myLoc.y / 8 - 1);
            if (thisDist < distance)
            {
                best     = GameController.direction.kDown;
                distance = thisDist;
            }
        }
        if (c.CanMove(myLoc, GameController.direction.kRight) && forbiddenDirection != GameController.direction.kRight)
        {
            float thisDist = Dist(targetx, targety, myLoc.x / 8 + 1, myLoc.y / 8);
            if (thisDist < distance)
            {
                best     = GameController.direction.kRight;
                distance = thisDist;
            }
        }
        return(best);
    }
Exemple #4
0
 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.W))
     {
         currentDirection = GameController.direction.kUp;
     }
     else if (Input.GetKeyDown(KeyCode.S))
     {
         currentDirection = GameController.direction.kDown;
     }
     else if (Input.GetKeyDown(KeyCode.A))
     {
         currentDirection = GameController.direction.kLeft;
     }
     else if (Input.GetKeyDown(KeyCode.D))
     {
         currentDirection = GameController.direction.kRight;
     }
 }
Exemple #5
0
    public GameController.direction GetAction(GameMaze m, GameController c, xyLoc yourLoc)
    {
        if ((yourLoc.x / 8 != lastx || yourLoc.y / 8 != lasty) &&
            (yourLoc.x % 8 == 0 && yourLoc.y % 8 == 0))
        {
            lastx = yourLoc.x / 8;
            lasty = yourLoc.y / 8;
            GetDistances(c, m, lastx, lasty);

            float lUtil, rUtil, dUtil, uUtil;

            lUtil = GetDirectionUtility(c, m, GameController.direction.kLeft);
            rUtil = GetDirectionUtility(c, m, GameController.direction.kRight);
            dUtil = GetDirectionUtility(c, m, GameController.direction.kDown);
            uUtil = GetDirectionUtility(c, m, GameController.direction.kUp);
            float max = Mathf.NegativeInfinity;
            if (lUtil > max)
            {
                lastDirection = GameController.direction.kLeft;
                max           = lUtil;
            }
            if (rUtil > max)
            {
                lastDirection = GameController.direction.kRight;
                max           = rUtil;
            }
            if (dUtil > max)
            {
                lastDirection = GameController.direction.kDown;
                max           = dUtil;
            }
            if (uUtil > max)
            {
                lastDirection = GameController.direction.kUp;
                max           = uUtil;
            }
        }

        return(lastDirection);
    }
Exemple #6
0
    GameController.direction MakeRandomAction(xyLoc myLoc, GameController.direction forbidden, GameController c)
    {
        while (true)
        {
            switch (Random.Range(0, 4))
            {
            case 0:
                if (c.CanMove(myLoc, GameController.direction.kUp) && forbidden != GameController.direction.kUp)
                {
                    return(GameController.direction.kUp);
                }
                break;

            case 1:
                if (c.CanMove(myLoc, GameController.direction.kLeft) && forbidden != GameController.direction.kLeft)
                {
                    return(GameController.direction.kLeft);
                }
                break;

            case 2:
                if (c.CanMove(myLoc, GameController.direction.kDown) && forbidden != GameController.direction.kDown)
                {
                    return(GameController.direction.kDown);
                }
                break;

            case 3:
                if (c.CanMove(myLoc, GameController.direction.kRight) && forbidden != GameController.direction.kRight)
                {
                    return(GameController.direction.kRight);
                }
                break;
            }
        }
    }
Exemple #7
0
    public GameController.direction GetAction(GameMaze m, GameController c, xyLoc myLoc)
    {
        timeElapsed += Time.deltaTime;

        //		Ghosts are forced to reverse direction by the system anytime the mode changes from:
        //		chase-to-scatter
        //		chase-to-frightened
        //		scatter-to-chase
        //		scatter-to-frightened
        if ((lastMode == ghostMode.kChase && currentMode == ghostMode.kScatter) ||
            (lastMode == ghostMode.kChase && currentMode == ghostMode.kFrightened) ||
            (lastMode == ghostMode.kScatter && currentMode == ghostMode.kChase) ||
            (lastMode == ghostMode.kScatter && currentMode == ghostMode.kFrightened))
        {
            lastMode      = currentMode;
            lastDirection = c.InvertDirection(lastDirection);
            return(lastDirection);
        }

        if (!c.AtIntersection(myLoc))
        {
            return(lastDirection);
        }

        GameController.direction forbiddenDirection = c.InvertDirection(lastDirection);


        int targetx, targety;

        switch (currentMode)
        {
        case ghostMode.kChase:
            if (timeElapsed >= 20)               // Implement: when do you leave chase mode?
            {
                lastMode = currentMode;
                //currentMode = // What mode do you change to?
                currentMode = ghostMode.kScatter;

                timeElapsed = 0;
            }
            GetTarget(out targetx, out targety, c);
            lastDirection = MoveToTarget(targetx, targety, myLoc, forbiddenDirection, c);
            return(lastDirection);

        case ghostMode.kScatter:
            if (timeElapsed >= 7)               // Implement: when do you leave chase mode?
            {
                lastMode = currentMode;
                //currentMode = // What mode do you change to?
                currentMode = ghostMode.kChase;

                timeElapsed = 0;
            }
            GetTarget(out targetx, out targety, c);
            lastDirection = MoveToTarget(targetx, targety, myLoc, forbiddenDirection, c);
            return(lastDirection);

        case ghostMode.kDead:
            GetTarget(out targetx, out targety, c);

            // Reached home location
            if (myLoc.x / 8 == targetx && myLoc.y / 8 == targety)
            {
                lastMode = currentMode;
                //currentMode = // What mode do you change to?
                Debug.Log("Reached target from death");
                currentMode = ghostMode.kChase;
                this.GetComponent <Renderer> ().material.color = this.GetComponent <GhostSpecialization> ().GetBaseColor();
                this.transform.localScale = Vector3.one;


                timeElapsed = 0;
            }
            else
            {
                lastDirection = MoveToTarget(targetx, targety, myLoc, forbiddenDirection, c);
            }
            return(lastDirection);

        case ghostMode.kFrightened:
            lastDirection = MakeRandomAction(myLoc, forbiddenDirection, c);
            return(lastDirection);

        case ghostMode.kLeavingHouse:
            // Ready to leave home area
            GetTarget(out targetx, out targety, c);
            if (myLoc.x / 8 == targetx && myLoc.y / 8 == targety)
            {
                lastMode = currentMode;
                //currentMode = // What mode do you change to?
                currentMode = ghostMode.kScatter;

                timeElapsed = 0;
            }
            else
            {
                lastDirection = MoveToTarget(targetx, targety, myLoc, forbiddenDirection, c);
            }
            return(lastDirection);

        case ghostMode.kInHouse:
            if (timeElapsed > this.GetComponent <GhostSpecialization> ().GetStartDelay())
            {
                lastMode = currentMode;
                //currentMode = // What mode do you change to?
                currentMode = ghostMode.kLeavingHouse;
                timeElapsed = 0;
            }
            break;
        }

        // Should never get here
        return(GameController.direction.kNone);
    }
Exemple #8
0
 void Start()
 {
     currentDirection = GameController.direction.kNone;
     lastDirection    = GameController.direction.kNone;
 }
Exemple #9
0
    xyLoc HandleAction(xyLoc currLoc, ControlledObject obj, bool canEat)
    {
        GameMaze m = board.GetComponent <BoardController> ().GetMaze();

        GameController.direction act = obj.GetAction(m, this, currLoc);
        switch (act)
        {
        case direction.kUp:
            if (CanMove(currLoc, direction.kUp))
            {
                currLoc.y++;
                if (canEat)
                {
                    pacmanLastMove = direction.kUp;
                }
            }
            break;

        case direction.kDown:
            if (CanMove(currLoc, direction.kDown))
            {
                currLoc.y--;
                if (canEat)
                {
                    pacmanLastMove = direction.kDown;
                }
            }
            break;

        case direction.kRight:
            if (CanMove(currLoc, direction.kRight))
            {
                currLoc.x++;
                if (canEat)
                {
                    pacmanLastMove = direction.kRight;
                }
            }
            break;

        case direction.kLeft:
            if (CanMove(currLoc, direction.kLeft))
            {
                currLoc.x--;
                if (canEat)
                {
                    pacmanLastMove = direction.kLeft;
                }
            }
            break;
        }
        if (currLoc.x < 0)
        {
            currLoc.x = pixelWidth - 9;
        }
        if (currLoc.x >= pixelWidth - 8)
        {
            currLoc.x = 0;
        }
        if (AtGridCenter(currLoc) && canEat)
        {
            bool powerUp, dot;
            eatenDots += board.GetComponent <BoardController> ().ClearCell(currLoc.x / 8, currLoc.y / 8, out dot, out powerUp);
            if (powerUp)
            {
                StartPowerUp();
                GetSound(gameSound.kEatPowerup).Play();
            }
            else if (dot)
            {
                GetSound(gameSound.kEatDot).Play();
            }
            if (eatenDots == 244)
            {
                eatenDots       = 0;
                currentGameMode = gameMode.kLevelComplete;
                GetSound(gameSound.kBackgroundSound).Stop();
            }
        }
        return(currLoc);
    }
Exemple #10
0
    float GetDirectionUtility(GameController c, GameMaze m, GameController.direction dir)
    {
        xyLoc ghostLoc = GetClosestGhostLoc(c);
        float util     = 0;
        int   x        = c.pacmanLoc.x / 8;
        int   y        = c.pacmanLoc.y / 8;
        xyLoc nextSpot = new xyLoc();

        switch (dir)
        {
        case GameController.direction.kLeft:
        {
            nextSpot = new xyLoc(x - 1, y);
            if (ghostLoc.x >= x)
            {
                util += GetGhostFleeValue(c) * GhostFleeM;
            }
        }
        break;

        case GameController.direction.kRight:
        {
            nextSpot = new xyLoc(x + 1, y);
            if (ghostLoc.x <= x)
            {
                util += GetGhostFleeValue(c) * GhostFleeM;
            }
        }
        break;

        case GameController.direction.kDown:
        {
            nextSpot = new xyLoc(x, y - 1);
            if (ghostLoc.y >= y)
            {
                util += GetGhostFleeValue(c) * GhostFleeM;
            }
        }
        break;

        case GameController.direction.kUp:
        {
            nextSpot = new xyLoc(x, y + 1);
            if (ghostLoc.y <= y)
            {
                util += GetGhostFleeValue(c) * GhostFleeM;
            }
        }
        break;

        default:
            break;
        }

        if (m.GetCellType(nextSpot) == GameMaze.occupancy.kWall)
        {
            return(-1);
        }

        if (powerUp)
        {
            util += GetChaseGhostValue(c) * ChaseGhostM;
        }

        if (MoveTowards(nextSpot) != MoveTowards(ghostLoc))
        {
            util += GetGhostFleeValue(c) * GhostFleeM;
        }

        if (MoveTowards(nextSpot) == MoveTowards(dotx, doty))
        {
            util += GetChaseDotValue(c, new xyLoc(dotx, doty)) * ChaseDotM;
        }

        if (powerUpsGotten < 4 && MoveTowards(nextSpot) == MoveTowards(pelletx, pellety))
        {
            util += GetGhostFleeValue(c) * ChasePowerPelletM;
        }
        util = Mathf.Clamp(util, 0, 1);
        return(util);
    }
Exemple #11
0
 public void Reset()
 {
     lastx         = lasty = 0;
     powerUp       = false;
     lastDirection = GameController.direction.kLeft;
 }
Exemple #12
0
 void Start()
 {
     powerUp       = false;
     distances     = new int[GameMaze.boardWidth, GameMaze.boardHeight];
     lastDirection = GameController.direction.kLeft;
 }