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.

347 lines
11 KiB

  1. # Original: https://raw.githubusercontent.com/Calinou/fps-test/master/scripts/player.gd
  2. # All heroes extend from here. Implements all common behavior
  3. extends RigidBody
  4. signal spawn
  5. # Set by lobby. Contains player metadata (hero, nickname, etc)
  6. var player_info
  7. # Basic movement settings
  8. # These are all public out here because they are customized by heroes
  9. # Walking speed and jumping height are defined later.
  10. var walk_speed = 0.8 # Actually acceleration; m/s/s
  11. var jump_speed = 5 # m/s
  12. var air_accel = .1 # m/s/s
  13. var floor_friction = 1-0.08
  14. var air_friction = 1-0.03
  15. var walk_speed_build = 0.006 # `walk_speed` per `switch_charge`
  16. var air_speed_build = 0.006 # `air_accel` per `switch_charge`
  17. sync var switch_charge = 0
  18. var switch_charge_cap = 200 # While switching is always at 100, things like speed boost might go higher!
  19. var movement_charge = 0.15 # In percent per meter (except when heroes change that)
  20. # Nodes
  21. onready var switch_text = get_node("MasterOnly/ChargeBar/ChargeText")
  22. onready var switch_bar = get_node("MasterOnly/ChargeBar")
  23. onready var switch_bar_extra = get_node("MasterOnly/ChargeBar/Extra")
  24. onready var switch_hero_action = get_node("MasterOnly/SwitchHero")
  25. onready var tp_camera = get_node("TPCamera")
  26. onready var master_only = get_node("MasterOnly")
  27. onready var debug_node = get_node("/root/Level/Debug")
  28. var recording
  29. var ai_instanced = false
  30. var friend_color = Color("#4ab0e5") # Blue
  31. var enemy_color = Color("#f04273") # Red
  32. # These meshes get colored with friendliness
  33. var colored_meshes = [
  34. "Yaw/MainMesh",
  35. "Yaw/Pitch/RotatedHead",
  36. ]
  37. func _ready():
  38. set_process_input(true)
  39. if is_network_master():
  40. get_node("TPCamera/Camera/Ray").add_exception(self)
  41. tp_camera.set_enabled(true)
  42. tp_camera.cam_view_sensitivity = 0.05
  43. if "is_ai" in player_info and player_info.is_ai and not ai_instanced:
  44. add_child(preload("res://scenes/ai.tscn").instance())
  45. ai_instanced = true
  46. spawn()
  47. else:
  48. get_node("PlayerName").set_text(player_info.username)
  49. # Remove HUD
  50. remove_child(master_only)
  51. func _input(event):
  52. if is_network_master():
  53. if Input.is_action_just_pressed("switch_hero"):
  54. switch_hero_interface()
  55. # Quit the game:
  56. if Input.is_action_pressed("quit"):
  57. quit()
  58. if "record" in player_info:
  59. recording.events.append([recording.time, event_to_obj(event)])
  60. if Input.is_action_just_pressed("enable_cheats"):
  61. switch_charge = 199
  62. func _process(delta):
  63. # All player code not caused by input, and not causing movement
  64. if is_network_master():
  65. # Check falling (cancel charge and respawn)
  66. var fall_height = -400 # This is essentially the respawn timer
  67. var switch_height = -150 # At this point, stop adding to switch_charge. This makes falls not charge you too much
  68. var vel = get_linear_velocity()
  69. if translation.y < switch_height:
  70. vel.y = 0 # Don't gain charge from falling when below switch_height
  71. build_charge(movement_charge * vel.length() * delta)
  72. if get_translation().y < fall_height:
  73. rpc("spawn")
  74. # Update switch charge GUI
  75. switch_text.set_text("%d%%" % int(switch_charge)) # We truncate, rather than round, so that switch is displayed AT 100%
  76. if switch_charge >= 100:
  77. switch_hero_action.show()
  78. else:
  79. switch_hero_action.hide()
  80. if switch_charge > switch_charge_cap:
  81. # There is however a cap
  82. switch_charge = switch_charge_cap
  83. switch_bar.value = switch_charge
  84. switch_bar_extra.value = switch_charge - 100
  85. # AI recording
  86. if "record" in player_info:
  87. recording.time += delta
  88. func _integrate_forces(state):
  89. if is_network_master():
  90. control_player(state)
  91. var status = get_status()
  92. rpc_unreliable("set_status", status)
  93. record_status(status)
  94. set_rotation()
  95. func _exit_tree():
  96. if "record" in player_info:
  97. write_recording()
  98. # Functions
  99. # =========
  100. # Build all charge with a multiplier for ~~balance~~
  101. func build_charge(amount):
  102. # If we used build_charge to cost charge, don't mess with it!
  103. if amount > 0:
  104. var losing_advantage = 1.2
  105. var uncapped_advantage = 1.3
  106. var obj = get_node("/root/Level/FullObjective/Objective")
  107. if (obj.left > obj.right) == player_info.is_right_team:
  108. # Is losing (left winning, we're on right or vice versa)
  109. amount *= losing_advantage
  110. if obj.right_active != player_info.is_right_team and obj.active:
  111. # Point against us (right active and left, or vice versa)
  112. amount *= uncapped_advantage
  113. else:
  114. # Only build down to 0
  115. amount = max(amount, -switch_charge)
  116. switch_charge += amount
  117. if is_network_master():
  118. rset_unreliable("switch_charge", switch_charge)
  119. return amount
  120. sync func spawn():
  121. emit_signal("spawn")
  122. if "record" in player_info:
  123. write_recording() # Write each spawn as a separate recording
  124. var placement = Vector3()
  125. var x_varies = 5
  126. var z_varies = 5
  127. # No Z, because that's the left-right question
  128. if player_info.is_right_team:
  129. placement = get_node("/root/Level/RightSpawn").get_translation()
  130. else:
  131. placement = get_node("/root/Level/LeftSpawn").get_translation()
  132. # So we don't all spawn on top of each other
  133. placement.x += rand_range(0, x_varies)
  134. placement.z += rand_range(0, z_varies)
  135. recording = { "time": 0, "states": [], "events": [], "spawn": Vector3() }
  136. recording.spawn = var2str(placement)
  137. recording.switch_charge = var2str(switch_charge)
  138. set_transform(Basis())
  139. set_translation(placement)
  140. set_linear_velocity(Vector3())
  141. tp_camera.cam_yaw = 0
  142. tp_camera.cam_pitch = 0
  143. func event_to_obj(event):
  144. var d = {}
  145. if event is InputEventMouseMotion:
  146. d.relative = {}
  147. d.relative.x = event.relative.x
  148. d.relative.y = event.relative.y
  149. d.type = "motion"
  150. if event is InputEventKey:
  151. d.scancode = event.scancode
  152. d.pressed = event.pressed
  153. d.echo = event.echo
  154. d.type = "key"
  155. if event is InputEventMouseButton:
  156. d.button_index = event.button_index
  157. d.pressed = event.pressed
  158. d.type = "mb"
  159. return d
  160. func begin():
  161. _set_color()
  162. func _set_color():
  163. var master_player = util.get_master_player()
  164. # Set color to blue (teammate) or red (enemy)
  165. var color
  166. if master_player.player_info.is_right_team == player_info.is_right_team:
  167. color = friend_color
  168. else:
  169. color = enemy_color
  170. # We have a base MaterialSettings to use inheritance with heroes
  171. # Unfortunately we cannot do this with the actual meshes,
  172. # because godot decides if you change the mesh you wanted to change the material as well
  173. # So "MaterialSettings" is a dummy mesh in player.tscn that's hidden
  174. # We call .duplicate() so we can set this color without messing with other players' colors
  175. var mat = get_node("MaterialSettings").get_surface_material(0).duplicate()
  176. mat.albedo_color = color
  177. for mesh in colored_meshes:
  178. get_node(mesh).set_surface_material(0, mat)
  179. func toggle_mouse_capture():
  180. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  181. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  182. else:
  183. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  184. # Update visual yaw + pitch components to match camera
  185. func set_rotation():
  186. get_node("Yaw").set_rotation(Vector3(0, deg2rad(tp_camera.cam_yaw), 0))
  187. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(-tp_camera.cam_pitch), 0, 0))
  188. func record_status(status):
  189. if "record" in player_info:
  190. for i in range(status.size()):
  191. status[i] = var2str(status[i])
  192. recording.states.append([recording.time, status])
  193. slave func set_status(s):
  194. set_transform(s[0])
  195. set_linear_velocity(s[1])
  196. set_angular_velocity(s[2])
  197. tp_camera.cam_yaw = s[3]
  198. tp_camera.cam_pitch = s[4]
  199. func get_status():
  200. return [
  201. get_transform(),
  202. get_linear_velocity(),
  203. get_angular_velocity(),
  204. tp_camera.cam_yaw,
  205. tp_camera.cam_pitch,
  206. ]
  207. func control_player(state):
  208. var aim = get_node("Yaw").get_global_transform().basis
  209. var direction = Vector3()
  210. if Input.is_action_pressed("move_forwards"):
  211. direction -= aim[2]
  212. if Input.is_action_pressed("move_backwards"):
  213. direction += aim[2]
  214. if Input.is_action_pressed("move_left"):
  215. direction -= aim[0]
  216. if Input.is_action_pressed("move_right"):
  217. direction += aim[0]
  218. direction = direction.normalized()
  219. var ray = get_node("Ray")
  220. # Detect jumpable
  221. var jumpable = false
  222. var jump_dot = 0.5 # If normal.dot(up) > jump_dot, we can jump
  223. for i in range(state.get_contact_count()):
  224. var n = state.get_contact_local_normal(i)
  225. if n.dot(Vector3(0,1,0)) > jump_dot:
  226. jumpable = true
  227. if jumpable: # We can navigate normally, we have a surface
  228. var up = state.get_total_gravity().normalized()
  229. var normal = ray.get_collision_normal()
  230. var floor_velocity = Vector3()
  231. var object = ray.get_collider()
  232. var accel = (1 + switch_charge * walk_speed_build) * walk_speed
  233. state.apply_impulse(Vector3(), direction * accel * get_mass())
  234. var lin_v = state.get_linear_velocity()
  235. lin_v.x *= floor_friction
  236. lin_v.z *= floor_friction
  237. state.set_linear_velocity(lin_v)
  238. if Input.is_action_just_pressed("jump"):
  239. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  240. else:
  241. var accel = (1 + switch_charge * air_speed_build) * air_accel
  242. state.apply_impulse(Vector3(), direction * accel * get_mass())
  243. var lin_v = state.get_linear_velocity()
  244. lin_v.x *= air_friction
  245. lin_v.z *= air_friction
  246. state.set_linear_velocity(lin_v)
  247. state.integrate_forces()
  248. func switch_hero_interface():
  249. if switch_charge >= 100:
  250. # Interface needs the mouse!
  251. toggle_mouse_capture()
  252. # Pause so if we have walls and such nothing funny happens
  253. get_tree().set_pause(true)
  254. var interface = preload("res://scenes/hero_select.tscn").instance()
  255. add_child(interface)
  256. interface.get_node("Confirm").connect("pressed", self, "switch_hero_master")
  257. func switch_hero_master():
  258. rpc("switch_hero", get_node("HeroSelect/Hero").get_selected_id())
  259. # Remove the mouse and enable looking again
  260. toggle_mouse_capture()
  261. get_tree().set_pause(false)
  262. sync func switch_hero(hero):
  263. var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
  264. var net_id = int(get_name())
  265. set_name("%d-delete" % net_id) # Can't have duplicate names
  266. new_hero.set_name("%d" % net_id)
  267. new_hero.set_network_master(net_id)
  268. new_hero.player_info = player_info
  269. get_node("/root/Level/Players").call_deferred("add_child", new_hero)
  270. # We must wait until after _ready is called, so that we don't end up at spawn
  271. new_hero.call_deferred("set_status", get_status())
  272. queue_free()
  273. func write_recording():
  274. if recording and recording.events.size() > 0:
  275. var save = File.new()
  276. var fname = "res://recordings/%d-%d-%d.rec" % [player_info.level, player_info.hero, randi() % 10000]
  277. save.open(fname, File.WRITE)
  278. save.store_line(to_json(recording))
  279. save.close()
  280. # Quits the game:
  281. func quit():
  282. get_tree().quit()
  283. # These aren't used by vanilla player, but are used by heroes in common
  284. func pick():
  285. var look_ray = get_node("TPCamera/Camera/Ray")
  286. return look_ray.get_collider()
  287. func pick_from(group):
  288. return group.find(pick())
  289. func pick_player():
  290. var players = get_node("/root/Level/Players").get_children()
  291. return players[pick_from(players)]
  292. func pick_by_friendly(pick_friendlies):
  293. var pick = pick_player()
  294. if (pick.player_info.is_right_team == player_info.is_right_team) == pick_friendlies:
  295. return pick
  296. else:
  297. return null
  298. # =========