2020-07-04 00:48:31 -04:00
|
|
|
// Package d2object implements objects placed on the map and their functionality
|
|
|
|
package d2object
|
2020-06-24 13:49:13 -04:00
|
|
|
|
|
|
|
import (
|
2020-07-06 20:12:53 -04:00
|
|
|
"math/rand"
|
2020-07-04 00:48:31 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-06-24 13:49:13 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
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-07-03 22:52:50 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
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-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-07-03 10:38:22 -04:00
|
|
|
objectRecord *d2datadict.ObjectRecord
|
2020-07-04 00:48:31 -04:00
|
|
|
drawLayer int
|
|
|
|
name string
|
2020-06-24 13:49:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateObject creates an instance of AnimatedComposite
|
2020-07-03 10:38:22 -04:00
|
|
|
func CreateObject(x, y int, objectRec *d2datadict.ObjectRecord, palettePath string) (*Object, error) {
|
2020-07-04 00:48:31 -04:00
|
|
|
locX, locY := float64(x), float64(y)
|
2020-07-03 10:38:22 -04:00
|
|
|
entity := &Object{
|
|
|
|
objectRecord: objectRec,
|
2020-07-14 00:25:23 -04:00
|
|
|
Position: d2vector.NewPosition(locX, locY),
|
2020-07-04 00:48:31 -04:00
|
|
|
name: d2common.TranslateString(objectRec.Name),
|
2020-07-03 10:38:22 -04:00
|
|
|
}
|
2020-07-04 00:48:31 -04:00
|
|
|
objectType := &d2datadict.ObjectTypes[objectRec.Index]
|
2020-07-03 10:38:22 -04:00
|
|
|
|
2020-07-04 00:48:31 -04:00
|
|
|
composite, err := d2asset.LoadComposite(d2enum.ObjectTypeItem, objectType.Token,
|
2020-07-03 22:52:50 -04:00
|
|
|
d2resource.PaletteUnits)
|
2020-06-24 13:49:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-03 10:38:22 -04:00
|
|
|
entity.composite = composite
|
|
|
|
|
2020-07-09 23:12:28 -04:00
|
|
|
entity.setMode(d2enum.ObjectAnimationModeNeutral, 0, false)
|
2020-07-01 00:06:06 -04:00
|
|
|
|
2020-07-04 00:48:31 -04:00
|
|
|
initObject(entity)
|
2020-07-03 10:38:22 -04:00
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
return entity, nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 22:52:50 -04:00
|
|
|
// setMode changes the graphical mode of this animated entity
|
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
|
|
|
// mode := d2enum.ObjectAnimationModeFromString(animationMode)
|
|
|
|
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 {
|
|
|
|
n := rand.Intn(frameCount)
|
|
|
|
ob.composite.SetCurrentFrame(n)
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-14 00:25:23 -04:00
|
|
|
int((renderOffset.X()-renderOffset.Y())*16),
|
|
|
|
int(((renderOffset.X() + renderOffset.Y()) * 8)),
|
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 {
|
|
|
|
target.PushBrightness(2)
|
|
|
|
defer target.Pop()
|
|
|
|
}
|
2020-07-03 10:38:22 -04:00
|
|
|
|
2020-06-24 13:49:13 -04:00
|
|
|
defer target.Pop()
|
|
|
|
ob.composite.Render(target)
|
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) {
|
|
|
|
ob.composite.Advance(elapsed)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPosition of the object
|
|
|
|
func (ob *Object) GetPosition() (x, y float64) {
|
2020-07-14 00:25:23 -04:00
|
|
|
w := ob.Position.Tile()
|
|
|
|
return w.X(), w.Y()
|
2020-07-04 00:48:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// Name gets the name of the object
|
|
|
|
func (ob *Object) Name() string {
|
|
|
|
return ob.name
|
|
|
|
}
|