eq2go/internal/spells/handlers_example.go

180 lines
5.4 KiB
Go

package spells
import "fmt"
// Example spell handlers to demonstrate native Go spell scripting
// HealSpellHandler handles healing spells
type HealSpellHandler struct {
healAmount float32
scalingFactor float32
targetRequired bool
}
// NewHealSpellHandler creates a new heal spell handler
func NewHealSpellHandler(healAmount, scalingFactor float32, targetRequired bool) *HealSpellHandler {
return &HealSpellHandler{
healAmount: healAmount,
scalingFactor: scalingFactor,
targetRequired: targetRequired,
}
}
// Execute runs the heal spell logic
func (h *HealSpellHandler) Execute(caster SpellCaster, target SpellTarget, spell *Spell, data *SpellScriptData) error {
// Example healing logic
if h.targetRequired && target == nil {
return fmt.Errorf("heal spell requires a target")
}
// Calculate heal amount based on caster level and data values
healAmount := h.healAmount + (float32(caster.GetLevel()) * h.scalingFactor)
if data.FloatValue > 0 {
healAmount += data.FloatValue
}
// TODO: Apply healing to target when entity system is integrated
// target.AddHP(int32(healAmount))
return nil
}
// GetType returns the handler type
func (h *HealSpellHandler) GetType() string {
return "heal"
}
// Validate checks if the script data is valid
func (h *HealSpellHandler) Validate(data *SpellScriptData) error {
if data.Type != SpellScriptDataTypeFloat {
return fmt.Errorf("heal handler expects float data type")
}
return nil
}
// DamageSpellHandler handles damage spells
type DamageSpellHandler struct {
damageAmount float32
damageType int32
requiresTarget bool
}
// NewDamageSpellHandler creates a new damage spell handler
func NewDamageSpellHandler(damageAmount float32, damageType int32, requiresTarget bool) *DamageSpellHandler {
return &DamageSpellHandler{
damageAmount: damageAmount,
damageType: damageType,
requiresTarget: requiresTarget,
}
}
// Execute runs the damage spell logic
func (d *DamageSpellHandler) Execute(caster SpellCaster, target SpellTarget, spell *Spell, data *SpellScriptData) error {
if d.requiresTarget && target == nil {
return fmt.Errorf("damage spell requires a target")
}
// Calculate damage based on spell data
damage := d.damageAmount
if data.IntValue > 0 {
damage += float32(data.IntValue)
}
// TODO: Apply damage to target when combat system is integrated
// combat.InflictDamage(caster, target, damage, d.damageType)
return nil
}
// GetType returns the handler type
func (d *DamageSpellHandler) GetType() string {
return "damage"
}
// Validate checks if the script data is valid
func (d *DamageSpellHandler) Validate(data *SpellScriptData) error {
if data.Type != SpellScriptDataTypeInt {
return fmt.Errorf("damage handler expects int data type")
}
return nil
}
// BuffSpellHandler handles buff spells
type BuffSpellHandler struct {
statType int16
bonusValue float32
duration int32
}
// NewBuffSpellHandler creates a new buff spell handler
func NewBuffSpellHandler(statType int16, bonusValue float32, duration int32) *BuffSpellHandler {
return &BuffSpellHandler{
statType: statType,
bonusValue: bonusValue,
duration: duration,
}
}
// Execute runs the buff spell logic
func (b *BuffSpellHandler) Execute(caster SpellCaster, target SpellTarget, spell *Spell, data *SpellScriptData) error {
// Apply buff to target
if target == nil {
target = caster.(SpellTarget) // Self-buff
}
// TODO: Add buff to target when buff system is integrated
// target.AddBuff(spell.GetSpellID(), b.statType, b.bonusValue, b.duration)
return nil
}
// GetType returns the handler type
func (b *BuffSpellHandler) GetType() string {
return "buff"
}
// Validate checks if the script data is valid
func (b *BuffSpellHandler) Validate(data *SpellScriptData) error {
// Buff handlers can accept various data types
return nil
}
// SpellHandlerRegistry manages spell handlers
type SpellHandlerRegistry struct {
handlers map[string]SpellScriptHandler
}
// NewSpellHandlerRegistry creates a new handler registry
func NewSpellHandlerRegistry() *SpellHandlerRegistry {
return &SpellHandlerRegistry{
handlers: make(map[string]SpellScriptHandler),
}
}
// RegisterHandler registers a spell handler
func (shr *SpellHandlerRegistry) RegisterHandler(name string, handler SpellScriptHandler) {
shr.handlers[name] = handler
}
// GetHandler retrieves a spell handler by name
func (shr *SpellHandlerRegistry) GetHandler(name string) SpellScriptHandler {
return shr.handlers[name]
}
// InitializeDefaultHandlers sets up common spell handlers
func (shr *SpellHandlerRegistry) InitializeDefaultHandlers() {
// Register common heal handlers
shr.RegisterHandler("minor_heal", NewHealSpellHandler(50.0, 2.0, true))
shr.RegisterHandler("major_heal", NewHealSpellHandler(200.0, 5.0, true))
shr.RegisterHandler("group_heal", NewHealSpellHandler(75.0, 3.0, false))
// Register common damage handlers
shr.RegisterHandler("fire_damage", NewDamageSpellHandler(30.0, 3, true)) // Heat damage
shr.RegisterHandler("cold_damage", NewDamageSpellHandler(25.0, 4, true)) // Cold damage
shr.RegisterHandler("magic_damage", NewDamageSpellHandler(40.0, 5, true)) // Magic damage
// Register common buff handlers
shr.RegisterHandler("strength_buff", NewBuffSpellHandler(ModifyStr, 10.0, 600000)) // 10 minute duration
shr.RegisterHandler("intelligence_buff", NewBuffSpellHandler(ModifyInt, 8.0, 600000))
shr.RegisterHandler("speed_buff", NewBuffSpellHandler(ModifySpeed, 15.0, 300000)) // 5 minute duration
}