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.

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