Fixed major spellcasting bug

- The player and enemy were using a cast progress variable that was part of the spell object, so they were altering it at the same time
This commit is contained in:
nc5432 2024-05-13 18:32:10 -04:00
parent 9f7cb83789
commit 8fce25f619
5 changed files with 161 additions and 151 deletions

View File

@ -10,7 +10,6 @@ class_name Spell
@export var damage: float = 1
@export var backfireStrength: float = 1
@export var castCombo: Array[String]
@export var castProgress: int = 0
@export var element: Data.Element
@export var stunning: bool
@export var timeout: float = 10

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
class_name Combatant extends Node2D
signal healthChanged(health: float)
signal stunned
@onready var renderer: AnimatedSprite2D = $AnimatedSprite2D
@onready var healthbar: HealthBar = $HealthBar
@ -16,6 +17,7 @@ var spellIndex: int
var anim: AnimationBase
var casting: bool = false
var castCooldown: float = 0
var castProgress: int = 0
# Called when the node enters the scene tree for the first time.
func _ready():
@ -51,7 +53,6 @@ func _finishedCasting() -> void:
func timing(delta) -> void:
castCooldown -= delta
if castCooldown <= 0:
print("Attempt cast")
cast()
func cast() -> void:
@ -95,12 +96,12 @@ func attemptCast():
Data.Difficulty.EASY:
await get_tree().create_timer(randf_range(0.5, 1.5)).timeout
if randi_range(0, 99) < 90:
spell.castProgress += 1
anim.setProgress(spell.castProgress)
castProgress += 1
anim.setProgress(castProgress)
if spell.castProgress == spell.castCombo.size():
if castProgress == spell.castCombo.size():
casting = false
spell.castProgress = 0
castProgress = 0
renderer.play("attack1")
spellbook.cooldowns[spellIndex] = spell.cooldown
data.player.alterHealth(-spell.damage, false)
@ -111,18 +112,18 @@ func attemptCast():
alterHealth(-spell.backfireStrength, true)
casting = false
anim.castFailed()
spell.castProgress = 0
spellbook.cooldowns[spellIndex] = spell.cooldown * (float(spell.castProgress) / float(spell.castCombo.size()))
castProgress = 0
spellbook.cooldowns[spellIndex] = spell.cooldown * (float(castProgress) / float(spell.castCombo.size()))
_finishedCasting()
Data.Difficulty.NORMAL:
await get_tree().create_timer(randf_range(0.4, 1)).timeout
if randi_range(0, 99) < 95:
spell.castProgress += 1
anim.setProgress(spell.castProgress)
castProgress += 1
anim.setProgress(castProgress)
if spell.castProgress == spell.castCombo.size():
if castProgress == spell.castCombo.size():
casting = false
spell.castProgress = 0
castProgress = 0
renderer.play("attack1")
spellbook.cooldowns[spellIndex] = spell.cooldown
data.player.alterHealth(-spell.damage, false)
@ -133,18 +134,18 @@ func attemptCast():
alterHealth(-spell.backfireStrength, true)
casting = false
anim.castFailed()
spell.castProgress = 0
spellbook.cooldowns[spellIndex] = spell.cooldown * (float(spell.castProgress) / float(spell.castCombo.size()))
castProgress = 0
spellbook.cooldowns[spellIndex] = spell.cooldown * (float(castProgress) / float(spell.castCombo.size()))
_finishedCasting()
Data.Difficulty.HARD:
await get_tree().create_timer(randf_range(0.2, 0.6)).timeout
if randi_range(0, 99) < 98:
spell.castProgress += 1
anim.setProgress(spell.castProgress)
castProgress += 1
anim.setProgress(castProgress)
if spell.castProgress == spell.castCombo.size():
if castProgress == spell.castCombo.size():
casting = false
spell.castProgress = 0
castProgress = 0
renderer.play("attack1")
spellbook.cooldowns[spellIndex] = spell.cooldown
data.player.alterHealth(-spell.damage, false)
@ -155,17 +156,17 @@ func attemptCast():
alterHealth(-spell.backfireStrength, true)
casting = false
anim.castFailed()
spell.castProgress = 0
spellbook.cooldowns[spellIndex] = spell.cooldown * (float(spell.castProgress) / float(spell.castCombo.size()))
castProgress = 0
spellbook.cooldowns[spellIndex] = spell.cooldown * (float(castProgress) / float(spell.castCombo.size()))
_finishedCasting()
Data.Difficulty.GAMER:
await get_tree().create_timer(.1).timeout
spell.castProgress += 1
anim.setProgress(spell.castProgress)
castProgress += 1
anim.setProgress(castProgress)
if spell.castProgress == spell.castCombo.size():
if castProgress == spell.castCombo.size():
casting = false
spell.castProgress = 0
castProgress = 0
renderer.play("attack1")
spellbook.cooldowns[spellIndex] = spell.cooldown
data.player.alterHealth(-spell.damage, false)
@ -178,6 +179,8 @@ func alterHealth(change: float, stun: bool) -> void:
if stun:
casting = false
renderer.play("hit")
if player:
stunned.emit()
healthChanged.emit(health)
func animationFinished() -> void:

View File

@ -4,5 +4,4 @@ class_name HealthBar extends Control
@export var maxHealth: float = 1
func healthChanged(health) -> void:
print("Health Changed")
bar.size.y = (health / maxHealth) * 100

View File

@ -2,7 +2,9 @@ extends Node
@onready var data: Data = get_node("/root/Root/Data")
@onready var castIndicator: CastIndicator = get_node("/root/Root/Arena/CastIndicator")
@export var avatar: Combatant
var casting = false
var spell: Spell
var spellIndex: int
@ -11,6 +13,8 @@ var anim: AnimationBase
# Called when the node enters the scene tree for the first time.
func _ready():
avatar.connect("stunned", stunned)
#connect(data.player.stunned, stunned)
data.spellbook.initCooldowns()
for spel: Spell in data.spellbook.spells:
if (spel == null): continue
@ -30,22 +34,21 @@ func _process(delta):
anim = data.animations[spell.animation].instantiate()
anim.setProgress(0, spell.castCombo.size())
get_node("/root").add_child(anim)
timer = 0
else:
timer += delta
if (Input.is_action_just_pressed(spell.castCombo[spell.castProgress])):
spell.castProgress += 1
anim.setProgress(spell.castProgress)
if (Input.is_action_just_pressed(spell.castCombo[avatar.castProgress])):
avatar.castProgress += 1
anim.setProgress(avatar.castProgress)
if spell.castProgress == spell.castCombo.size() - 1:
castIndicator.setDirs(spell.castCombo[spell.castProgress])
elif spell.castProgress < spell.castCombo.size() - 1:
castIndicator.setDirs(spell.castCombo[spell.castProgress], spell.castCombo[spell.castProgress + 1])
if avatar.castProgress == spell.castCombo.size() - 1:
castIndicator.setDirs(spell.castCombo[avatar.castProgress])
elif avatar.castProgress < spell.castCombo.size() - 1:
castIndicator.setDirs(spell.castCombo[avatar.castProgress], spell.castCombo[avatar.castProgress + 1])
if spell.castProgress == spell.castCombo.size():
if avatar.castProgress == spell.castCombo.size():
casting = false
spell.castProgress = 0
avatar.castProgress = 0
avatar.renderer.play("attack1")
data.spellbook.cooldowns[spellIndex] = spell.cooldown
castIndicator.hide()
@ -54,10 +57,16 @@ func _process(delta):
avatar.alterHealth(-spell.backfireStrength, true)
casting = false
anim.castFailed()
spell.castProgress = 0
data.spellbook.cooldowns[spellIndex] = spell.cooldown * (float(spell.castProgress) / float(spell.castCombo.size()))
avatar.castProgress = 0
data.spellbook.cooldowns[spellIndex] = spell.cooldown * (float(avatar.castProgress) / float(spell.castCombo.size()))
castIndicator.hide()
for i in range(data.spellbook.cooldowns.size()):
data.spellbook.cooldowns[i] -= delta
if data.spellbook.cooldowns[i] < 0: data.spellbook.cooldowns[i] = 0
func stunned():
casting = false
avatar.castProgress = 0
data.spellbook.cooldowns[spellIndex] = spell.cooldown * (float(avatar.castProgress) / float(spell.castCombo.size()))
castIndicator.hide()