1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-09-27 21:56:19 -04:00

Place objects according to sub-cell (#165)

This commit is contained in:
Ziemas 2019-11-14 02:31:47 +01:00 committed by Tim Sarbin
parent f2ac62bf18
commit e2bee09469

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"image" "image"
"log" "log"
"math"
"strings" "strings"
"time" "time"
@ -33,22 +34,23 @@ type AnimatedEntity struct {
// LocationX represents the tile X position of the entity // LocationX represents the tile X position of the entity
LocationX float64 LocationX float64
// LocationY represents the tile Y position of the entity // LocationY represents the tile Y position of the entity
LocationY float64 subcellX, subcellY float64 // Subcell coordinates within the current tile
dccLayers map[string]d2dcc.DCC LocationY float64
Cof *d2cof.COF dccLayers map[string]d2dcc.DCC
palette d2enum.PaletteType Cof *d2cof.COF
base string palette d2enum.PaletteType
token string base string
animationMode string token string
weaponClass string animationMode string
lastFrameTime time.Time weaponClass string
framesToAnimate int lastFrameTime time.Time
animationSpeed int framesToAnimate int
direction int animationSpeed int
currentFrame int direction int
frames map[string][]*ebiten.Image currentFrame int
frameLocations map[string][]d2common.Rectangle frames map[string][]*ebiten.Image
object *d2datadict.ObjectLookupRecord frameLocations map[string][]d2common.Rectangle
object *d2datadict.ObjectLookupRecord
} }
// CreateAnimatedEntity creates an instance of AnimatedEntity // CreateAnimatedEntity creates an instance of AnimatedEntity
@ -62,6 +64,10 @@ func CreateAnimatedEntity(x, y int32, object *d2datadict.ObjectLookupRecord, fil
result.dccLayers = make(map[string]d2dcc.DCC) result.dccLayers = make(map[string]d2dcc.DCC)
result.LocationX = float64(x) / 5 result.LocationX = float64(x) / 5
result.LocationY = float64(y) / 5 result.LocationY = float64(y) / 5
result.subcellX = 1 + math.Mod(float64(x), 5)
result.subcellY = 1 + math.Mod(float64(y), 5)
return result return result
} }
@ -155,10 +161,16 @@ func (v *AnimatedEntity) Render(target *ebiten.Image, offsetX, offsetY int) {
if v.frames[frameName] == nil { if v.frames[frameName] == nil {
continue continue
} }
// TODO: Probably not pixel perfect, should render from center of sub-tile?
// Location within the current tile
localX := (v.subcellX - v.subcellY) * 16
localY := (v.subcellX + v.subcellY) * 8
// TODO: Transparency op maybe, but it'l murder batch calls // TODO: Transparency op maybe, but it'l murder batch calls
opts := &ebiten.DrawImageOptions{} opts := &ebiten.DrawImageOptions{}
opts.GeoM.Translate(float64(v.frameLocations[frameName][v.currentFrame].Left+offsetX), opts.GeoM.Translate(float64(v.frameLocations[frameName][v.currentFrame].Left+offsetX)+localX,
float64(v.frameLocations[frameName][v.currentFrame].Top+offsetY+40)) float64(v.frameLocations[frameName][v.currentFrame].Top+offsetY)+localY)
if err := target.DrawImage(v.frames[frameName][v.currentFrame], opts); err != nil { if err := target.DrawImage(v.frames[frameName][v.currentFrame], opts); err != nil {
log.Panic(err.Error()) log.Panic(err.Error())
} }