WorldDrop.gd 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. extends Object
  2. class_name WorldDrop
  3. # Drop management
  4. static func PushDrop(item : Item, agent : BaseAgent):
  5. if agent:
  6. var inst : WorldInstance = WorldAgent.GetInstanceFromAgent(agent)
  7. if inst and inst.map and not inst.map.HasFlags(WorldMap.Flags.NO_DROP):
  8. var dropPos : Vector2 = agent.position
  9. dropPos.x += randf_range(-agent.entityRadius, agent.entityRadius)
  10. dropPos.y += randf_range(-agent.entityRadius, agent.entityRadius)
  11. var drop : Drop = Drop.new(item, dropPos)
  12. inst.drops[drop.get_instance_id()] = drop
  13. var dropID : int = drop.get_instance_id()
  14. drop.timer = Callback.SelfDestructTimer(inst, ActorCommons.DropDelay, WorldDrop.Timeout, [dropID, inst])
  15. Network.NotifyInstance(inst, "DropAdded", [dropID, item.cellID, item.cellCustomfield, drop.position])
  16. static func PopDrop(dropID : int, inst : WorldInstance) -> bool:
  17. if inst and inst.drops.has(dropID):
  18. var drop : Drop = inst.drops[dropID]
  19. inst.drops.erase(dropID)
  20. Network.NotifyInstance(inst, "DropRemoved", [dropID])
  21. if drop:
  22. if drop.timer != null:
  23. drop.timer.stop()
  24. drop.timer.queue_free()
  25. drop.queue_free()
  26. return true
  27. return false
  28. static func Timeout(dropID : int, inst : WorldInstance):
  29. if inst and inst.drops.has(dropID):
  30. var drop : Drop = inst.drops[dropID]
  31. if drop:
  32. drop.timer = null
  33. PopDrop(dropID, inst)
  34. static func PickupDrop(dropID : int, agent : BaseAgent) -> bool:
  35. if agent and ActorCommons.IsAlive(agent) and agent.inventory:
  36. var inst : WorldInstance = WorldAgent.GetInstanceFromAgent(agent)
  37. if inst and dropID in inst.drops:
  38. var drop : Drop = inst.drops[dropID]
  39. if drop and drop.item:
  40. var cell : ItemCell = DB.GetItem(drop.item.cellID, drop.item.cellCustomfield)
  41. if cell and \
  42. agent.position.distance_squared_to(drop.position) < ActorCommons.PickupSquaredDistance \
  43. and PopDrop(dropID, inst) \
  44. and agent.inventory.AddItem(cell, drop.item.count):
  45. return true
  46. return false