Esempio n. 1
0
 public static void SetStatus(GhostStatus status)
 {
     foreach (GhostObject ghost in Ghosts)
     {
         ghost.Status = status;
     }
 }
    IEnumerator BackToTheRoom()
    {
        transform.position = Vector3.Lerp(transform.position, GhostsRoom.position, speed * Time.deltaTime);
        yield return(null);

        ghostStatus = GhostStatus.InRoom;
    }
Esempio n. 3
0
 private void ChangeGhostsStatus(GhostStatus ghostStatus)
 {
     foreach (var ghost in ghosts)
     {
         timer.Stop();
         ghost.modeStatus = ghostStatus;
         timer.Start();
     }
 }
    // Use this for initialization
    void Start()
    {
        Playermask = LayerMask.GetMask("Player");
        Wallsmask  = LayerMask.GetMask("Walls", "GhostsDoor");

        ghostStatus = GhostStatus.InRoom;

        ChooseTheFirtDirection();

        animator = GetComponent <Animator>();
    }
    // Update is called once per frame
    void Update()
    {
        if (GameManager.instance.IsCompleted())
        {
            return;
        }

        if (GameManager.instance.IsLost())
        {
            ghostStatus = GhostStatus.InRoom;
            ChooseTheFirtDirection();
            return;
        }

        if (GameManager.instance.IsStrong())
        {
            if (ghostStatus == GhostStatus.Patroling || ghostStatus == GhostStatus.Chasing || ghostStatus == GhostStatus.ScanForPlayer)
            {
                ghostStatus = GhostStatus.Scared;
            }
        }
        switch (ghostStatus)
        {
        case GhostStatus.InRoom:
        case GhostStatus.ReadyToExit:
            GetOutTheRoom();
            break;

        case GhostStatus.ReadyToEnter:
            break;

        case GhostStatus.ScanForPlayer:
            //make a decision Patroling or Chasing
            ScanForThePlayer();
            break;

        case GhostStatus.Chasing:
            LetChase();
            break;

        case GhostStatus.Patroling:
            Patrol();
            break;

        case GhostStatus.Scared:
            Scared();
            break;

        case GhostStatus.Eaten:
            StartCoroutine(BackToTheRoom());
            break;
        }
    }
Esempio n. 6
0
 private Ghost(string name, CellLocation homeLocation, CellLocation currentLocation, Direction direction, CellLocation scatterTarget, IGhostStrategy chaseStrategy, IGhostStrategy currentStrategy, GhostStatus ghostStatus, int numberOfCoinsRequiredToExitHouse)
 {
     Name            = name;
     Home            = homeLocation;
     Location        = currentLocation;
     Direction       = direction;
     ChaseStrategy   = chaseStrategy;
     CurrentStrategy = currentStrategy;
     ScatterTarget   = scatterTarget;
     Status          = ghostStatus;
     NumberOfCoinsRequiredToExitHouse = numberOfCoinsRequiredToExitHouse;
 }
    private void Scared()
    {
        if (!GameManager.instance.IsStrong())
        {
            ghostStatus = GhostStatus.Patroling;
            return;
        }
        Vector3 Diretion = ChooseDirection();

        SetGhstAnimation();
        transform.Translate(Diretion.x * speed * Time.deltaTime, Diretion.y * speed * Time.deltaTime, 0);
    }
Esempio n. 8
0
    IEnumerator GhostDead()
    {
        // NOTE Reset all the params and status
        animator.SetBool("Scared", false);
        animator.SetBool("Recover", false);
        Status = GhostStatus.Dead;
        animator.SetBool("Dead", true);
        yield return(new WaitForSeconds(5.0f));

        animator.SetBool("Dead", false);
        Status       = GhostStatus.Normal;
        scareRoutine = null;
    }
Esempio n. 9
0
    IEnumerator GhostScared()
    {
        Status = GhostStatus.Scared;
        animator.SetBool("Scared", true);
        yield return(new WaitForSeconds(7.0f));

        animator.SetBool("Recover", true);
        yield return(new WaitForSeconds(3.0f));

        animator.SetBool("Recover", false);
        animator.SetBool("Scared", false);
        Status       = GhostStatus.Normal;
        scareRoutine = null;
    }
        private IEnumerable <object[]> GetDataForStatus(GhostStatus status)
        {
            yield return(CreateTestData(1, PercentagesInMilliseconds.Percent40, status));

            for (var level = 2; level <= 4; level++)
            {
                yield return(CreateTestData(level, PercentagesInMilliseconds.Percent45, status));
            }

            for (var level = 5; level <= MoveClockTests.MaxLevel; level++)
            {
                yield return(CreateTestData(level, PercentagesInMilliseconds.Percent50, status));
            }
        }
    private void ScanForThePlayer()
    {
        RaycastHit2D hitup = Physics2D.Raycast(transform.position, Vector2.up, rayScanRange, Playermask);

        if (hitup.collider != null)
        {
            if (hitup.collider.gameObject.tag == "Player")
            {
                ghostStatus = GhostStatus.Chasing;
                return;
            }
        }

        RaycastHit2D hitright = Physics2D.Raycast(transform.position, Vector2.right, rayScanRange, Playermask);

        if (hitup.collider != null)
        {
            if (hitright.collider.gameObject.tag == "Player")
            {
                ghostStatus = GhostStatus.Chasing;
                return;
            }
        }

        RaycastHit2D hitdown = Physics2D.Raycast(transform.position, Vector2.down, rayScanRange, Playermask);

        if (hitup.collider != null)
        {
            if (hitdown.collider.gameObject.tag == "Player")
            {
                ghostStatus = GhostStatus.Chasing;
                return;
            }
        }

        RaycastHit2D hitleft = Physics2D.Raycast(transform.position, Vector2.left, rayScanRange, Playermask);

        if (hitup.collider != null)
        {
            if (hitleft.collider.gameObject.tag == "Player")
            {
                ghostStatus = GhostStatus.Chasing;
                return;
            }
        }

        ghostStatus = GhostStatus.Patroling;
    }
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.tag == "Player")
     {
         if (ghostStatus == GhostStatus.Scared)
         {
             animator.SetBool("movingUp", false);
             animator.SetBool("movingDown", false);
             animator.SetBool("movingRight", false);
             animator.SetBool("movingLeft", false);
             animator.SetBool("isScared", false);
             animator.SetBool("isEaten", true);
             ghostStatus = GhostStatus.Eaten;
             GameManager.instance.UpdatePointsFromGhosts();
         }
     }
 }
 private void GetOutTheRoom()
 {
     if (ghostStatus == GhostStatus.InRoom)
     {
         transform.position = Vector3.Lerp(transform.position, GhostsRoom.position, speed * Time.deltaTime);
         if (transform.position == GhostsRoom.position)
         {
             ghostStatus = GhostStatus.ReadyToExit;
         }
     }
     if (ghostStatus == GhostStatus.ReadyToExit)
     {
         transform.position = Vector3.Lerp(transform.position, FrontOfGhostsRoom.position, speed * Time.deltaTime);
     }
     if (transform.position == FrontOfGhostsRoom.position)
     {
         ghostStatus = GhostStatus.ScanForPlayer;
     }
 }
    private void Patrol()
    {
        if (ghostStatus != GhostStatus.Patroling)
        {
            return;
        }
        Vector3 Diretion = ChooseDirection();

        SetGhstAnimation();

        RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(Diretion.x, Diretion.y), rayScanRange, Playermask);

        if (hit.collider != null)
        {
            ghostStatus = GhostStatus.Chasing;
        }

        transform.Translate(Diretion.x * speed * Time.deltaTime, Diretion.y * speed * Time.deltaTime, 0);
    }
Esempio n. 15
0
        public GhostObject(ContentManager Content, Color color, TileObject startTile, int inBaseTimeLeft)
        {
            List <Texture2D> ghostSprites = new List <Texture2D>();

            leftEyeTexture  = Content.Load <Texture2D>("Sprites\\Ghost\\Eyeball");
            rightEyeTexture = Content.Load <Texture2D>("Sprites\\Ghost\\Eyeball");

            spriteNormalGhost = Content.Load <Texture2D>("Sprites\\Ghost\\ghost");
            spriteBlueGhost   = Content.Load <Texture2D>("Sprites\\Ghost\\blue_ghost");

            sprite = spriteNormalGhost;

            this.CurrentTile = startTile;
            this.Position    = this.CurrentTile.Position;
            this.Status      = GhostStatus.InsideBase;

            this.color  = color;
            this.center = new Vector2(sprite.Height / 2, sprite.Height / 2);

            this.spriteScale    = (float)Globals.GHOST_IMAGE_SIZE / (float)sprite.Height;
            this.eyeSpriteScale = (float)Globals.GHOST_EYES_IMAGE_SIZE / (float)leftEyeTexture.Height;

            this.leftEyeOffset.Y  = -1 * (float)(sprite.Height * .15) * spriteScale;
            this.leftEyeOffset.X  = -1 * (float)(sprite.Width / 2 * .18) * spriteScale;
            this.rightEyeOffset.Y = -1 * (float)(sprite.Height * .15) * spriteScale;
            this.rightEyeOffset.X = (float)(sprite.Width / 2 * .18) * spriteScale;
            this.eyeCenter        = new Vector2(leftEyeTexture.Width / 2, leftEyeTexture.Height / 2);
            this.eyeRotation      = (float)Math.Atan2(0, 1);

            spriteIndex       = 0;
            spriteTimeElapsed = 0;
            currentDirection  = MovementDirection.Up;
            distanceTraveled  = 0.0f;

            this.inbaseTimeLeft = inBaseTimeLeft;

            ai = new GhostAI(this);
        }
 public static GhostStatus GetStatus(string id)
 {
     foreach (var status in instance.statuses)
     {
         if (status.id != id)
         {
             continue;
         }
         GhostStatus s = new GhostStatus();
         s.id          = status.id;
         s.isUsed      = status.isUsed;
         s.prefab      = status.prefab;
         s.hp          = (int)(status.hp * instance.hp);
         s.damage      = (int)(status.damage * instance.damage);
         s.attackSpeed = status.attackSpeed * instance.attackSpeed;
         s.speed       = status.speed * instance.speed;
         s.cost        = status.cost;
         s.name        = status.name;
         s.sprite      = status.sprite;
         return(s);
     }
     throw new Exception($"{id.ToString().Fill("White")}를 찾을 수 없음");
 }
Esempio n. 17
0
    private void Update()
    {
        // NOTE Simple AI chasing pacman
        target = GameObject.Find("Pacman").transform;
        if (GameManager.IsInputEnabled)
        {
            Vector2 dir = new Vector2(target.position.x - transform.position.x, target.position.y - transform.position.y);
            characterMove.LastInput    = Vector2.ClampMagnitude(dir, 1.0f);
            characterMove.CurrentInput = Random.insideUnitCircle;
        }

        // NOTE Don't switch BGM until pause panel is destroyed and ghosts start moving
        if (GameObject.FindWithTag("Panel") != null)
        {
            Status = GhostStatus.Pause;
        }

        // NOTE Once pacman eat a power pellet
        if (scareRoutine == null && PacStudentController.PowerUp)
        {
            scareRoutine = StartCoroutine(GhostScared());
        }
    }
Esempio n. 18
0
 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.name == "Pacman")
     {
         if (Status == GhostStatus.Scared)
         {
             // NOTE Collide with pacman when scared -> this ghost dies
             StopAllCoroutines();
             PacStudentController.Score += 300;
             Status = GhostStatus.Dead;
             animator.SetBool("Scared", false);
             animator.SetBool("Recover", false);
             StartCoroutine(GhostDead());
         }
         if (Status == GhostStatus.Normal)
         {
             // NOTE Collide with pacman when normal(not sared or dead)
             if (PacStudentController.HealthPoint > 0)
             {
                 PacStudentController.HealthPoint -= 1;
             }
         }
     }
 }
Esempio n. 19
0
 public bool ShouldGhostMove(int gameLevel, int coinsRemaining, string ghostName, GhostStatus status, bool inTunnel)
 => status != GhostStatus.Edible || _counter % 2 == 1;
Esempio n. 20
0
 private Ghost WithNewStatus(GhostStatus newGhostStatus)
 => new Ghost(Name, Home, Location, Direction, ChaseStrategy, ScatterStrategy, FrightenedStrategy, CurrentStrategy, newGhostStatus, NumberOfCoinsRequiredToExitHouse);
Esempio n. 21
0
        public void GhostShouldTravelAtDifferentSpeedsWhenInTheTunnel(int level, int moveSpeedMilliseconds, GhostStatus ghostStatus)
        {
            var ghostName = Guid.NewGuid().ToString();

            TestMoveClock(moveSpeedMilliseconds,
                          moveClock => moveClock.ShouldGhostMove(level, coinsRemaining: int.MaxValue, ghostName, ghostStatus, inTunnel: true));
        }
 private object[] CreateTestData(int level, int milliseconds, GhostStatus status)
 {
     return(new object[] { level, milliseconds, status });
 }
    private void LetChase()
    {
        RaycastHit2D hit;

        if (ghostStatus != GhostStatus.Chasing)
        {
            return;
        }
        switch (ghostDirection)
        {
        case GhostDirection.Up:
            hit = Physics2D.Raycast(transform.position, new Vector2(0.0f, 1.0f), rayScanRange, Playermask);
            if (hit.collider == null)
            {
                ghostStatus = GhostStatus.Patroling;
            }
            else
            {
                transform.Translate(0.0f * speed * Time.deltaTime, 1.0f * speed * Time.deltaTime, 0);
            }
            break;

        case GhostDirection.Right:
            hit = Physics2D.Raycast(transform.position, new Vector2(1.0f, 0.0f), rayScanRange, Playermask);
            if (hit.collider == null)
            {
                ghostStatus = GhostStatus.Patroling;
            }
            else
            {
                transform.Translate(1.0f * speed * Time.deltaTime, 0.0f * speed * Time.deltaTime, 0);
            }

            break;

        case GhostDirection.Down:
            hit = Physics2D.Raycast(transform.position, new Vector2(0.0f, -1.0f), rayScanRange, Playermask);
            if (hit.collider == null)
            {
                ghostStatus = GhostStatus.Patroling;
            }
            else
            {
                transform.Translate(0.0f * speed * Time.deltaTime, -1.0f * speed * Time.deltaTime, 0);
            }

            break;

        case GhostDirection.Left:
            hit = Physics2D.Raycast(transform.position, new Vector2(-1.0f, 0.0f), rayScanRange, Playermask);
            if (hit.collider == null)
            {
                ghostStatus = GhostStatus.Patroling;
            }
            else
            {
                transform.Translate(-1.0f * speed * Time.deltaTime, 0.0f * speed * Time.deltaTime, 0);
            }
            break;
        }
    }
Esempio n. 24
0
 private Ghost WithNewEdibleAndDirectionAndStrategy(GhostStatus newGhostStatus, Direction newDirection, IGhostStrategy newCurrentStrategy)
 => new Ghost(Name, Home, Location, newDirection, ScatterTarget, ChaseStrategy, newCurrentStrategy, newGhostStatus, NumberOfCoinsRequiredToExitHouse);
Esempio n. 25
0
 // Use this for initialization
 void Start()
 {
     pumpkinCollider = GetComponent <SphereCollider> ();
     status          = GetComponent <GhostStatus> ();
     StartCoroutine(FindLight());
 }
Esempio n. 26
0
        public void Update(GameTime gameTime)
        {
            if (Status == GhostStatus.InsideBase)
            {
                inbaseTimeLeft -= gameTime.ElapsedGameTime.Milliseconds;
                if (inbaseTimeLeft <= 0)
                {
                    Status = GhostStatus.LeavingBase;
                }
            }
            if (Status == GhostStatus.Scared)
            {
                statusTimeLeft -= gameTime.ElapsedGameTime.Milliseconds;
                if (statusTimeLeft <= 0)
                {
                    Status = (inbaseTimeLeft > 0 ? GhostStatus.InsideBase : GhostStatus.Chase);
                }
            }
            else if (Status == GhostStatus.EyesOnly)
            {
                if (CurrentTile == MapObject.HealTile)
                {
                    Status           = GhostStatus.LeavingBase;
                    currentDirection = MovementDirection.Up;
                    futureDirection  = MovementDirection.Up;
                }
            }
            else if (Status == GhostStatus.LeavingBase && CurrentTile.IsGate)
            {
                Status           = GhostStatus.Chase;
                currentDirection = MovementDirection.Up;
                futureDirection  = MovementDirection.Up;
            }

            // Does ghost have the "opportunity" to change direction (He needs to have travelled a full tile)
            if ((distanceTraveled >= Globals.MAP_TILE_PIXEL_SIZE || distanceTraveled == 0.0f) && Status != GhostStatus.InsideBase)
            {
                distanceTraveled %= Globals.MAP_TILE_PIXEL_SIZE;

                // Change ghosts direction if he's allowed to go that way
                if (MapObject.DirectionAllowed(CurrentTile, futureDirection, Status == GhostStatus.LeavingBase || Status == GhostStatus.EyesOnly))
                {
                    ChangeDirection(futureDirection);
                }
                // See if ghost is running into a wall, stop him if he is.
                if (!MapObject.DirectionAllowed(CurrentTile, currentDirection, Status == GhostStatus.LeavingBase || Status == GhostStatus.EyesOnly))
                {
                    currentSpeed = 0.0f;
                }

                futureDirection = ai.GetFutureDirection();
            }

            switch (currentDirection)
            {
            case MovementDirection.Up:
                Position.Y = Position.Y - currentSpeed;
                break;

            case MovementDirection.Down:
                Position.Y = Position.Y + currentSpeed;
                break;

            case MovementDirection.Left:
                Position.X = Position.X - currentSpeed;
                break;

            case MovementDirection.Right:
                Position.X = Position.X + currentSpeed;
                break;
            }
            distanceTraveled += currentSpeed;

            // Update current tile that ghost is in
            CurrentTile = MapObject.UpdateCurrentTile(Position, center, CurrentTile);
        }
Esempio n. 27
0
        public void GhostShouldTravelAtDifferentSpeedsForDifferentLevelsWhenFrightened(int level, int moveSpeedMilliseconds, GhostStatus ghostStatus)
        {
            var ghostName = Guid.NewGuid().ToString();

            TestMoveClock(moveSpeedMilliseconds,
                          moveClock => moveClock.ShouldGhostMove(level, coinsRemaining: int.MaxValue, ghostName, ghostStatus, false));
        }
Esempio n. 28
0
        public bool ShouldGhostMove(int gameLevel, int coinsRemaining, string ghostName, GhostStatus ghostStatus, bool inTunnel)
        {
            if (!_ghostsLastMoves.TryGetValue(ghostName, out var ghostLastMoved))
            {
                ghostLastMoved = _internalClock;
                _ghostsLastMoves[ghostName] = ghostLastMoved;
                return(true);
            }

            int GetPercentageFrightened()
            {
                if (gameLevel == 1)
                {
                    return(50);
                }
                if (gameLevel < 5)
                {
                    return(55);
                }
                return(60);
            }

            int GetPercentageAlive()
            {
                if (gameLevel == 1)
                {
                    return(75);
                }
                if (gameLevel < 5)
                {
                    return(85);
                }
                return(95);
            }

            int GetPercentageInTunnel()
            {
                if (gameLevel == 1)
                {
                    return(40);
                }
                if (gameLevel < 5)
                {
                    return(45);
                }
                return(50);
            }

            int GetPercentageCruiseElroy()
            {
                int GetPoints()
                {
                    switch (gameLevel)
                    {
                    case 1:
                        return(20);

                    case 2:
                        return(30);

                    case 3:
                    case 4:
                    case 5:
                        return(40);

                    case 6:
                    case 7:
                    case 8:
                        return(50);

                    case 9:
                    case 10:
                    case 11:
                        return(60);

                    case 12:
                    case 13:
                    case 14:
                        return(80);

                    case 15:
                    case 16:
                    case 17:
                    case 18:
                        return(100);

                    default:
                        return(120);
                    }
                }

                var points = GetPoints();

                if (coinsRemaining <= points / 2)
                {
                    if (gameLevel == 1)
                    {
                        return(85);
                    }
                    if (gameLevel < 5)
                    {
                        return(95);
                    }
                    return(105);
                }

                if (coinsRemaining < points)
                {
                    if (gameLevel == 1)
                    {
                        return(80);
                    }
                    if (gameLevel < 5)
                    {
                        return(90);
                    }
                    return(100);
                }

                return(GetPercentageAlive());
            }

            var speed = (inTunnel, ghostStatus, ghostName) switch
            {
                (true, _, _) => PercentageToTime(GetPercentageInTunnel()),
                (_, GhostStatus.RunningHome, _) => PercentageToTime(160),
                (_, GhostStatus.Edible, _) => PercentageToTime(GetPercentageFrightened()),
                (_, GhostStatus.Flash, _) => PercentageToTime(GetPercentageFrightened()),
                (_, _, GhostNames.Blinky) => PercentageToTime(GetPercentageCruiseElroy()),
                _ => PercentageToTime(GetPercentageAlive())
            };

            if ((ghostLastMoved + speed) <= _internalClock)
            {
                _ghostsLastMoves[ghostName] = ghostLastMoved + speed;
                return(true);
            }

            return(false);
        }