1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-14 00:06:45 -05:00
OpenDiablo2/d2core/d2map/d2mapentity/player.go
presiyan-ivanov 2835ff4cf1
Improve run/walk/neutral animation handling. Initial parsing of Level… (#372)
* Improve run/walk/neutral animation handling. Initial parsing of LevelDetail records. Support for holding mouse buttons.

- Run/walk/neutral positions now map to a different animation mode(and speed) depending if in or out of town.

- Run/walk toggle which can be activated/deactivated with R key.

- Temporary(and incorrect) loading and mapping for LevelDetails records.

- Zone change label which shows the level name from LevelDetailsRecord when the player enters a different zone.

- Allow holding mouse left/right button to repeatedly generate an action.

* Remove duplicate load of LevelDetails. Replace numbers in zone change logic with their corresponding RegionIdType

* Move zone change check at the correct place

Co-authored-by: Presiyan Ivanov <presiyan-ivanov@users.noreply.github.com>
2020-06-22 15:55:32 -04:00

131 lines
3.5 KiB
Go

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()
}