2020-06-21 18:40:37 -04:00
|
|
|
package d2mapentity
|
2019-11-14 22:20:01 -05:00
|
|
|
|
|
|
|
import (
|
2020-06-18 14:11:04 -04:00
|
|
|
"image/color"
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
2020-01-26 00:39:13 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-06-24 13:49:13 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-06-25 14:56:49 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2inventory"
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
2019-11-14 22:20:01 -05:00
|
|
|
)
|
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
type Player struct {
|
2020-06-24 13:49:13 -04:00
|
|
|
mapEntity
|
|
|
|
composite *d2asset.Composite
|
2020-06-20 00:40:49 -04:00
|
|
|
Equipment d2inventory.CharacterEquipment
|
2020-06-25 14:56:49 -04:00
|
|
|
Stats d2hero.HeroStatsState
|
|
|
|
Class d2enum.Hero
|
2020-06-20 00:40:49 -04:00
|
|
|
Id string
|
|
|
|
direction int
|
2020-06-25 00:39:09 -04:00
|
|
|
name string
|
2020-06-20 00:40:49 -04:00
|
|
|
nameLabel d2ui.Label
|
|
|
|
lastPathSize int
|
|
|
|
isInTown bool
|
|
|
|
animationMode string
|
2020-06-22 15:55:32 -04:00
|
|
|
isRunToggled bool
|
|
|
|
isRunning bool
|
2020-06-26 20:03:00 -04:00
|
|
|
isCasting bool
|
2019-11-14 22:20:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-22 15:55:32 -04:00
|
|
|
// 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
|
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
func CreatePlayer(id, name string, x, y int, direction int, heroType d2enum.Hero, stats d2hero.HeroStatsState, equipment d2inventory.CharacterEquipment) *Player {
|
2019-12-24 01:48:45 -05:00
|
|
|
object := &d2datadict.ObjectLookupRecord{
|
2020-06-20 00:40:49 -04:00
|
|
|
Mode: d2enum.AnimationModePlayerTownNeutral.String(),
|
2019-12-24 01:48:45 -05:00
|
|
|
Base: "/data/global/chars",
|
|
|
|
Token: heroType.GetToken(),
|
2020-06-13 18:32:09 -04:00
|
|
|
Class: equipment.RightHand.GetWeaponClass(),
|
|
|
|
SH: equipment.Shield.GetItemCode(),
|
2019-12-24 01:48:45 -05:00
|
|
|
// TODO: Offhand class?
|
2020-06-13 18:32:09 -04:00
|
|
|
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(),
|
2019-11-14 22:20:01 -05:00
|
|
|
}
|
2019-12-24 01:48:45 -05:00
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
composite, err := d2asset.LoadComposite(object, d2resource.PaletteUnits)
|
2019-12-24 01:48:45 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:56:49 -04:00
|
|
|
stats.NextLevelExp = d2datadict.GetExperienceBreakpoint(heroType, stats.Level)
|
|
|
|
stats.Stamina = stats.MaxStamina
|
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
result := &Player{
|
2020-06-24 13:49:13 -04:00
|
|
|
Id: id,
|
|
|
|
mapEntity: createMapEntity(x, y),
|
|
|
|
composite: composite,
|
|
|
|
Equipment: equipment,
|
2020-06-25 14:56:49 -04:00
|
|
|
Stats: stats,
|
2020-06-24 13:49:13 -04:00
|
|
|
direction: direction,
|
2020-06-25 00:39:09 -04:00
|
|
|
name: name,
|
2020-06-25 14:56:49 -04:00
|
|
|
Class: heroType,
|
2020-06-24 13:49:13 -04:00
|
|
|
nameLabel: d2ui.CreateLabel(d2resource.FontFormal11, d2resource.PaletteStatic),
|
|
|
|
isRunToggled: true,
|
|
|
|
isInTown: true,
|
|
|
|
isRunning: true,
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-06-24 13:49:13 -04:00
|
|
|
result.SetSpeed(baseRunSpeed)
|
|
|
|
result.mapEntity.directioner = result.rotate
|
2020-06-18 14:11:04 -04:00
|
|
|
result.nameLabel.Alignment = d2ui.LabelAlignCenter
|
|
|
|
result.nameLabel.SetText(name)
|
|
|
|
result.nameLabel.Color = color.White
|
2020-06-20 00:40:49 -04:00
|
|
|
err = result.SetMode(d2enum.AnimationModePlayerTownNeutral.String(), equipment.RightHand.GetWeaponClass(), direction)
|
2020-06-18 14:11:04 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
2019-11-14 22:20:01 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:40:49 -04:00
|
|
|
func (p *Player) SetIsInTown(isInTown bool) {
|
|
|
|
p.isInTown = isInTown
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:55:32 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:40:49 -04:00
|
|
|
func (p Player) IsInTown() bool {
|
|
|
|
return p.isInTown
|
|
|
|
}
|
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
func (v *Player) Advance(tickTime float64) {
|
2020-02-22 20:44:30 -05:00
|
|
|
v.Step(tickTime)
|
2020-06-26 20:03:00 -04:00
|
|
|
if v.IsCasting() && v.composite.GetPlayedCount() >= 1 {
|
|
|
|
v.isCasting = false
|
|
|
|
v.SetAnimationMode(v.GetAnimationMode().String())
|
|
|
|
}
|
2020-06-24 13:49:13 -04:00
|
|
|
v.composite.Advance(tickTime)
|
2020-06-20 00:40:49 -04:00
|
|
|
if v.lastPathSize != len(v.path) {
|
|
|
|
v.lastPathSize = len(v.path)
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
if v.composite.GetAnimationMode() != v.animationMode {
|
|
|
|
v.animationMode = v.composite.GetAnimationMode()
|
2020-06-20 00:40:49 -04:00
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (v *Player) Render(target d2interface.Surface) {
|
2020-06-24 13:49:13 -04:00
|
|
|
target.PushTranslation(
|
|
|
|
v.offsetX+int((v.subcellX-v.subcellY)*16),
|
|
|
|
v.offsetY+int(((v.subcellX+v.subcellY)*8)-5),
|
|
|
|
)
|
|
|
|
defer target.Pop()
|
|
|
|
v.composite.Render(target)
|
2020-06-25 00:39:09 -04:00
|
|
|
//v.nameLabel.X = v.offsetX
|
|
|
|
//v.nameLabel.Y = v.offsetY - 100
|
|
|
|
//v.nameLabel.Render(target)
|
2019-11-14 22:20:01 -05:00
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
func (v *Player) SetMode(animationMode, weaponClass string, direction int) error {
|
|
|
|
v.composite.SetMode(animationMode, weaponClass, direction)
|
|
|
|
v.direction = direction
|
|
|
|
v.weaponClass = weaponClass
|
|
|
|
|
|
|
|
err := v.composite.SetMode(animationMode, weaponClass, direction)
|
|
|
|
if err != nil {
|
|
|
|
err = v.composite.SetMode(animationMode, "HTH", direction)
|
|
|
|
v.weaponClass = "HTH"
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Player) GetAnimationMode() d2enum.PlayerAnimationMode {
|
|
|
|
if v.IsRunning() && !v.IsAtTarget() {
|
|
|
|
return d2enum.AnimationModePlayerRun
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsInTown() {
|
|
|
|
if !v.IsAtTarget() {
|
|
|
|
return d2enum.AnimationModePlayerTownWalk
|
|
|
|
}
|
|
|
|
|
|
|
|
return d2enum.AnimationModePlayerTownNeutral
|
|
|
|
}
|
|
|
|
|
|
|
|
if !v.IsAtTarget() {
|
|
|
|
return d2enum.AnimationModePlayerWalk
|
|
|
|
}
|
|
|
|
|
2020-06-26 20:03:00 -04:00
|
|
|
if v.IsCasting() {
|
|
|
|
return d2enum.AnimationModePlayerCast
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
return d2enum.AnimationModePlayerNeutral
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Player) SetAnimationMode(animationMode string) error {
|
|
|
|
return v.composite.SetMode(animationMode, v.weaponClass, v.direction)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rotate sets direction and changes animation
|
|
|
|
func (v *Player) rotate(direction int) {
|
|
|
|
newAnimationMode := v.GetAnimationMode().String()
|
|
|
|
|
|
|
|
if newAnimationMode != v.composite.GetAnimationMode() || direction != v.direction {
|
|
|
|
v.SetMode(newAnimationMode, v.weaponClass, direction)
|
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|
2020-06-25 00:39:09 -04:00
|
|
|
|
|
|
|
func (v *Player) Name() string {
|
|
|
|
return v.name
|
|
|
|
}
|
2020-06-26 20:03:00 -04:00
|
|
|
|
|
|
|
func (v *Player) IsCasting() bool {
|
|
|
|
return v.isCasting
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Player) SetCasting() {
|
|
|
|
v.isCasting = true
|
|
|
|
v.SetAnimationMode(d2enum.AnimationModePlayerCast.String())
|
|
|
|
}
|
2020-06-27 18:58:41 -04:00
|
|
|
|
|
|
|
func (v *Player) Selectable() bool {
|
|
|
|
// Players are selectable when in town
|
|
|
|
return v.IsInTown()
|
|
|
|
}
|