Skip to content

Commit

Permalink
Merge pull request #3 from OctoD/feat/previous-values-on-attribute-ch…
Browse files Browse the repository at this point in the history
…anged

Feat/previous values on attribute changed
  • Loading branch information
OctoD authored May 19, 2021
2 parents db771f0 + 042a4bb commit 90c0671
Show file tree
Hide file tree
Showing 35 changed files with 990 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.import
.DS_Store
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ func _on_attribute_changed(attribute) -> void:
print(attribute.name + ":" + str(attribute.current_value))
```

> Note: the passed attribute is a dictionary with these keys/values
>
> `prev_max`: float
>
> `prev_value`: float
>
> `current_max`: float
>
> `current_value`: float
>
> `name`: String
Start your game and watch your mana regen!

## Creating your own effects
Expand Down Expand Up @@ -151,7 +163,7 @@ Ideal for:

`GameplayAttributesMap` allows you to connect to two `signal`s:

- `attribute_changed` emitted when an attribute changes a current or maximum value
- `attribute_changed` emitted when an attribute changes a current or maximum value. A dictionary will be passed with the previous max/current values and the newly set max/current values
- `effect_activated` emitted when an effect decides to emit the same signal (useful for ui notification)
- `effect_applied` emitted when an effect decides to emit the same signal (useful for ui notification)
- `effect_deactivated` emitted when an effect decides to emit the same signal (useful for ui notification)
Expand Down
20 changes: 18 additions & 2 deletions addons/godot-gameplay-attributes/nodes/GameplayAttribute.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,30 @@ func get_max_value() -> float:


func set_current_value(value: float) -> void:
var prev_max = max_value
var prev_value = current_value
current_value = clamp(value, 0, max_value)
emit_signal("attribute_changed", self)
emit_signal("attribute_changed", {
"prev_max": prev_max,
"prev_value": prev_value,
"current_max": max_value,
"current_value": current_value,
"name": self.name
})


func set_max_value(value: float) -> void:
if value >= 0:
var prev_value = current_value
var prev_max = max_value
max_value = value
emit_signal("attribute_changed", self)
emit_signal("attribute_changed", {
"prev_max": prev_max,
"prev_value": prev_value,
"current_max": max_value,
"current_value": current_value,
"name": self.name
})


func _get_configuration_warning():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func get_attribute_max_value(attribute_name: String) -> float:


func get_gameplay_effect(effect_name: String) -> GameplayEffect:
var found: GameplayEffect = find_node(effect_name)
return found
return find_node(effect_name) as GameplayEffect


func get_attributes() -> Array:
Expand All @@ -81,7 +80,7 @@ func get_effects() -> Array:
return effects


func _on_attribute_changed(attribute: GameplayAttribute) -> void:
func _on_attribute_changed(attribute: Dictionary) -> void:
emit_signal("attribute_changed", attribute)


Expand Down
4 changes: 3 additions & 1 deletion addons/godot-gameplay-attributes/nodes/GameplayEffect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func play_effect() -> void:
func pause_effect() -> void:
pass


func setup_effect() -> void:
_connect_to_parent_signal()

Expand All @@ -53,11 +54,12 @@ func get_kinematic_owner():
if parent:
return parent.get_parent()


func get_parent_attribute_map():
return get_parent()


func _on_attribute_changed(attribute: GameplayAttribute) -> void:
func _on_attribute_changed(attribute: Dictionary) -> void:
if should_activate(EffectActivationEvent.AttributeChanged):
apply_effect();

Expand Down
2 changes: 1 addition & 1 deletion addons/godot-gameplay-attributes/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="GameplayAttributes"
description="Gameplay attributes and effects for your game."
author="OctoD"
version="0.0.1"
version="0.4.0"
script="gameplay_attributes.gd"
8 changes: 8 additions & 0 deletions example/DamageEffect.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extends DamageGameplayEffect
class_name TestDamage

func setup_effect() -> void:
.setup_effect()
attribute_name = "Health"
damage_min = 5
damage_max = 10
7 changes: 7 additions & 0 deletions example/RestoreHealth.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends RestoreGameplayEffect
class_name RestoreHealthEffect

func setup_effect() -> void:
.setup_effect()
attribute_name = "Health"
value = 10
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/attribute_icon.png-b2f25314d318c523571e1561cd1646ad.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/godot-gameplay-attributes/assets/attribute_icon.png"
dest_files=[ "res://.import/attribute_icon.png-b2f25314d318c523571e1561cd1646ad.stex" ]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/damage_gameplay_effect.png-fac0dcc8f7cb5414b375513d1f0685a2.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/godot-gameplay-attributes/assets/damage_gameplay_effect.png"
dest_files=[ "res://.import/damage_gameplay_effect.png-fac0dcc8f7cb5414b375513d1f0685a2.stex" ]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/fall_gameplay_effect.png-b7a50b15a461293cda46c20921a046aa.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/godot-gameplay-attributes/assets/fall_gameplay_effect.png"
dest_files=[ "res://.import/fall_gameplay_effect.png-b7a50b15a461293cda46c20921a046aa.stex" ]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/restore_attribute_icon.png-9af1ea74225417cfc555be54f7f9f70a.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/godot-gameplay-attributes/assets/restore_attribute_icon.png"
dest_files=[ "res://.import/restore_attribute_icon.png-9af1ea74225417cfc555be54f7f9f70a.stex" ]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/timed_icon.png-c6a1a1379fcb51e63e380efcd0482704.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/godot-gameplay-attributes/assets/timed_icon.png"
dest_files=[ "res://.import/timed_icon.png-c6a1a1379fcb51e63e380efcd0482704.stex" ]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
36 changes: 36 additions & 0 deletions example/addons/godot-gameplay-attributes/gameplay_attributes.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
tool
extends EditorPlugin


func _enter_tree():
var attribute_node_icon = load("res://addons/godot-gameplay-attributes/assets/attribute_icon.png");
var gameplay_effect_icon = load("res://addons/godot-gameplay-attributes/assets/timed_icon.png")
var damage_effect_icon = load("res://addons/godot-gameplay-attributes/assets/damage_gameplay_effect.png")
var restore_effect_icon = load("res://addons/godot-gameplay-attributes/assets/restore_attribute_icon.png")

# basenodes
add_custom_type("GameplayAttributeMap", "Node", load("res://addons/godot-gameplay-attributes/nodes/GameplayAttributeMap.gd"), attribute_node_icon);
add_custom_type("GameplayAttribute", "Node", load("res://addons/godot-gameplay-attributes/nodes/GameplayAttribute.gd"), attribute_node_icon);
add_custom_type("GameplayEffect", "Node", load("res://addons/godot-gameplay-attributes/nodes/GameplayEffect.gd"), gameplay_effect_icon);
add_custom_type("TimedGameplayEffect", "GameplayEffect", load("res://addons/godot-gameplay-attributes/nodes/TimedGameplayEffect.gd"), gameplay_effect_icon);

# premade gameplay effects
add_custom_type("AttributeConsumeGameplayEffect", "TimedGameplayEffect", load("res://addons/godot-gameplay-attributes/premade_gameplay_effects/AttributeConsumeGameplayEffect.gd"), gameplay_effect_icon);
add_custom_type("AttributeRegenGameplayEffect", "TimedGameplayEffect", load("res://addons/godot-gameplay-attributes/premade_gameplay_effects/AttributeRegenGameplayEffect.gd"), gameplay_effect_icon);
add_custom_type("DamageGameplayEffect", "GameplayEffect", load("res://addons/godot-gameplay-attributes/premade_gameplay_effects/DamageGameplayEffect.gd"), damage_effect_icon);
add_custom_type("RestoreGameplayEffect", "GameplayEffect", load("res://addons/godot-gameplay-attributes/premade_gameplay_effects/RestoreGameplayEffect.gd"), restore_effect_icon);


func _exit_tree():
# basenodes

remove_custom_type("GameplayAttribute")
remove_custom_type("GameplayAttributeMap")
remove_custom_type("GameplayEffect")
remove_custom_type("TimedGameplayEffect")

# premade gameplay effects
remove_custom_type("AttributeConsumeGameplayEffect")
remove_custom_type("AttributeRegenGameplayEffect")
remove_custom_type("DamageGameplayEffect")
remove_custom_type("RestoreGameplayEffect")
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
tool
extends Node
class_name GameplayAttribute


export(float) var max_value = 0.0 setget set_max_value, get_max_value
export(float) var current_value = 0.0 setget set_current_value, get_current_value


signal attribute_changed


func get_current_value() -> float:
return current_value


func get_max_value() -> float:
return max_value


func set_current_value(value: float) -> void:
var prev_max = max_value
var prev_value = current_value
current_value = clamp(value, 0, max_value)
emit_signal("attribute_changed", {
"prev_max": prev_max,
"prev_value": prev_value,
"current_max": max_value,
"current_value": current_value,
"name": self.name
})


func set_max_value(value: float) -> void:
if value >= 0:
var prev_value = current_value
var prev_max = max_value
max_value = value
emit_signal("attribute_changed", {
"prev_max": prev_max,
"prev_value": prev_value,
"current_max": max_value,
"current_value": current_value,
"name": self.name
})


func _get_configuration_warning():
if get_parent().has_signal("attribute_changed"):
return ""

return "GameplayAttribute must be a direct child of a GameplayAttributeMap node."
Loading

0 comments on commit 90c0671

Please sign in to comment.