player.gd 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. extends KinematicBody2D
  2. const MOTION_SPEED = 90.0
  3. slave var slave_pos = Vector2()
  4. slave var slave_motion = Vector2()
  5. export var stunned = false
  6. # Use sync because it will be called everywhere
  7. sync func setup_bomb(bomb_name, pos, by_who):
  8. var bomb = preload("res://bomb.tscn").instance()
  9. bomb.set_name(bomb_name) # Ensure unique name for the bomb
  10. bomb.position = pos
  11. bomb.from_player = by_who
  12. # No need to set network mode to bomb, will be owned by master by default
  13. get_node("../..").add_child(bomb)
  14. var current_anim = ""
  15. var prev_bombing = false
  16. var bomb_index = 0
  17. func _physics_process(delta):
  18. var motion = Vector2()
  19. if is_network_master():
  20. if Input.is_action_pressed("move_left"):
  21. motion += Vector2(-1, 0)
  22. if Input.is_action_pressed("move_right"):
  23. motion += Vector2(1, 0)
  24. if Input.is_action_pressed("move_up"):
  25. motion += Vector2(0, -1)
  26. if Input.is_action_pressed("move_down"):
  27. motion += Vector2(0, 1)
  28. var bombing = Input.is_action_pressed("set_bomb")
  29. if stunned:
  30. bombing = false
  31. motion = Vector2()
  32. if bombing and not prev_bombing:
  33. var bomb_name = get_name() + str(bomb_index)
  34. var bomb_pos = position
  35. rpc("setup_bomb", bomb_name, bomb_pos, get_tree().get_network_unique_id())
  36. prev_bombing = bombing
  37. rset("slave_motion", motion)
  38. rset("slave_pos", position)
  39. else:
  40. position = slave_pos
  41. motion = slave_motion
  42. var new_anim = "standing"
  43. if motion.y < 0:
  44. new_anim = "walk_up"
  45. elif motion.y > 0:
  46. new_anim = "walk_down"
  47. elif motion.x < 0:
  48. new_anim = "walk_left"
  49. elif motion.x > 0:
  50. new_anim = "walk_right"
  51. if stunned:
  52. new_anim = "stunned"
  53. if new_anim != current_anim:
  54. current_anim = new_anim
  55. get_node("anim").play(current_anim)
  56. # FIXME: Use move_and_slide
  57. move_and_slide(motion * MOTION_SPEED)
  58. if not is_network_master():
  59. slave_pos = position # To avoid jitter
  60. slave func stun():
  61. stunned = true
  62. master func exploded(by_who):
  63. if stunned:
  64. return
  65. rpc("stun") # Stun slaves
  66. stun() # Stun master - could use sync to do both at once
  67. func set_player_name(new_name):
  68. get_node("label").set_text(new_name)
  69. func _ready():
  70. stunned = false
  71. slave_pos = position