2019-11-10 08:51:02 -05:00
|
|
|
package d2mapengine
|
2019-10-31 21:17:13 -04:00
|
|
|
|
|
|
|
import (
|
2019-11-15 09:04:27 -05:00
|
|
|
"fmt"
|
|
|
|
"image/color"
|
2019-11-06 18:25:19 -05:00
|
|
|
"math"
|
2019-11-06 21:25:09 -05:00
|
|
|
"strings"
|
2019-10-31 21:17:13 -04:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2enum"
|
2019-11-11 23:48:55 -05:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2helper"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2interface"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core"
|
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common"
|
2019-11-17 08:09:54 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2audio"
|
2019-10-31 21:17:13 -04:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2019-11-15 09:04:27 -05:00
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
2019-10-31 21:17:13 -04:00
|
|
|
)
|
|
|
|
|
2019-11-15 11:03:58 -05:00
|
|
|
type RegionTile struct {
|
|
|
|
tileX, tileY int // tile coordinates
|
|
|
|
offX, offY int // world space coordinates of tile origin
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:17:13 -04:00
|
|
|
type EngineRegion struct {
|
2019-11-10 03:36:53 -05:00
|
|
|
Rect d2common.Rectangle
|
2019-11-01 14:12:23 -04:00
|
|
|
Region *Region
|
2019-11-15 11:03:58 -05:00
|
|
|
Tiles []RegionTile
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Engine struct {
|
2019-11-10 03:36:53 -05:00
|
|
|
soundManager *d2audio.Manager
|
|
|
|
gameState *d2core.GameState
|
|
|
|
fileProvider d2interface.FileProvider
|
2019-11-01 14:12:23 -04:00
|
|
|
regions []EngineRegion
|
|
|
|
OffsetX float64
|
|
|
|
OffsetY float64
|
2019-11-15 09:04:27 -05:00
|
|
|
ShowTiles int
|
2019-11-17 01:14:58 -05:00
|
|
|
Hero *d2core.Hero
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
2019-11-10 03:36:53 -05:00
|
|
|
func CreateMapEngine(gameState *d2core.GameState, soundManager *d2audio.Manager, fileProvider d2interface.FileProvider) *Engine {
|
2019-10-31 21:17:13 -04:00
|
|
|
result := &Engine{
|
|
|
|
gameState: gameState,
|
|
|
|
soundManager: soundManager,
|
|
|
|
fileProvider: fileProvider,
|
2019-11-01 14:12:23 -04:00
|
|
|
regions: make([]EngineRegion, 0),
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-17 01:14:58 -05:00
|
|
|
func (v *Engine) GetRegion(regionIndex int) *EngineRegion {
|
|
|
|
return &v.regions[regionIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) CenterCameraOn(x, y float64) {
|
|
|
|
v.OffsetX = -(x - 400)
|
|
|
|
v.OffsetY = -(y - 300)
|
|
|
|
}
|
|
|
|
|
2019-11-17 00:52:13 -05:00
|
|
|
func (v *Engine) GenerateMap(regionType d2enum.RegionIdType, levelPreset int, fileIndex int) {
|
2019-11-18 22:29:34 -05:00
|
|
|
region := LoadRegion(v.gameState.Seed, regionType, levelPreset, v.fileProvider, fileIndex)
|
2019-11-17 00:52:13 -05:00
|
|
|
fmt.Printf("Loading region: %v\n", region.RegionPath)
|
2019-11-01 14:12:23 -04:00
|
|
|
v.regions = append(v.regions, EngineRegion{
|
2019-11-10 03:36:53 -05:00
|
|
|
Rect: d2common.Rectangle{0, 0, int(region.TileWidth), int(region.TileHeight)},
|
2019-11-01 14:12:23 -04:00
|
|
|
Region: region,
|
|
|
|
})
|
2019-11-15 11:03:58 -05:00
|
|
|
|
|
|
|
for i, _ := range v.regions {
|
|
|
|
v.GenTiles(&v.regions[i])
|
2019-11-17 08:09:54 -05:00
|
|
|
v.GenTilesCache(&v.regions[i])
|
2019-11-15 11:03:58 -05:00
|
|
|
}
|
2019-11-06 21:25:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) GenerateAct1Overworld() {
|
2019-11-06 18:25:19 -05:00
|
|
|
v.soundManager.PlayBGM("/data/global/music/Act1/town1.wav") // TODO: Temp stuff here
|
2019-11-18 22:29:34 -05:00
|
|
|
region := LoadRegion(v.gameState.Seed, d2enum.RegionAct1Town, 1, v.fileProvider, -1)
|
2019-11-06 21:25:09 -05:00
|
|
|
v.regions = append(v.regions, EngineRegion{
|
2019-11-10 03:36:53 -05:00
|
|
|
Rect: d2common.Rectangle{0, 0, int(region.TileWidth), int(region.TileHeight)},
|
2019-11-06 21:25:09 -05:00
|
|
|
Region: region,
|
|
|
|
})
|
|
|
|
if strings.Contains(region.RegionPath, "E1") {
|
2019-11-18 22:29:34 -05:00
|
|
|
region2 := LoadRegion(v.gameState.Seed, d2enum.RegionAct1Town, 2, v.fileProvider, -1)
|
2019-11-06 21:25:09 -05:00
|
|
|
v.regions = append(v.regions, EngineRegion{
|
2019-11-10 03:36:53 -05:00
|
|
|
Rect: d2common.Rectangle{int(region.TileWidth - 1), 0, int(region2.TileWidth), int(region2.TileHeight)},
|
2019-11-06 21:25:09 -05:00
|
|
|
Region: region2,
|
|
|
|
})
|
|
|
|
} else if strings.Contains(region.RegionPath, "S1") {
|
2019-11-18 22:29:34 -05:00
|
|
|
region2 := LoadRegion(v.gameState.Seed, d2enum.RegionAct1Town, 3, v.fileProvider, -1)
|
2019-11-06 21:25:09 -05:00
|
|
|
v.regions = append(v.regions, EngineRegion{
|
2019-11-10 03:36:53 -05:00
|
|
|
Rect: d2common.Rectangle{0, int(region.TileHeight - 1), int(region2.TileWidth), int(region2.TileHeight)},
|
2019-11-06 21:25:09 -05:00
|
|
|
Region: region2,
|
|
|
|
})
|
|
|
|
}
|
2019-11-07 23:44:03 -05:00
|
|
|
|
2019-11-15 11:03:58 -05:00
|
|
|
for i, _ := range v.regions {
|
|
|
|
v.GenTiles(&v.regions[i])
|
2019-11-17 08:09:54 -05:00
|
|
|
v.GenTilesCache(&v.regions[i])
|
2019-11-15 11:03:58 -05:00
|
|
|
}
|
|
|
|
|
2019-11-10 03:36:53 -05:00
|
|
|
sx, sy := d2helper.IsoToScreen(int(region.StartX), int(region.StartY), 0, 0)
|
2019-11-07 23:44:03 -05:00
|
|
|
v.OffsetX = float64(sx) - 400
|
|
|
|
v.OffsetY = float64(sy) - 300
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-11-06 21:25:09 -05:00
|
|
|
func (v *Engine) GetRegionAt(x, y int) *EngineRegion {
|
2019-11-06 18:25:19 -05:00
|
|
|
if v.regions == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, region := range v.regions {
|
|
|
|
if !region.Rect.IsInRect(x, y) {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-06 21:25:09 -05:00
|
|
|
return ®ion
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
return nil
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) Render(target *ebiten.Image) {
|
2019-11-06 18:25:19 -05:00
|
|
|
for _, region := range v.regions {
|
2019-11-06 21:25:09 -05:00
|
|
|
v.RenderRegion(region, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 11:03:58 -05:00
|
|
|
func (v *Engine) GenTiles(region *EngineRegion) {
|
2019-11-06 21:25:09 -05:00
|
|
|
for y := 0; y < int(region.Region.TileHeight); y++ {
|
|
|
|
offX := -((y + region.Rect.Top) * 80) + (region.Rect.Left * 80)
|
|
|
|
offY := ((y + region.Rect.Top) * 40) + (region.Rect.Left * 40)
|
|
|
|
for x := 0; x < int(region.Region.TileWidth); x++ {
|
2019-11-15 11:03:58 -05:00
|
|
|
region.Tiles = append(region.Tiles, RegionTile{
|
|
|
|
tileX: x,
|
|
|
|
tileY: y,
|
|
|
|
offX: offX,
|
|
|
|
offY: offY,
|
|
|
|
})
|
|
|
|
|
2019-11-06 21:25:09 -05:00
|
|
|
offX += 80
|
|
|
|
offY += 40
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 21:25:09 -05:00
|
|
|
}
|
|
|
|
|
2019-11-17 08:09:54 -05:00
|
|
|
func (v *Engine) GenTilesCache(region *EngineRegion) {
|
2019-11-17 14:21:48 -05:00
|
|
|
for tileIdx := range region.Tiles {
|
|
|
|
t := ®ion.Tiles[tileIdx]
|
2019-11-17 08:09:54 -05:00
|
|
|
if t.tileY < len(region.Region.DS1.Tiles) && t.tileX < len(region.Region.DS1.Tiles[t.tileY]) {
|
2019-11-21 19:54:56 -05:00
|
|
|
tile := ®ion.Region.DS1.Tiles[t.tileY][t.tileX]
|
2019-11-17 08:09:54 -05:00
|
|
|
for i := range tile.Floors {
|
|
|
|
if tile.Floors[i].Hidden || tile.Floors[i].Prop1 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-19 00:38:02 -05:00
|
|
|
region.Region.generateFloorCache(&tile.Floors[i], t.tileX, t.tileY)
|
2019-11-17 08:09:54 -05:00
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
for i := range tile.Shadows {
|
2019-11-17 08:09:54 -05:00
|
|
|
if tile.Shadows[i].Hidden || tile.Shadows[i].Prop1 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
region.Region.generateShadowCache(&tile.Shadows[i], t.tileX, t.tileY)
|
2019-11-17 08:09:54 -05:00
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
for i := range tile.Walls {
|
|
|
|
if tile.Walls[i].Hidden || tile.Walls[i].Prop1 == 0 {
|
2019-11-17 08:09:54 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
region.Region.generateWallCache(&tile.Walls[i], t.tileX, t.tileY)
|
2019-11-17 08:09:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 11:03:58 -05:00
|
|
|
func (v *Engine) RenderRegion(region EngineRegion, target *ebiten.Image) {
|
2019-11-15 14:12:23 -05:00
|
|
|
for tileIdx := range region.Tiles {
|
|
|
|
sx, sy := d2helper.IsoToScreen(region.Tiles[tileIdx].tileX+region.Rect.Left, region.Tiles[tileIdx].tileY+region.Rect.Top, int(v.OffsetX), int(v.OffsetY))
|
2019-11-18 18:23:33 -05:00
|
|
|
if sx > -160 && sy > -380 && sx <= 880 && sy <= 1240 {
|
2019-11-16 22:53:55 -05:00
|
|
|
v.RenderPass1(region.Region, region.Tiles[tileIdx].offX, region.Tiles[tileIdx].offY, region.Tiles[tileIdx].tileX, region.Tiles[tileIdx].tileY, target)
|
2019-11-17 16:06:02 -05:00
|
|
|
if v.ShowTiles > 0 {
|
|
|
|
v.DrawTileLines(region.Region, region.Tiles[tileIdx].offX, region.Tiles[tileIdx].offY, region.Tiles[tileIdx].tileX, region.Tiles[tileIdx].tileY, target)
|
|
|
|
}
|
|
|
|
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for tileIdx := range region.Tiles {
|
|
|
|
sx, sy := d2helper.IsoToScreen(region.Tiles[tileIdx].tileX+region.Rect.Left, region.Tiles[tileIdx].tileY+region.Rect.Top, int(v.OffsetX), int(v.OffsetY))
|
2019-11-18 18:23:33 -05:00
|
|
|
if sx > -160 && sy > -380 && sx <= 880 && sy <= 1240 {
|
2019-11-16 22:53:55 -05:00
|
|
|
v.RenderPass2(region.Region, region.Tiles[tileIdx].offX, region.Tiles[tileIdx].offY, region.Tiles[tileIdx].tileX, region.Tiles[tileIdx].tileY, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for tileIdx := range region.Tiles {
|
|
|
|
sx, sy := d2helper.IsoToScreen(region.Tiles[tileIdx].tileX+region.Rect.Left, region.Tiles[tileIdx].tileY+region.Rect.Top, int(v.OffsetX), int(v.OffsetY))
|
2019-11-18 18:23:33 -05:00
|
|
|
if sx > -160 && sy > -380 && sx <= 880 && sy <= 1240 {
|
2019-11-16 22:53:55 -05:00
|
|
|
v.RenderPass3(region.Region, region.Tiles[tileIdx].offX, region.Tiles[tileIdx].offY, region.Tiles[tileIdx].tileX, region.Tiles[tileIdx].tileY, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) RenderPass1(region *Region, offX, offY, x, y int, target *ebiten.Image) {
|
|
|
|
tile := region.DS1.Tiles[y][x]
|
2019-11-17 19:01:17 -05:00
|
|
|
// Draw lower walls
|
|
|
|
for i := range tile.Walls {
|
2019-11-22 23:30:02 -05:00
|
|
|
if !tile.Walls[i].Type.LowerWall() || tile.Walls[i].Prop1 == 0 || tile.Walls[i].Hidden {
|
2019-11-17 19:01:17 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, d2enum.RegionLayerTypeWalls, i, target)
|
|
|
|
}
|
|
|
|
|
2019-11-16 22:53:55 -05:00
|
|
|
for i := range tile.Floors {
|
|
|
|
if tile.Floors[i].Hidden || tile.Floors[i].Prop1 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, d2enum.RegionLayerTypeFloors, i, target)
|
|
|
|
}
|
|
|
|
for i := range tile.Shadows {
|
|
|
|
if tile.Shadows[i].Hidden || tile.Shadows[i].Prop1 == 0 {
|
|
|
|
continue
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-16 22:53:55 -05:00
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, d2enum.RegionLayerTypeShadows, i, target)
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-15 11:03:58 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-16 22:53:55 -05:00
|
|
|
func (v *Engine) RenderPass2(region *Region, offX, offY, x, y int, target *ebiten.Image) {
|
2019-11-15 11:03:58 -05:00
|
|
|
tile := region.DS1.Tiles[y][x]
|
2019-11-16 22:53:55 -05:00
|
|
|
|
|
|
|
// Draw upper walls
|
|
|
|
for i := range tile.Walls {
|
2019-11-22 23:30:02 -05:00
|
|
|
if !tile.Walls[i].Type.UpperWall() || tile.Walls[i].Hidden {
|
2019-11-16 22:53:55 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, d2enum.RegionLayerTypeWalls, i, target)
|
|
|
|
}
|
|
|
|
|
2019-11-06 21:25:09 -05:00
|
|
|
for _, obj := range region.AnimationEntities {
|
|
|
|
if int(math.Floor(obj.LocationX)) == x && int(math.Floor(obj.LocationY)) == y {
|
|
|
|
obj.Render(target, offX+int(v.OffsetX), offY+int(v.OffsetY))
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 23:44:03 -05:00
|
|
|
for _, npc := range region.NPCs {
|
|
|
|
if int(math.Floor(npc.AnimatedEntity.LocationX)) == x && int(math.Floor(npc.AnimatedEntity.LocationY)) == y {
|
|
|
|
npc.Render(target, offX+int(v.OffsetX), offY+int(v.OffsetY))
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 01:14:58 -05:00
|
|
|
if v.Hero != nil && int(v.Hero.AnimatedEntity.LocationX) == x && int(v.Hero.AnimatedEntity.LocationY) == y {
|
|
|
|
v.Hero.Render(target, offX+int(v.OffsetX), offY+int(v.OffsetY))
|
|
|
|
}
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) RenderPass3(region *Region, offX, offY, x, y int, target *ebiten.Image) {
|
|
|
|
tile := region.DS1.Tiles[y][x]
|
|
|
|
// Draw ceilings
|
2019-11-06 21:25:09 -05:00
|
|
|
for i := range tile.Walls {
|
2019-11-22 23:30:02 -05:00
|
|
|
if tile.Walls[i].Type != d2enum.Roof {
|
2019-11-06 21:25:09 -05:00
|
|
|
continue
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
2019-11-11 23:48:55 -05:00
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, d2enum.RegionLayerTypeWalls, i, target)
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
2019-11-15 09:04:27 -05:00
|
|
|
|
2019-11-16 22:53:55 -05:00
|
|
|
func (v *Engine) DrawTileLines(region *Region, offX, offY, x, y int, target *ebiten.Image) {
|
2019-11-15 09:04:27 -05:00
|
|
|
if v.ShowTiles > 0 {
|
2019-11-17 08:09:54 -05:00
|
|
|
subtileColor := color.RGBA{80, 80, 255, 100}
|
2019-11-15 09:04:27 -05:00
|
|
|
tileColor := color.RGBA{255, 255, 255, 255}
|
|
|
|
|
|
|
|
ebitenutil.DrawLine(target, float64(offX)+v.OffsetX, float64(offY)+v.OffsetY, float64(offX)+v.OffsetX+80, float64(offY)+v.OffsetY+40, tileColor)
|
|
|
|
ebitenutil.DrawLine(target, float64(offX)+v.OffsetX, float64(offY)+v.OffsetY, float64(offX)+v.OffsetX-80, float64(offY)+v.OffsetY+40, tileColor)
|
|
|
|
|
|
|
|
coords := fmt.Sprintf("%v,%v", x, y)
|
|
|
|
ebitenutil.DebugPrintAt(target, coords, offX+int(v.OffsetX)-10, offY+int(v.OffsetY)+10)
|
|
|
|
|
|
|
|
if v.ShowTiles > 1 {
|
|
|
|
for i := 1; i <= 4; i++ {
|
|
|
|
x := (16 * i)
|
|
|
|
y := (8 * i)
|
|
|
|
ebitenutil.DrawLine(target, float64(offX-x)+v.OffsetX, float64(offY+y)+v.OffsetY,
|
|
|
|
float64(offX-x)+v.OffsetX+80, float64(offY+y)+v.OffsetY+40, subtileColor)
|
|
|
|
ebitenutil.DrawLine(target, float64(offX+x)+v.OffsetX, float64(offY+y)+v.OffsetY,
|
|
|
|
float64(offX+x)+v.OffsetX-80, float64(offY+y)+v.OffsetY+40, subtileColor)
|
|
|
|
}
|
2019-11-17 08:09:54 -05:00
|
|
|
|
|
|
|
tile := region.DS1.Tiles[y][x]
|
|
|
|
for i := range tile.Floors {
|
2019-11-21 20:34:29 -05:00
|
|
|
floorSpec := fmt.Sprintf("f: %v-%v", tile.Floors[i].Style, tile.Floors[i].Sequence)
|
2019-11-17 08:09:54 -05:00
|
|
|
ebitenutil.DebugPrintAt(target, floorSpec, offX+int(v.OffsetX)-20, offY+int(v.OffsetY)+10+((i+1)*14))
|
|
|
|
}
|
2019-11-21 20:34:29 -05:00
|
|
|
// wallSpec := fmt.Sprintf("w: %v-%v", tile.Walls[0].Style, tile.Walls[0].Sequence)
|
2019-11-17 08:09:54 -05:00
|
|
|
// ebitenutil.DebugPrintAt(target, wallSpec, offX+int(v.OffsetX)-20, offY+int(v.OffsetY)+34)
|
2019-11-15 09:04:27 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|