A team game with an emphasis on movement (with no shooting), inspired by Overwatch and Zineth
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.7 KiB

  1. extends Node # Networking functionality
  2. var player
  3. var is_placing = false
  4. var placing_node
  5. var placed = []
  6. var max_placed = 5
  7. var start_action
  8. var confirm_action
  9. var delete_action
  10. var scene
  11. signal start_placement
  12. signal confirm_placement
  13. func _init(parent, scene_path):
  14. player = parent
  15. player.add_child(self)
  16. # Set the network master to the player's network master, which happens to be its name
  17. # This allows it to use master, slave keywords appropriately
  18. var net_id = int(player.get_name())
  19. set_name("Placement") # We need to share common name for networking
  20. set_network_master(net_id)
  21. scene = load(scene_path)
  22. rpc("request_placed", get_tree().get_network_unique_id())
  23. master func request_placed(from):
  24. for node in placed:
  25. rpc_id(from, "slave_place", node.transform)
  26. func place_input(radius=-1):
  27. # We allow you to just click to place, without needing to press E
  28. var confirm = Input.is_action_just_pressed(confirm_action)
  29. if Input.is_action_just_pressed(start_action) or (confirm and not is_placing):
  30. # Press button twice to cancel
  31. if is_placing:
  32. # We changed our mind, delete the placing wall
  33. placing_node.queue_free()
  34. is_placing = false
  35. else:
  36. # Make a floating placement wall
  37. placing_node = create()
  38. is_placing = true
  39. if Input.is_action_just_pressed(delete_action):
  40. var pick = player.pick_from(placed)
  41. if pick != -1:
  42. rpc("remove_placed", pick)
  43. if is_placing or confirm:
  44. position_placement(placing_node)
  45. if radius > 0: # A radius is specified
  46. var distance = placing_node.get_translation() - player.get_translation()
  47. if distance.length() > radius:
  48. placing_node.out_of_range()
  49. confirm = false
  50. else:
  51. placing_node.within_range()
  52. if confirm:
  53. # Order matters here: confirm_placement resets placing_node so we have to do anything with it first
  54. rpc("slave_place", placing_node.transform)
  55. confirm_placement(placing_node)
  56. func confirm_placement(node, tf=null):
  57. if tf:
  58. node.set_transform(tf)
  59. node.place()
  60. placed.append(node)
  61. check_count()
  62. placing_node = null
  63. is_placing = false
  64. func check_count():
  65. # If we've made max_walls, remove the first we made
  66. if placed.size() > max_placed:
  67. placed[0].queue_free()
  68. placed.pop_front()
  69. # When placing, make the about-to-disappear wall translucent
  70. if placed.size() >= max_placed:
  71. placed[0].make_last()
  72. # Find the point we're looking at, and put the wall there
  73. func position_placement(node):
  74. var aim = player.get_node("Yaw/Pitch").get_global_transform().basis
  75. var look_ray = player.get_node("TPCamera/Camera/Ray")
  76. var pos = look_ray.get_collision_point()
  77. node.set_translation(pos)
  78. var normal = look_ray.get_collision_normal()
  79. var towards = normal + pos
  80. var up = aim[2] # Wall should be horizontal to my view
  81. # This helps nearly horizontal walls be easier to make flat
  82. # We have two methods I'm deciding between
  83. var use_method_two = true
  84. if not use_method_two:
  85. # Method one: only allow horizontal or vertical, based on whether the surface faces you
  86. var on_wall_margin = 0.75
  87. if normal.dot(Vector3(0,1,0)) < on_wall_margin:
  88. var margin = 0.8
  89. if up.dot(normal) > margin: # The wall is facing us
  90. # We want flat
  91. up = Vector3(0,1,0)
  92. else:
  93. # We want straight
  94. up.y = 0
  95. else:
  96. # Method two: Make y more aggressive than other dimensions
  97. up.y = 3 * tanh(up.y)
  98. up = up.normalized()
  99. node.look_at(towards, up)
  100. func clear():
  101. for node in placed:
  102. node.queue_free()
  103. placed = []
  104. slave func slave_place(tf):
  105. var node = create()
  106. confirm_placement(node, tf)
  107. sync func remove_placed(index):
  108. placed[index].queue_free()
  109. placed.remove(index)
  110. func create():
  111. var node = scene.instance()
  112. player.get_node("/root/Level").add_child(node)
  113. # We have to call_deferred because we're `load`ing instead of `preload`ing. TODO: preload?
  114. node.init(player)
  115. return node