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.

343 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. # ... And it's network value
  86. rset_unreliable("switch_charge", switch_charge)
  87. # AI recording
  88. if "record" in player_info:
  89. recording.time += delta
  90. func _integrate_forces(state):
  91. if is_network_master():
  92. control_player(state)
  93. var status = get_status()
  94. rpc_unreliable("set_status", status)
  95. record_status(status)
  96. set_rotation()
  97. func _exit_tree():
  98. if "record" in player_info:
  99. write_recording()
  100. # Functions
  101. # =========
  102. # Build all charge with a multiplier for ~~balance~~
  103. func build_charge(amount):
  104. # If we used build_charge to cost charge, don't mess with it!
  105. if amount > 0:
  106. var losing_advantage = 1.2
  107. var uncapped_advantage = 1.3
  108. var obj = get_node("/root/Level/FullObjective/Objective")
  109. if (obj.left > obj.right) == player_info.is_right_team:
  110. # Is losing (left winning, we're on right or vice versa)
  111. amount *= losing_advantage
  112. if obj.right_active != player_info.is_right_team and obj.active:
  113. # Point against us (right active and left, or vice versa)
  114. amount *= uncapped_advantage
  115. switch_charge += amount
  116. sync func spawn():
  117. emit_signal("spawn")
  118. if "record" in player_info:
  119. write_recording() # Write each spawn as a separate recording
  120. var placement = Vector3()
  121. var x_varies = 5
  122. var z_varies = 5
  123. # No Z, because that's the left-right question
  124. if player_info.is_right_team:
  125. placement = get_node("/root/Level/RightSpawn").get_translation()
  126. else:
  127. placement = get_node("/root/Level/LeftSpawn").get_translation()
  128. # So we don't all spawn on top of each other
  129. placement.x += rand_range(0, x_varies)
  130. placement.z += rand_range(0, z_varies)
  131. recording = { "time": 0, "states": [], "events": [], "spawn": Vector3() }
  132. recording.spawn = var2str(placement)
  133. recording.switch_charge = var2str(switch_charge)
  134. set_transform(Basis())
  135. set_translation(placement)
  136. set_linear_velocity(Vector3())
  137. tp_camera.cam_yaw = 0
  138. tp_camera.cam_pitch = 0
  139. func event_to_obj(event):
  140. var d = {}
  141. if event is InputEventMouseMotion:
  142. d.relative = {}
  143. d.relative.x = event.relative.x
  144. d.relative.y = event.relative.y
  145. d.type = "motion"
  146. if event is InputEventKey:
  147. d.scancode = event.scancode
  148. d.pressed = event.pressed
  149. d.echo = event.echo
  150. d.type = "key"
  151. if event is InputEventMouseButton:
  152. d.button_index = event.button_index
  153. d.pressed = event.pressed
  154. d.type = "mb"
  155. return d
  156. func begin():
  157. _set_color()
  158. func _set_color():
  159. var master_player = util.get_master_player()
  160. # Set color to blue (teammate) or red (enemy)
  161. var color
  162. if master_player.player_info.is_right_team == player_info.is_right_team:
  163. color = friend_color
  164. else:
  165. color = enemy_color
  166. # We have a base MaterialSettings to use inheritance with heroes
  167. # Unfortunately we cannot do this with the actual meshes,
  168. # because godot decides if you change the mesh you wanted to change the material as well
  169. # So "MaterialSettings" is a dummy mesh in player.tscn that's hidden
  170. # We call .duplicate() so we can set this color without messing with other players' colors
  171. var mat = get_node("MaterialSettings").get_surface_material(0).duplicate()
  172. mat.albedo_color = color
  173. for mesh in colored_meshes:
  174. get_node(mesh).set_surface_material(0, mat)
  175. func toggle_mouse_capture():
  176. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  177. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  178. else:
  179. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  180. # Update visual yaw + pitch components to match camera
  181. func set_rotation():
  182. get_node("Yaw").set_rotation(Vector3(0, deg2rad(tp_camera.cam_yaw), 0))
  183. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(-tp_camera.cam_pitch), 0, 0))
  184. func record_status(status):
  185. if "record" in player_info:
  186. for i in range(status.size()):
  187. status[i] = var2str(status[i])
  188. recording.states.append([recording.time, status])
  189. slave func set_status(s):
  190. set_transform(s[0])
  191. set_linear_velocity(s[1])
  192. set_angular_velocity(s[2])
  193. tp_camera.cam_yaw = s[3]
  194. tp_camera.cam_pitch = s[4]
  195. func get_status():
  196. return [
  197. get_transform(),
  198. get_linear_velocity(),
  199. get_angular_velocity(),
  200. tp_camera.cam_yaw,
  201. tp_camera.cam_pitch,
  202. ]
  203. func control_player(state):
  204. var aim = get_node("Yaw").get_global_transform().basis
  205. var direction = Vector3()
  206. if Input.is_action_pressed("move_forwards"):
  207. direction -= aim[2]
  208. if Input.is_action_pressed("move_backwards"):
  209. direction += aim[2]
  210. if Input.is_action_pressed("move_left"):
  211. direction -= aim[0]
  212. if Input.is_action_pressed("move_right"):
  213. direction += aim[0]
  214. direction = direction.normalized()
  215. var ray = get_node("Ray")
  216. # Detect jumpable
  217. var jumpable = false
  218. var jump_dot = 0.5 # If normal.dot(up) > jump_dot, we can jump
  219. for i in range(state.get_contact_count()):
  220. var n = state.get_contact_local_normal(i)
  221. if n.dot(Vector3(0,1,0)) > jump_dot:
  222. jumpable = true
  223. if jumpable: # We can navigate normally, we have a surface
  224. var up = state.get_total_gravity().normalized()
  225. var normal = ray.get_collision_normal()
  226. var floor_velocity = Vector3()
  227. var object = ray.get_collider()
  228. var accel = (1 + switch_charge * walk_speed_build) * walk_speed
  229. state.apply_impulse(Vector3(), direction * accel * get_mass())
  230. var lin_v = state.get_linear_velocity()
  231. lin_v.x *= floor_friction
  232. lin_v.z *= floor_friction
  233. state.set_linear_velocity(lin_v)
  234. if Input.is_action_just_pressed("jump"):
  235. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  236. else:
  237. var accel = (1 + switch_charge * air_speed_build) * air_accel
  238. state.apply_impulse(Vector3(), direction * accel * get_mass())
  239. var lin_v = state.get_linear_velocity()
  240. lin_v.x *= air_friction
  241. lin_v.z *= air_friction
  242. state.set_linear_velocity(lin_v)
  243. state.integrate_forces()
  244. func switch_hero_interface():
  245. if switch_charge >= 100:
  246. # Interface needs the mouse!
  247. toggle_mouse_capture()
  248. # Pause so if we have walls and such nothing funny happens
  249. get_tree().set_pause(true)
  250. var interface = preload("res://scenes/hero_select.tscn").instance()
  251. add_child(interface)
  252. interface.get_node("Confirm").connect("pressed", self, "switch_hero_master")
  253. func switch_hero_master():
  254. rpc("switch_hero", get_node("HeroSelect/Hero").get_selected_id())
  255. # Remove the mouse and enable looking again
  256. toggle_mouse_capture()
  257. get_tree().set_pause(false)
  258. sync func switch_hero(hero):
  259. var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
  260. var net_id = int(get_name())
  261. set_name("%d-delete" % net_id) # Can't have duplicate names
  262. new_hero.set_name("%d" % net_id)
  263. new_hero.set_network_master(net_id)
  264. new_hero.player_info = player_info
  265. get_node("/root/Level/Players").call_deferred("add_child", new_hero)
  266. # We must wait until after _ready is called, so that we don't end up at spawn
  267. new_hero.call_deferred("set_status", get_status())
  268. queue_free()
  269. func write_recording():
  270. if recording and recording.events.size() > 0:
  271. var save = File.new()
  272. var fname = "res://recordings/%d-%d-%d.rec" % [player_info.level, player_info.hero, randi() % 10000]
  273. save.open(fname, File.WRITE)
  274. save.store_line(to_json(recording))
  275. save.close()
  276. # Quits the game:
  277. func quit():
  278. get_tree().quit()
  279. # These aren't used by vanilla player, but are used by heroes in common
  280. func pick():
  281. var look_ray = get_node("TPCamera/Camera/Ray")
  282. return look_ray.get_collider()
  283. func pick_from(group):
  284. return group.find(pick())
  285. func pick_player():
  286. var players = get_node("/root/Level/Players").get_children()
  287. return players[pick_from(players)]
  288. func pick_by_friendly(pick_friendlies):
  289. var pick = pick_player()
  290. if (pick.player_info.is_right_team == player_info.is_right_team) == pick_friendlies:
  291. return pick
  292. else:
  293. return null
  294. # =========