2019-11-10 03:36:53 -05:00
|
|
|
package d2render
|
2019-11-06 18:25:19 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-17 18:23:21 -05:00
|
|
|
"image"
|
2019-12-13 00:33:11 -05:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-06 23:58:36 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2enum"
|
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2interface"
|
2019-11-17 16:06:02 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data"
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data/d2cof"
|
2019-12-06 23:58:36 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data/d2datadict"
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data/d2dcc"
|
|
|
|
"github.com/OpenDiablo2/D2Shared/d2helper"
|
2019-11-06 18:25:19 -05:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
)
|
|
|
|
|
2019-11-09 23:37:02 -05:00
|
|
|
var DccLayerNames = []string{"HD", "TR", "LG", "RA", "LA", "RH", "LH", "SH", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8"}
|
|
|
|
|
2019-11-25 20:05:18 -05:00
|
|
|
type LayerCacheEntry struct {
|
2019-12-17 18:23:21 -05:00
|
|
|
frameSheet *ebiten.Image
|
|
|
|
frameWidth int
|
|
|
|
frameHeight int
|
2019-11-25 20:05:18 -05:00
|
|
|
compositeMode ebiten.CompositeMode
|
|
|
|
offsetX, offsetY int32
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// AnimatedEntity represents an entity on the map that can be animated
|
2019-11-06 18:25:19 -05:00
|
|
|
type AnimatedEntity struct {
|
2019-12-06 09:44:52 -05:00
|
|
|
fileProvider d2interface.FileProvider
|
|
|
|
LocationX float64
|
2019-11-13 20:31:47 -05:00
|
|
|
LocationY float64
|
2019-12-06 09:44:52 -05:00
|
|
|
TileX, TileY int // Coordinates of the tile the unit is within
|
|
|
|
subcellX, subcellY float64 // Subcell coordinates within the current tile
|
2019-11-13 20:31:47 -05:00
|
|
|
dccLayers map[string]d2dcc.DCC
|
|
|
|
Cof *d2cof.COF
|
|
|
|
palette d2enum.PaletteType
|
|
|
|
base string
|
|
|
|
token string
|
|
|
|
animationMode string
|
|
|
|
weaponClass string
|
2019-11-15 13:40:26 -05:00
|
|
|
lastFrameTime float64
|
2019-11-13 20:31:47 -05:00
|
|
|
framesToAnimate int
|
2019-11-15 13:40:26 -05:00
|
|
|
animationSpeed float64
|
2019-11-13 20:31:47 -05:00
|
|
|
direction int
|
|
|
|
currentFrame int
|
2019-11-17 16:06:02 -05:00
|
|
|
offsetX, offsetY int32
|
2019-12-17 18:23:21 -05:00
|
|
|
object *d2datadict.ObjectLookupRecord
|
|
|
|
layerCache []LayerCacheEntry
|
|
|
|
drawOrder [][]d2enum.CompositeType
|
|
|
|
TargetX float64
|
|
|
|
TargetY float64
|
|
|
|
action int32
|
|
|
|
repetitions int32
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// CreateAnimatedEntity creates an instance of AnimatedEntity
|
2019-11-11 23:48:55 -05:00
|
|
|
func CreateAnimatedEntity(x, y int32, object *d2datadict.ObjectLookupRecord, fileProvider d2interface.FileProvider, palette d2enum.PaletteType) AnimatedEntity {
|
2019-11-10 10:44:13 -05:00
|
|
|
result := AnimatedEntity{
|
2019-11-14 22:20:01 -05:00
|
|
|
fileProvider: fileProvider,
|
|
|
|
base: object.Base,
|
|
|
|
token: object.Token,
|
|
|
|
object: object,
|
|
|
|
palette: palette,
|
2019-11-26 21:04:36 -05:00
|
|
|
layerCache: make([]LayerCacheEntry, d2enum.CompositeTypeMax),
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-10 10:44:13 -05:00
|
|
|
result.dccLayers = make(map[string]d2dcc.DCC)
|
2019-12-06 09:44:52 -05:00
|
|
|
result.LocationX = float64(x)
|
|
|
|
result.LocationY = float64(y)
|
2019-12-02 16:55:48 -05:00
|
|
|
result.TargetX = result.LocationX
|
|
|
|
result.TargetY = result.LocationY
|
2019-11-13 20:31:47 -05:00
|
|
|
|
2019-12-06 09:44:52 -05:00
|
|
|
result.TileX = int(result.LocationX / 5)
|
|
|
|
result.TileY = int(result.LocationY / 5)
|
|
|
|
result.subcellX = 1 + math.Mod(result.LocationX, 5)
|
|
|
|
result.subcellY = 1 + math.Mod(result.LocationY, 5)
|
2019-11-13 20:31:47 -05:00
|
|
|
|
2019-11-06 18:25:19 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// SetMode changes the graphical mode of this animated entity
|
2019-11-14 22:20:01 -05:00
|
|
|
func (v *AnimatedEntity) SetMode(animationMode, weaponClass string, direction int) {
|
2019-11-10 10:44:13 -05:00
|
|
|
cofPath := fmt.Sprintf("%s/%s/COF/%s%s%s.COF", v.base, v.token, v.token, animationMode, weaponClass)
|
2019-11-14 22:20:01 -05:00
|
|
|
v.Cof = d2cof.LoadCOF(cofPath, v.fileProvider)
|
2019-11-26 21:04:36 -05:00
|
|
|
if v.Cof.NumberOfDirections == 0 || v.Cof.NumberOfLayers == 0 || v.Cof.FramesPerDirection == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-12-09 17:25:29 -05:00
|
|
|
resetAnimation := v.animationMode != animationMode || v.weaponClass != weaponClass
|
2019-11-06 18:25:19 -05:00
|
|
|
v.animationMode = animationMode
|
|
|
|
v.weaponClass = weaponClass
|
|
|
|
v.direction = direction
|
2019-11-09 23:37:02 -05:00
|
|
|
if v.direction >= v.Cof.NumberOfDirections {
|
|
|
|
v.direction = v.Cof.NumberOfDirections - 1
|
|
|
|
}
|
2019-11-10 10:44:13 -05:00
|
|
|
v.dccLayers = make(map[string]d2dcc.DCC)
|
2019-11-09 23:37:02 -05:00
|
|
|
for _, cofLayer := range v.Cof.CofLayers {
|
|
|
|
layerName := DccLayerNames[cofLayer.Type]
|
2019-11-14 22:20:01 -05:00
|
|
|
v.dccLayers[layerName] = v.LoadLayer(layerName, v.fileProvider)
|
2019-11-10 10:44:13 -05:00
|
|
|
if !v.dccLayers[layerName].IsValid() {
|
2019-11-09 23:37:02 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 17:25:29 -05:00
|
|
|
v.updateFrameCache(resetAnimation)
|
2019-11-09 23:37:02 -05:00
|
|
|
}
|
|
|
|
|
2019-11-10 10:44:13 -05:00
|
|
|
func (v *AnimatedEntity) LoadLayer(layer string, fileProvider d2interface.FileProvider) d2dcc.DCC {
|
2019-11-14 22:20:01 -05:00
|
|
|
layerName := "TR"
|
2019-11-09 23:37:02 -05:00
|
|
|
switch strings.ToUpper(layer) {
|
|
|
|
case "HD": // Head
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.HD
|
2019-11-09 23:37:02 -05:00
|
|
|
case "TR": // Torso
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.TR
|
2019-11-09 23:37:02 -05:00
|
|
|
case "LG": // Legs
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.LG
|
2019-11-09 23:37:02 -05:00
|
|
|
case "RA": // RightArm
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.RA
|
2019-11-09 23:37:02 -05:00
|
|
|
case "LA": // LeftArm
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.LA
|
2019-11-09 23:37:02 -05:00
|
|
|
case "RH": // RightHand
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.RH
|
2019-11-09 23:37:02 -05:00
|
|
|
case "LH": // LeftHand
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.LH
|
2019-11-09 23:37:02 -05:00
|
|
|
case "SH": // Shield
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.SH
|
2019-11-09 23:37:02 -05:00
|
|
|
case "S1": // Special1
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.S1
|
2019-11-09 23:37:02 -05:00
|
|
|
case "S2": // Special2
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.S2
|
2019-11-09 23:37:02 -05:00
|
|
|
case "S3": // Special3
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.S3
|
2019-11-09 23:37:02 -05:00
|
|
|
case "S4": // Special4
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.S4
|
2019-11-09 23:37:02 -05:00
|
|
|
case "S5": // Special5
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.S5
|
2019-11-09 23:37:02 -05:00
|
|
|
case "S6": // Special6
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.S6
|
2019-11-09 23:37:02 -05:00
|
|
|
case "S7": // Special7
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.S7
|
2019-11-09 23:37:02 -05:00
|
|
|
case "S8": // Special8
|
2019-11-11 23:48:55 -05:00
|
|
|
layerName = v.object.S8
|
2019-11-09 23:37:02 -05:00
|
|
|
}
|
|
|
|
if len(layerName) == 0 {
|
2019-11-10 10:44:13 -05:00
|
|
|
return d2dcc.DCC{}
|
2019-11-09 23:37:02 -05:00
|
|
|
}
|
|
|
|
dccPath := fmt.Sprintf("%s/%s/%s/%s%s%s%s%s.dcc", v.base, v.token, layer, v.token, layer, layerName, v.animationMode, v.weaponClass)
|
2019-11-14 22:20:01 -05:00
|
|
|
result := d2dcc.LoadDCC(dccPath, fileProvider)
|
|
|
|
if !result.IsValid() {
|
|
|
|
dccPath = fmt.Sprintf("%s/%s/%s/%s%s%s%s%s.dcc", v.base, v.token, layer, v.token, layer, layerName, v.animationMode, "HTH")
|
|
|
|
result = d2dcc.LoadDCC(dccPath, fileProvider)
|
|
|
|
}
|
|
|
|
return result
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-12-06 23:58:36 -05:00
|
|
|
// If an npc has a path to pause at each location.
|
|
|
|
// Waits for animation to end and all repetitions to be exhausted.
|
|
|
|
func (v AnimatedEntity) Wait() bool {
|
|
|
|
// currentFrame might skip the final frame if framesToAdd doesn't match up,
|
|
|
|
// bail immediately after the last repetition if that happens.
|
|
|
|
return v.repetitions < 0 || (v.repetitions == 0 && v.currentFrame >= v.framesToAnimate-1)
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// Render draws this animated entity onto the target
|
2019-11-06 18:25:19 -05:00
|
|
|
func (v *AnimatedEntity) Render(target *ebiten.Image, offsetX, offsetY int) {
|
2019-11-13 15:08:09 -05:00
|
|
|
if v.animationSpeed > 0 {
|
2019-11-15 13:40:26 -05:00
|
|
|
now := d2helper.Now()
|
|
|
|
framesToAdd := math.Floor((now - v.lastFrameTime) / v.animationSpeed)
|
|
|
|
if framesToAdd > 0 {
|
|
|
|
v.lastFrameTime += v.animationSpeed * framesToAdd
|
|
|
|
v.currentFrame += int(math.Floor(framesToAdd))
|
|
|
|
for v.currentFrame >= v.framesToAnimate {
|
|
|
|
v.currentFrame -= v.framesToAnimate
|
2019-12-06 23:58:36 -05:00
|
|
|
v.repetitions = d2helper.MinInt32(-1, v.repetitions-1)
|
2019-11-13 15:08:09 -05:00
|
|
|
}
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 21:04:36 -05:00
|
|
|
|
2019-11-17 16:06:02 -05:00
|
|
|
localX := (v.subcellX - v.subcellY) * 16
|
|
|
|
localY := ((v.subcellX + v.subcellY) * 8) - 5
|
2019-11-25 20:05:18 -05:00
|
|
|
|
2019-11-26 21:04:36 -05:00
|
|
|
if v.drawOrder == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, layerIdx := range v.drawOrder[v.currentFrame] {
|
2019-12-17 18:23:21 -05:00
|
|
|
if v.currentFrame < 0 || v.layerCache[layerIdx].frameSheet == nil || v.currentFrame >= v.framesToAnimate {
|
2019-11-26 21:04:36 -05:00
|
|
|
continue
|
2019-11-25 20:05:18 -05:00
|
|
|
}
|
|
|
|
opts := &ebiten.DrawImageOptions{}
|
2019-12-17 18:23:21 -05:00
|
|
|
layer := v.layerCache[layerIdx]
|
2019-11-25 20:05:18 -05:00
|
|
|
x := float64(v.offsetX) + float64(offsetX) + localX + float64(v.layerCache[layerIdx].offsetX)
|
|
|
|
y := float64(v.offsetY) + float64(offsetY) + localY + float64(v.layerCache[layerIdx].offsetY)
|
|
|
|
opts.GeoM.Translate(x, y)
|
|
|
|
opts.CompositeMode = v.layerCache[layerIdx].compositeMode
|
2019-12-17 18:23:21 -05:00
|
|
|
xOffset := layer.frameWidth * v.currentFrame
|
|
|
|
sheetIndex := image.Rect(xOffset, 0, xOffset+layer.frameWidth, layer.frameHeight)
|
|
|
|
if err := target.DrawImage(layer.frameSheet.SubImage(sheetIndex).(*ebiten.Image), opts); err != nil {
|
2019-11-25 20:05:18 -05:00
|
|
|
log.Panic(err.Error())
|
|
|
|
}
|
2019-11-09 23:37:02 -05:00
|
|
|
}
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-12-09 17:25:29 -05:00
|
|
|
func (v *AnimatedEntity) updateFrameCache(resetAnimation bool) {
|
|
|
|
if resetAnimation {
|
|
|
|
v.currentFrame = 0
|
|
|
|
}
|
2019-11-17 16:06:02 -05:00
|
|
|
// TODO: This animation data madness is incorrect, yet tasty
|
|
|
|
animDataTemp := d2data.AnimationData[strings.ToLower(v.token+v.animationMode+v.weaponClass)]
|
|
|
|
if animDataTemp == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
animationData := animDataTemp[0]
|
2019-11-15 13:40:26 -05:00
|
|
|
v.animationSpeed = 1.0 / ((float64(animationData.AnimationSpeed) * 25.0) / 256.0)
|
2019-11-06 18:25:19 -05:00
|
|
|
v.framesToAnimate = animationData.FramesPerDirection
|
2019-11-15 13:40:26 -05:00
|
|
|
v.lastFrameTime = d2helper.Now()
|
2019-11-25 20:05:18 -05:00
|
|
|
|
2019-11-26 21:04:36 -05:00
|
|
|
v.drawOrder = make([][]d2enum.CompositeType, v.framesToAnimate)
|
|
|
|
|
|
|
|
var dccDirection int
|
|
|
|
switch v.Cof.NumberOfDirections {
|
|
|
|
case 4:
|
|
|
|
dccDirection = d2dcc.CofToDir4[v.direction]
|
|
|
|
case 8:
|
|
|
|
dccDirection = d2dcc.CofToDir8[v.direction]
|
|
|
|
case 16:
|
|
|
|
dccDirection = d2dcc.CofToDir16[v.direction]
|
|
|
|
case 32:
|
|
|
|
dccDirection = d2dcc.CofToDir32[v.direction]
|
|
|
|
default:
|
|
|
|
dccDirection = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for frame := 0; frame < v.framesToAnimate; frame++ {
|
|
|
|
v.drawOrder[frame] = v.Cof.Priority[v.direction][frame]
|
2019-11-25 20:05:18 -05:00
|
|
|
}
|
|
|
|
|
2019-11-17 16:06:02 -05:00
|
|
|
for cofLayerIdx := range v.Cof.CofLayers {
|
2019-11-26 21:04:36 -05:00
|
|
|
layerType := v.Cof.CofLayers[cofLayerIdx].Type
|
|
|
|
layerName := DccLayerNames[layerType]
|
2019-11-17 16:06:02 -05:00
|
|
|
dccLayer := v.dccLayers[layerName]
|
|
|
|
if !dccLayer.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-25 20:05:18 -05:00
|
|
|
|
|
|
|
minX := int32(10000)
|
|
|
|
minY := int32(10000)
|
|
|
|
maxX := int32(-10000)
|
|
|
|
maxY := int32(-10000)
|
2019-11-26 21:04:36 -05:00
|
|
|
for frameIdx := range dccLayer.Directions[dccDirection].Frames {
|
|
|
|
minX = d2helper.MinInt32(minX, int32(dccLayer.Directions[dccDirection].Frames[frameIdx].Box.Left))
|
|
|
|
minY = d2helper.MinInt32(minY, int32(dccLayer.Directions[dccDirection].Frames[frameIdx].Box.Top))
|
|
|
|
maxX = d2helper.MaxInt32(maxX, int32(dccLayer.Directions[dccDirection].Frames[frameIdx].Box.Right()))
|
|
|
|
maxY = d2helper.MaxInt32(maxY, int32(dccLayer.Directions[dccDirection].Frames[frameIdx].Box.Bottom()))
|
2019-11-17 16:06:02 -05:00
|
|
|
}
|
2019-11-25 20:05:18 -05:00
|
|
|
|
2019-11-26 21:04:36 -05:00
|
|
|
v.layerCache[layerType].offsetX = minX
|
|
|
|
v.layerCache[layerType].offsetY = minY
|
2019-11-25 20:05:18 -05:00
|
|
|
actualWidth := maxX - minX
|
|
|
|
actualHeight := maxY - minY
|
|
|
|
|
|
|
|
if (actualWidth <= 0) || (actualHeight < 0) {
|
|
|
|
log.Printf("Animated entity created with an invalid size of (%d, %d)", actualWidth, actualHeight)
|
|
|
|
return
|
2019-11-17 16:06:02 -05:00
|
|
|
}
|
2019-11-25 20:05:18 -05:00
|
|
|
|
|
|
|
transparency := byte(255)
|
|
|
|
if v.Cof.CofLayers[cofLayerIdx].Transparent {
|
|
|
|
switch v.Cof.CofLayers[cofLayerIdx].DrawEffect {
|
|
|
|
//Lets pick whatever we have that's closest.
|
|
|
|
case d2enum.DrawEffectPctTransparency25:
|
|
|
|
transparency = byte(64)
|
|
|
|
case d2enum.DrawEffectPctTransparency50:
|
2019-11-25 19:58:30 -05:00
|
|
|
transparency = byte(128)
|
2019-11-25 20:05:18 -05:00
|
|
|
case d2enum.DrawEffectPctTransparency75:
|
|
|
|
transparency = byte(192)
|
|
|
|
case d2enum.DrawEffectModulate:
|
2019-11-26 21:04:36 -05:00
|
|
|
v.layerCache[layerType].compositeMode = ebiten.CompositeModeLighter
|
2019-11-25 20:05:18 -05:00
|
|
|
case d2enum.DrawEffectBurn:
|
|
|
|
// Flies in tal rasha's tomb use this
|
|
|
|
case d2enum.DrawEffectNormal:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:23:21 -05:00
|
|
|
pixels := make([]byte, int32(v.framesToAnimate)*(actualWidth*actualHeight*4))
|
2019-11-25 20:05:18 -05:00
|
|
|
|
|
|
|
for animationIdx := 0; animationIdx < v.framesToAnimate; animationIdx++ {
|
2019-11-26 21:04:36 -05:00
|
|
|
if animationIdx >= len(dccLayer.Directions[dccDirection].Frames) {
|
2019-11-17 16:06:02 -05:00
|
|
|
log.Printf("Invalid animation index of %d for animated entity", animationIdx)
|
2019-11-17 00:52:13 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-12-17 18:23:21 -05:00
|
|
|
sheetOffset := int(actualWidth) * animationIdx
|
|
|
|
combinedWidth := int(actualWidth) * v.framesToAnimate
|
2019-11-25 20:05:18 -05:00
|
|
|
|
2019-11-26 21:04:36 -05:00
|
|
|
frame := dccLayer.Directions[dccDirection].Frames[animationIdx]
|
|
|
|
for y := 0; y < dccLayer.Directions[dccDirection].Box.Height; y++ {
|
|
|
|
for x := 0; x < dccLayer.Directions[dccDirection].Box.Width; x++ {
|
|
|
|
paletteIndex := frame.PixelData[x+(y*dccLayer.Directions[dccDirection].Box.Width)]
|
2019-11-06 18:25:19 -05:00
|
|
|
if paletteIndex == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-10 08:51:02 -05:00
|
|
|
color := d2datadict.Palettes[v.palette].Colors[paletteIndex]
|
2019-11-26 21:04:36 -05:00
|
|
|
actualX := (x + dccLayer.Directions[dccDirection].Box.Left) - int(minX)
|
|
|
|
actualY := (y + dccLayer.Directions[dccDirection].Box.Top) - int(minY)
|
2019-12-17 18:23:21 -05:00
|
|
|
idx := (sheetOffset + actualX + ((actualY) * combinedWidth)) * 4
|
|
|
|
pixels[idx] = color.R
|
|
|
|
pixels[idx+1] = color.G
|
|
|
|
pixels[idx+2] = color.B
|
|
|
|
pixels[idx+3] = transparency
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 18:23:21 -05:00
|
|
|
v.layerCache[layerType].frameSheet, _ = ebiten.NewImage(int(actualWidth)*v.framesToAnimate, int(actualHeight), ebiten.FilterNearest)
|
|
|
|
_ = v.layerCache[layerType].frameSheet.ReplacePixels(pixels)
|
|
|
|
v.layerCache[layerType].frameWidth = int(actualWidth)
|
|
|
|
v.layerCache[layerType].frameHeight = int(actualHeight)
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-17 16:06:02 -05:00
|
|
|
|
|
|
|
func (v AnimatedEntity) GetDirection() int {
|
|
|
|
return v.direction
|
|
|
|
}
|
2019-12-02 16:55:48 -05:00
|
|
|
|
|
|
|
func (v *AnimatedEntity) getStepLength(tickTime float64) (float64, float64) {
|
2019-12-06 09:44:52 -05:00
|
|
|
speed := 6.0
|
2019-12-02 16:55:48 -05:00
|
|
|
length := tickTime * speed
|
|
|
|
|
|
|
|
angle := 359 - d2helper.GetAngleBetween(
|
|
|
|
v.LocationX,
|
|
|
|
v.LocationY,
|
|
|
|
v.TargetX,
|
|
|
|
v.TargetY,
|
|
|
|
)
|
2019-12-06 17:52:35 -05:00
|
|
|
radians := (math.Pi / 180.0) * float64(angle)
|
2019-12-02 16:55:48 -05:00
|
|
|
oneStepX := length * math.Cos(radians)
|
|
|
|
oneStepY := length * math.Sin(radians)
|
|
|
|
return oneStepX, oneStepY
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AnimatedEntity) Step(tickTime float64) {
|
|
|
|
stepX, stepY := v.getStepLength(tickTime)
|
|
|
|
|
|
|
|
if d2helper.AlmostEqual(v.LocationX, v.TargetX, stepX) {
|
|
|
|
v.LocationX = v.TargetX
|
|
|
|
}
|
|
|
|
if d2helper.AlmostEqual(v.LocationY, v.TargetY, stepY) {
|
|
|
|
v.LocationY = v.TargetY
|
|
|
|
}
|
|
|
|
if v.LocationX != v.TargetX {
|
|
|
|
v.LocationX += stepX
|
|
|
|
}
|
|
|
|
if v.LocationY != v.TargetY {
|
|
|
|
v.LocationY += stepY
|
|
|
|
}
|
2019-12-06 09:44:52 -05:00
|
|
|
|
|
|
|
v.subcellX = 1 + math.Mod(v.LocationX, 5)
|
|
|
|
v.subcellY = 1 + math.Mod(v.LocationY, 5)
|
|
|
|
v.TileX = int(v.LocationX / 5)
|
|
|
|
v.TileY = int(v.LocationY / 5)
|
|
|
|
|
2019-12-02 16:55:48 -05:00
|
|
|
if v.LocationX == v.TargetX && v.LocationY == v.TargetY {
|
2019-12-06 23:58:36 -05:00
|
|
|
|
|
|
|
v.repetitions = 3 + rand.Int31n(5)
|
|
|
|
newAnimationMode := d2enum.AnimationModeObjectNeutral
|
|
|
|
// TODO: Figure out what 1-3 are for, 4 is correct.
|
|
|
|
switch v.action {
|
|
|
|
case 1:
|
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterNeutral
|
|
|
|
case 2:
|
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterNeutral
|
|
|
|
case 3:
|
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterNeutral
|
|
|
|
case 4:
|
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterSkill1
|
|
|
|
v.repetitions = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.animationMode != newAnimationMode.String() {
|
|
|
|
v.SetMode(newAnimationMode.String(), v.weaponClass, v.direction)
|
2019-12-02 16:55:48 -05:00
|
|
|
}
|
2019-12-06 23:58:36 -05:00
|
|
|
|
2019-12-02 16:55:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTarget sets target coordinates and changes animation based on proximity and direction
|
2019-12-06 23:58:36 -05:00
|
|
|
func (v *AnimatedEntity) SetTarget(tx, ty float64, action int32) {
|
2019-12-02 16:55:48 -05:00
|
|
|
angle := 359 - d2helper.GetAngleBetween(
|
|
|
|
v.LocationX,
|
|
|
|
v.LocationY,
|
|
|
|
tx,
|
|
|
|
ty,
|
|
|
|
)
|
2019-12-06 23:58:36 -05:00
|
|
|
|
|
|
|
v.action = action
|
2019-12-06 09:44:52 -05:00
|
|
|
// TODO: Check if is in town and if is player.
|
|
|
|
newAnimationMode := d2enum.AnimationModeMonsterWalk.String()
|
2019-12-02 16:55:48 -05:00
|
|
|
if tx != v.LocationX || ty != v.LocationY {
|
|
|
|
v.TargetX, v.TargetY = tx, ty
|
2019-12-06 09:44:52 -05:00
|
|
|
newAnimationMode = d2enum.AnimationModeMonsterWalk.String()
|
2019-12-02 16:55:48 -05:00
|
|
|
}
|
2019-12-06 09:44:52 -05:00
|
|
|
|
2019-12-06 17:52:35 -05:00
|
|
|
newDirection := angleToDirection(float64(angle), v.Cof.NumberOfDirections)
|
2019-12-02 16:55:48 -05:00
|
|
|
if newDirection != v.GetDirection() || newAnimationMode != v.animationMode {
|
|
|
|
v.SetMode(newAnimationMode, v.weaponClass, newDirection)
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 09:44:52 -05:00
|
|
|
|
|
|
|
func angleToDirection(angle float64, numberOfDirections int) int {
|
2019-12-06 23:58:36 -05:00
|
|
|
if numberOfDirections == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:44:52 -05:00
|
|
|
degreesPerDirection := 360.0 / float64(numberOfDirections)
|
|
|
|
offset := 45.0 - (degreesPerDirection / 2)
|
|
|
|
newDirection := int((angle - offset) / degreesPerDirection)
|
|
|
|
if newDirection >= numberOfDirections {
|
|
|
|
newDirection = newDirection - numberOfDirections
|
|
|
|
} else if newDirection < 0 {
|
|
|
|
newDirection = numberOfDirections + newDirection
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDirection
|
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
|
|
|
|
func (v *AnimatedEntity) Advance(tickTime float64) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AnimatedEntity) GetPosition() (float64, float64) {
|
|
|
|
return float64(v.TileX), float64(v.TileY)
|
|
|
|
}
|