Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | blue, blue |
I’m new to Godot, and programming in general, but I decided to try to recreate Earthbound’s battle system. I’m stuck quite early on, I’ve a test simulating an enemy’s attack to the player’s health, and I decided to try to animate the health, just to see how it would look.
Unfortunately, I haven’t found a way to add such things without it being incredibly tedious. So I decided to ask here, see if anyone with a bit more experience could give me a bit of guidance after I failed to find an answer through searching. Attached should be the code, a screenshot of the nodes, and the sprites I’ve been trying to hook it up with. Thanks in advance! (The textureprogress node is placeholder, I was trying to mess around with it to get it to work, but that’s what lead me here.)
extends Node2D
signal health_changed
signal ko
export var max_health = 20
onready var current_health = 0
var rolling_health = 0
enum STATES {ALIVE, DEAD}
var state = STATES.ALIVE
func _ready():
$ProgressBar.max_value = max_health
update_health(max_health)
func update_health(new_value):
$Tween.interpolate_property($ProgressBar, "value", rolling_health, new_value, 3, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
if not $Tween.is_active():
$Tween.start()
func _process(delta):
rolling_health = $ProgressBar.value
$Label.text = str(rolling_health)
$TextureProgress.value = rolling_health
func take_damage(count):
if state == STATES.DEAD:
return
current_health = rolling_health
current_health -= count
if current_health <= 0:
current_health = 0
state = STATES.DEAD
emit_signal("ko")
update_health(current_health)
#emit_signal("health_changed", rolling_health)
func _on_Button_pressed():
take_damage(5)