129 lines
3.7 KiB
GDScript
129 lines
3.7 KiB
GDScript
class_name PlayerManager
|
|
extends Node
|
|
|
|
@export var food_damage: int = 1
|
|
@export var temperature_damage: int = 1
|
|
@export var temperature_endure: int = 50
|
|
|
|
var tilemap_types: TileMapTileTypes = TileMapTileTypes.new()
|
|
#
|
|
var game_manager: GameManager = null
|
|
var board_position: Vector2i = Vector2i(8, 10)
|
|
|
|
@onready var behavior_tree: BehaviorTree = $BehaviorTree
|
|
|
|
var food: int = 0
|
|
# var water: int = 0
|
|
var temperature_timer: int = 0
|
|
var health: int = 0
|
|
#
|
|
var inventory_slot: Vector2i = tilemap_types.EMPTY
|
|
|
|
|
|
func _ready() -> void:
|
|
call_deferred("defer_ready")
|
|
|
|
|
|
func defer_ready() -> void:
|
|
behavior_tree.game_manager = game_manager
|
|
update_board()
|
|
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
if Input.is_action_just_pressed("key_3"):
|
|
game_manager.camera.go_to_zooming(game_manager.world.tilemap_player.cell_to_local(board_position), 2)
|
|
|
|
|
|
# SECTION: board access/mangement
|
|
|
|
func update_board() -> void:
|
|
game_manager.world.tilemap_player.clear_cells()
|
|
game_manager.world.tilemap_player.set_cell(board_position, tilemap_types.PLAYER)
|
|
|
|
|
|
# SECTION: inventory system
|
|
|
|
func pick_up_item(tilemap_pos: Vector2i) -> void:
|
|
var pick_up_cell: TileData = game_manager.world.tilemap_interactive.get_cell(tilemap_pos)
|
|
if not pick_up_cell:
|
|
push_warning("Player trying to pick up item that does not exist at ", tilemap_pos)
|
|
return
|
|
|
|
var pick_up_item_type: Vector2i = game_manager.world.tilemap_interactive.tilemap.get_cell_atlas_coords(tilemap_pos)
|
|
|
|
if inventory_slot != tilemap_types.EMPTY:
|
|
# set the type of the item on the tilemap to the one in the inventory, switching them
|
|
game_manager.world.tilemap_interactive.set_cell(tilemap_pos, inventory_slot)
|
|
|
|
inventory_slot = pick_up_item_type
|
|
|
|
|
|
# SECTION: player movement
|
|
|
|
func walk_towards(position: Vector2i) -> void:
|
|
walk_along(game_manager.world.find_path(board_position, position))
|
|
|
|
func walk_along(path: Array[Vector2i]) -> void:
|
|
if len(path) > 1:
|
|
var next_position: Vector2i = path[1]
|
|
var direction: Vector2i = find_direction(board_position, next_position)
|
|
move_player(direction)
|
|
|
|
|
|
func move_player(direction: Vector2i) -> void:
|
|
var new_position: Vector2 = board_position + direction
|
|
if game_manager.world.is_walkable(new_position):
|
|
board_position = new_position
|
|
else:
|
|
push_warning("Player trying to move to non-walkable position, prevented ", new_position)
|
|
|
|
|
|
func find_direction(pos_a: Vector2i, pos_b: Vector2i) -> Vector2i:
|
|
var direction: Vector2i = Vector2i(0, 0)
|
|
if pos_a.x < pos_b.x:
|
|
direction.x = 1
|
|
elif pos_a.x > pos_b.x:
|
|
direction.x = -1
|
|
|
|
if pos_a.y < pos_b.y:
|
|
direction.y = 1
|
|
elif pos_a.y > pos_b.y:
|
|
direction.y = -1
|
|
|
|
return direction
|
|
|
|
|
|
# SECTION: game tick
|
|
|
|
func tick_handle_temperature(cell_temperature: int):
|
|
if cell_temperature == 0:
|
|
temperature_timer = 0
|
|
elif temperature_timer > temperature_endure:
|
|
temperature_timer += cell_temperature
|
|
health -= temperature_damage
|
|
|
|
|
|
func tick_handle_food():
|
|
if food <= 0:
|
|
health -= food_damage
|
|
|
|
|
|
func game_tick() -> void:
|
|
behavior_tree.game_tick()
|
|
|
|
var player_positon_array: Array[Vector2i] = game_manager.world.tilemap_player.get_cells_by_type(tilemap_types.PLAYER)
|
|
if len(player_positon_array) > 0:
|
|
var player_positon: Vector2i = player_positon_array[0]
|
|
var cell_temperature: int = game_manager.world.tilemap_temperature.get_custom_data(player_positon, "temperature", 0) as int
|
|
tick_handle_temperature(cell_temperature)
|
|
else:
|
|
push_error("No player found on tilemap")
|
|
|
|
tick_handle_food()
|
|
|
|
if health < 0:
|
|
game_manager.player_health_depleted()
|
|
|
|
update_board()
|