Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Place objects according to sub-cell #165

Merged
merged 1 commit into from
Nov 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions d2render/animated_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"image"
"log"
"math"
"strings"
"time"

Expand Down Expand Up @@ -33,22 +34,23 @@ type AnimatedEntity struct {
// LocationX represents the tile X position of the entity
LocationX float64
// LocationY represents the tile Y position of the entity
LocationY float64
dccLayers map[string]d2dcc.DCC
Cof *d2cof.COF
palette d2enum.PaletteType
base string
token string
animationMode string
weaponClass string
lastFrameTime time.Time
framesToAnimate int
animationSpeed int
direction int
currentFrame int
frames map[string][]*ebiten.Image
frameLocations map[string][]d2common.Rectangle
object *d2datadict.ObjectLookupRecord
subcellX, subcellY float64 // Subcell coordinates within the current tile
LocationY float64
dccLayers map[string]d2dcc.DCC
Cof *d2cof.COF
palette d2enum.PaletteType
base string
token string
animationMode string
weaponClass string
lastFrameTime time.Time
framesToAnimate int
animationSpeed int
direction int
currentFrame int
frames map[string][]*ebiten.Image
frameLocations map[string][]d2common.Rectangle
object *d2datadict.ObjectLookupRecord
}

// CreateAnimatedEntity creates an instance of AnimatedEntity
Expand All @@ -62,6 +64,10 @@ func CreateAnimatedEntity(x, y int32, object *d2datadict.ObjectLookupRecord, fil
result.dccLayers = make(map[string]d2dcc.DCC)
result.LocationX = float64(x) / 5
result.LocationY = float64(y) / 5

result.subcellX = 1 + math.Mod(float64(x), 5)
result.subcellY = 1 + math.Mod(float64(y), 5)

return result
}

Expand Down Expand Up @@ -155,10 +161,16 @@ func (v *AnimatedEntity) Render(target *ebiten.Image, offsetX, offsetY int) {
if v.frames[frameName] == nil {
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
opts := &ebiten.DrawImageOptions{}
opts.GeoM.Translate(float64(v.frameLocations[frameName][v.currentFrame].Left+offsetX),
float64(v.frameLocations[frameName][v.currentFrame].Top+offsetY+40))
opts.GeoM.Translate(float64(v.frameLocations[frameName][v.currentFrame].Left+offsetX)+localX,
float64(v.frameLocations[frameName][v.currentFrame].Top+offsetY)+localY)
if err := target.DrawImage(v.frames[frameName][v.currentFrame], opts); err != nil {
log.Panic(err.Error())
}
Expand Down