Generation mobile PacMan 04-- props props eating monster

Copyright notice:

  • This article Original starting in the following website:
  1. Blog Park "excellent dream maker culture" in space: https: //www.cnblogs.com/raymondking123
  2. Excellent dream maker culture official blog: https: //91make.top
  3. Excellent game dream maker culture lecture: https: //91make.ke.qq.com
  4. "Excellent dream maker culture" of micro-channel public number: umaketop
  • You are free to reprint, but must include the full copyright notice

aims

Realize the function

Props will randomly appear on the coordinate beans
player will change color when eating monster props

Random props

Can get coordinates of all beans in AStar collection algorithm
the set of coordinates stored
in random props get when you create a coordinate position most props created randomly created to achieve the purpose of beans in the bean coordinate the collection

  public void SpawnPowerUp()
    {
        //游戏结束后将不创建道具
        if (gameOver)
            return;
        //使用预制体创建道具
        GameObject powerUp = Instantiate(powerUpPrefab);
        //道具创建的位置
        powerUp.transform.position = walkableList[Random.Range(0,walkableList.Count-1)]-liftBottom;
        //道具创建时设置父节点
        powerUp.transform.SetParent(powerUpParent.transform);
    }

Change the color of the monster

There is a isPowerUp in the game controller to determine whether the player has been PowerUp props

//获得道具时
 if (GameController.gameController.isPowerUp)
{
    //将怪物的颜色设置淡一些
    this.GetComponent<SpriteRenderer>().material.color = new Color(0.5f,0.5f,0.8f,1.0f);
}
else
{
    //道具失效后怪物的颜色将变为原来的颜色
    this.GetComponent<SpriteRenderer>().material.color = Color.white;
}

Completed a monster run away when players eat props

Logic:
When the players eat props, monsters will choose a corner conduct wayfinding, but also to go to the waypoints moved state.
When you select a corner, calculate the distance to the corner of the monster, the player to calculate the distance from the corner, if it is a short distance from the monster, it is placed in the corner of a candidate, the last point in the random selection of a target candidate corner.
Implementation:
Select the corner Code:

    public Vector2 GetEscapeCorner(Vector3 monsterPos)
    {
        //获得逃跑目标点
        List<Vector2> escapable = new List<Vector2>();
        for(int i=0;i<v3corners.Length;i++)
        {
            Vector3 playerPos = playerTrans.position;
            float dPlayer = (playerPos - v3corners[i]).magnitude;
            float dMonster = (monsterPos - v3corners[i]).magnitude;
            if(dMonster<dPlayer)
            {
                escapable.Add(corners[i].position);
            }
        }
        Vector2 selectCorner = escapable[UnityEngine.Random.Range(0, escapable.Count)];
        return selectCorner;
    }

Inner monsters, found that the state of the player:

    class SeekState : WaypointState
    {
        public SeekState(List<Vector2> path): base(path)
        {

        }
        public override void Update(Monster e)
        {
            if(GameController.instance.isPlayerPowerUp)
            {
                Vector2 corner = GameController.instance.GetEscapeCorner(e.transform.position);
                Vector2 p = GameController.instance.leftBottom;
                List<Vector2> path = e.finder.Find((Vector2)e.transform.position-p, corner-p);
                for (int i = 0; i < path.Count; i++)
                {
                    path[i] += p;
                }
                e.stateMaginer.ChangeState(new EscapeState(path));
                return;
            }
            base.Update(e);
        }
    }

Guess you like

Origin www.cnblogs.com/raymondking123/p/11596127.html