
- Mostly finished the settings menu - Got basic AI working on easy difficulty - Changed when animations are loaded - Music now changes when you hit play - Changed how AI will work. All enemies should only need the combatant script now
129 lines
3.5 KiB
GDScript
129 lines
3.5 KiB
GDScript
class_name Combatant extends Node2D
|
|
|
|
signal healthChanged(health: float)
|
|
|
|
@onready var renderer: AnimatedSprite2D = $AnimatedSprite2D
|
|
@onready var healthbar: HealthBar = $HealthBar
|
|
@onready var data: Data = get_node("/root/Root/Data")
|
|
|
|
@export var spellbook: Spellbook
|
|
@export var maxHealth: float = 10
|
|
@export var health: float = maxHealth
|
|
@export var player: bool = false
|
|
|
|
var spell: Spell
|
|
var spellIndex: int
|
|
var anim: AnimationBase
|
|
var casting: bool = false
|
|
var castCooldown: float = 0
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
renderer.animation_finished.connect(animationFinished)
|
|
renderer.play("idle")
|
|
spellbook.initCooldowns()
|
|
healthbar.maxHealth = maxHealth
|
|
if !player:
|
|
for spel: Spell in spellbook.spells:
|
|
if (spel == null): continue
|
|
if !data.animations.has(spel.animation):
|
|
data.animations[spel.animation] = load(spel.animation)
|
|
data.opponent = self
|
|
renderer.flip_h = true
|
|
healthbar.position.x *= -1
|
|
_finishedCasting()
|
|
else:
|
|
data.player = self
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta):
|
|
if !player && !casting:
|
|
timing(delta)
|
|
|
|
func _finishedCasting() -> void:
|
|
casting = false
|
|
match data.difficulty:
|
|
Data.Difficulty.EASY: castCooldown = randf_range(2.7, 3.3)
|
|
Data.Difficulty.NORMAL: castCooldown = randf_range(0.8, 1.3)
|
|
Data.Difficulty.HARD: castCooldown = randf_range(0.3, 0.7)
|
|
Data.Difficulty.GAMER: castCooldown = 0
|
|
|
|
func timing(delta) -> void:
|
|
castCooldown -= delta
|
|
if castCooldown <= 0:
|
|
print("Attempt cast")
|
|
cast()
|
|
|
|
func cast() -> void:
|
|
casting = true
|
|
match data.difficulty:
|
|
Data.Difficulty.EASY:
|
|
spellIndex = randi_range(0, spellbook.spells.size() - 1)
|
|
spell = spellbook.spells[spellIndex]
|
|
anim = data.animations[spell.animation].instantiate()
|
|
anim.setProgress(0, spell.castCombo.size())
|
|
anim.inverted = true
|
|
get_node("/root").add_child(anim)
|
|
attemptCast()
|
|
Data.Difficulty.NORMAL:
|
|
pass
|
|
Data.Difficulty.HARD:
|
|
pass
|
|
Data.Difficulty.GAMER:
|
|
pass
|
|
|
|
func attemptCast():
|
|
match data.difficulty:
|
|
Data.Difficulty.EASY:
|
|
print("Awaiting")
|
|
await get_tree().create_timer(randf_range(0.5, 1.5)).timeout
|
|
print("Awaited")
|
|
if randi_range(1, 100) < 85:
|
|
spell.castProgress += 1
|
|
anim.setProgress(spell.castProgress)
|
|
|
|
if spell.castProgress == spell.castCombo.size():
|
|
casting = false
|
|
spell.castProgress = 0
|
|
renderer.play("attack1")
|
|
spellbook.cooldowns[spellIndex] = spell.cooldown
|
|
data.player.alterHealth(-spell.damage, false)
|
|
_finishedCasting()
|
|
else:
|
|
attemptCast()
|
|
else:
|
|
alterHealth(-spell.backfireStrength, true)
|
|
casting = false
|
|
anim.castFailed()
|
|
spell.castProgress = 0
|
|
spellbook.cooldowns[spellIndex] = spell.cooldown * (float(spell.castProgress) / float(spell.castCombo.size()))
|
|
_finishedCasting()
|
|
Data.Difficulty.NORMAL:
|
|
await get_tree().create_timer(randf_range(0.5, 1.5)).timeout
|
|
if randi_range(1, 100) < 50:
|
|
pass #Success
|
|
else:
|
|
pass #fail
|
|
Data.Difficulty.HARD:
|
|
await get_tree().create_timer(randf_range(0.5, 1.5)).timeout
|
|
if randi_range(1, 100) < 50:
|
|
pass #Success
|
|
else:
|
|
pass #fail
|
|
Data.Difficulty.GAMER:
|
|
await get_tree().create_timer(randf_range(0.5, 1.5)).timeout
|
|
if randi_range(1, 100) < 50:
|
|
pass #Success
|
|
else:
|
|
pass #fail
|
|
|
|
func alterHealth(change: float, stun: bool) -> void:
|
|
health += change
|
|
if stun:
|
|
casting = false
|
|
renderer.play("hit")
|
|
healthChanged.emit(health)
|
|
|
|
func animationFinished() -> void:
|
|
renderer.play("idle")
|