Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / ObjectManager.cs
  1.  
  2. using Godot;
  3.  
  4. public static class ObjectManager
  5. {
  6. /// <summary>
  7. /// 根据资源路径获取实例对象, 该对象必须实现 IPoolItem 接口
  8. /// </summary>
  9. /// <param name="resPath">资源路径</param>
  10. public static IPoolItem GetPoolItem(string resPath)
  11. {
  12. var item = ObjectPool.GetItem(resPath);
  13. if (item == null)
  14. {
  15. item = (IPoolItem)ResourceManager.LoadAndInstantiate<Node>(resPath);
  16. item.Logotype = resPath;
  17. }
  18.  
  19. return item;
  20. }
  21. /// <summary>
  22. /// 根据资源路径获取实例对象, 该对象必须实现 IPoolItem 接口
  23. /// </summary>
  24. /// <param name="resPath">资源路径</param>
  25. public static T GetPoolItem<T>(string resPath) where T : IPoolItem
  26. {
  27. var item = ObjectPool.GetItem<T>(resPath);
  28. if (item == null)
  29. {
  30. item = (T)(IPoolItem)ResourceManager.LoadAndInstantiate(resPath);
  31. item.Logotype = resPath;
  32. }
  33.  
  34. return item;
  35. }
  36.  
  37. /// <summary>
  38. /// 根据类型直接获取实例对象
  39. /// </summary>
  40. public static T GetPoolItemByClass<T>() where T : IPoolItem, new()
  41. {
  42. var name = typeof(T).FullName;
  43. var item = ObjectPool.GetItem<T>(name);
  44. if (item == null)
  45. {
  46. item = new T();
  47. item.Logotype = name;
  48. }
  49.  
  50. return item;
  51. }
  52.  
  53. public static T GetActivityObject<T>(string id) where T : ActivityObject, IPoolItem
  54. {
  55. var item = ObjectPool.GetItem<T>(id);
  56. if (item == null)
  57. {
  58. item = ActivityObject.Create<T>(id);
  59. item.Logotype = id;
  60. }
  61.  
  62. return item;
  63. }
  64. public static Bullet GetBullet(string id)
  65. {
  66. var bullet = ObjectPool.GetItem<Bullet>(id);
  67. if (bullet == null)
  68. {
  69. bullet = ActivityObject.Create<Bullet>(id);
  70. bullet.Logotype = id;
  71. }
  72.  
  73. return bullet;
  74. }
  75. public static Laser GetLaser(string resPath)
  76. {
  77. var bullet = ObjectPool.GetItem<Laser>(resPath);
  78. if (bullet == null)
  79. {
  80. bullet = ResourceManager.LoadAndInstantiate<Laser>(resPath);
  81. bullet.Logotype = resPath;
  82. }
  83.  
  84. return bullet;
  85. }
  86. }