Advertisement
thatenbykiki

player.gd (PetFighter)

Apr 25th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody2D
  2.  
  3. signal player_is_interacting
  4.  
  5. const TILE_SIZE := 16
  6.  
  7. var initial_position := Vector2.ZERO
  8. var input_direction := Vector2.ZERO
  9. var is_moving := false
  10. var percent_moved_to_next_tile := 0.0
  11.  
  12. @export var sprite: AnimatedSprite2D
  13. @export var sightline: RayCast2D
  14. @export var interact_area: Area2D
  15. @export var speed := 2.0
  16.  
  17.  
  18. func _ready():
  19.     initial_position = position
  20.  
  21.  
  22. func _physics_process(delta):
  23.     if not is_moving:
  24.         player_input()
  25.         update_animation()
  26.     elif input_direction != Vector2.ZERO and sightline.is_colliding() == false:
  27.         move(delta)
  28.     else:
  29.         is_moving = false
  30.    
  31.     move_and_slide()
  32.  
  33.  
  34. func player_input():
  35.     if input_direction.y == 0:
  36.         input_direction.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
  37.     if input_direction.x == 0:
  38.         input_direction.y = int(Input.is_action_pressed("move_down")) - int(Input.is_action_pressed("move_up"))
  39.    
  40.     if input_direction != Vector2.ZERO:
  41.         initial_position = position
  42.         is_moving = true
  43.    
  44.     if Input.is_action_just_pressed("interact"):
  45.         player_is_interacting.emit()
  46.  
  47.  
  48. func move(delta):
  49.     percent_moved_to_next_tile += speed * delta
  50.     if percent_moved_to_next_tile >= 1.0:
  51.         position = initial_position + (TILE_SIZE * input_direction)
  52.         percent_moved_to_next_tile = 0.0
  53.         is_moving = false
  54.     else:
  55.         position = initial_position + (TILE_SIZE * input_direction * percent_moved_to_next_tile)
  56.  
  57.  
  58. func update_animation():
  59.     match input_direction:
  60.         Vector2.UP:
  61.             sprite.play("walk_up")
  62.             sightline.rotation_degrees = 180
  63.             interact_area.rotation_degrees = 180
  64.         Vector2.DOWN:
  65.             sprite.play("walk_down")
  66.             sightline.rotation_degrees = 0
  67.             interact_area.rotation_degrees = 0
  68.         Vector2.LEFT:
  69.             sprite.play("walk_left")
  70.             sightline.rotation_degrees = 90
  71.             interact_area.rotation_degrees = 90
  72.         Vector2.RIGHT:
  73.             sprite.play("walk_right")
  74.             sightline.rotation_degrees = -90
  75.             interact_area.rotation_degrees = -90
  76.    
  77.     sightline.force_raycast_update()
  78.    
  79.     if input_direction == Vector2.ZERO:
  80.         sprite.stop()
  81.    
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement