Skip to content

Commit

Permalink
Merge pull request #165 from aronand/PlayerAttributesAsDictionary
Browse files Browse the repository at this point in the history
refactor: Store player attributes in Dictionary instead of Array
  • Loading branch information
Phazorknight authored Apr 16, 2024
2 parents 195b5cc + 2a41e7c commit fe807eb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
6 changes: 3 additions & 3 deletions COGITO/SceneManagement/CogitoPlayerState.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var player_state_dir : String = CogitoSceneManager.cogito_state_dir + CogitoScen
@export var player_sanity : Vector2

#New way of saving player attributes
@export var player_attributes : Array[Vector2]
@export var player_attributes : Dictionary

#Saving parameters from the player interaction component
@export var interaction_component_state : Array
Expand All @@ -34,8 +34,8 @@ var player_state_dir : String = CogitoSceneManager.cogito_state_dir + CogitoScen
@export var player_state_savetime : int
@export var player_state_slot_name : String

func add_player_attribute_to_state_data(attribute_data:Vector2):
player_attributes.append(attribute_data)
func add_player_attribute_to_state_data(name: String, attribute_data:Vector2):
player_attributes[name] = attribute_data

func clear_saved_attribute_data():
player_attributes.clear()
Expand Down
13 changes: 9 additions & 4 deletions COGITO/SceneManagement/cogito_scene_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ func load_player_state(player, passed_slot:String):

# New way of loading player attributes:
var loaded_attribute_data = _player_state.player_attributes
for i in loaded_attribute_data.size():
player.player_attributes[i].set_attribute(loaded_attribute_data[i].x,loaded_attribute_data[i].y)
for attribute in loaded_attribute_data:
var attribute_data: Vector2 = loaded_attribute_data[attribute]
var cur_value = attribute_data.x
var max_value = attribute_data.y
player.player_attributes[attribute].set_attribute(cur_value, max_value)

player.global_position = _player_state.player_position
player.global_rotation = _player_state.player_rotation
Expand Down Expand Up @@ -163,8 +166,10 @@ func save_player_state(player, slot:String):
## New way of saving attributes:
_player_state.clear_saved_attribute_data()
for attribute in player.player_attributes:
var attribute_data : Vector2 = Vector2(attribute.value_current,attribute.value_max)
_player_state.add_player_attribute_to_state_data(attribute_data)
var cur_value = player.player_attributes[attribute].value_current
var max_value = player.player_attributes[attribute].value_max
var attribute_data := Vector2(cur_value, max_value)
_player_state.add_player_attribute_to_state_data(attribute, attribute_data)

## Adding a screenshot
var screenshot_path : String = str(_player_state.player_state_dir + _active_slot + ".png")
Expand Down
2 changes: 1 addition & 1 deletion COGITO/Scripts/Player_Hud_Manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func _setup_player():
ui_attribute_area.remove_child(n)
n.queue_free()

for attribute in player.player_attributes:
for attribute in player.player_attributes.values():
var spawned_attribute_ui = ui_attribute_prefab.instantiate()
ui_attribute_area.add_child(spawned_attribute_ui)
if attribute.attribute_name == "health":
Expand Down
70 changes: 36 additions & 34 deletions COGITO/Scripts/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var ladder_on_cooldown : bool = false
var WIGGLE_INTENSITY_MODIFIER = 1

### NEW PLAYER ATTRIBUTE SYSTEM:
var player_attributes : Array[Node]
var player_attributes : Dictionary
var stamina_attribute : CogitoAttribute = null
var visibility_attribute : CogitoAttribute

Expand Down Expand Up @@ -179,23 +179,24 @@ func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

### NEW PLAYER ATTRIBUTE SETUP:
player_attributes = find_children("","CogitoAttribute",false) #Grabs all attached player attributes
for attribute in player_attributes:
# Grabs all attached player attributes
for attribute in find_children("","CogitoAttribute",false):
player_attributes[attribute.attribute_name] = attribute
print("Cogito Attribute found: ", attribute.attribute_name)
if attribute.attribute_name == "health": # Hookup Health attribute signal to detect player death
attribute.death.connect(_on_death)
if attribute.attribute_name == "stamina": # Saving reference to stamina attribute for movements that require stamina checks
stamina_attribute = attribute
if attribute.attribute_name == "visibility": # Saving reference to visibilty attribute for that require visibility checks
visibility_attribute = attribute
if attribute.attribute_name == "sanity":
if visibility_attribute: # Hooking up sanity attribute to visibility attribute
visibility_attribute.attribute_changed.connect(attribute.on_visibility_changed)
visibility_attribute.check_current_visibility()

# If found, hookup health attribute signal to detect player death
var health_attribute = player_attributes.get("health")
if health_attribute:
health_attribute.death.connect(_on_death)
# Save reference to stamina attribute for movements that require stamina checks (null if not found)
stamina_attribute = player_attributes.get("stamina")
# Save reference to visibilty attribute for that require visibility checks (null if not found)
visibility_attribute = player_attributes.get("visibility")
# Hookup sanity attribute to visibility attribute
var sanity_attribute = player_attributes.get("sanity")
if sanity_attribute and visibility_attribute:
visibility_attribute.attribute_changed.connect(sanity_attribute.on_visibility_changed)
visibility_attribute.check_current_visibility()

# Pause Menu setup
if pause_menu:
Expand All @@ -218,27 +219,28 @@ func slide_audio_init():

# Use these functions to manipulate player attributes.
func increase_attribute(attribute_name: String, value: float, value_type: ConsumableItemPD.ValueType) -> bool:
for attribute in player_attributes:
if attribute.attribute_name == attribute_name:
if value_type == ConsumableItemPD.ValueType.CURRENT:
if attribute.value_current == attribute.value_max:
return false
else:
attribute.add(value)
return true
if value_type == ConsumableItemPD.ValueType.MAX:
attribute.value_max += value
attribute.add(value)
return true

print("Player.gd increase attribute: No match in for loop")
var attribute = player_attributes.get(attribute_name)
if not attribute:
print("Player.gd increase attribute: Attribute not found")
return false
if value_type == ConsumableItemPD.ValueType.CURRENT:
if attribute.value_current == attribute.value_max:
return false
attribute.add(value)
return true
elif value_type == ConsumableItemPD.ValueType.MAX:
attribute.value_max += value
attribute.add(value)
return true
return false


func decrease_attribute(attribute_name: String, value: float):
for attribute in player_attributes:
if attribute.attribute_name == attribute_name:
attribute.subtract(value)
var attribute = player_attributes.get(attribute_name)
if not attribute:
print("Player.gd decrease attribute: Attribute not found")
return
attribute.subtract(value)


func _on_death():
Expand Down

0 comments on commit fe807eb

Please sign in to comment.