package d2mapentity import ( "image/color" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2inventory" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2render" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui" ) type Player struct { *AnimatedComposite Equipment d2inventory.CharacterEquipment Id string direction int Name string nameLabel d2ui.Label lastPathSize int isInTown bool animationMode string isRunToggled bool isRunning bool } // run speed should be walkspeed * 1.5, since in the original game it is 6 yards walk and 9 yards run. var baseWalkSpeed = 6.0 var baseRunSpeed = 9.0 func CreatePlayer(id, name string, x, y int, direction int, heroType d2enum.Hero, equipment d2inventory.CharacterEquipment) *Player { object := &d2datadict.ObjectLookupRecord{ Mode: d2enum.AnimationModePlayerTownNeutral.String(), Base: "/data/global/chars", Token: heroType.GetToken(), Class: equipment.RightHand.GetWeaponClass(), SH: equipment.Shield.GetItemCode(), // TODO: Offhand class? HD: equipment.Head.GetArmorClass(), TR: equipment.Torso.GetArmorClass(), LG: equipment.Legs.GetArmorClass(), RA: equipment.RightArm.GetArmorClass(), LA: equipment.LeftArm.GetArmorClass(), RH: equipment.RightHand.GetItemCode(), LH: equipment.LeftHand.GetItemCode(), } entity, err := CreateAnimatedComposite(x, y, object, d2resource.PaletteUnits) if err != nil { panic(err) } entity.SetSpeed(baseRunSpeed) result := &Player{ Id: id, AnimatedComposite: entity, Equipment: equipment, direction: direction, Name: name, nameLabel: d2ui.CreateLabel(d2resource.FontFormal11, d2resource.PaletteStatic), isRunToggled: true, isInTown: true, isRunning: true, } result.nameLabel.Alignment = d2ui.LabelAlignCenter result.nameLabel.SetText(name) result.nameLabel.Color = color.White result.SetPlayer(result) err = result.SetMode(d2enum.AnimationModePlayerTownNeutral.String(), equipment.RightHand.GetWeaponClass(), direction) if err != nil { panic(err) } return result } func (p *Player) SetIsInTown(isInTown bool) { p.isInTown = isInTown } func (p *Player) ToggleRunWalk() { p.isRunToggled = !p.isRunToggled } func (p *Player) IsRunToggled() bool { return p.isRunToggled } func (p *Player) IsRunning() bool { return p.isRunning } func (p *Player) SetIsRunning(isRunning bool) { p.isRunning = isRunning if isRunning { p.SetSpeed(baseRunSpeed) } else { p.SetSpeed(baseWalkSpeed) } } func (p Player) IsInTown() bool { return p.isInTown } func (v *Player) Advance(tickTime float64) { v.Step(tickTime) v.AnimatedComposite.Advance(tickTime) if v.lastPathSize != len(v.path) { v.lastPathSize = len(v.path) } if v.AnimatedComposite.composite.GetAnimationMode() != v.animationMode { v.animationMode = v.AnimatedComposite.composite.GetAnimationMode() } } func (v *Player) Render(target d2render.Surface) { v.AnimatedComposite.Render(target) offX := v.AnimatedComposite.offsetX + int((v.AnimatedComposite.subcellX-v.AnimatedComposite.subcellY)*16) offY := v.AnimatedComposite.offsetY + int(((v.AnimatedComposite.subcellX+v.AnimatedComposite.subcellY)*8)-5) v.nameLabel.X = offX v.nameLabel.Y = offY - 100 v.nameLabel.Render(target) } func (v *Player) GetPosition() (float64, float64) { return v.AnimatedComposite.GetPosition() }