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

78 lines
1.9 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. 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. print(get_node("..").get_translation())
  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. while float(recording.events[0][0]) <= time:
  63. # events[0][1] is first event's EVENT
  64. var event_obj = recording.events.pop_front()[1]
  65. var event = obj_to_event(event_obj)
  66. Input.parse_input_event(event)
  67. #._input(event)
  68. #get_node("TPCamera")._input(event)