63 lines
1.5 KiB
GDScript
63 lines
1.5 KiB
GDScript
@tool
|
|
extends StaticBody2D
|
|
|
|
enum Direction { LEFT, RIGHT, FRONT }
|
|
|
|
@onready var sprite: Sprite2D = $Sprite2D
|
|
const SEAT_HEIGHT_OFFSET := 8
|
|
var player: Player
|
|
var input_released: bool = true
|
|
var move_input: Vector2 = Vector2.ZERO
|
|
|
|
@export var direction: Direction = Direction.RIGHT:
|
|
set(value):
|
|
direction = value
|
|
flip_chair()
|
|
|
|
func _ready() -> void:
|
|
flip_chair()
|
|
|
|
func flip_chair() -> void:
|
|
if !sprite:
|
|
return
|
|
var atlas_tex := sprite.texture as AtlasTexture
|
|
match direction:
|
|
Direction.LEFT:
|
|
atlas_tex.region.position.x = 32
|
|
sprite.flip_h = true
|
|
Direction.RIGHT:
|
|
atlas_tex.region.position.x = 32
|
|
sprite.flip_h = false
|
|
Direction.FRONT:
|
|
atlas_tex.region.position.x = -3
|
|
sprite.flip_h = false
|
|
|
|
func _on_interacted(p_player: Player) -> void:
|
|
if player:
|
|
unmount()
|
|
else:
|
|
player = p_player
|
|
input_released = move_input.length() == 0
|
|
player.animated_sprite.animation = "sit"
|
|
player.animated_sprite.flip_h = direction == Direction.LEFT
|
|
player.position = Vector2(position.x, position.y - SEAT_HEIGHT_OFFSET)
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if player:
|
|
move_input = Input.get_vector("move_left","move_right","move_up","move_down")
|
|
if move_input.length() == 0:
|
|
input_released = true
|
|
elif input_released:
|
|
unmount()
|
|
|
|
|
|
func unmount() -> void:
|
|
if direction == Direction.FRONT:
|
|
player.position = Vector2(position.x -16, position.y)
|
|
else:
|
|
player.position = Vector2(position.x, position.y + 8)
|
|
player.animated_sprite.animation = "down"
|
|
player = null
|
|
input_released = true
|