2020-06-21 18:40:37 -04:00
|
|
|
package d2mapentity
|
2019-11-14 22:20:01 -05:00
|
|
|
|
|
|
|
import (
|
2020-07-23 12:56:50 -04:00
|
|
|
"fmt"
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-07-21 08:51:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
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-09-14 14:49:31 -04:00
|
|
|
name string
|
|
|
|
animationMode string
|
|
|
|
composite *d2asset.Composite
|
|
|
|
Equipment *d2inventory.CharacterEquipment
|
|
|
|
Stats *d2hero.HeroStatsState
|
2020-09-20 17:52:01 -04:00
|
|
|
Skills map[int]*d2hero.HeroSkill
|
2020-09-20 11:55:44 -04:00
|
|
|
LeftSkill *d2hero.HeroSkill
|
|
|
|
RightSkill *d2hero.HeroSkill
|
2020-09-14 14:49:31 -04:00
|
|
|
Class d2enum.Hero
|
|
|
|
lastPathSize int
|
|
|
|
isInTown bool
|
|
|
|
isRunToggled bool
|
|
|
|
isRunning bool
|
|
|
|
isCasting bool
|
|
|
|
onFinishedCasting func()
|
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.
|
2020-07-22 15:03:03 -04:00
|
|
|
const baseWalkSpeed = 6.0
|
|
|
|
const baseRunSpeed = 9.0
|
2020-06-22 15:55:32 -04:00
|
|
|
|
2020-08-05 21:27:45 -04:00
|
|
|
// ID returns the Player uuid
|
|
|
|
func (p *Player) ID() string {
|
|
|
|
return p.mapEntity.uuid
|
|
|
|
}
|
|
|
|
|
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-07-23 12:56:50 -04:00
|
|
|
func (p *Player) IsInTown() bool {
|
2020-06-20 00:40:49 -04:00
|
|
|
return p.isInTown
|
|
|
|
}
|
|
|
|
|
2020-10-25 03:42:31 -04:00
|
|
|
const (
|
|
|
|
half = 0.5
|
|
|
|
)
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Advance is called once per frame and processes a
|
|
|
|
// single game tick.
|
2020-07-23 12:56:50 -04:00
|
|
|
func (p *Player) Advance(tickTime float64) {
|
|
|
|
p.Step(tickTime)
|
|
|
|
|
2020-10-28 15:11:41 -04:00
|
|
|
if err := p.SetAnimationMode(p.GetAnimationMode()); err != nil {
|
|
|
|
fmt.Printf("failed to set animationMode to: %d, err: %v\n", p.GetAnimationMode(), err)
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:53:18 -04:00
|
|
|
if p.IsCasting() {
|
|
|
|
if p.composite.GetPlayedCount() >= 1 {
|
|
|
|
p.isCasting = false
|
2020-09-14 14:49:31 -04:00
|
|
|
}
|
|
|
|
|
2020-10-22 16:53:18 -04:00
|
|
|
// skills are casted after the first half of the casting animation is played
|
2020-10-25 03:42:31 -04:00
|
|
|
percentDone := float64(p.composite.GetCurrentFrame()) / float64(p.composite.GetFrameCount())
|
|
|
|
isHalfDoneCasting := percentDone >= half
|
|
|
|
|
2020-10-22 16:53:18 -04:00
|
|
|
if isHalfDoneCasting && p.onFinishedCasting != nil {
|
|
|
|
p.onFinishedCasting()
|
|
|
|
p.onFinishedCasting = nil
|
2020-07-23 12:56:50 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := p.composite.Advance(tickTime); err != nil {
|
2020-08-05 21:27:45 -04:00
|
|
|
fmt.Printf("failed to advance composite animation of player: %s, err: %v\n", p.ID(), err)
|
2020-06-26 20:03:00 -04:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if p.lastPathSize != len(p.path) {
|
|
|
|
p.lastPathSize = len(p.path)
|
2020-06-20 00:40:49 -04:00
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if p.composite.GetAnimationMode() != p.animationMode {
|
|
|
|
p.animationMode = p.composite.GetAnimationMode()
|
2020-06-20 00:40:49 -04:00
|
|
|
}
|
2020-10-21 23:25:53 -04:00
|
|
|
|
2020-10-23 09:00:51 -04:00
|
|
|
charstats := p.composite.AssetManager.Records.Character.Stats[p.Class]
|
|
|
|
staminaDrain := float64(charstats.StaminaRunDrain)
|
|
|
|
|
|
|
|
// This number has been determined by trying it out and checking if the stamina drain is
|
|
|
|
// the same as in d2 with the drain value from the assets.
|
|
|
|
// (We stopped the time for a lvl 1 babarian to loose all stamina which is around 25 seconds
|
|
|
|
// if i Remember correctly)
|
|
|
|
const magicStaminaDrainDivisor = 5
|
|
|
|
|
2020-10-21 23:25:53 -04:00
|
|
|
// Drain and regenerate Stamina
|
|
|
|
if p.IsRunning() && !p.atTarget() && !p.IsInTown() {
|
2020-10-23 09:00:51 -04:00
|
|
|
p.Stats.Stamina -= staminaDrain * tickTime / magicStaminaDrainDivisor
|
2020-10-22 01:12:06 -04:00
|
|
|
if p.Stats.Stamina <= 0 {
|
2020-10-21 23:25:53 -04:00
|
|
|
p.SetSpeed(baseWalkSpeed)
|
|
|
|
p.Stats.Stamina = 0
|
|
|
|
}
|
|
|
|
} else if p.Stats.Stamina < float64(p.Stats.MaxStamina) {
|
2020-10-23 09:00:51 -04:00
|
|
|
p.Stats.Stamina += staminaDrain * tickTime / magicStaminaDrainDivisor
|
2020-10-21 23:25:53 -04:00
|
|
|
if p.IsRunning() {
|
|
|
|
p.SetSpeed(baseRunSpeed)
|
|
|
|
}
|
|
|
|
}
|
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-07-23 12:56:50 -04:00
|
|
|
func (p *Player) Render(target d2interface.Surface) {
|
|
|
|
renderOffset := p.Position.RenderOffset()
|
2020-06-24 13:49:13 -04:00
|
|
|
target.PushTranslation(
|
2020-10-23 09:00:51 -04:00
|
|
|
int((renderOffset.X()-renderOffset.Y())*subtileWidth),
|
|
|
|
int(((renderOffset.X()+renderOffset.Y())*subtileHeight)+subtileOffsetY),
|
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()
|
2020-07-23 12:56:50 -04:00
|
|
|
|
|
|
|
if err := p.composite.Render(target); err != nil {
|
2020-08-05 21:27:45 -04:00
|
|
|
fmt.Printf("failed to render the composite of player: %s, err: %v\n", p.ID(), err)
|
2020-07-23 12:56:50 -04:00
|
|
|
}
|
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-07-23 12:56:50 -04:00
|
|
|
func (p *Player) GetAnimationMode() d2enum.PlayerAnimationMode {
|
|
|
|
if p.IsRunning() && !p.atTarget() {
|
2020-07-09 23:12:28 -04:00
|
|
|
return d2enum.PlayerAnimationModeRun
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if p.IsInTown() {
|
|
|
|
if !p.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-23 12:56:50 -04:00
|
|
|
if !p.atTarget() {
|
2020-07-09 23:12:28 -04:00
|
|
|
return d2enum.PlayerAnimationModeWalk
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if p.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-23 12:56:50 -04:00
|
|
|
func (p *Player) SetAnimationMode(animationMode d2enum.PlayerAnimationMode) error {
|
|
|
|
return p.composite.SetMode(animationMode, p.composite.GetWeaponClass())
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// rotate sets direction and changes animation
|
2020-07-23 12:56:50 -04:00
|
|
|
func (p *Player) rotate(direction int) {
|
|
|
|
newAnimationMode := p.GetAnimationMode()
|
2020-06-24 13:49:13 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if newAnimationMode.String() != p.composite.GetAnimationMode() {
|
|
|
|
if err := p.composite.SetMode(newAnimationMode, p.composite.GetWeaponClass()); err != nil {
|
|
|
|
fmt.Printf("failed to update animationMode of %s, err: %v\n", p.composite.GetWeaponClass(), err)
|
|
|
|
}
|
2020-07-03 22:52:50 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if direction != p.composite.GetDirection() {
|
|
|
|
p.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-09-14 14:49:31 -04:00
|
|
|
// SetDirection will rotate the player and change the animation
|
|
|
|
func (p *Player) SetDirection(direction int) {
|
|
|
|
p.rotate(direction)
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Name returns the player name.
|
2020-07-23 12:56:50 -04:00
|
|
|
func (p *Player) Name() string {
|
|
|
|
return p.name
|
2020-06-25 00:39:09 -04:00
|
|
|
}
|
2020-06-26 20:03:00 -04:00
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// IsCasting returns true if
|
2020-07-23 12:56:50 -04:00
|
|
|
func (p *Player) IsCasting() bool {
|
|
|
|
return p.isCasting
|
2020-06-26 20:03:00 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 14:49:31 -04:00
|
|
|
// StartCasting sets a flag indicating the player is casting a skill and
|
2020-07-09 16:11:01 -04:00
|
|
|
// sets the animation mode to the casting animation.
|
2020-10-10 18:47:51 -04:00
|
|
|
// This handles all types of skills - melee, ranged, kick, summon, etc.
|
2020-10-24 10:08:45 -04:00
|
|
|
// NB: onFinishedCasting is called when the casting animation is >50% complete
|
2020-10-10 18:47:51 -04:00
|
|
|
func (p *Player) StartCasting(animMode d2enum.PlayerAnimationMode, onFinishedCasting func()) {
|
|
|
|
// passive skills, auras, etc.
|
|
|
|
if animMode == d2enum.PlayerAnimationModeNone {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
p.isCasting = true
|
2020-09-14 14:49:31 -04:00
|
|
|
p.onFinishedCasting = onFinishedCasting
|
2020-10-10 18:47:51 -04:00
|
|
|
|
|
|
|
if err := p.SetAnimationMode(animMode); err != nil {
|
2020-08-05 21:27:45 -04:00
|
|
|
fmtStr := "failed to set animationMode of player: %s to: %d, err: %v\n"
|
2020-10-10 18:47:51 -04:00
|
|
|
fmt.Printf(fmtStr, p.ID(), animMode, err)
|
2020-07-23 12:56:50 -04:00
|
|
|
}
|
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-07-23 12:56:50 -04:00
|
|
|
func (p *Player) Selectable() bool {
|
2020-06-27 18:58:41 -04:00
|
|
|
// Players are selectable when in town
|
2020-07-23 12:56:50 -04:00
|
|
|
return p.IsInTown()
|
2020-06-27 18:58:41 -04:00
|
|
|
}
|
2020-07-21 08:51:09 -04:00
|
|
|
|
|
|
|
// GetPosition returns the entity's position
|
|
|
|
func (p *Player) GetPosition() d2vector.Position {
|
|
|
|
return p.mapEntity.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVelocity returns the entity's velocity vector
|
|
|
|
func (p *Player) GetVelocity() d2vector.Vector {
|
|
|
|
return p.mapEntity.velocity
|
|
|
|
}
|
2020-08-01 19:03:09 -04:00
|
|
|
|
|
|
|
// GetSize returns the current frame size
|
|
|
|
func (p *Player) GetSize() (width, height int) {
|
|
|
|
width, height = p.composite.GetSize()
|
2020-10-25 18:36:12 -04:00
|
|
|
// https://github.com/OpenDiablo2/OpenDiablo2/issues/820
|
2020-08-01 19:03:09 -04:00
|
|
|
height = (height * 2) - (height / 2)
|
|
|
|
|
|
|
|
return width, height
|
|
|
|
}
|