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.

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