generated from krampus/template-godot4
79 lines
2.4 KiB
GDScript
79 lines
2.4 KiB
GDScript
class_name Citizen extends CharacterBody2D
|
|
|
|
var direction: Board.Direction:
|
|
set(new_direction):
|
|
direction = new_direction
|
|
if direction == Board.Direction.NONE:
|
|
idle()
|
|
else:
|
|
walk()
|
|
|
|
var speed: float = 50
|
|
var board: Board
|
|
var current_tile_coords: Vector2i
|
|
|
|
@onready var animated_sprite: AnimatedSprite2D = %AnimatedSprite
|
|
@onready var sprite_collision: CollisionShape2D = %SpriteCollision
|
|
@onready var tile_area: Area2D = %TileArea
|
|
|
|
func set_offset(offset: Vector2) -> void:
|
|
animated_sprite.position = offset
|
|
sprite_collision.position = offset
|
|
|
|
func _process(delta: float) -> void:
|
|
var motion: Vector2
|
|
match direction:
|
|
Board.Direction.UP:
|
|
#position = position.move_toward(position + Vector2.UP, speed * delta)
|
|
motion = Vector2.UP * speed * delta
|
|
Board.Direction.DOWN:
|
|
#position = position.move_toward(position + Vector2.DOWN, speed * delta)
|
|
motion = Vector2.DOWN * speed * delta
|
|
Board.Direction.RIGHT:
|
|
#position = position.move_toward(position + Vector2.RIGHT, speed * delta)
|
|
motion = Vector2.RIGHT * speed * delta
|
|
Board.Direction.LEFT:
|
|
#position = position.move_toward(position + Vector2.LEFT, speed * delta)
|
|
motion = Vector2.LEFT * speed * delta
|
|
move_and_collide(motion)
|
|
|
|
func idle() -> void:
|
|
animated_sprite.play("idle")
|
|
|
|
func walk() -> void:
|
|
match direction:
|
|
Board.Direction.UP:
|
|
animated_sprite.play("walk_up")
|
|
Board.Direction.DOWN:
|
|
animated_sprite.play("walk_down")
|
|
Board.Direction.LEFT:
|
|
animated_sprite.flip_h = true
|
|
animated_sprite.play("walk_right")
|
|
Board.Direction.RIGHT:
|
|
animated_sprite.flip_h = false
|
|
animated_sprite.play("walk_right")
|
|
|
|
func check_for_wall() -> void:
|
|
var tile_walls = board.wall_map.get_cell_scene(current_tile_coords).walls
|
|
if tile_walls.has(direction):
|
|
direction = Board.get_next_direction(direction)
|
|
check_for_wall()
|
|
|
|
func check_for_turn(tile: Tile) -> void:
|
|
if tile is Turn:
|
|
direction = tile.direction
|
|
|
|
func check_for_building() -> void:
|
|
if board.buildings.has(current_tile_coords + Board.get_direction_vector(direction)):
|
|
var building = board.buildings[current_tile_coords + Board.get_direction_vector(direction)]
|
|
if !building.can_citizen_enter(current_tile_coords, direction):
|
|
direction = Board.get_next_direction(direction)
|
|
check_for_building()
|
|
|
|
func handle_tile_area_entered(area: Area2D):
|
|
var tile = area.get_parent()
|
|
current_tile_coords = board.tile_map.local_to_map(tile.position)
|
|
check_for_building()
|
|
check_for_turn(tile)
|
|
check_for_wall()
|