项目实训(二十)太空陨石游戏的游戏管理
时间:2023-11-26 23:37:00
文章目录
- 前言
- 一、场景加载
- 二、产生行星
- 三、游戏结束,计算分数,得到排名
前言
AsteroidsGameManager: 管理游戏脚本,添加计时器。计时结束后,委托事件触发,实例化自己的角色,主机生成游戏内容和行星。判断游戏结束(通过生命)||全员加载(然后开始倒计时),游戏结束后通过判断分数计算冠军,并在线计算冠军socket记录信息。
`
一、场景加载
检查场景是否全部加载。
private bool CheckAllPlayerLoadedLevel()///检查场景是否加载。 {
foreach (Player p in PhotonNetwork.PlayerList) {
object playerLoadedLevel; if (p.CustomProperties.TryGetValue(AsteroidsGame.PLAYER_LOADED_LEVEL, out playerLoadedLevel)) {
if ((bool) playerLoadedLevel) {
continue; } } return false; } return true; }
public override void OnEnable()//添加计时委托 {
base.OnEnable(); CountdownTimer.OnCountdownTimerHasExpired = OnCountdownTimerIsExpired;///给委托添加一种方法,将在这里执行开始游戏 } public void Start()///通知别人我加载了。 {
Hashtable props = new Hashtable {
{
AsteroidsGame.PLAYER_LOADED_LEVEL, true}
};
PhotonNetwork.LocalPlayer.SetCustomProperties(props);//“通知其他人”我加载好了场景
}
二、生成行星
private IEnumerator SpawnAsteroid()//开始生成行星:PhotonNetwork.InstantiateRoomObject("BigAsteroid",
{
while (true)
{
yield return new WaitForSeconds(Random.Range(AsteroidsGame.ASTEROIDS_MIN_SPAWN_TIME, AsteroidsGame.ASTEROIDS_MAX_SPAWN_TIME));
Vector2 direction = Random.insideUnitCircle;
Vector3 position = Vector3.zero;
if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
{
// Make it appear on the left/right side
position = new Vector3(Mathf.Sign(direction.x) * Camera.main.orthographicSize * Camera.main.aspect, 0, direction.y * Camera.main.orthographicSize);
}
else
{
// Make it appear on the top/bottom
position = new Vector3(direction.x * Camera.main.orthographicSize * Camera.main.aspect, 0, Mathf.Sign(direction.y) * Camera.main.orthographicSize);
}
// Offset slightly so we are not out of screen at creation time (as it would destroy the asteroid right away)
position -= position.normalized * 0.1f;
Vector3 force = -position.normalized * 1000.0f;
Vector3 torque = Random.insideUnitSphere * Random.Range(500.0f, 1500.0f);
object[] instantiationData = {
force, torque, true};
PhotonNetwork.InstantiateRoomObject("BigAsteroid", position, Quaternion.Euler(Random.value * 360.0f, Random.value * 360.0f, Random.value * 360.0f), 0, instantiationData);
}
}
游戏开始后,全部加载好后,计时结束后,委托事件触发这里,实例化自己的角色,主机生成游戏内容
private void StartGame()//全部加载好后,计时结束后,委托事件触发这里------实例化自己的角色,主机生成游戏内容
{
Debug.Log("StartGame!");
// on rejoin, we have to figure out if the spaceship exists or not
// if this is a rejoin (the ship is already network instantiated and will be setup via event) we don't need to call PN.Instantiate
float angularStart = (360.0f / PhotonNetwork.CurrentRoom.PlayerCount) * PhotonNetwork.LocalPlayer.GetPlayerNumber();
float x = 20.0f * Mathf.Sin(angularStart * Mathf.Deg2Rad);//转弧度制
float z = 20.0f * Mathf.Cos(angularStart * Mathf.Deg2Rad);
Vector3 position = new Vector3(x, 0.0f, z);
Quaternion rotation = Quaternion.Euler(0.0f, angularStart, 0.0f);
PhotonNetwork.Instantiate("Spaceship", position, rotation, 0); // avoid this call on rejoin (ship was network instantiated before)
if (PhotonNetwork.IsMasterClient)
{
StartCoroutine(SpawnAsteroid());
}
}
三、游戏结束并计算分数,并得出排名
private IEnumerator EndOfGame(float waitTime)
{
//isGameStart = false;
yield return new WaitForSeconds(waitTime);
recordPanel.SetActive(true);
GameObject winnerGo = null;
int currentMaxScore = -9999;
Photon.Realtime.Player winner = null;
foreach (Photon.Realtime.Player p in PhotonNetwork.PlayerList)
{
GameObject recordEntryGo = Instantiate(recordEntryPrefab, vlg.transform);
recordEntryGo.GetComponent<RecordEntry>().InitializeOnlyScore(p.NickName, p.GetScore());
recordEntryGo.transform.SetParent(vlg.transform);
//计算综合分数决定冠军
if (p.GetScore() > currentMaxScore)
{
winnerGo = recordEntryGo;
currentMaxScore = p.GetScore();
winner = p;
}
}
winnerGo.GetComponent<RecordEntry>().ShowWinnerIcon();
---
用此方法可以得到下一关的生命数
PhotonNetwork.LocalPlayer.SetCustomProperties(props);//下一关生命