1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-14 00:06:45 -05:00
OpenDiablo2/d2core/d2map/d2mapentity/player.go
danhale-git 6104adc700
D2map lint warnings (#566)
* Comments and newlines in engine.go

* Comments and newlines in object.go

* Comments and newlines in animated_entity.go

* Comments and newlines in missile.go

* Comments and newlines in npc.go

* Comments and newlines in player.go

* Removed object.go (incorrectly merged it in during rebase).

* Comments and newlines in renderer.go.

* Comments and newlines in map_entity.go.

* Comments and newlines in walk_mesh.go.

* Comments and newlines in viewport.go and tile_cache.go.

* Comments and newlines in stamp.go and wilderness_tile_types.go.

* Comments and newlines in everything else.
2020-07-09 16:11:01 -04:00

224 lines
6.2 KiB
Go

package d2mapentity
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2inventory"
)
// Player is the player character entity.
type Player struct {
mapEntity
composite *d2asset.Composite
Equipment d2inventory.CharacterEquipment
Stats d2hero.HeroStatsState
Class d2enum.Hero
Id string
name string
// nameLabel d2ui.Label
lastPathSize int
isInTown bool
animationMode string
isRunToggled bool
isRunning bool
isCasting 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
// CreatePlayer creates a new player entity and returns a pointer to it.
func CreatePlayer(id, name string, x, y int, direction int, heroType d2enum.Hero, stats d2hero.HeroStatsState, equipment d2inventory.CharacterEquipment) *Player {
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(),
}
composite, err := d2asset.LoadComposite(d2enum.ObjectTypePlayer, heroType.GetToken(),
d2resource.PaletteUnits)
if err != nil {
panic(err)
}
stats.NextLevelExp = d2datadict.GetExperienceBreakpoint(heroType, stats.Level)
stats.Stamina = stats.MaxStamina
result := &Player{
Id: id,
mapEntity: createMapEntity(x, y),
composite: composite,
Equipment: equipment,
Stats: stats,
name: name,
Class: heroType,
//nameLabel: d2ui.CreateLabel(d2resource.FontFormal11, d2resource.PaletteStatic),
isRunToggled: true,
isInTown: true,
isRunning: true,
}
result.SetSpeed(baseRunSpeed)
result.mapEntity.directioner = result.rotate
//result.nameLabel.Alignment = d2ui.LabelAlignCenter
//result.nameLabel.SetText(name)
//result.nameLabel.Color = color.White
err = composite.SetMode(d2enum.AnimationModePlayerTownNeutral.String(), equipment.RightHand.GetWeaponClass())
if err != nil {
panic(err)
}
composite.SetDirection(direction)
composite.Equip(layerEquipment)
return result
}
// SetIsInTown sets a flag indicating that the player is in town.
func (p *Player) SetIsInTown(isInTown bool) {
p.isInTown = isInTown
}
// ToggleRunWalk sets a flag indicating whether the player is running.
func (p *Player) ToggleRunWalk() {
p.isRunToggled = !p.isRunToggled
}
// IsRunToggled returns true if the UI button to toggle running is,
// toggled i.e. not in it's default state.
func (p *Player) IsRunToggled() bool {
return p.isRunToggled
}
// IsRunning returns true if the player is currently
func (p *Player) IsRunning() bool {
return p.isRunning
}
// SetIsRunning alters the player speed and sets a flag indicating
// that the player is running.
func (p *Player) SetIsRunning(isRunning bool) {
p.isRunning = isRunning
if isRunning {
p.SetSpeed(baseRunSpeed)
} else {
p.SetSpeed(baseWalkSpeed)
}
}
// IsInTown returns true if the player is currently in town.
func (p Player) IsInTown() bool {
return p.isInTown
}
// Advance is called once per frame and processes a
// single game tick.
func (v *Player) Advance(tickTime float64) {
v.Step(tickTime)
if v.IsCasting() && v.composite.GetPlayedCount() >= 1 {
v.isCasting = false
v.SetAnimationMode(v.GetAnimationMode().String())
}
v.composite.Advance(tickTime)
if v.lastPathSize != len(v.path) {
v.lastPathSize = len(v.path)
}
if v.composite.GetAnimationMode() != v.animationMode {
v.animationMode = v.composite.GetAnimationMode()
}
}
// Render renders the animated composite for this entity.
func (v *Player) Render(target d2interface.Surface) {
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)
// v.nameLabel.X = v.offsetX
// v.nameLabel.Y = v.offsetY - 100
// v.nameLabel.Render(target)
}
// GetAnimationMode returns the current animation mode based on what the player is doing and where they are.
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
}
if v.IsCasting() {
return d2enum.AnimationModePlayerCast
}
return d2enum.AnimationModePlayerNeutral
}
// SetAnimationMode sets the animation mode for this entity's animated composite.
func (v *Player) SetAnimationMode(animationMode string) error {
return v.composite.SetMode(animationMode, v.composite.GetWeaponClass())
}
// rotate sets direction and changes animation
func (v *Player) rotate(direction int) {
newAnimationMode := v.GetAnimationMode().String()
if newAnimationMode != v.composite.GetAnimationMode() {
v.composite.SetMode(newAnimationMode, v.composite.GetWeaponClass())
}
if direction != v.composite.GetDirection() {
v.composite.SetDirection(direction)
}
}
// Name returns the player name.
func (v *Player) Name() string {
return v.name
}
// IsCasting returns true if
func (v *Player) IsCasting() bool {
return v.isCasting
}
// SetCasting sets a flag indicating the player is casting a skill and
// sets the animation mode to the casting animation.
func (v *Player) SetCasting() {
v.isCasting = true
v.SetAnimationMode(d2enum.AnimationModePlayerCast.String())
}
// Selectable returns true if the player is in town.
func (v *Player) Selectable() bool {
// Players are selectable when in town
return v.IsInTown()
}