2019-11-06 22:12:15 -05:00
|
|
|
package common
|
2019-11-06 18:25:19 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-08 11:05:51 -05:00
|
|
|
"image"
|
2019-11-06 18:25:19 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/palettedefs"
|
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-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-11-06 22:12:15 -05:00
|
|
|
// LocationX represents the tile X position of the entity
|
|
|
|
LocationX float64
|
|
|
|
// LocationY represents the tile Y position of the entity
|
|
|
|
LocationY float64
|
2019-11-09 23:37:02 -05:00
|
|
|
dccLayers map[string]*DCC
|
|
|
|
Cof *Cof
|
2019-11-06 22:12:15 -05:00
|
|
|
palette palettedefs.PaletteType
|
2019-11-06 18:25:19 -05:00
|
|
|
base string
|
|
|
|
token string
|
|
|
|
animationMode string
|
|
|
|
weaponClass string
|
|
|
|
lastFrameTime time.Time
|
|
|
|
framesToAnimate int
|
|
|
|
animationSpeed int
|
|
|
|
direction int
|
|
|
|
currentFrame int
|
2019-11-09 23:37:02 -05:00
|
|
|
frames map[string][]*ebiten.Image
|
|
|
|
frameLocations map[string][]Rectangle
|
|
|
|
object Object
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// CreateAnimatedEntity creates an instance of AnimatedEntity
|
2019-11-07 23:44:03 -05:00
|
|
|
func CreateAnimatedEntity(object Object, fileProvider FileProvider, palette palettedefs.PaletteType) *AnimatedEntity {
|
2019-11-06 18:25:19 -05:00
|
|
|
result := &AnimatedEntity{
|
2019-11-07 23:44:03 -05:00
|
|
|
base: object.Lookup.Base,
|
|
|
|
token: object.Lookup.Token,
|
2019-11-09 23:37:02 -05:00
|
|
|
object: object,
|
2019-11-06 18:25:19 -05:00
|
|
|
palette: palette,
|
|
|
|
}
|
2019-11-09 23:37:02 -05:00
|
|
|
result.dccLayers = make(map[string]*DCC)
|
|
|
|
result.LocationX = float64(object.X) / 5
|
|
|
|
result.LocationY = float64(object.Y) / 5
|
2019-11-06 18:25:19 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// DirectionLookup is used to decode the direction offset indexes
|
2019-11-06 18:25:19 -05:00
|
|
|
var DirectionLookup = []int{3, 15, 4, 8, 0, 9, 5, 10, 1, 11, 6, 12, 2, 13, 7, 14}
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
// SetMode changes the graphical mode of this animated entity
|
2019-11-06 18:25:19 -05:00
|
|
|
func (v *AnimatedEntity) SetMode(animationMode, weaponClass string, direction int, provider FileProvider) {
|
2019-11-09 23:37:02 -05:00
|
|
|
cofPath := fmt.Sprintf("%s/%s/Cof/%s%s%s.Cof", v.base, v.token, v.token, animationMode, weaponClass)
|
|
|
|
v.Cof = LoadCof(cofPath, provider)
|
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
|
|
|
|
}
|
|
|
|
v.frames = make(map[string][]*ebiten.Image)
|
|
|
|
v.frameLocations = make(map[string][]Rectangle)
|
|
|
|
v.dccLayers = make(map[string]*DCC)
|
|
|
|
for _, cofLayer := range v.Cof.CofLayers {
|
|
|
|
layerName := DccLayerNames[cofLayer.Type]
|
|
|
|
v.dccLayers[layerName] = v.LoadLayer(layerName, provider)
|
|
|
|
if v.dccLayers[layerName] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.cacheFrames(layerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AnimatedEntity) LoadLayer(layer string, fileProvider FileProvider) *DCC {
|
|
|
|
layerName := "tr"
|
|
|
|
switch strings.ToUpper(layer) {
|
|
|
|
case "HD": // Head
|
|
|
|
layerName = v.object.Lookup.HD
|
|
|
|
case "TR": // Torso
|
|
|
|
layerName = v.object.Lookup.TR
|
|
|
|
case "LG": // Legs
|
|
|
|
layerName = v.object.Lookup.LG
|
|
|
|
case "RA": // RightArm
|
|
|
|
layerName = v.object.Lookup.RA
|
|
|
|
case "LA": // LeftArm
|
|
|
|
layerName = v.object.Lookup.LA
|
|
|
|
case "RH": // RightHand
|
|
|
|
layerName = v.object.Lookup.RH
|
|
|
|
case "LH": // LeftHand
|
|
|
|
layerName = v.object.Lookup.LH
|
|
|
|
case "SH": // Shield
|
|
|
|
layerName = v.object.Lookup.SH
|
|
|
|
case "S1": // Special1
|
|
|
|
layerName = v.object.Lookup.S1
|
|
|
|
case "S2": // Special2
|
|
|
|
layerName = v.object.Lookup.S2
|
|
|
|
case "S3": // Special3
|
|
|
|
layerName = v.object.Lookup.S3
|
|
|
|
case "S4": // Special4
|
|
|
|
layerName = v.object.Lookup.S4
|
|
|
|
case "S5": // Special5
|
|
|
|
layerName = v.object.Lookup.S5
|
|
|
|
case "S6": // Special6
|
|
|
|
layerName = v.object.Lookup.S6
|
|
|
|
case "S7": // Special7
|
|
|
|
layerName = v.object.Lookup.S7
|
|
|
|
case "S8": // Special8
|
|
|
|
layerName = v.object.Lookup.S8
|
|
|
|
}
|
|
|
|
if len(layerName) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
return LoadDCC(dccPath, fileProvider)
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
for v.lastFrameTime.Add(time.Millisecond * time.Duration(v.animationSpeed)).Before(time.Now()) {
|
|
|
|
v.lastFrameTime = v.lastFrameTime.Add(time.Millisecond * time.Duration(v.animationSpeed))
|
|
|
|
v.currentFrame++
|
|
|
|
if v.currentFrame >= v.framesToAnimate {
|
|
|
|
v.currentFrame = 0
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 23:37:02 -05:00
|
|
|
for idx := 0; idx < v.Cof.NumberOfLayers; idx++ {
|
|
|
|
priority := v.Cof.Priority[v.direction][v.currentFrame][idx]
|
|
|
|
if int(priority) >= len(DccLayerNames) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
frameName := DccLayerNames[priority]
|
|
|
|
if v.frames[frameName] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO: Transparency op maybe, but it'l murder batch calls
|
|
|
|
opts := &ebiten.DrawImageOptions{}
|
|
|
|
opts.GeoM.Translate(float64(v.frameLocations[frameName][v.currentFrame].Left+offsetX),
|
|
|
|
float64(v.frameLocations[frameName][v.currentFrame].Top+offsetY+40))
|
|
|
|
target.DrawImage(v.frames[frameName][v.currentFrame], opts)
|
|
|
|
}
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-11-09 23:37:02 -05:00
|
|
|
func (v *AnimatedEntity) cacheFrames(layerName string) {
|
|
|
|
dcc := v.dccLayers[layerName]
|
2019-11-07 23:44:03 -05:00
|
|
|
v.currentFrame = 0
|
|
|
|
animationData := AnimationData[strings.ToLower(v.token+v.animationMode+v.weaponClass)][0]
|
2019-11-07 17:27:21 -05:00
|
|
|
v.animationSpeed = int(1000.0 / ((float64(animationData.AnimationSpeed) * 25.0) / 256.0))
|
2019-11-06 18:25:19 -05:00
|
|
|
v.framesToAnimate = animationData.FramesPerDirection
|
|
|
|
v.lastFrameTime = time.Now()
|
2019-11-09 23:37:02 -05:00
|
|
|
minX := int32(10000)
|
|
|
|
minY := int32(10000)
|
|
|
|
maxX := int32(-10000)
|
|
|
|
maxY := int32(-10000)
|
|
|
|
for _, layer := range dcc.Directions {
|
2019-11-06 18:25:19 -05:00
|
|
|
minX = MinInt32(minX, int32(layer.Box.Left))
|
|
|
|
minY = MinInt32(minY, int32(layer.Box.Top))
|
|
|
|
maxX = MaxInt32(maxX, int32(layer.Box.Right()))
|
|
|
|
maxY = MaxInt32(maxY, int32(layer.Box.Bottom()))
|
|
|
|
}
|
|
|
|
frameW := maxX - minX
|
|
|
|
frameH := maxY - minY
|
2019-11-09 23:37:02 -05:00
|
|
|
v.frames[layerName] = make([]*ebiten.Image, v.framesToAnimate)
|
|
|
|
v.frameLocations[layerName] = make([]Rectangle, v.framesToAnimate)
|
|
|
|
for frameIndex := range v.frames[layerName] {
|
|
|
|
v.frames[layerName][frameIndex], _ = ebiten.NewImage(int(frameW), int(frameH), ebiten.FilterNearest)
|
|
|
|
for layerIdx := 0; layerIdx < v.Cof.NumberOfLayers; layerIdx++ {
|
|
|
|
transparency := byte(255)
|
|
|
|
if v.Cof.CofLayers[layerIdx].Transparent {
|
|
|
|
transparency = byte(128)
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-09 23:37:02 -05:00
|
|
|
|
|
|
|
direction := dcc.Directions[v.direction]
|
2019-11-06 18:25:19 -05:00
|
|
|
frame := direction.Frames[frameIndex]
|
2019-11-08 11:05:51 -05:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, int(frameW), int(frameH)))
|
2019-11-06 18:25:19 -05:00
|
|
|
for y := 0; y < direction.Box.Height; y++ {
|
|
|
|
for x := 0; x < direction.Box.Width; x++ {
|
|
|
|
paletteIndex := frame.PixelData[x+(y*direction.Box.Width)]
|
|
|
|
|
|
|
|
if paletteIndex == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
color := Palettes[v.palette].Colors[paletteIndex]
|
|
|
|
actualX := x + direction.Box.Left - int(minX)
|
|
|
|
actualY := y + direction.Box.Top - int(minY)
|
2019-11-08 11:05:51 -05:00
|
|
|
img.Pix[(actualX*4)+(actualY*int(frameW)*4)] = color.R
|
|
|
|
img.Pix[(actualX*4)+(actualY*int(frameW)*4)+1] = color.G
|
|
|
|
img.Pix[(actualX*4)+(actualY*int(frameW)*4)+2] = color.B
|
2019-11-09 23:37:02 -05:00
|
|
|
img.Pix[(actualX*4)+(actualY*int(frameW)*4)+3] = transparency
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 11:05:51 -05:00
|
|
|
newImage, _ := ebiten.NewImageFromImage(img, ebiten.FilterNearest)
|
|
|
|
img = nil
|
2019-11-09 23:37:02 -05:00
|
|
|
v.frames[layerName][frameIndex] = newImage
|
|
|
|
v.frameLocations[layerName][frameIndex] = Rectangle{
|
2019-11-06 18:25:19 -05:00
|
|
|
Left: int(minX),
|
|
|
|
Top: int(minY),
|
|
|
|
Width: int(frameW),
|
|
|
|
Height: int(frameH),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|