Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- signal player_is_interacting
- const TILE_SIZE := 16
- var initial_position := Vector2.ZERO
- var input_direction := Vector2.ZERO
- var is_moving := false
- var percent_moved_to_next_tile := 0.0
- @export var sprite: AnimatedSprite2D
- @export var sightline: RayCast2D
- @export var interact_area: Area2D
- @export var speed := 2.0
- func _ready():
- initial_position = position
- func _physics_process(delta):
- if not is_moving:
- player_input()
- update_animation()
- elif input_direction != Vector2.ZERO and sightline.is_colliding() == false:
- move(delta)
- else:
- is_moving = false
- move_and_slide()
- func player_input():
- if input_direction.y == 0:
- input_direction.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
- if input_direction.x == 0:
- input_direction.y = int(Input.is_action_pressed("move_down")) - int(Input.is_action_pressed("move_up"))
- if input_direction != Vector2.ZERO:
- initial_position = position
- is_moving = true
- if Input.is_action_just_pressed("interact"):
- player_is_interacting.emit()
- func move(delta):
- percent_moved_to_next_tile += speed * delta
- if percent_moved_to_next_tile >= 1.0:
- position = initial_position + (TILE_SIZE * input_direction)
- percent_moved_to_next_tile = 0.0
- is_moving = false
- else:
- position = initial_position + (TILE_SIZE * input_direction * percent_moved_to_next_tile)
- func update_animation():
- match input_direction:
- Vector2.UP:
- sprite.play("walk_up")
- sightline.rotation_degrees = 180
- interact_area.rotation_degrees = 180
- Vector2.DOWN:
- sprite.play("walk_down")
- sightline.rotation_degrees = 0
- interact_area.rotation_degrees = 0
- Vector2.LEFT:
- sprite.play("walk_left")
- sightline.rotation_degrees = 90
- interact_area.rotation_degrees = 90
- Vector2.RIGHT:
- sprite.play("walk_right")
- sightline.rotation_degrees = -90
- interact_area.rotation_degrees = -90
- sightline.force_raycast_update()
- if input_direction == Vector2.ZERO:
- sprite.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement