WorldAgent.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. extends Node
  2. class_name WorldAgent
  3. static var agents : Dictionary[int, BaseAgent] = {}
  4. static var defaultSpawnLocation : SpawnObject = SpawnObject.new()
  5. # From Agent getters
  6. static func GetInstanceFromAgent(agent : BaseAgent) -> SubViewport:
  7. return agent.get_parent()
  8. static func GetMapFromAgent(agent : BaseAgent) -> WorldMap:
  9. var map : WorldMap = null
  10. var inst : WorldInstance = GetInstanceFromAgent(agent)
  11. if inst:
  12. assert(inst.map != null, "Agent's base map is incorrect, instance is not referenced inside a map")
  13. map = inst.map
  14. return map
  15. static func GetNeighboursFromAgent(checkedAgent : BaseAgent) -> Array[Array]:
  16. var neighbours : Array[Array] = []
  17. var instance : WorldInstance = GetInstanceFromAgent(checkedAgent)
  18. if instance:
  19. neighbours.append(instance.npcs)
  20. neighbours.append(instance.mobs)
  21. neighbours.append(instance.players)
  22. return neighbours
  23. # Basic Agent container handling
  24. static func GetAgent(agentID : int) -> BaseAgent:
  25. var agent : BaseAgent = null
  26. if agents.has(agentID):
  27. agent = agents.get(agentID)
  28. return agent
  29. static func AddAgent(agent : BaseAgent):
  30. assert(agent != null, "Agent is null, can't add it")
  31. if agent and not agents.has(agent.get_rid().get_id()):
  32. agents[agent.get_rid().get_id()] = agent
  33. static func RemoveAgent(agent : BaseAgent):
  34. assert(agent != null, "Agent is null, can't remove it")
  35. if agent:
  36. if agent is AIAgent:
  37. var inst : WorldInstance = agent.get_parent()
  38. if inst and inst.timers and agent.spawnInfo and agent.spawnInfo.is_persistant:
  39. Callback.SelfDestructTimer(inst.timers, agent.spawnInfo.respawn_delay, WorldAgent.CreateAgent, [agent.spawnInfo, inst.id])
  40. if agent.leader != null:
  41. agent.leader.RemoveFollower(agent)
  42. PopAgent(agent)
  43. agents.erase(agent.get_rid().get_id())
  44. agent.queue_free()
  45. static func HasAgent(inst : WorldInstance, agent : BaseAgent):
  46. var hasAgent : bool = false
  47. assert(agent != null and inst != null, "Agent or instance are invalid, could not check if the agent is inside the instance")
  48. if agent and inst:
  49. if agent is PlayerAgent:
  50. hasAgent = inst.players.has(agent)
  51. elif agent is MonsterAgent:
  52. hasAgent = inst.mobs.has(agent)
  53. elif agent is NpcAgent:
  54. hasAgent = inst.npcs.has(agent)
  55. return hasAgent
  56. static func PopAgent(agent : BaseAgent):
  57. assert(agent != null, "Agent is null, can't pop it")
  58. if agent:
  59. var inst : WorldInstance = GetInstanceFromAgent(agent)
  60. if inst:
  61. Network.NotifyNeighbours(agent, "RemoveEntity", [], false)
  62. if agent is PlayerAgent:
  63. inst.players.erase(agent)
  64. if inst.players.size() == 0:
  65. inst.QueryProcessMode()
  66. elif agent is MonsterAgent:
  67. inst.mobs.erase(agent)
  68. elif agent is NpcAgent:
  69. inst.npcs.erase(agent)
  70. inst.remove_child(agent)
  71. static func PushAgent(agent : BaseAgent, inst : WorldInstance):
  72. assert(agent != null, "Agent is null, can't push it")
  73. assert(inst != null, "Instance is null, can't push the agent in it")
  74. if agent and inst:
  75. if not HasAgent(inst, agent):
  76. if agent is PlayerAgent:
  77. inst.players.push_back(agent)
  78. inst.RefreshProcessMode()
  79. elif agent is MonsterAgent:
  80. inst.mobs.push_back(agent)
  81. elif agent is NpcAgent:
  82. inst.npcs.push_back(agent)
  83. inst.add_child.call_deferred(agent)
  84. else:
  85. RemoveAgent(agent)
  86. static func CreateAgent(spawn : SpawnObject, instanceID : int = 0, nickname : String = "") -> BaseAgent:
  87. var agent : BaseAgent = null
  88. var data : EntityData = DB.EntitiesDB.get(spawn.id, null)
  89. if not data:
  90. return null
  91. var position : Vector2 = WorldNavigation.GetSpawnPosition(spawn.map, spawn, !(data._behaviour & AICommons.Behaviour.IMMOBILE))
  92. if Vector2i(position) == Vector2i.ZERO:
  93. return null
  94. agent = Instantiate.CreateAgent(spawn, data, spawn.nick if nickname.length() == 0 else nickname)
  95. if not agent:
  96. return null
  97. AddAgent(agent)
  98. Launcher.World.Warp(agent, spawn.map, position, instanceID)
  99. return agent
  100. static func _post_launch():
  101. defaultSpawnLocation.map = Launcher.World.GetMap(LauncherCommons.DefaultStartMapID)
  102. defaultSpawnLocation.spawn_position = LauncherCommons.DefaultStartPos
  103. defaultSpawnLocation.type = "Player"
  104. defaultSpawnLocation.id = DB.PlayerHash