201 lines
7.1 KiB
GDScript3
201 lines
7.1 KiB
GDScript3
extends Actor
|
|
|
|
export var player_number: int = 1
|
|
|
|
# Declare member variables here. Examples:
|
|
# var a = 2
|
|
# var b = "text"
|
|
|
|
onready var player_inventory :InventoryManager = InventoryManager.new()
|
|
onready var stamina_component = $"%Stamina_Component"
|
|
onready var health_component = $"%Health_Component"
|
|
|
|
var punch_item :Item = preload("res://assets/items/punch.tres")
|
|
|
|
const state_stamina_cost :Dictionary = {
|
|
"jump": 10,
|
|
"attack_sword":20,
|
|
"attack_punch":10,
|
|
"roll":40,
|
|
"ledge_climb":10
|
|
}
|
|
|
|
const state_damage_amount :Dictionary = {
|
|
"attack_sword":30,
|
|
"attack_punch":10,
|
|
}
|
|
|
|
var player_info_index :int
|
|
var player_data :PlayerData
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
# PlayerInfo.player_health = health_component.health
|
|
PlayerInfo.player_inventory = player_inventory
|
|
# PlayerInfo.player_stamina = stamina_component.stamina
|
|
|
|
player_info_index = PlayerInfo.register_player()
|
|
print ("Registered as player: ", player_info_index)
|
|
player_data = PlayerInfo.get_player_data(player_info_index)
|
|
PlayerInfo.get_player_data(player_info_index).player_health = health_component.health
|
|
print ("My health is: ", player_data.player_health)
|
|
player_data.player_inventory = player_inventory
|
|
player_data.player_stamina = stamina_component.stamina
|
|
|
|
# Set initial player position in world.
|
|
global_position = LevelInfo.player_start_position
|
|
|
|
player_inventory.add_to_inventory(punch_item)
|
|
player_inventory.select_primary(punch_item)
|
|
|
|
movement_component.state_stamina_cost = state_stamina_cost
|
|
|
|
puppet_pos = position
|
|
|
|
|
|
#var stamina_recovery :float = 0.0
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta):
|
|
#PlayerInfo.player_position = global_position.round()
|
|
player_data.player_position = global_position.round()
|
|
# stamina_recovery += delta
|
|
# if stamina_recovery > 1.0:
|
|
# stamina_component.recover(1)
|
|
# stamina_recovery = 0.0
|
|
# #print("recover ", stamina_component.stamina)
|
|
|
|
#PlayerInfo.player_stamina = stamina_component.stamina
|
|
player_data.player_stamina = stamina_component.stamina
|
|
|
|
##TODO: well I'm not sure I like this but making a whole state receiver is silly
|
|
if state_damage_amount.has(movement_state_machine.current_state.name):
|
|
$Hitbox_Component.damage_amount = state_damage_amount[movement_state_machine.current_state.name]
|
|
else:
|
|
$Hitbox_Component.damage_amount = 10
|
|
|
|
match movement_state_machine.current_state.name:
|
|
"idle":
|
|
if Input.is_action_just_released("switch_primary_item_" + str(player_number)):
|
|
var _selected_item = player_inventory.switch_primary()
|
|
if _selected_item:
|
|
print("Switched Primary item to: ", _selected_item.name)
|
|
elif Input.is_action_just_released("switch_secondary_item_" + str(player_number)):
|
|
var _selected_item = player_inventory.switch_secondary()
|
|
if _selected_item:
|
|
print("Switched Secondary item to: ", _selected_item.name)
|
|
|
|
|
|
|
|
func hit_Receiver(damage):
|
|
$Hurtbox_Component.set_hurtbox(false)
|
|
health_component.take_damage(damage)
|
|
if $Health_Component.health > 0:
|
|
movement_state_machine.change_to_known_state('hurt')
|
|
#PlayerInfo.player_health = health_component.health
|
|
player_data.player_health = health_component.health
|
|
print ("Got Hurt, What happend: " , PlayerInfo.get_player_data(player_info_index).player_health, " vs ", player_data.player_health)
|
|
if health_component.health <= 0:
|
|
return
|
|
yield( get_tree().create_timer(2 + movement_state_machine.get_state_reference('hurt').timeout_seconds), "timeout")
|
|
$Hurtbox_Component.set_hurtbox(true)
|
|
|
|
func touch_the_thing(the_thing: Interactable) -> bool:
|
|
print("You see! a THING...", the_thing.name)
|
|
if the_thing is HealthPickup:
|
|
# Do some healthy stuff.
|
|
the_thing.trigger_interaction()
|
|
return false
|
|
if the_thing is LevelTransition:
|
|
if movement_state_machine.current_state.name != 'enter_right':
|
|
if the_thing._transition_type == 'door_right':
|
|
# Make sure we're facing right.
|
|
transform.x.x = movement_component.RIGHT
|
|
movement_state_machine.change_to_known_state('enter_right')
|
|
elif the_thing._transition_type == 'door_left':
|
|
transform.x.x = movement_component.LEFT
|
|
movement_state_machine.change_to_known_state('enter_right')
|
|
the_thing.trigger_interaction()
|
|
if the_thing is ItemPickup:
|
|
player_inventory.add_to_inventory(the_thing.item, the_thing.count)
|
|
#player_inventory.select_secondary(the_thing.item)
|
|
the_thing.trigger_interaction()
|
|
return true
|
|
|
|
func enough_stamina_for(_state_name :String) -> bool:
|
|
if state_stamina_cost.has(_state_name):
|
|
if stamina_component.stamina >= state_stamina_cost[_state_name]:
|
|
return true
|
|
else:
|
|
return false
|
|
## Return true by default because we don't have a cost for this move
|
|
return true
|
|
|
|
var state_to_item_map :Dictionary = {
|
|
"sword": "attack_sword" ,
|
|
"gun": "attack_shoot",
|
|
"punch" : "attack_punch",
|
|
"mushroom" : "attack_shoot"
|
|
}
|
|
|
|
##TODO: We need to find a way to 'use' the item.
|
|
func use_primary_item() -> String:
|
|
# if player_inventory.primary_selection.consumable == false:
|
|
# if state_to_item_map.has(player_inventory.primary_selection.name):
|
|
# player_inventory.remove_from_inventory(player_inventory.primary_selection)
|
|
# return state_to_item_map[player_inventory.primary_selection.name]
|
|
|
|
if player_inventory.primary_selection != null: # Have to make sure we even have a second
|
|
var _item_name :String = player_inventory.primary_selection.name
|
|
if state_to_item_map.has(_item_name):
|
|
var state_name = state_to_item_map[_item_name]
|
|
## This item state has a cost associated with it.
|
|
if enough_stamina_for(state_name):
|
|
#print("Yup: ", state_stamina_cost[state_name], "<=", stamina_component.stamina)
|
|
player_inventory.remove_from_inventory(player_inventory.primary_selection)
|
|
return state_to_item_map[_item_name]
|
|
else:
|
|
## TODO: maybe some sort of 'fail' animation state
|
|
#print("Nope: ", state_stamina_cost[state_name], "<=", stamina_component.stamina)
|
|
return '' ## you can't use this
|
|
player_inventory.remove_from_inventory(player_inventory.primary_selection)
|
|
return state_to_item_map[_item_name]
|
|
|
|
return ''
|
|
|
|
func use_secondary_item() -> String:
|
|
if player_inventory.secondary_selection != null: # Have to make sure we even have a second
|
|
var _item_name :String = player_inventory.secondary_selection.name
|
|
if state_to_item_map.has(_item_name):
|
|
var state_name = state_to_item_map[_item_name]
|
|
if enough_stamina_for(state_name):
|
|
player_inventory.remove_from_inventory(player_inventory.secondary_selection)
|
|
return state_to_item_map[_item_name]
|
|
else:
|
|
## TODO: maybe some sort of 'fail' animation state
|
|
return ''
|
|
return ''
|
|
|
|
#func check_state_change(_new_state_name: String) -> bool:
|
|
# match _new_state_name:
|
|
# "attack_sword":
|
|
# print("nope.")
|
|
# return false
|
|
# "attack_shoot":
|
|
# return true
|
|
# _: # None
|
|
# return true
|
|
# return true
|
|
|
|
|
|
func _on_Movement_StateMachine_state_changed(old_state_name, new_state):
|
|
# if new_state.name == "jump":
|
|
# stamina_component.reduce_stamina(state_stamina_cost["jump"])
|
|
if state_stamina_cost.has(new_state.name):
|
|
stamina_component.reduce_stamina(state_stamina_cost[new_state.name])
|
|
|
|
|
|
func _on_Health_Component_health_depleted():
|
|
movement_state_machine.change_to_known_state('death')
|