Player.gd 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. extends KinematicBody2D
  2. signal player_velchange(velocity)
  3. # Declare member variables here. Examples:
  4. # var a = 2
  5. # var b = "text"
  6. var _timer = null
  7. const GRAVITY = 200.0
  8. const WALK_SPEED = 200
  9. var velocity = Vector2()
  10. var old_pos = Vector2()
  11. var since_last_post = 0
  12. var post_interval = 250
  13. var id
  14. # Called when the node enters the scene tree for the first time.
  15. #func _ready():
  16. func _physics_process(delta):
  17. var velocity_old = velocity
  18. if Input.is_action_pressed("ui_left"):
  19. velocity.x = -WALK_SPEED
  20. elif Input.is_action_pressed("ui_right"):
  21. velocity.x = WALK_SPEED
  22. else:
  23. velocity.x = 0
  24. if Input.is_action_pressed("ui_up"):
  25. velocity.y = -WALK_SPEED
  26. elif Input.is_action_pressed("ui_down"):
  27. velocity.y = WALK_SPEED
  28. else:
  29. velocity.y = 0
  30. if velocity_old != velocity:
  31. emit_signal("player_velchange", velocity)
  32. # We don't need to multiply velocity by delta because "move_and_slide" already takes delta time into account.
  33. # The second parameter of "move_and_slide" is the normal pointing up.
  34. # In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal.
  35. velocity = move_and_slide(velocity, Vector2(0, -1))
  36. for i in get_slide_count():
  37. var collision = get_slide_collision(i)
  38. if collision:
  39. print(collision)
  40. remote func setTarget(targetid, pos):
  41. #print("setting target for:", targetid)
  42. if targetid != id:
  43. get_node("../"+"Player "+str(targetid)).setTarget(targetid, pos)
  44. # Called every frame. 'delta' is the elapsed time since the previous frame.
  45. func _process(delta):
  46. since_last_post += delta
  47. if since_last_post >= post_interval/1000 and get_tree().has_network_peer():
  48. if old_pos != self.position or since_last_post >= post_interval/100:
  49. rpc_id(0, "setTarget", get_tree().get_network_unique_id(), self.position)
  50. old_pos = self.position
  51. since_last_post = 0