broken_seals/game/scripts/ai/EntityAIGD.gd

109 lines
2.6 KiB
GDScript

extends EntityAI
class_name EntityAIGD
# Copyright Péter Magyar relintai@gmail.com
# MIT License, functionality from this class needs to be protable to the entity spell system
var _data : Dictionary = {
"target_aura_spells": {},
"spells": []
}
func _on_set_owner():
if not is_instance_valid(owner):
return
if not owner.sentity_data:
return
var ent_data : EntityData = owner.sentity_data
if not ent_data.entity_class_data:
return
var class_data : EntityClassData = ent_data.entity_class_data
for i in range(class_data.get_num_spells()):
var spell : Spell = class_data.get_spell(i)
if spell.get_num_target_aura_applys() > 0:
var aura : Aura = spell.get_target_aura_apply(0)
if not _data["target_aura_spells"].has(aura.aura_group):
_data["target_aura_spells"][aura.aura_group] = []
_data["target_aura_spells"][aura.aura_group].append({ "aura_id": aura.id, "spell_id": spell.id, "rank": spell.rank })
continue
_data["spells"].append(spell.id)
for key in _data["target_aura_spells"]:
var arr : Array = _data["target_aura_spells"][key]
arr.sort_custom(self, "sort_spells_by_rank")
func _update(delta):
if owner.ai_state == EntityEnums.AI_STATE_ATTACK:
attack(delta)
func attack(delta):
var target : Entity = owner.starget
if target == null:
owner.ai_state = EntityEnums.AI_STATE_REGENERATE
owner.target_movement_direction = Vector2()
return
var cast : bool = false
if not owner.gets_has_global_cooldown():
var taspellsdict : Dictionary = _data["target_aura_spells"]
for taskey in taspellsdict.keys():
for tas in taspellsdict[taskey]:
var spell_id : int = tas["spell_id"]
if not owner.hass_spell_id(spell_id):
continue
if taskey == null:
if target.sget_aura_by(owner, tas["aura_id"]) == null and not owner.hass_cooldown(spell_id):
owner.crequest_spell_cast(spell_id)
cast = true
break
else:
if target.sget_aura_with_group_by(owner, taskey) == null and not owner.hass_cooldown(spell_id):
owner.crequest_spell_cast(spell_id)
cast = true
break
if cast:
break
if not cast:
var sps : Array = _data["spells"]
for spell_id in sps:
if not owner.hass_spell_id(spell_id):
continue
if not owner.hass_cooldown(spell_id):
owner.crequest_spell_cast(spell_id)
cast = true
break
if owner.sis_casting():
owner.target_movement_direction = Vector2()
return
var dir : Vector3 = target.translation - owner.translation
owner.target_movement_direction = Vector2(dir.x, dir.z)
func sort_spells_by_rank(a, b):
if a == null or b == null:
return true
return a["rank"] > b["rank"]