1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-10 06:16:27 -05:00
OpenDiablo2/d2core/d2map/d2mapentity/object.go
2020-07-03 14:00:56 -04:00

137 lines
3.5 KiB
Go

package d2mapentity
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
)
// Object represents a composite of animations that can be projected onto the map.
type Object struct {
mapEntity
composite *d2asset.Composite
direction int
highlight bool
// nameLabel d2ui.Label
objectLookup *d2datadict.ObjectLookupRecord
objectRecord *d2datadict.ObjectRecord
objectType *d2datadict.ObjectTypeRecord
}
// CreateObject creates an instance of AnimatedComposite
func CreateObject(x, y int, objectRec *d2datadict.ObjectRecord, palettePath string) (*Object, error) {
entity := &Object{
mapEntity: createMapEntity(x, y),
objectRecord: objectRec,
objectType: &d2datadict.ObjectTypes[objectRec.Index],
// nameLabel: d2ui.CreateLabel(renderer, d2resource.FontFormal11, d2resource.PaletteStatic),
}
object := &d2datadict.ObjectLookupRecord{
Base: "/Data/Global/Objects",
Token: entity.objectType.Token,
Mode: "NU",
Class: "HTH",
HD: "LIT",
TR: "LIT",
LG: "LIT",
RA: "LIT",
LA: "LIT",
RH: "LIT",
LH: "LIT",
SH: "LIT",
S1: "LIT",
S2: "LIT",
S3: "LIT",
S4: "LIT",
S5: "LIT",
S6: "LIT",
S7: "LIT",
S8: "LIT",
}
entity.objectLookup = object
composite, err := d2asset.LoadComposite(object, palettePath)
if err != nil {
return nil, err
}
entity.composite = composite
entity.mapEntity.directioner = entity.rotate
entity.drawLayer = entity.objectRecord.OrderFlag[d2enum.AnimationModeObjectNeutral]
entity.SetMode(object.Mode, object.Class, 0)
// stop torches going crazy for now
// need initFunc handling to set objects up properly
if objectRec.HasAnimationMode[d2enum.AnimationModeObjectOpened] {
entity.SetMode("ON", "HTH", 0)
}
return entity, nil
}
// SetMode changes the graphical mode of this animated entity
func (ob *Object) SetMode(animationMode, weaponClass string, direction int) error {
ob.composite.SetMode(animationMode, weaponClass, direction)
ob.direction = direction
ob.weaponClass = weaponClass
err := ob.composite.SetMode(animationMode, weaponClass, direction)
if err != nil {
err = ob.composite.SetMode(animationMode, "HTH", direction)
ob.weaponClass = "HTH"
}
mode := d2enum.ObjectAnimationModeFromString(animationMode)
ob.mapEntity.drawLayer = ob.objectRecord.OrderFlag[mode]
// For objects their txt record entry overrides animationdata
speed := ob.objectRecord.FrameDelta[mode]
if speed != 0 {
ob.composite.SetSpeed(speed)
}
return err
}
func (ob *Object) Highlight() {
ob.highlight = true
}
func (ob *Object) Selectable() bool {
mode := d2enum.ObjectAnimationModeFromString(ob.composite.GetAnimationMode())
return ob.objectRecord.Selectable[mode]
}
// Render draws this animated entity onto the target
func (ob *Object) Render(target d2interface.Surface) {
target.PushTranslation(
ob.offsetX+int((ob.subcellX-ob.subcellY)*16),
ob.offsetY+int(((ob.subcellX+ob.subcellY)*8)-5),
)
if ob.highlight {
// ob.nameLabel.SetText(d2common.TranslateString(ob.objectRecord.Name))
// ob.nameLabel.SetPosition(-50, -50)
// ob.nameLabel.Render(target)
target.PushBrightness(2)
defer target.Pop()
}
defer target.Pop()
ob.composite.Render(target)
ob.highlight = false
}
// rotate sets direction and changes animation
func (ob *Object) rotate(direction int) {
}
func (ob *Object) Advance(elapsed float64) {
ob.composite.Advance(elapsed)
}