2020-06-21 18:40:37 -04:00
|
|
|
package d2maprenderer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-07-23 12:56:50 -04:00
|
|
|
"fmt"
|
2020-06-21 18:40:37 -04:00
|
|
|
"image/color"
|
|
|
|
"log"
|
2020-06-23 01:05:01 -04:00
|
|
|
"math"
|
2020-06-21 18:40:37 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2ds1"
|
2020-06-28 21:40:52 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-09-12 16:25:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
2020-07-03 14:00:56 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-09-12 16:25:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-07-03 14:00:56 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapengine"
|
2020-06-21 18:40:37 -04:00
|
|
|
)
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
const (
|
|
|
|
screenMiddleX = 400
|
2020-08-05 21:27:45 -04:00
|
|
|
two = 2
|
|
|
|
|
|
|
|
dbgOffsetXY = 40
|
|
|
|
dbgBoxWidth = 220
|
|
|
|
dbgBoxHeight = 60
|
|
|
|
dbgBoxPadding = 10
|
|
|
|
|
2020-08-16 14:59:03 -04:00
|
|
|
dbgCollisionSize = 5
|
2020-08-05 21:27:45 -04:00
|
|
|
dbgCollisionOffsetX = -3
|
|
|
|
dbgCollisionOffsetY = 4
|
|
|
|
|
|
|
|
whiteHalfOpacity = 0xffffff7f
|
|
|
|
blackQuarterOpacity = 0x00000040
|
|
|
|
lightGreenFullOpacity = 0x40ff00ff
|
|
|
|
magentaFullOpacity = 0xff00ffff
|
|
|
|
yellowFullOpacity = 0xffff00ff
|
|
|
|
lightBlueQuarterOpacity = 0x5050ff32
|
|
|
|
whiteQuarterOpacity = 0xffffff64
|
|
|
|
redQuarterOpacity = 0x74000064
|
|
|
|
|
|
|
|
subtilesPerTile = 5
|
|
|
|
orthoSubTileWidth = 16
|
|
|
|
orthoSubTileHeight = 8
|
|
|
|
orthoTileWidth = subtilesPerTile * orthoSubTileWidth
|
|
|
|
orthoTileHeight = subtilesPerTile * orthoSubTileHeight
|
2020-07-26 14:52:54 -04:00
|
|
|
)
|
|
|
|
|
2020-07-18 23:37:35 -04:00
|
|
|
// MapRenderer manages the game viewport and Camera. It requests tile and entity data from MapEngine and renders it.
|
2020-06-21 18:40:37 -04:00
|
|
|
type MapRenderer struct {
|
2020-09-12 16:51:30 -04:00
|
|
|
asset *d2asset.AssetManager
|
2020-07-22 15:03:03 -04:00
|
|
|
renderer d2interface.Renderer // Used for drawing operations
|
|
|
|
mapEngine *d2mapengine.MapEngine // The map engine that is being rendered
|
|
|
|
palette d2interface.Palette // The palette used for this map
|
|
|
|
viewport *Viewport // Used for rendering offsets
|
|
|
|
Camera Camera // Used to determine where on the map we are rendering
|
|
|
|
imageCacheRecords map[uint32]d2interface.Surface
|
|
|
|
mapDebugVisLevel int // Map debug visibility index (0=none, 1=tiles, 2=sub-tiles)
|
2020-07-21 08:51:09 -04:00
|
|
|
entityDebugVisLevel int // Entity Debug visibility index (0=none, 1=vectors)
|
|
|
|
lastFrameTime float64 // The last time the map was rendered
|
|
|
|
currentFrame int // Current render frame (for animations)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// CreateMapRenderer creates a new MapRenderer, sets the required fields and returns a pointer to it.
|
2020-09-12 16:51:30 -04:00
|
|
|
func CreateMapRenderer(asset *d2asset.AssetManager, renderer d2interface.Renderer,
|
|
|
|
mapEngine *d2mapengine.MapEngine,
|
2020-07-26 14:52:54 -04:00
|
|
|
term d2interface.Terminal, startX, startY float64) *MapRenderer {
|
2020-06-21 18:40:37 -04:00
|
|
|
result := &MapRenderer{
|
2020-09-12 16:51:30 -04:00
|
|
|
asset: asset,
|
2020-07-03 14:00:56 -04:00
|
|
|
renderer: renderer,
|
2020-06-21 18:40:37 -04:00
|
|
|
mapEngine: mapEngine,
|
|
|
|
viewport: NewViewport(0, 0, 800, 600),
|
|
|
|
}
|
|
|
|
|
2020-07-18 23:37:35 -04:00
|
|
|
result.Camera = Camera{}
|
2020-07-25 09:36:15 -04:00
|
|
|
rx, ry := result.WorldToOrtho(startX, startY)
|
|
|
|
startPosition := d2vector.NewPosition(rx, ry)
|
2020-07-18 23:37:35 -04:00
|
|
|
result.Camera.position = &startPosition
|
|
|
|
result.viewport.SetCamera(&result.Camera)
|
2020-06-21 18:40:37 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
var err error
|
|
|
|
err = term.BindAction("mapdebugvis", "set map debug visualization level", func(level int) {
|
2020-07-21 08:51:09 -04:00
|
|
|
result.mapDebugVisLevel = level
|
|
|
|
})
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not bind the mapdebugvis action, err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = term.BindAction("entitydebugvis", "set entity debug visualization level", func(level int) {
|
2020-07-21 08:51:09 -04:00
|
|
|
result.entityDebugVisLevel = level
|
2020-06-21 18:40:37 -04:00
|
|
|
})
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not bind the entitydebugvis action, err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:37:11 -04:00
|
|
|
if mapEngine.LevelType().ID != 0 {
|
2020-06-21 18:40:37 -04:00
|
|
|
result.generateTileCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// RegenerateTileCache calls MapRenderer.generateTileCache().
|
2020-06-21 18:40:37 -04:00
|
|
|
func (mr *MapRenderer) RegenerateTileCache() {
|
|
|
|
mr.generateTileCache()
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// SetMapEngine sets the MapEngine this renderer is rendering.
|
2020-06-21 18:40:37 -04:00
|
|
|
func (mr *MapRenderer) SetMapEngine(mapEngine *d2mapengine.MapEngine) {
|
|
|
|
mr.mapEngine = mapEngine
|
|
|
|
mr.generateTileCache()
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Render determines the width and height of map tiles that should be rendered. The following four render passes are
|
|
|
|
// made in succession:
|
|
|
|
//
|
|
|
|
// Pass 1: Lower wall tiles, tile shadows and floor tiles.
|
|
|
|
//
|
|
|
|
// Pass 2: Entities below walls.
|
|
|
|
//
|
|
|
|
// Pass 3: Upper wall tiles and entities above walls.
|
|
|
|
//
|
|
|
|
// Pass 4: Roof tiles.
|
2020-06-29 00:41:58 -04:00
|
|
|
func (mr *MapRenderer) Render(target d2interface.Surface) {
|
2020-06-23 01:05:01 -04:00
|
|
|
mapSize := mr.mapEngine.Size()
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
stxf, styf := mr.viewport.ScreenToWorld(screenMiddleX, -200)
|
|
|
|
etxf, etyf := mr.viewport.ScreenToWorld(screenMiddleX, 1050)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-23 01:05:01 -04:00
|
|
|
startX := int(math.Max(0, math.Floor(stxf)))
|
|
|
|
startY := int(math.Max(0, math.Floor(styf)))
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-23 01:05:01 -04:00
|
|
|
endX := int(math.Min(float64(mapSize.Width), math.Ceil(etxf)))
|
|
|
|
endY := int(math.Min(float64(mapSize.Height), math.Ceil(etyf)))
|
|
|
|
|
|
|
|
mr.renderPass1(target, startX, startY, endX, endY)
|
2020-06-29 00:30:27 -04:00
|
|
|
mr.renderPass2(target, startX, startY, endX, endY)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-21 08:51:09 -04:00
|
|
|
if mr.mapDebugVisLevel > 0 {
|
|
|
|
mr.renderMapDebug(mr.mapDebugVisLevel, target, startX, startY, endX, endY)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-23 01:05:01 -04:00
|
|
|
mr.renderPass3(target, startX, startY, endX, endY)
|
2020-06-27 14:30:23 -04:00
|
|
|
mr.renderPass4(target, startX, startY, endX, endY)
|
2020-07-21 08:51:09 -04:00
|
|
|
|
|
|
|
if mr.entityDebugVisLevel > 0 {
|
|
|
|
mr.renderEntityDebug(target)
|
|
|
|
}
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-07-18 23:37:35 -04:00
|
|
|
// MoveCameraTo sets the position of the Camera to the given x and y coordinates.
|
|
|
|
func (mr *MapRenderer) MoveCameraTo(position *d2vector.Position) {
|
|
|
|
mr.Camera.MoveTo(position)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-07-18 23:37:35 -04:00
|
|
|
// MoveCameraBy adds the given vector to the current position of the Camera.
|
|
|
|
func (mr *MapRenderer) MoveCameraBy(vector *d2vector.Vector) {
|
|
|
|
mr.Camera.MoveBy(vector)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveCameraTargetBy adds the given vector to the current position of the Camera.
|
|
|
|
func (mr *MapRenderer) MoveCameraTargetBy(vector *d2vector.Vector) {
|
|
|
|
mr.Camera.MoveTargetBy(vector)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// ScreenToWorld returns the world position for the given screen (pixel) position.
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) ScreenToWorld(x, y int) (worldX, worldY float64) {
|
2020-06-21 18:40:37 -04:00
|
|
|
return mr.viewport.ScreenToWorld(x, y)
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// ScreenToOrtho returns the orthogonal position, without accounting for the isometric angle, for the given screen
|
|
|
|
// (pixel) position.
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) ScreenToOrtho(x, y int) (orthoX, orthoY float64) {
|
2020-06-21 18:40:37 -04:00
|
|
|
return mr.viewport.ScreenToOrtho(x, y)
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// WorldToOrtho returns the orthogonal position for the given isometric world position.
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) WorldToOrtho(x, y float64) (orthoX, orthoY float64) {
|
2020-06-21 18:40:37 -04:00
|
|
|
return mr.viewport.WorldToOrtho(x, y)
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Lower wall tiles, tile shadows and floor tiles.
|
2020-06-29 00:41:58 -04:00
|
|
|
func (mr *MapRenderer) renderPass1(target d2interface.Surface, startX, startY, endX, endY int) {
|
2020-06-23 01:05:01 -04:00
|
|
|
for tileY := startY; tileY < endY; tileY++ {
|
|
|
|
for tileX := startX; tileX < endX; tileX++ {
|
2020-06-21 18:40:37 -04:00
|
|
|
tile := mr.mapEngine.TileAt(tileX, tileY)
|
2020-06-23 01:05:01 -04:00
|
|
|
mr.viewport.PushTranslationWorld(float64(tileX), float64(tileY))
|
|
|
|
mr.renderTilePass1(tile, target)
|
|
|
|
mr.viewport.PopTranslation()
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Entities below walls.
|
2020-06-29 00:41:58 -04:00
|
|
|
func (mr *MapRenderer) renderPass2(target d2interface.Surface, startX, startY, endX, endY int) {
|
2020-06-27 14:30:23 -04:00
|
|
|
for tileY := startY; tileY < endY; tileY++ {
|
|
|
|
for tileX := startX; tileX < endX; tileX++ {
|
|
|
|
mr.viewport.PushTranslationWorld(float64(tileX), float64(tileY))
|
|
|
|
|
2020-08-16 14:59:03 -04:00
|
|
|
tileEnt := make([]d2interface.MapEntity, 0)
|
|
|
|
|
2020-06-27 14:30:23 -04:00
|
|
|
// TODO: Do not loop over every entity every frame
|
2020-08-05 21:27:45 -04:00
|
|
|
for _, mapEntity := range mr.mapEngine.Entities() {
|
2020-07-21 08:51:09 -04:00
|
|
|
pos := mapEntity.GetPosition()
|
|
|
|
vec := pos.World()
|
|
|
|
entityX, entityY := vec.X(), vec.Y()
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-29 00:30:27 -04:00
|
|
|
if mapEntity.GetLayer() != 1 {
|
2020-06-27 14:30:23 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-29 00:30:27 -04:00
|
|
|
if (int(entityX) != tileX) || (int(entityY) != tileY) {
|
2020-06-27 14:30:23 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-08-16 14:59:03 -04:00
|
|
|
tileEnt = append(tileEnt, mapEntity)
|
|
|
|
}
|
|
|
|
|
|
|
|
for subY := 0; subY < 5; subY++ {
|
|
|
|
for subX := 0; subX < 5; subX++ {
|
|
|
|
for _, mapEntity := range tileEnt {
|
|
|
|
pos := mapEntity.GetPosition()
|
|
|
|
if (int(pos.SubTileOffset().X()) != subX) || (int(pos.SubTileOffset().Y()) != subY) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
target.PushTranslation(mr.viewport.GetTranslationScreen())
|
|
|
|
mapEntity.Render(target)
|
|
|
|
target.Pop()
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 14:30:23 -04:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-27 14:30:23 -04:00
|
|
|
mr.viewport.PopTranslation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Upper wall tiles and entities above walls.
|
2020-06-29 00:41:58 -04:00
|
|
|
func (mr *MapRenderer) renderPass3(target d2interface.Surface, startX, startY, endX, endY int) {
|
2020-06-23 01:05:01 -04:00
|
|
|
for tileY := startY; tileY < endY; tileY++ {
|
|
|
|
for tileX := startX; tileX < endX; tileX++ {
|
2020-06-21 18:40:37 -04:00
|
|
|
tile := mr.mapEngine.TileAt(tileX, tileY)
|
2020-06-23 01:05:01 -04:00
|
|
|
mr.viewport.PushTranslationWorld(float64(tileX), float64(tileY))
|
|
|
|
mr.renderTilePass2(tile, target)
|
|
|
|
|
2020-08-16 14:59:03 -04:00
|
|
|
tileEnt := make([]d2interface.MapEntity, 0)
|
|
|
|
|
2020-06-23 01:05:01 -04:00
|
|
|
// TODO: Do not loop over every entity every frame
|
2020-08-05 21:27:45 -04:00
|
|
|
for _, mapEntity := range mr.mapEngine.Entities() {
|
2020-07-21 08:51:09 -04:00
|
|
|
pos := mapEntity.GetPosition()
|
|
|
|
vec := pos.World()
|
|
|
|
entityX, entityY := vec.X(), vec.Y()
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-29 00:30:27 -04:00
|
|
|
if mapEntity.GetLayer() == 1 {
|
2020-06-23 01:05:01 -04:00
|
|
|
continue
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-29 00:30:27 -04:00
|
|
|
if (int(entityX) != tileX) || (int(entityY) != tileY) {
|
2020-06-27 14:30:23 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-08-16 14:59:03 -04:00
|
|
|
tileEnt = append(tileEnt, mapEntity)
|
|
|
|
}
|
|
|
|
|
|
|
|
for subY := 0; subY < 5; subY++ {
|
|
|
|
for subX := 0; subX < 5; subX++ {
|
|
|
|
for _, mapEntity := range tileEnt {
|
|
|
|
pos := mapEntity.GetPosition()
|
|
|
|
if (int(pos.SubTileOffset().X()) != subX) || (int(pos.SubTileOffset().Y()) != subY) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
target.PushTranslation(mr.viewport.GetTranslationScreen())
|
|
|
|
mapEntity.Render(target)
|
|
|
|
target.Pop()
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-23 01:05:01 -04:00
|
|
|
mr.viewport.PopTranslation()
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Roof tiles.
|
2020-06-29 00:41:58 -04:00
|
|
|
func (mr *MapRenderer) renderPass4(target d2interface.Surface, startX, startY, endX, endY int) {
|
2020-06-23 01:05:01 -04:00
|
|
|
for tileY := startY; tileY < endY; tileY++ {
|
|
|
|
for tileX := startX; tileX < endX; tileX++ {
|
2020-06-21 18:40:37 -04:00
|
|
|
tile := mr.mapEngine.TileAt(tileX, tileY)
|
2020-06-23 01:05:01 -04:00
|
|
|
mr.viewport.PushTranslationWorld(float64(tileX), float64(tileY))
|
|
|
|
mr.renderTilePass3(tile, target)
|
|
|
|
mr.viewport.PopTranslation()
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
func (mr *MapRenderer) renderTilePass1(tile *d2mapengine.MapTile, target d2interface.Surface) {
|
|
|
|
for _, wall := range tile.Components.Walls {
|
2020-06-21 18:40:37 -04:00
|
|
|
if !wall.Hidden && wall.Prop1 != 0 && wall.Type.LowerWall() {
|
|
|
|
mr.renderWall(wall, mr.viewport, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
for _, floor := range tile.Components.Floors {
|
2020-06-21 18:40:37 -04:00
|
|
|
if !floor.Hidden && floor.Prop1 != 0 {
|
|
|
|
mr.renderFloor(floor, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
for _, shadow := range tile.Components.Shadows {
|
2020-06-21 18:40:37 -04:00
|
|
|
if !shadow.Hidden && shadow.Prop1 != 0 {
|
|
|
|
mr.renderShadow(shadow, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
func (mr *MapRenderer) renderTilePass2(tile *d2mapengine.MapTile, target d2interface.Surface) {
|
|
|
|
for _, wall := range tile.Components.Walls {
|
2020-06-21 18:40:37 -04:00
|
|
|
if !wall.Hidden && wall.Type.UpperWall() {
|
|
|
|
mr.renderWall(wall, mr.viewport, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
func (mr *MapRenderer) renderTilePass3(tile *d2mapengine.MapTile, target d2interface.Surface) {
|
|
|
|
for _, wall := range tile.Components.Walls {
|
2020-07-09 23:12:28 -04:00
|
|
|
if wall.Type == d2enum.TileRoof {
|
2020-06-21 18:40:37 -04:00
|
|
|
mr.renderWall(wall, mr.viewport, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (mr *MapRenderer) renderFloor(tile d2ds1.FloorShadowRecord, target d2interface.Surface) {
|
|
|
|
var img d2interface.Surface
|
2020-06-21 18:40:37 -04:00
|
|
|
if !tile.Animated {
|
|
|
|
img = mr.getImageCacheRecord(tile.Style, tile.Sequence, 0, tile.RandomIndex)
|
|
|
|
} else {
|
|
|
|
img = mr.getImageCacheRecord(tile.Style, tile.Sequence, 0, byte(mr.currentFrame))
|
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
if img == nil {
|
|
|
|
log.Printf("Render called on uncached floor {%v,%v}", tile.Style, tile.Sequence)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mr.viewport.PushTranslationOrtho(-80, float64(tile.YAdjust))
|
|
|
|
defer mr.viewport.PopTranslation()
|
|
|
|
|
|
|
|
target.PushTranslation(mr.viewport.GetTranslationScreen())
|
|
|
|
defer target.Pop()
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := target.Render(img); err != nil {
|
|
|
|
fmt.Printf("failed to render the floor, err: %v\n", err)
|
|
|
|
}
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (mr *MapRenderer) renderWall(tile d2ds1.WallRecord, viewport *Viewport, target d2interface.Surface) {
|
2020-06-21 18:40:37 -04:00
|
|
|
img := mr.getImageCacheRecord(tile.Style, tile.Sequence, tile.Type, tile.RandomIndex)
|
|
|
|
if img == nil {
|
|
|
|
log.Printf("Render called on uncached wall {%v,%v,%v}", tile.Style, tile.Sequence, tile.Type)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-14 00:25:23 -04:00
|
|
|
viewport.PushTranslationOrtho(-80, float64(tile.YAdjust))
|
2020-06-21 18:40:37 -04:00
|
|
|
defer viewport.PopTranslation()
|
|
|
|
|
|
|
|
target.PushTranslation(viewport.GetTranslationScreen())
|
|
|
|
defer target.Pop()
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := target.Render(img); err != nil {
|
|
|
|
fmt.Printf("failed to render the wall, err: %v\n", err)
|
|
|
|
}
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (mr *MapRenderer) renderShadow(tile d2ds1.FloorShadowRecord, target d2interface.Surface) {
|
2020-06-21 18:40:37 -04:00
|
|
|
img := mr.getImageCacheRecord(tile.Style, tile.Sequence, 13, tile.RandomIndex)
|
|
|
|
if img == nil {
|
|
|
|
log.Printf("Render called on uncached shadow {%v,%v}", tile.Style, tile.Sequence)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mr.viewport.PushTranslationOrtho(-80, float64(tile.YAdjust)).PopTranslation()
|
|
|
|
|
|
|
|
target.PushTranslation(mr.viewport.GetTranslationScreen())
|
2020-08-05 21:27:45 -04:00
|
|
|
defer target.Pop()
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-08-05 21:27:45 -04:00
|
|
|
target.PushColor(color.RGBA{R: 255, G: 255, B: 255, A: 160}) //nolint:gomnd // Not a magic number...
|
|
|
|
defer target.Pop()
|
2020-06-21 18:40:37 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := target.Render(img); err != nil {
|
|
|
|
fmt.Printf("failed to render the shadow, err: %v\n", err)
|
|
|
|
}
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-07-21 08:51:09 -04:00
|
|
|
func (mr *MapRenderer) renderMapDebug(mapDebugVisLevel int, target d2interface.Surface, startX, startY, endX, endY int) {
|
2020-06-23 01:05:01 -04:00
|
|
|
for tileY := startY; tileY < endY; tileY++ {
|
|
|
|
for tileX := startX; tileX < endX; tileX++ {
|
|
|
|
mr.viewport.PushTranslationWorld(float64(tileX), float64(tileY))
|
2020-07-21 08:51:09 -04:00
|
|
|
mr.renderTileDebug(tileX, tileY, mapDebugVisLevel, target)
|
2020-06-23 01:05:01 -04:00
|
|
|
mr.viewport.PopTranslation()
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 21:27:45 -04:00
|
|
|
//nolint:funlen // doesn't make sense to split this function
|
2020-07-21 08:51:09 -04:00
|
|
|
func (mr *MapRenderer) renderEntityDebug(target d2interface.Surface) {
|
2020-08-05 21:27:45 -04:00
|
|
|
entities := mr.mapEngine.Entities()
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-07-21 08:51:09 -04:00
|
|
|
for idx := range entities {
|
|
|
|
e := entities[idx]
|
|
|
|
pos := e.GetPosition()
|
|
|
|
world := pos
|
2020-08-05 21:27:45 -04:00
|
|
|
x, y := world.X()/subtilesPerTile, world.Y()/subtilesPerTile
|
2020-07-21 08:51:09 -04:00
|
|
|
velocity := e.GetVelocity()
|
2020-07-25 09:36:54 -04:00
|
|
|
velocity = *velocity.Clone()
|
2020-07-21 08:51:09 -04:00
|
|
|
vx, vy := mr.viewport.WorldToOrtho(velocity.X(), velocity.Y())
|
|
|
|
screenX, screenY := mr.viewport.WorldToScreen(x, y)
|
|
|
|
|
2020-08-05 21:27:45 -04:00
|
|
|
offX, offY := dbgOffsetXY, -dbgOffsetXY
|
2020-07-21 08:51:09 -04:00
|
|
|
|
2020-08-01 19:03:09 -04:00
|
|
|
entScreenXf, entScreenYf := mr.WorldToScreenF(e.GetPositionF())
|
|
|
|
entScreenX := int(math.Floor(entScreenXf))
|
|
|
|
entScreenY := int(math.Floor(entScreenYf))
|
|
|
|
entityWidth, entityHeight := e.GetSize()
|
2020-08-05 21:27:45 -04:00
|
|
|
halfWidth, halfHeight := entityWidth/two, entityHeight/two
|
2020-08-01 19:03:09 -04:00
|
|
|
l, r := entScreenX-halfWidth, entScreenX+halfWidth
|
|
|
|
t, b := entScreenY-halfHeight, entScreenY+halfHeight
|
|
|
|
mx, my := mr.renderer.GetCursorPos()
|
|
|
|
xWithin := (l <= mx) && (r >= mx)
|
|
|
|
yWithin := (t <= my) && (b >= my)
|
|
|
|
within := xWithin && yWithin
|
|
|
|
|
2020-09-08 15:58:35 -04:00
|
|
|
boxLineColor := d2util.Color(magentaFullOpacity)
|
|
|
|
boxHoverColor := d2util.Color(yellowFullOpacity)
|
2020-08-01 19:03:09 -04:00
|
|
|
|
|
|
|
boxColor := boxLineColor
|
|
|
|
|
|
|
|
if within {
|
|
|
|
boxColor = boxHoverColor
|
|
|
|
}
|
|
|
|
|
2020-08-05 21:27:45 -04:00
|
|
|
stack := 0
|
2020-08-01 19:03:09 -04:00
|
|
|
// box
|
2020-07-21 08:51:09 -04:00
|
|
|
mr.viewport.PushTranslationWorld(x, y)
|
2020-08-05 21:27:45 -04:00
|
|
|
|
2020-07-21 08:51:09 -04:00
|
|
|
target.PushTranslation(screenX, screenY)
|
2020-08-05 21:27:45 -04:00
|
|
|
stack++
|
|
|
|
|
2020-08-01 19:03:09 -04:00
|
|
|
target.PushTranslation(-halfWidth, -halfHeight)
|
2020-08-05 21:27:45 -04:00
|
|
|
stack++
|
|
|
|
|
2020-08-01 19:03:09 -04:00
|
|
|
target.DrawLine(0, entityHeight, boxColor)
|
|
|
|
target.DrawLine(entityWidth, 0, boxColor)
|
2020-08-05 21:27:45 -04:00
|
|
|
|
2020-08-01 19:03:09 -04:00
|
|
|
target.PushTranslation(entityWidth, entityHeight)
|
2020-08-05 21:27:45 -04:00
|
|
|
stack++
|
|
|
|
|
2020-08-01 19:03:09 -04:00
|
|
|
target.DrawLine(-entityWidth, 0, boxColor)
|
|
|
|
target.DrawLine(0, -entityHeight, boxColor)
|
2020-08-05 21:27:45 -04:00
|
|
|
target.PopN(stack)
|
2020-07-21 08:51:09 -04:00
|
|
|
mr.viewport.PopTranslation()
|
2020-08-01 19:03:09 -04:00
|
|
|
|
|
|
|
// hover
|
|
|
|
if within {
|
|
|
|
mr.viewport.PushTranslationWorld(x, y)
|
|
|
|
target.PushTranslation(screenX, screenY)
|
2020-09-08 15:58:35 -04:00
|
|
|
target.DrawLine(offX, offY, d2util.Color(whiteHalfOpacity))
|
2020-08-05 21:27:45 -04:00
|
|
|
target.PushTranslation(offX+dbgBoxPadding, offY-dbgBoxPadding*two)
|
|
|
|
target.PushTranslation(-dbgOffsetXY, -dbgOffsetXY)
|
2020-09-08 15:58:35 -04:00
|
|
|
target.DrawRect(dbgBoxWidth, dbgBoxHeight, d2util.Color(blackQuarterOpacity))
|
2020-08-01 19:03:09 -04:00
|
|
|
target.Pop()
|
|
|
|
target.DrawTextf("World (%.2f, %.2f)\nVelocity (%.2f, %.2f)", x, y, vx, vy)
|
|
|
|
target.Pop()
|
2020-09-08 15:58:35 -04:00
|
|
|
target.DrawLine(int(vx), int(vy), d2util.Color(lightGreenFullOpacity))
|
2020-08-01 19:03:09 -04:00
|
|
|
target.Pop()
|
|
|
|
mr.viewport.PopTranslation()
|
|
|
|
}
|
2020-07-21 08:51:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// WorldToScreen returns the screen (pixel) position for the given isometric world position as two ints.
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) WorldToScreen(x, y float64) (screenX, screenY int) {
|
2020-06-25 00:39:09 -04:00
|
|
|
return mr.viewport.WorldToScreen(x, y)
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// WorldToScreenF returns the screen (pixel) position for the given isometric world position as two float64s.
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) WorldToScreenF(x, y float64) (screenX, screenY float64) {
|
2020-06-25 00:39:09 -04:00
|
|
|
return mr.viewport.WorldToScreenF(x, y)
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) renderTileDebug(ax, ay, debugVisLevel int, target d2interface.Surface) {
|
2020-09-08 15:58:35 -04:00
|
|
|
subTileColor := d2util.Color(lightBlueQuarterOpacity)
|
|
|
|
tileColor := d2util.Color(whiteQuarterOpacity)
|
|
|
|
tileCollisionColor := d2util.Color(redQuarterOpacity)
|
2020-06-21 18:40:37 -04:00
|
|
|
|
|
|
|
screenX1, screenY1 := mr.viewport.WorldToScreen(float64(ax), float64(ay))
|
|
|
|
screenX2, screenY2 := mr.viewport.WorldToScreen(float64(ax+1), float64(ay))
|
|
|
|
screenX3, screenY3 := mr.viewport.WorldToScreen(float64(ax), float64(ay+1))
|
|
|
|
|
|
|
|
target.PushTranslation(screenX1, screenY1)
|
|
|
|
defer target.Pop()
|
|
|
|
|
|
|
|
target.DrawLine(screenX2-screenX1, screenY2-screenY1, tileColor)
|
|
|
|
target.DrawLine(screenX3-screenX1, screenY3-screenY1, tileColor)
|
|
|
|
target.PushTranslation(-10, 10)
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf("%v, %v", ax, ay)
|
2020-06-21 18:40:37 -04:00
|
|
|
target.Pop()
|
|
|
|
|
|
|
|
if debugVisLevel > 1 {
|
|
|
|
for i := 1; i <= 4; i++ {
|
2020-08-05 21:27:45 -04:00
|
|
|
x2 := i * orthoSubTileWidth
|
|
|
|
y2 := i * orthoSubTileHeight
|
2020-06-21 18:40:37 -04:00
|
|
|
|
|
|
|
target.PushTranslation(-x2, y2)
|
2020-08-05 21:27:45 -04:00
|
|
|
target.DrawLine(orthoTileWidth, orthoTileHeight, subTileColor)
|
2020-06-21 18:40:37 -04:00
|
|
|
target.Pop()
|
|
|
|
|
|
|
|
target.PushTranslation(x2, y2)
|
2020-08-05 21:27:45 -04:00
|
|
|
target.DrawLine(-orthoTileWidth, orthoTileHeight, subTileColor)
|
2020-06-21 18:40:37 -04:00
|
|
|
target.Pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
tile := mr.mapEngine.TileAt(ax, ay)
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
for i, wall := range tile.Components.Walls {
|
2020-06-21 22:14:06 -04:00
|
|
|
if wall.Type.Special() {
|
2020-08-05 21:27:45 -04:00
|
|
|
target.PushTranslation(-20, 10+(i+1)*14) // what are these magic numbers??
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf("s: %v-%v", wall.Style, wall.Sequence)
|
2020-06-21 22:14:06 -04:00
|
|
|
target.Pop()
|
|
|
|
}
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for yy := 0; yy < 5; yy++ {
|
|
|
|
for xx := 0; xx < 5; xx++ {
|
2020-08-05 21:27:45 -04:00
|
|
|
isoX := (xx - yy) * orthoSubTileWidth
|
|
|
|
isoY := (xx + yy) * orthoSubTileHeight
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
blocked := tile.GetSubTileFlags(xx, yy).BlockWalk
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
if blocked {
|
2020-08-05 21:27:45 -04:00
|
|
|
target.PushTranslation(isoX+dbgCollisionOffsetX, isoY+dbgCollisionOffsetY)
|
|
|
|
target.DrawRect(dbgCollisionSize, dbgCollisionSize, tileCollisionColor)
|
2020-06-21 18:40:37 -04:00
|
|
|
target.Pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// Advance is called once per frame and maintains the MapRenderer's record previous render timestamp and current frame.
|
2020-06-21 18:40:37 -04:00
|
|
|
func (mr *MapRenderer) Advance(elapsed float64) {
|
|
|
|
frameLength := 0.1
|
|
|
|
|
|
|
|
mr.lastFrameTime += elapsed
|
|
|
|
framesAdvanced := int(mr.lastFrameTime / frameLength)
|
|
|
|
mr.lastFrameTime -= float64(framesAdvanced) * frameLength
|
|
|
|
|
|
|
|
mr.currentFrame += framesAdvanced
|
|
|
|
if mr.currentFrame > 9 {
|
|
|
|
mr.currentFrame = 0
|
|
|
|
}
|
2020-07-18 23:37:35 -04:00
|
|
|
|
|
|
|
mr.Camera.Advance(elapsed)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
func (mr *MapRenderer) loadPaletteForAct(levelType d2enum.RegionIdType) (d2interface.Palette,
|
|
|
|
error) {
|
2020-06-21 18:40:37 -04:00
|
|
|
var palettePath string
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
switch levelType {
|
|
|
|
case d2enum.RegionAct1Town, d2enum.RegionAct1Wilderness, d2enum.RegionAct1Cave, d2enum.RegionAct1Crypt,
|
|
|
|
d2enum.RegionAct1Monestary, d2enum.RegionAct1Courtyard, d2enum.RegionAct1Barracks,
|
|
|
|
d2enum.RegionAct1Jail, d2enum.RegionAct1Cathedral, d2enum.RegionAct1Catacombs, d2enum.RegionAct1Tristram:
|
|
|
|
palettePath = d2resource.PaletteAct1
|
|
|
|
case d2enum.RegionAct2Town, d2enum.RegionAct2Sewer, d2enum.RegionAct2Harem, d2enum.RegionAct2Basement,
|
|
|
|
d2enum.RegionAct2Desert, d2enum.RegionAct2Tomb, d2enum.RegionAct2Lair, d2enum.RegionAct2Arcane:
|
|
|
|
palettePath = d2resource.PaletteAct2
|
|
|
|
case d2enum.RegionAct3Town, d2enum.RegionAct3Jungle, d2enum.RegionAct3Kurast, d2enum.RegionAct3Spider,
|
|
|
|
d2enum.RegionAct3Dungeon, d2enum.RegionAct3Sewer:
|
|
|
|
palettePath = d2resource.PaletteAct3
|
|
|
|
case d2enum.RegionAct4Town, d2enum.RegionAct4Mesa, d2enum.RegionAct4Lava, d2enum.RegionAct5Lava:
|
|
|
|
palettePath = d2resource.PaletteAct4
|
|
|
|
case d2enum.RegonAct5Town, d2enum.RegionAct5Siege, d2enum.RegionAct5Barricade, d2enum.RegionAct5Temple,
|
|
|
|
d2enum.RegionAct5IceCaves, d2enum.RegionAct5Baal:
|
|
|
|
palettePath = d2resource.PaletteAct5
|
|
|
|
default:
|
|
|
|
return nil, errors.New("failed to find palette for region")
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
return mr.asset.LoadPalette(palettePath)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
2020-06-22 09:23:56 -04:00
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// ViewportToLeft moves the viewport to the left.
|
2020-06-22 09:23:56 -04:00
|
|
|
func (mr *MapRenderer) ViewportToLeft() {
|
|
|
|
mr.viewport.toLeft()
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// ViewportToRight moves the viewport to the right.
|
2020-06-22 09:23:56 -04:00
|
|
|
func (mr *MapRenderer) ViewportToRight() {
|
|
|
|
mr.viewport.toRight()
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:11:01 -04:00
|
|
|
// ViewportDefault resets the viewport to it's default position.
|
2020-06-22 09:23:56 -04:00
|
|
|
func (mr *MapRenderer) ViewportDefault() {
|
|
|
|
mr.viewport.resetAlign()
|
|
|
|
}
|
2020-07-18 23:37:35 -04:00
|
|
|
|
2020-07-26 15:17:00 -04:00
|
|
|
// SetCameraTarget sets the Camera target
|
2020-07-18 23:37:35 -04:00
|
|
|
func (mr *MapRenderer) SetCameraTarget(position *d2vector.Position) {
|
|
|
|
mr.Camera.SetTarget(position)
|
|
|
|
}
|
2020-07-22 15:03:03 -04:00
|
|
|
|
2020-07-26 15:17:00 -04:00
|
|
|
// SetCameraPosition sets the Camera position
|
|
|
|
func (mr *MapRenderer) SetCameraPosition(position *d2vector.Position) {
|
|
|
|
mr.Camera.MoveTo(position)
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:03:03 -04:00
|
|
|
// InvalidateImageCache the global region image cache. Call this when you are changing regions.
|
|
|
|
func (mr *MapRenderer) InvalidateImageCache() {
|
|
|
|
mr.imageCacheRecords = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mr *MapRenderer) getImageCacheRecord(style, sequence byte, tileType d2enum.TileType, randomIndex byte) d2interface.Surface {
|
|
|
|
lookupIndex := uint32(style)<<24 | uint32(sequence)<<16 | uint32(tileType)<<8 | uint32(randomIndex)
|
|
|
|
return mr.imageCacheRecords[lookupIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mr *MapRenderer) setImageCacheRecord(style, sequence byte, tileType d2enum.TileType, randomIndex byte, image d2interface.Surface) {
|
|
|
|
lookupIndex := uint32(style)<<24 | uint32(sequence)<<16 | uint32(tileType)<<8 | uint32(randomIndex)
|
|
|
|
|
|
|
|
if mr.imageCacheRecords == nil {
|
|
|
|
mr.imageCacheRecords = make(map[uint32]d2interface.Surface)
|
|
|
|
}
|
|
|
|
|
|
|
|
mr.imageCacheRecords[lookupIndex] = image
|
|
|
|
}
|