完成了怪物的状态机与玩家移动,就项目而言已经基本完成主要内容。接下来就是对游戏不断进行完善。
一、让玩家按下方向键,角色就可以一直向一个方向移懂,而不需要一直控制方向键来控制角色移动。
private Vector2 curDir;
if (CanGo(curDir) == false)
curDir = Vector2.zero;
if (dir != Vector2.zero)
curDir = dir;
dest += curDir;
二、玩家得分计算
添加一个游戏控制器,开控制加分
public class GameController : MonoBehaviour
{
public Text scoreText;
private int score = 0;
public void OnEatBean()
{
score += 10;
scoreText.text = string.Format("Score:{0}", score);
}
public static GameController instance;
private void Awake()
{
instance = this;
}
}
将pacman碰撞器设为触发器,然后在豆子上添加触发检测调用控制器的得分方法
public class Pacdot : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "pacman")
{
GameController.instance.OnEatBean();
Destroy(this.gameObject);
}
}
}
三、玩家碰到怪物结束游戏
玩家触发到怪物
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag=="Blinky")
{
GameController.instance.OnMeetMonster();
GetComponent<Animator>().SetTrigger("Die");
}
}
停止怪物与玩家在游戏结束后继续操作
if (GameController.instance.gameOver)
return;
玩家死亡动画 UI界面显示 这里跟得分的操作差不多
public void OnMeetMonster()
{
gameOver = true;
gameovertext.enabled = true;
}
四、过关判定
public bool IsLevelUp()
{
for (int row = 0; row < beans.childCount; row++)
{
for(int col=0;col<beans.GetChild(row).childCount;col++)
{
Transform bean = beans.GetChild(row).GetChild(col);
if (bean.GetComponent<SpriteRenderer>().enabled)
return false;
}
}
return true;
}
public void OnGameWin()
{
gameOver = true;
gameovertext.enabled = true;
gameovertext.text = "You Win";
}
调用
if (GameController.instance.IsLevelUp())
{
GameController.instance.OnGameWin();
}
if (GameController.instance.gameOver)
{ return;
}
五、道具生成
public void Start()
{
walkableList= GenWalkableList();
InvokeRepeating("SpawnPower", powerUpspawnSpeed, powerUpspawnSpeed);
}
public void SpawnPower()
{
GameObject powerUp= Instantiate(powerUpPrefabs);
powerUp.transform.position = walkableList[Random.Range(0, walkableList.Count )]-LeftBottom;
powerUp.transform.SetParent(powerUps.transform);
}
六、道具效果
public void OnMeetPowerUp()
{
isPlayerPoweredUp = true;
Invoke("LostPowerUp",powerUpEffectSec);
}
private void LostPowerUp()
{
isPlayerPoweredUp = false;
}
Monster
if (GameController.instance.isPlayerPoweredUp)
{
this.GetComponent<SpriteRenderer>().material.color = new Color(0.5f,0.5f,0.8f,1f);
}
else
{
this.GetComponent<SpriteRenderer>().material.color = Color.white;
}
七、道具效果叠加与生成时间随机化
private float curPowerUpEffectTime = 0;
private float totalPowerUpEffectTime = 0;
public void Update()
{
if (isPlayerPoweredUp)
{
curPowerUpEffectTime += Time.deltaTime;
if (curPowerUpEffectTime > totalPowerUpEffectTime)
{
isPlayerPoweredUp = false;
curPowerUpEffectTime = 0;
totalPowerUpEffectTime = 0;
}
}
}
public void OnMeetPowerUp()
{
isPlayerPoweredUp = true;
totalPowerUpEffectTime += powerUpEffectSec;
}
生成时间随机化
Invoke("SpawnPowerUp", Random.Range(powerUpspawnSpeedMin,powerUpspawnSpeedMax));
七、敌人躲避吃了道具的玩家
public Vector2 GetEscapeCorner(Vector3 monsterPos)
{
List<Vector2> escapable = new List<Vector2>();
for (int i = 0; i < corners.Length; i++)
{
Vector3 playerPos = playertrans.position;
float dPlayer =( playerPos - corners[i]).magnitude;
float dMonster = (monsterPos - corners[i]).magnitude;
if (dMonster < dPlayer)
{
escapable.Add(corners[i]);
}
}
Vector2 selectedCorner = escapable[Random.Range(0, escapable.Count)];
return selectedCorner;
}
|