2020-06-21 18:40:37 -04:00
|
|
|
package d2mapentity
|
2019-11-14 22:20:01 -05:00
|
|
|
|
|
|
|
import (
|
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"
|
2019-11-14 22:20:01 -05:00
|
|
|
)
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Player is the player character entity.
|
2020-06-13 18:32:09 -04:00
|
|
|
type Player struct {
|
2020-06-24 13:49:13 -04:00
|
|
|
mapEntity
|
2020-07-03 14:00:56 -04:00
|
|
|
composite *d2asset.Composite
|
|
|
|
Equipment d2inventory.CharacterEquipment
|
|
|
|
Stats d2hero.HeroStatsState
|
|
|
|
Class d2enum.Hero
|
|
|
|
Id string
|
|
|
|
name string
|
|
|
|
// nameLabel d2ui.Label
|
2020-06-20 00:40:49 -04:00
|
|
|
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-07-09 16:11:01 -04:00
|
|
|
// CreatePlayer creates a new player entity and returns a pointer to it.
|
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 {
|
2020-07-03 22:52:50 -04:00
|
|
|
layerEquipment := &[d2enum.CompositeTypeMax]string{
|
|
|
|
d2enum.CompositeTypeHead: equipment.Head.GetArmorClass(),
|
|
|
|
d2enum.CompositeTypeTorso: equipment.Torso.GetArmorClass(),
|
|
|
|
d2enum.CompositeTypeLegs: equipment.Legs.GetArmorClass(),
|
|
|
|
d2enum.CompositeTypeRightArm: equipment.RightArm.GetArmorClass(),
|
|
|
|
d2enum.CompositeTypeLeftArm: equipment.LeftArm.GetArmorClass(),
|
|
|
|
d2enum.CompositeTypeRightHand: equipment.RightHand.GetItemCode(),
|
|
|
|
d2enum.CompositeTypeLeftHand: equipment.LeftHand.GetItemCode(),
|
|
|
|
d2enum.CompositeTypeShield: equipment.Shield.GetItemCode(),
|
2019-11-14 22:20:01 -05:00
|
|
|
}
|
2019-12-24 01:48:45 -05:00
|
|
|
|
2020-07-03 22:52:50 -04:00
|
|
|
composite, err := d2asset.LoadComposite(d2enum.ObjectTypePlayer, heroType.GetToken(),
|
|
|
|
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-07-03 14:00:56 -04:00
|
|
|
Id: id,
|
2020-07-16 12:06:08 -04:00
|
|
|
mapEntity: newMapEntity(x, y),
|
2020-07-03 14:00:56 -04:00
|
|
|
composite: composite,
|
|
|
|
Equipment: equipment,
|
|
|
|
Stats: stats,
|
|
|
|
name: name,
|
|
|
|
Class: heroType,
|
|
|
|
//nameLabel: d2ui.CreateLabel(d2resource.FontFormal11, d2resource.PaletteStatic),
|
2020-06-24 13:49:13 -04:00
|
|
|
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-07-03 14:00:56 -04:00
|
|
|
//result.nameLabel.Alignment = d2ui.LabelAlignCenter
|
|
|
|
//result.nameLabel.SetText(name)
|
|
|
|
//result.nameLabel.Color = color.White
|
2020-07-09 23:12:28 -04:00
|
|
|
err = composite.SetMode(d2enum.PlayerAnimationModeTownNeutral, equipment.RightHand.GetWeaponClass())
|
2020-06-18 14:11:04 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-03 22:52:50 -04:00
|
|
|
composite.SetDirection(direction)
|
|
|
|
composite.Equip(layerEquipment)
|
|
|
|
|
2019-11-14 22:20:01 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// SetIsInTown sets a flag indicating that the player is in town.
|
2020-06-20 00:40:49 -04:00
|
|
|
func (p *Player) SetIsInTown(isInTown bool) {
|
|
|
|
p.isInTown = isInTown
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// ToggleRunWalk sets a flag indicating whether the player is running.
|
2020-06-22 15:55:32 -04:00
|
|
|
func (p *Player) ToggleRunWalk() {
|
|
|
|
p.isRunToggled = !p.isRunToggled
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// IsRunToggled returns true if the UI button to toggle running is,
|
|
|
|
// toggled i.e. not in it's default state.
|
2020-06-22 15:55:32 -04:00
|
|
|
func (p *Player) IsRunToggled() bool {
|
|
|
|
return p.isRunToggled
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// IsRunning returns true if the player is currently
|
2020-06-22 15:55:32 -04:00
|
|
|
func (p *Player) IsRunning() bool {
|
|
|
|
return p.isRunning
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// SetIsRunning alters the player speed and sets a flag indicating
|
|
|
|
// that the player is running.
|
2020-06-22 15:55:32 -04:00
|
|
|
func (p *Player) SetIsRunning(isRunning bool) {
|
|
|
|
p.isRunning = isRunning
|
|
|
|
|
|
|
|
if isRunning {
|
|
|
|
p.SetSpeed(baseRunSpeed)
|
|
|
|
} else {
|
|
|
|
p.SetSpeed(baseWalkSpeed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// IsInTown returns true if the player is currently in town.
|
2020-06-20 00:40:49 -04:00
|
|
|
func (p Player) IsInTown() bool {
|
|
|
|
return p.isInTown
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Advance is called once per frame and processes a
|
|
|
|
// single game tick.
|
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-07-09 16:11:01 -04:00
|
|
|
|
2020-06-26 20:03:00 -04:00
|
|
|
if v.IsCasting() && v.composite.GetPlayedCount() >= 1 {
|
|
|
|
v.isCasting = false
|
2020-07-09 23:12:28 -04:00
|
|
|
v.SetAnimationMode(v.GetAnimationMode())
|
2020-06-26 20:03:00 -04:00
|
|
|
}
|
2020-06-24 13:49:13 -04:00
|
|
|
v.composite.Advance(tickTime)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
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-07-09 16:11:01 -04:00
|
|
|
// Render renders the animated composite for this entity.
|
2020-06-29 00:41:58 -04:00
|
|
|
func (v *Player) Render(target d2interface.Surface) {
|
2020-07-13 09:06:50 -04:00
|
|
|
renderOffset := v.Position.RenderOffset()
|
2020-06-24 13:49:13 -04:00
|
|
|
target.PushTranslation(
|
2020-07-13 09:06:50 -04:00
|
|
|
int((renderOffset.X()-renderOffset.Y())*16),
|
2020-07-16 12:06:08 -04:00
|
|
|
int(((renderOffset.X()+renderOffset.Y())*8)-5),
|
2020-06-24 13:49:13 -04:00
|
|
|
)
|
2020-07-13 09:06:50 -04:00
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
defer target.Pop()
|
|
|
|
v.composite.Render(target)
|
2020-07-09 16:11:01 -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-07-09 16:11:01 -04:00
|
|
|
// GetAnimationMode returns the current animation mode based on what the player is doing and where they are.
|
2020-06-24 13:49:13 -04:00
|
|
|
func (v *Player) GetAnimationMode() d2enum.PlayerAnimationMode {
|
2020-07-16 12:06:08 -04:00
|
|
|
if v.IsRunning() && !v.atTarget() {
|
2020-07-09 23:12:28 -04:00
|
|
|
return d2enum.PlayerAnimationModeRun
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsInTown() {
|
2020-07-16 12:06:08 -04:00
|
|
|
if !v.atTarget() {
|
2020-07-09 23:12:28 -04:00
|
|
|
return d2enum.PlayerAnimationModeTownWalk
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 23:12:28 -04:00
|
|
|
return d2enum.PlayerAnimationModeTownNeutral
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-16 12:06:08 -04:00
|
|
|
if !v.atTarget() {
|
2020-07-09 23:12:28 -04:00
|
|
|
return d2enum.PlayerAnimationModeWalk
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 20:03:00 -04:00
|
|
|
if v.IsCasting() {
|
2020-07-09 23:12:28 -04:00
|
|
|
return d2enum.PlayerAnimationModeCast
|
2020-06-26 20:03:00 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 23:12:28 -04:00
|
|
|
return d2enum.PlayerAnimationModeNeutral
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-13 09:06:50 -04:00
|
|
|
// SetAnimationMode sets the Composite's animation mode weapon class and direction.
|
2020-07-09 23:12:28 -04:00
|
|
|
func (v *Player) SetAnimationMode(animationMode d2enum.PlayerAnimationMode) error {
|
2020-07-03 22:52:50 -04:00
|
|
|
return v.composite.SetMode(animationMode, v.composite.GetWeaponClass())
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// rotate sets direction and changes animation
|
|
|
|
func (v *Player) rotate(direction int) {
|
2020-07-09 23:12:28 -04:00
|
|
|
newAnimationMode := v.GetAnimationMode()
|
2020-06-24 13:49:13 -04:00
|
|
|
|
2020-07-09 23:12:28 -04:00
|
|
|
if newAnimationMode.String() != v.composite.GetAnimationMode() {
|
2020-07-03 22:52:50 -04:00
|
|
|
v.composite.SetMode(newAnimationMode, v.composite.GetWeaponClass())
|
|
|
|
}
|
|
|
|
|
|
|
|
if direction != v.composite.GetDirection() {
|
|
|
|
v.composite.SetDirection(direction)
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
}
|
2020-06-25 00:39:09 -04:00
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Name returns the player name.
|
2020-06-25 00:39:09 -04:00
|
|
|
func (v *Player) Name() string {
|
|
|
|
return v.name
|
|
|
|
}
|
2020-06-26 20:03:00 -04:00
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// IsCasting returns true if
|
2020-06-26 20:03:00 -04:00
|
|
|
func (v *Player) IsCasting() bool {
|
|
|
|
return v.isCasting
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// SetCasting sets a flag indicating the player is casting a skill and
|
|
|
|
// sets the animation mode to the casting animation.
|
2020-06-26 20:03:00 -04:00
|
|
|
func (v *Player) SetCasting() {
|
|
|
|
v.isCasting = true
|
2020-07-09 23:12:28 -04:00
|
|
|
v.SetAnimationMode(d2enum.PlayerAnimationModeCast)
|
2020-06-26 20:03:00 -04:00
|
|
|
}
|
2020-06-27 18:58:41 -04:00
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Selectable returns true if the player is in town.
|
2020-06-27 18:58:41 -04:00
|
|
|
func (v *Player) Selectable() bool {
|
|
|
|
// Players are selectable when in town
|
|
|
|
return v.IsInTown()
|
|
|
|
}
|