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.

82 lines
2.1 KiB

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