1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-13 15:56:03 -05:00
OpenDiablo2/d2core/d2map/d2mapentity/player.go

263 lines
7.4 KiB
Go
Raw Normal View History

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"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2inventory"
2019-11-14 22:20:01 -05:00
)
// Player is the player character entity.
type Player struct {
mapEntity
name string
animationMode string
composite *d2asset.Composite
Equipment *d2inventory.CharacterEquipment
Stats *d2hero.HeroStatsState
Removing d2datadict singletons (#738) * Remove weapons, armor, misc, itemCommon, itemTyps datadict singletons - removed loader calls from d2app - removed the HeroObjects singleton from `d2core/d2inventory` - added an InventoryItemFactory in d2inventory - package-level functions that use data records are now methods of the InventoryItemFactory - renamed ItemGenerator in d2item to ItemFactory - package-level functions that use records are now methods of ItemFactory - d2map.MapEntityFactory now has an item factory instance for creating items - fixed a bug in unique item record loader where it loaded an empty record - added a PlayerStateFactory for creating a player state (uses the asset manager) - updated the test inventory/equipment code in d2player to handle errors from the ItemFactory - character select and character creation screens have a player state and inventory item factory - updated item tests to use the item factory * minor edit * Removed d2datadict.Experience singleton added a HeroStatsFactory, much like the other factories. The factory gets an asset manager reference in order to use data records. * removed d2datadict.AutoMagic singleton * removed d2datadict.AutoMap singleton * removed d2datadict.BodyLocations singleton * removed d2datadict.Books singleton * Removed singletons for level records - removed loader calls in d2app - changed type references from d2datadict to d2records - added a `MapGenerator` in d2mapgen which uses thew asset manager and map engine - package-level map generation functions are now MapGenerator methods - `d2datadict.GetLevelDetails(id int)` is now a method of the RecordManager * remove SkillCalc and MissileCalc singletons * Removed CharStats and ItemStatCost singletons - added an ItemStatFactory which uses the asset manager to create stats - package-level functions for stats in d2item are now StatFactory methods - changed type references from d2datadict to d2records - `d2player.GetAllPlayerStates` is now a method of the `PlayerStateFactory` * Removed DkillDesc and Skills singletons from d2datadict - removed loader calls from d2app - diablo2stats.Stat instances are given a reference to the factory for doing record lookups * update the stats test to use mock a asset manager and stat factory * fixed diablo2stats tests and diablo2item tests * removed CompCodes singleton from d2datadict * remove cubemain singleton from d2datadict * removed DifficultyLevels singleton from d2datadict * removed ElemTypes singleton from d2datadict * removed events.go loader from d2datadict (was unused) * removed Gems singleton from d2datadict * removed Hireling and Inventory singletons from d2datadict * removed MagicPrefix and MagicSuffix singletons from d2datadict * removed ItemRatios singleton from d2datadict * removed Missiles singleton from d2datadict * removed MonModes singleton * Removed all monster and npc singletons from d2datadict - MapStamp instances now get a reference to their factory for doing record lookups * removed SoundEntry and SoundEnviron singletons from d2datadict
2020-09-20 17:52:01 -04:00
Skills map[int]*d2hero.HeroSkill
LeftSkill *d2hero.HeroSkill
RightSkill *d2hero.HeroSkill
Class d2enum.Hero
lastPathSize int
isInTown bool
isRunToggled bool
isRunning bool
isCasting bool
onFinishedCasting func()
2019-11-14 22:20:01 -05:00
}
// run speed should be walkspeed * 1.5, since in the original game it is 6 yards walk and 9 yards run.
const baseWalkSpeed = 6.0
const baseRunSpeed = 9.0
// ID returns the Player uuid
func (p *Player) ID() string {
return p.mapEntity.uuid
}
// 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.
2020-07-23 12:56:50 -04:00
func (p *Player) IsInTown() bool {
return p.isInTown
}
const (
half = 0.5
)
// 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)
if err := p.SetAnimationMode(p.GetAnimationMode()); err != nil {
fmt.Printf("failed to set animationMode to: %d, err: %v\n", p.GetAnimationMode(), err)
}
if p.IsCasting() {
if p.composite.GetPlayedCount() >= 1 {
p.isCasting = false
}
// skills are casted after the first half of the casting animation is played
percentDone := float64(p.composite.GetCurrentFrame()) / float64(p.composite.GetFrameCount())
isHalfDoneCasting := percentDone >= half
if isHalfDoneCasting && p.onFinishedCasting != nil {
p.onFinishedCasting()
p.onFinishedCasting = nil
2020-07-23 12:56:50 -04:00
}
}
2020-07-23 12:56:50 -04:00
if err := p.composite.Advance(tickTime); err != nil {
fmt.Printf("failed to advance composite animation of player: %s, err: %v\n", p.ID(), err)
}
2020-07-23 12:56:50 -04:00
if p.lastPathSize != len(p.path) {
p.lastPathSize = len(p.path)
}
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()
}
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
// Drain and regenerate Stamina
if p.IsRunning() && !p.atTarget() && !p.IsInTown() {
p.Stats.Stamina -= staminaDrain * tickTime / magicStaminaDrainDivisor
if p.Stats.Stamina <= 0 {
p.SetSpeed(baseWalkSpeed)
p.Stats.Stamina = 0
}
} else if p.Stats.Stamina < float64(p.Stats.MaxStamina) {
p.Stats.Stamina += staminaDrain * tickTime / magicStaminaDrainDivisor
if p.IsRunning() {
p.SetSpeed(baseRunSpeed)
}
}
}
// 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()
target.PushTranslation(
int((renderOffset.X()-renderOffset.Y())*subtileWidth),
int(((renderOffset.X()+renderOffset.Y())*subtileHeight)+subtileOffsetY),
)
defer target.Pop()
2020-07-23 12:56:50 -04:00
if err := p.composite.Render(target); err != nil {
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
}
// 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() {
return d2enum.PlayerAnimationModeRun
}
2020-07-23 12:56:50 -04:00
if p.IsInTown() {
if !p.atTarget() {
return d2enum.PlayerAnimationModeTownWalk
}
return d2enum.PlayerAnimationModeTownNeutral
}
2020-07-23 12:56:50 -04:00
if !p.atTarget() {
return d2enum.PlayerAnimationModeWalk
}
2020-07-23 12:56:50 -04:00
if p.IsCasting() {
return d2enum.PlayerAnimationModeCast
}
return d2enum.PlayerAnimationModeNeutral
}
// 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())
}
// rotate sets direction and changes animation
2020-07-23 12:56:50 -04:00
func (p *Player) rotate(direction int) {
newAnimationMode := p.GetAnimationMode()
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-23 12:56:50 -04:00
if direction != p.composite.GetDirection() {
p.composite.SetDirection(direction)
}
}
// SetDirection will rotate the player and change the animation
func (p *Player) SetDirection(direction int) {
p.rotate(direction)
}
// Name returns the player name.
2020-07-23 12:56:50 -04:00
func (p *Player) Name() string {
return p.name
}
// IsCasting returns true if
2020-07-23 12:56:50 -04:00
func (p *Player) IsCasting() bool {
return p.isCasting
}
// StartCasting sets a flag indicating the player is casting a skill and
// sets the animation mode to the casting animation.
// This handles all types of skills - melee, ranged, kick, summon, etc.
// NB: onFinishedCasting is called when the casting animation is >50% complete
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
p.onFinishedCasting = onFinishedCasting
if err := p.SetAnimationMode(animMode); err != nil {
fmtStr := "failed to set animationMode of player: %s to: %d, err: %v\n"
fmt.Printf(fmtStr, p.ID(), animMode, err)
2020-07-23 12:56:50 -04:00
}
}
// Selectable returns true if the player is in town.
2020-07-23 12:56:50 -04:00
func (p *Player) Selectable() bool {
// Players are selectable when in town
2020-07-23 12:56:50 -04:00
return p.IsInTown()
}
// 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
}
// GetSize returns the current frame size
func (p *Player) GetSize() (width, height int) {
width, height = p.composite.GetSize()
// https://github.com/OpenDiablo2/OpenDiablo2/issues/820
height = (height * 2) - (height / 2)
return width, height
}