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

84 lines
2.2 KiB

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