• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

109 - Manager - PlayerManager.cs - 经理_玩家

作者:

贺及楼

成为作者

更新日期:2023-09-17 10:58:53

  1. public class PlayerManager : BaseManager
  2. {
  3. public PlayerManager(GameFacade facade) : base(facade) { }
  4. private UserData userData;
  5. private Dictionary<RoleType, RoleData> roleDataDict = new Dictionary<RoleType, RoleData>();
  6. private Transform rolePositions;
  7. private RoleType currentRoleType;
  8. private GameObject currentRoleGameObject;
  9. private GameObject playerSyncRequest;
  10. private GameObject remoteRoleGameObject;
  11. private ShootRequest shootRequest;
  12. private AttackRequest attackRequest;
  13. public void UpdateResult(int totalCount,int winCount){}
  14. public void SetCurrentRoleType(RoleType rt){}
  15. public UserData UserData{set { userData = value; }get { return userData; }}
  16. public override void OnInit(){}
  17. private void InitRoleDataDict(){}
  18. public void SpawnRoles(){}
  19. public GameObject GetCurrentRoleGameObject(){}
  20. private RoleData GetRoleData(RoleType rt){}
  21. public void AddControlScript(){}
  22. public void CreateSyncRequest(){}
  23. public void Shoot(箭预制体,V3位置, V3方向){}
  24. public void RemoteShoot(RoleType rt, V3位置, V3方向){}
  25. public void SendAttack(伤害){}
  26. public void GameOver() {}
  27. }
  1. public class PlayerManager : BaseManager
  2. {
  3. public PlayerManager(GameFacade facade) : base(facade) { }
  4. private UserData userData;
  5. private Dictionary<RoleType, RoleData> roleDataDict = new Dictionary<RoleType, RoleData>();
  6. private Transform rolePositions;
  7. private RoleType currentRoleType;
  8. private GameObject currentRoleGameObject;
  9. private GameObject playerSyncRequest;
  10. private GameObject remoteRoleGameObject;
  11. private ShootRequest shootRequest;
  12. private AttackRequest attackRequest;
  13. public void UpdateResult(int totalCount,int winCount){}
  14. public void SetCurrentRoleType(RoleType rt){}
  15. public UserData UserData{set { userData = value; }get { return userData; }}
  16. public override void OnInit(){}
  17. private void InitRoleDataDict(){}
  18. public void SpawnRoles(){}
  19. public GameObject GetCurrentRoleGameObject(){}
  20. private RoleData GetRoleData(RoleType rt){}
  21. public void AddControlScript(){}
  22. public void CreateSyncRequest(){}
  23. public void Shoot(GameObject arrowPrefab,Vector3 pos,Quaternion rotation){}
  24. public void RemoteShoot(RoleType rt, Vector3 pos, Vector3 rotation){}
  25. public void SendAttack(int damage){}
  26. public void GameOver() {}
  27. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Common;
  5. public class PlayerManager : BaseManager
  6. {
  7. public PlayerManager(GameFacade facade) : base(facade) { }
  8. private UserData userData;
  9. private Dictionary<RoleType, RoleData> roleDataDict = new Dictionary<RoleType, RoleData>();
  10. private Transform rolePositions;
  11. private RoleType currentRoleType;
  12. private GameObject currentRoleGameObject;
  13. private GameObject playerSyncRequest;
  14. private GameObject remoteRoleGameObject;
  15. private ShootRequest shootRequest;
  16. private AttackRequest attackRequest;
  17. public void UpdateResult(int totalCount,int winCount)
  18. {
  19. userData.TotalCount = totalCount;
  20. userData.WinCount = winCount;
  21. }
  22. public void SetCurrentRoleType(RoleType rt)
  23. {
  24. currentRoleType = rt;
  25. }
  26. public UserData UserData
  27. {
  28. set { userData = value; }
  29. get { return userData; }
  30. }
  31. public override void OnInit()
  32. {
  33. rolePositions = GameObject.Find("RolePositions").transform;
  34. InitRoleDataDict();
  35. }
  36. private void InitRoleDataDict()
  37. {
  38. roleDataDict.Add(RoleType.Blue, new RoleData(RoleType.Blue, "Hunter_BLUE", "Arrow_BLUE", "Explosion_BLUE",rolePositions.Find("Position1")));
  39. roleDataDict.Add(RoleType.Red, new RoleData(RoleType.Red, "Hunter_RED", "Arrow_RED", "Explosion_RED", rolePositions.Find("Position2")));
  40. }
  41. public void SpawnRoles()
  42. {
  43. foreach(RoleData rd in roleDataDict.Values)
  44. {
  45. GameObject go= GameObject.Instantiate(rd.RolePrefab, rd.SpawnPosition, Quaternion.identity);
  46. go.tag = "Player";
  47. if (rd.RoleType == currentRoleType)
  48. {
  49. currentRoleGameObject = go;
  50. currentRoleGameObject.GetComponent<PlayerInfo>().isLocal = true;
  51. }
  52. else
  53. {
  54. remoteRoleGameObject = go;
  55. }
  56. }
  57. }
  58. public GameObject GetCurrentRoleGameObject()
  59. {
  60. return currentRoleGameObject;
  61. }
  62. private RoleData GetRoleData(RoleType rt)
  63. {
  64. RoleData rd = null;
  65. roleDataDict.TryGetValue(rt, out rd);
  66. return rd;
  67. }
  68. public void AddControlScript()
  69. {
  70. currentRoleGameObject.AddComponent<PlayerMove>();
  71. PlayerAttack playerAttack = currentRoleGameObject.AddComponent<PlayerAttack>();
  72. RoleType rt = currentRoleGameObject.GetComponent<PlayerInfo>().roleType;
  73. RoleData rd = GetRoleData(rt);
  74. playerAttack.arrowPrefab = rd.ArrowPrefab;
  75. playerAttack.SetPlayerMng(this);
  76. }
  77. public void CreateSyncRequest()
  78. {
  79. playerSyncRequest=new GameObject("PlayerSyncRequest");
  80. playerSyncRequest.AddComponent<MoveRequest>().SetLocalPlayer(currentRoleGameObject.transform, currentRoleGameObject.GetComponent<PlayerMove>())
  81. .SetRemotePlayer(remoteRoleGameObject.transform);
  82. shootRequest=playerSyncRequest.AddComponent<ShootRequest>();
  83. shootRequest.playerMng = this;
  84. attackRequest = playerSyncRequest.AddComponent<AttackRequest>();
  85. }
  86. public void Shoot(GameObject arrowPrefab,Vector3 pos,Quaternion rotation)
  87. {
  88. facade.PlayNormalSound(AudioManager.Sound_Timer);
  89. GameObject.Instantiate(arrowPrefab, pos, rotation).GetComponent<Arrow>().isLocal = true;
  90. shootRequest.SendRequest(arrowPrefab.GetComponent<Arrow>().roleType, pos, rotation.eulerAngles);
  91. }
  92. public void RemoteShoot(RoleType rt, Vector3 pos, Vector3 rotation)
  93. {
  94. GameObject arrowPrefab = GetRoleData(rt).ArrowPrefab;
  95. Transform transform = GameObject.Instantiate(arrowPrefab).GetComponent<Transform>();
  96. transform.position = pos;
  97. transform.eulerAngles = rotation;
  98. }
  99. public void SendAttack(int damage)
  100. {
  101. attackRequest.SendRequest(damage);
  102. }
  103. public void GameOver()
  104. {
  105. //private GameObject currentRoleGameObject;
  106. //private GameObject playerSyncRequest;
  107. //private GameObject remoteRoleGameObject;
  108. //private ShootRequest shootRequest;
  109. //private AttackRequest attackRequest;
  110. GameObject.Destroy(currentRoleGameObject);
  111. GameObject.Destroy(playerSyncRequest);
  112. GameObject.Destroy(remoteRoleGameObject);
  113. shootRequest = null;
  114. attackRequest = null;
  115. }
  116. }