A team game with an emphasis on movement (with no shooting), inspired by Overwatch and Zineth

92 lines
2.3 KiB

  1. extends Node
  2. var recording
  3. var time
  4. var set_spawn = true
  5. func _ready():
  6. if is_network_master():
  7. get_node("..").connect("spawn", self, "read_recording")
  8. read_recording()
  9. set_spawn = true
  10. time = 0
  11. set_physics_process(true)
  12. func _physics_process(delta):
  13. if is_network_master():
  14. if set_spawn:
  15. set_spawn()
  16. set_spawn = false
  17. play_keys()
  18. # It's actually better to do this 2nd
  19. # Since input is, on average, called 1/2way through a frame
  20. time += delta
  21. func set_spawn():
  22. get_node("..").set_translation(str2var(recording.spawn))
  23. print(recording.switch_charge)
  24. get_node("..").switch_charge = str2var(recording.switch_charge)
  25. func read_recording():
  26. # Gather all existing recordings
  27. var possible = []
  28. var begin = "%d-%d" % [get_node("..").player_info.level, get_node("..").player_info.hero]
  29. var path = "res://recordings/"
  30. var dir = Directory.new()
  31. dir.open(path)
  32. dir.list_dir_begin()
  33. while true:
  34. var fname = dir.get_next()
  35. if fname == "":
  36. # Indicates end of directory
  37. break
  38. if fname.begins_with(begin):
  39. possible.append(fname)
  40. dir.list_dir_end()
  41. # Now pick a random one
  42. var fname = possible[randi() % possible.size()]
  43. print("Reading recording: " + fname)
  44. # Read the file into recording.events for later use
  45. var frec = File.new()
  46. frec.open(path + fname, File.READ)
  47. recording = parse_json(frec.get_as_text())
  48. frec.close()
  49. set_spawn()
  50. func apply_dict(from, to):
  51. if typeof(from) != TYPE_DICTIONARY:
  52. return from
  53. else:
  54. for key in from:
  55. to[key] = apply_dict(from[key], to[key])
  56. return to
  57. func obj_to_event(d):
  58. var e
  59. if d.type == "motion": e = InputEventMouseMotion.new()
  60. if d.type == "key": e = InputEventKey.new()
  61. if d.type == "mb": e = InputEventMouseButton.new()
  62. d.erase("type") # Not in the event
  63. apply_dict(d, e)
  64. return e
  65. func play_keys():
  66. # events[0] is first event
  67. # events[0][0] is first event's TIME
  68. if recording.events.size() == 0:
  69. get_node("..").spawn() # This may cause spawn twice, I hope this isn't a problem
  70. # get_node("..").switch_charge = 0 # This needs to reset so the recording is accurate
  71. read_recording()
  72. while float(recording.events[0][0]) <= time:
  73. # events[0][1] is first event's EVENT
  74. var event_obj = recording.events.pop_front()[1]
  75. var event = obj_to_event(event_obj)
  76. Input.parse_input_event(event)
  77. #._input(event)
  78. #get_node("TPCamera")._input(event)