前言
FPS游戏之分数,生命数
PlayerOverviewPanel:通过颜色区分人物,在unity场景中显示多个玩家的分数以及生命数
一、在unity中的制作
1.在unity中建立一个UI界面
2.代码控制
foreach (Player p in PhotonNetwork.PlayerList)
{
GameObject entry = Instantiate(PlayerOverviewEntryPrefab);
entry.transform.SetParent(gameObject.transform);
entry.transform.localScale = Vector3.one;
//entry.GetComponent<Text>().color = AsteroidsGame.GetColor(p.GetPlayerNumber());
entry.GetComponent<Text>().color = AppKeepLauncher.Launcher.GetColor(p.GetPlayerNumber());
entry.GetComponent<Text>().text = string.Format("{0}\nScore: {1}\n", p.NickName, p.GetScore());
playerListEntries.Add(p.ActorNumber, entry);
}
public override void OnPlayerLeftRoom(Player otherPlayer)
{
GameObject go = null;
if (this.playerListEntries.TryGetValue(otherPlayer.ActorNumber, out go))
{
Destroy(playerListEntries[otherPlayer.ActorNumber]);
playerListEntries.Remove(otherPlayer.ActorNumber);
}
}
public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
{
GameObject entry;
if (playerListEntries.TryGetValue(targetPlayer.ActorNumber, out entry))
{
entry.GetComponent<Text>().text = string.Format("{0}\nScore: {1}\nLives: {2}", targetPlayer.NickName, targetPlayer.GetScore(), targetPlayer.CustomProperties[AsteroidsGame.PLAYER_LIVES]);
}
}
二、人物分数问题
当子弹打中人,然后从子弹身上获得存子弹的owner,从而给子弹的拥有者加分
首先在创建子弹时存入设计他的那个owner
private void CreateBullet()
{
// bullet = Instantiate(BulletPrefab, position, Quaternion.identity) as GameObject;
// bullet.GetComponent<Bullet>().InitializeBullet(photonView.Owner, (rotation * Vector3.forward), Mathf.Abs(lag));
//TODO:入池
GameObject tmpBullet = BulletObjectPool.Instance.GetInstanceFromPoolWithoutActive();
tmpBullet.transform.position = MuzzlePoint.position;
tmpBullet.transform.rotation = MuzzlePoint.rotation;
tmpBullet.SetActive(true);
tmpBullet.GetComponent<Bullet>().Initialize(photonView.Owner);
GameObject bul = Instantiate(BulletPrefab, MuzzlePoint.position, MuzzlePoint.rotation);
bul.GetComponent<Bullet>().Initialize(photonView.Owner);
if (Physics.Raycast(prevPosition, (bulletTransform.position - prevPosition).normalized, out RaycastHit tmp_Hit, (bulletTransform.position - prevPosition).magnitude))
{
ForthGame.Player damagerScript = tmp_Hit.collider.gameObject.GetComponent<ForthGame.Player>();
if (damagerScript != null)//是玩家
{
damagerScript.TakeDamage(25);
damagerScript.AddScoreViaBullet(Owner);
}
public void AddScoreViaBullet(Photon.Realtime.Player owner)//单独写一个方法是因为下面那个方法是实现的接口,不能改参数
{
if(photonView.IsMine)
{
owner.AddScore(1);
}
}
三、人物生命数问题
人物如果被判断死亡,生命数会减一,若大于0,则会执行复活代码,最后死亡
public void TakeDamage(int _damage)
{
if (photonView.IsMine)//自己扣血
{
playerHP.TakeDamage(_damage);
if (IsDeath())
{
object lives;
PhotonNetwork.LocalPlayer.CustomProperties.TryGetValue(Const.PLAYER_LIVES, out lives);
ExitGames.Client.Photon.Hashtable hashtable = new ExitGames.Client.Photon.Hashtable()
{
{
Const.PLAYER_LIVES, ((int)lives-1) } };
PhotonNetwork.LocalPlayer.SetCustomProperties(hashtable);
if (globalCamera)
globalCamera.SetActive(true);
if(((int)lives - 1) >= 1)
{
Respawn?.Invoke(3);
}
PhotonNetwork.Destroy(this.gameObject);
}
}
}
private void CheckEndOfGame()
{
int alivePlayerNum = 0;
foreach (Photon.Realtime.Player p in PhotonNetwork.PlayerList)
{
object lives;
if (p.CustomProperties.TryGetValue(Const.PLAYER_LIVES, out lives))//**********************
{
if ((int)lives >= 1)
{
alivePlayerNum++;
}
}
}
if (alivePlayerNum <= 1)//少于或仅一人存活
{
if (PhotonNetwork.IsMasterClient)
{
StopAllCoroutines();
}
StartCoroutine(EndOfGame(2));
}
}
四、最终效果
最终成果如下: