player.gd 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. extends KinematicBody2D
  2. const MOTION_SPEED = 90.0
  3. puppet var puppet_pos = Vector2()
  4. puppet var puppet_motion = Vector2()
  5. export var stunned = false
  6. # Use sync because it will be called everywhere
  7. remotesync 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 master to bomb, will be owned by server 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 = String(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("puppet_motion", motion)
  38. rset("puppet_pos", position)
  39. else:
  40. position = puppet_pos
  41. motion = puppet_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. puppet_pos = position # To avoid jitter
  60. puppet func stun():
  61. stunned = true
  62. master func exploded(_by_who):
  63. if stunned:
  64. return
  65. rpc("stun") # Stun puppets
  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. puppet_pos = position