1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-18 02:16:23 -05:00
OpenDiablo2/d2core/d2map/d2mapentity/object.go

137 lines
3.4 KiB
Go
Raw Normal View History

// Package d2mapentity implements map entities
remove d2asset singleton (#726) * export d2asset singleton * add *d2asset.AssetManager to d2app - d2app now has a reference to an asset manager which it will use for loading - added asset loader methods to the asset manager - functions in d2asset are now wrappers for asset manager methods * add asset manager reference to audio provider - d2app asset manager reference is now passed to audio provider - asset manager is created in main.go for now to pass into audio provider - CreateSoundEffect is now a method, no longer exported, uses the asset manager reference * d2app passes asset manager refence to map engine test * in d2asset, all calls to LoadFile replaced with call to Singleton.Loadfile * blizzard intro and credits screen - d2app passes reference to the asset manager to these screens * asset manager for d2map - adding MapStampFactory, takes an asset manager reference - embedded MapStampFactory into the MapEngine - LoadStamp is now a method of the MapStampFactory * d2asset: removed LoadFileStream, LoadFile, and FileExists * d2gui changes - singleton now has an asset manager reference - calls to d2asset loader functions removed - createButton is now a method of LayoutManager - moved LayoutEntry to its own file * map entity factory - Map engine has an embedded map entity factory - Map stamp factory gets a reference to the map engine's entity factory - Stamps are given a reference to the map engine entity factory when created - Character select gets a map entity factory - Embedded the stamp factory into the MapEngine * asset manager for d2ui - d2ui is passed an asset manager reference when created - all calls to d2asset loader functions in d2ui now refer to the asset manager - d2gamescreen gets a ui manager when created - help overlay is now passed a ui manager when created * d2gamescreen + d2player: asset manager references added an asset manager reference to - inventory panel + inventory grid - mini panel - game controls - help overlay - character select - main menu - select hero class - hero stats panel * Removed d2asset.LoadAnimation all references to this function have been replaced with calls to the asset manager method * adding asset to help overlay, bugfix for 4d59c91 * Removed d2asset.LoadFont and d2asset.LoadAnimationWithEffect all references to these have been replaced with calls to the asset manager methods * MapRenderer now gets an asset manager reference * removed d2asset.LoadPalette all references have been replaced with calls to an asset manager instance * merged d2object with d2mapentity d2object was only being used to create objects in the map, so the provider function is now a method of the map entity factory. calls to d2asset have been removed. * removed d2asset.LoadComposite all calls are now made to the asset manager method * removed d2asset singleton all singleton references have been removed, a single instance of the asset manager is passed around the entire app * rename Initialize to NewAssetManager
2020-09-12 16:51:30 -04:00
package d2mapentity
import (
2020-09-12 16:25:09 -04:00
"fmt"
"math/rand"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
"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"
)
// Object represents a composite of animations that can be projected onto the map.
type Object struct {
2020-09-12 16:25:09 -04:00
uuid string
Position d2vector.Position
composite *d2asset.Composite
highlight bool
// nameLabel d2ui.Label
objectRecord *d2records.ObjectDetailsRecord
drawLayer int
name string
}
// setMode changes the graphical mode of this animated entity
func (ob *Object) setMode(animationMode d2enum.ObjectAnimationMode, direction int, randomFrame bool) error {
err := ob.composite.SetMode(animationMode, "HTH")
if err != nil {
return err
}
ob.composite.SetDirection(direction)
ob.drawLayer = ob.objectRecord.OrderFlag[d2enum.ObjectAnimationModeNeutral]
// For objects their txt record entry overrides animationdata
speed := ob.objectRecord.FrameDelta[animationMode]
if speed != 0 {
ob.composite.SetAnimSpeed(speed)
}
frameCount := ob.objectRecord.FrameCount[animationMode]
if frameCount != 0 {
ob.composite.SetSubLoop(0, frameCount)
}
ob.composite.SetPlayLoop(ob.objectRecord.CycleAnimation[animationMode])
ob.composite.SetCurrentFrame(ob.objectRecord.StartFrame[animationMode])
if randomFrame {
n := rand.Intn(frameCount)
ob.composite.SetCurrentFrame(n)
}
return err
}
// ID returns the object uuid
func (ob *Object) ID() string {
return ob.uuid
}
// Highlight sets the entity highlighted flag to true.
func (ob *Object) Highlight() {
ob.highlight = true
}
2020-09-12 16:25:09 -04:00
// Selectable returns if the object is selectable or not
func (ob *Object) Selectable() bool {
mode := ob.composite.ObjectAnimationMode()
return ob.objectRecord.Selectable[mode]
}
// Render draws this animated entity onto the target
func (ob *Object) Render(target d2interface.Surface) {
renderOffset := ob.Position.RenderOffset()
target.PushTranslation(
int((renderOffset.X()-renderOffset.Y())*subtileWidth),
int(((renderOffset.X() + renderOffset.Y()) * subtileHeight)),
)
if ob.highlight {
target.PushBrightness(highlightBrightness)
defer target.Pop()
}
defer target.Pop()
2020-09-12 16:25:09 -04:00
if err := ob.composite.Render(target); err != nil {
fmt.Printf("failed to render composite animation, err: %v\n", err)
}
ob.highlight = false
}
// Advance updates the animation
func (ob *Object) Advance(elapsed float64) {
2020-09-12 16:25:09 -04:00
if err := ob.composite.Advance(elapsed); err != nil {
fmt.Printf("failed to advance composiste animation, err: %v\n", err)
}
}
// GetLayer returns which layer of the map the object is drawn
func (ob *Object) GetLayer() int {
return ob.drawLayer
}
// GetPositionF of the object but differently
func (ob *Object) GetPositionF() (x, y float64) {
w := ob.Position.World()
return w.X(), w.Y()
}
// Label gets the name of the object
func (ob *Object) Label() string {
return ob.name
}
// GetPosition returns the object's position
func (ob *Object) GetPosition() d2vector.Position {
return ob.Position
}
// GetVelocity returns the object's velocity vector
func (ob *Object) GetVelocity() d2vector.Vector {
return *d2vector.VectorZero()
}
// GetSize returns the current frame size
func (ob *Object) GetSize() (width, height int) {
return ob.composite.GetSize()
}