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.

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