2020-10-22 02:41:21 -04:00
|
|
|
// Package d2mapentity implements map entities
|
2020-09-12 16:51:30 -04:00
|
|
|
package d2mapentity
|
2020-06-24 13:49:13 -04:00
|
|
|
|
|
|
|
import (
|
2020-09-12 16:25:09 -04:00
|
|
|
"fmt"
|
2020-07-06 20:12:53 -04:00
|
|
|
"math/rand"
|
2020-07-04 00:48:31 -04:00
|
|
|
|
2020-09-20 20:30:27 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
|
|
|
|
|
2020-06-27 15:44:28 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-07-14 00:25:23 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
2020-06-24 13:49:13 -04:00
|
|
|
"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
|
2020-07-14 00:25:23 -04:00
|
|
|
Position d2vector.Position
|
|
|
|
composite *d2asset.Composite
|
|
|
|
highlight bool
|
2020-07-03 14:00:56 -04:00
|
|
|
// nameLabel d2ui.Label
|
2020-12-18 13:02:34 -05:00
|
|
|
objectRecord *d2records.ObjectDetailRecord
|
2020-07-04 00:48:31 -04:00
|
|
|
drawLayer int
|
|
|
|
name string
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-03 22:52:50 -04:00
|
|
|
// setMode changes the graphical mode of this animated entity
|
2020-10-26 05:04:50 -04:00
|
|
|
// nolint:unparam // direction may not always be passed 0 in the future
|
2020-07-09 23:12:28 -04:00
|
|
|
func (ob *Object) setMode(animationMode d2enum.ObjectAnimationMode, direction int, randomFrame bool) error {
|
2020-07-03 22:52:50 -04:00
|
|
|
err := ob.composite.SetMode(animationMode, "HTH")
|
2020-06-24 13:49:13 -04:00
|
|
|
if err != nil {
|
2020-07-03 22:52:50 -04:00
|
|
|
return err
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
2020-06-27 18:58:41 -04:00
|
|
|
|
2020-07-03 22:52:50 -04:00
|
|
|
ob.composite.SetDirection(direction)
|
|
|
|
|
2020-07-09 23:12:28 -04:00
|
|
|
ob.drawLayer = ob.objectRecord.OrderFlag[d2enum.ObjectAnimationModeNeutral]
|
2020-06-27 15:44:28 -04:00
|
|
|
|
2020-06-27 14:30:23 -04:00
|
|
|
// For objects their txt record entry overrides animationdata
|
2020-07-09 23:12:28 -04:00
|
|
|
speed := ob.objectRecord.FrameDelta[animationMode]
|
2020-06-27 15:44:28 -04:00
|
|
|
if speed != 0 {
|
2020-07-03 22:52:50 -04:00
|
|
|
ob.composite.SetAnimSpeed(speed)
|
2020-06-27 15:44:28 -04:00
|
|
|
}
|
2020-06-27 18:58:41 -04:00
|
|
|
|
2020-07-09 23:12:28 -04:00
|
|
|
frameCount := ob.objectRecord.FrameCount[animationMode]
|
2020-07-06 20:12:53 -04:00
|
|
|
|
|
|
|
if frameCount != 0 {
|
|
|
|
ob.composite.SetSubLoop(0, frameCount)
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:12:28 -04:00
|
|
|
ob.composite.SetPlayLoop(ob.objectRecord.CycleAnimation[animationMode])
|
|
|
|
ob.composite.SetCurrentFrame(ob.objectRecord.StartFrame[animationMode])
|
2020-07-06 20:12:53 -04:00
|
|
|
|
|
|
|
if randomFrame {
|
2020-10-26 02:38:15 -04:00
|
|
|
// nolint:gosec // not concerned with crypto-strong randomness
|
2020-07-06 20:12:53 -04:00
|
|
|
n := rand.Intn(frameCount)
|
|
|
|
ob.composite.SetCurrentFrame(n)
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-05 21:27:45 -04:00
|
|
|
// ID returns the object uuid
|
|
|
|
func (ob *Object) ID() string {
|
|
|
|
return ob.uuid
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Highlight sets the entity highlighted flag to true.
|
2020-06-27 18:58:41 -04:00
|
|
|
func (ob *Object) Highlight() {
|
|
|
|
ob.highlight = true
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
// Selectable returns if the object is selectable or not
|
2020-06-27 18:58:41 -04:00
|
|
|
func (ob *Object) Selectable() bool {
|
2020-07-09 23:12:28 -04:00
|
|
|
mode := ob.composite.ObjectAnimationMode()
|
2020-06-27 18:58:41 -04:00
|
|
|
return ob.objectRecord.Selectable[mode]
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
// Render draws this animated entity onto the target
|
2020-06-29 00:41:58 -04:00
|
|
|
func (ob *Object) Render(target d2interface.Surface) {
|
2020-07-14 00:25:23 -04:00
|
|
|
renderOffset := ob.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),
|
2020-10-26 02:38:15 -04:00
|
|
|
int((renderOffset.X()+renderOffset.Y())*subtileHeight),
|
2020-06-24 13:49:13 -04:00
|
|
|
)
|
2020-07-03 10:38:22 -04:00
|
|
|
|
2020-06-27 18:58:41 -04:00
|
|
|
if ob.highlight {
|
2020-10-25 03:42:31 -04:00
|
|
|
target.PushBrightness(highlightBrightness)
|
2020-06-27 18:58:41 -04:00
|
|
|
defer target.Pop()
|
|
|
|
}
|
2020-07-03 10:38:22 -04:00
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-06-27 18:58:41 -04:00
|
|
|
ob.highlight = false
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-04 00:48:31 -04:00
|
|
|
// Advance updates the animation
|
2020-06-24 13:49:13 -04:00
|
|
|
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)
|
|
|
|
}
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
2020-07-04 00:48:31 -04:00
|
|
|
|
|
|
|
// 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) {
|
2020-07-14 00:25:23 -04:00
|
|
|
w := ob.Position.World()
|
|
|
|
return w.X(), w.Y()
|
2020-07-04 00:48:31 -04:00
|
|
|
}
|
|
|
|
|
2020-08-02 21:26:07 -04:00
|
|
|
// Label gets the name of the object
|
|
|
|
func (ob *Object) Label() string {
|
2020-07-04 00:48:31 -04:00
|
|
|
return ob.name
|
|
|
|
}
|
2020-07-21 08:51:09 -04:00
|
|
|
|
|
|
|
// 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 {
|
2020-07-25 09:36:54 -04:00
|
|
|
return *d2vector.VectorZero()
|
2020-07-21 08:51:09 -04:00
|
|
|
}
|
2020-08-01 19:03:09 -04:00
|
|
|
|
|
|
|
// GetSize returns the current frame size
|
|
|
|
func (ob *Object) GetSize() (width, height int) {
|
|
|
|
return ob.composite.GetSize()
|
|
|
|
}
|