2019-10-31 21:17:13 -04:00
|
|
|
package Map
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
2019-11-01 14:12:23 -04:00
|
|
|
"math/rand"
|
2019-10-31 21:17:13 -04:00
|
|
|
|
2019-11-02 17:38:39 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/Common"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/Sound"
|
2019-10-31 21:17:13 -04:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EngineRegion struct {
|
|
|
|
Rect image.Rectangle
|
2019-11-01 14:12:23 -04:00
|
|
|
Region *Region
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Engine struct {
|
|
|
|
soundManager *Sound.Manager
|
|
|
|
gameState *Common.GameState
|
|
|
|
fileProvider Common.FileProvider
|
2019-11-01 14:12:23 -04:00
|
|
|
regions []EngineRegion
|
|
|
|
OffsetX float64
|
|
|
|
OffsetY float64
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateMapEngine(gameState *Common.GameState, soundManager *Sound.Manager, fileProvider Common.FileProvider) *Engine {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) GenerateMap(regionType RegionIdType, levelPreset int) {
|
2019-11-01 14:12:23 -04:00
|
|
|
randomSource := rand.NewSource(v.gameState.Seed)
|
|
|
|
region := LoadRegion(randomSource, regionType, levelPreset, v.fileProvider)
|
|
|
|
v.regions = append(v.regions, EngineRegion{
|
|
|
|
Rect: image.Rectangle{image.Point{0, 0}, image.Point{int(region.TileWidth), int(region.TileHeight)}},
|
|
|
|
Region: region,
|
|
|
|
})
|
|
|
|
v.soundManager.PlayBGM("/data/global/music/Act1/tristram.wav") // TODO: Temp stuff here
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) Render(target *ebiten.Image) {
|
2019-11-01 14:12:23 -04:00
|
|
|
for y := 0; y < int(v.regions[0].Region.TileHeight); y++ {
|
|
|
|
offX := -(y * 80)
|
|
|
|
offY := y * 40
|
|
|
|
for x := 0; x < int(v.regions[0].Region.TileWidth); x++ {
|
2019-11-01 22:41:49 -04:00
|
|
|
sx, sy := Common.IsoToScreen(x, y, int(v.OffsetX), int(v.OffsetY))
|
|
|
|
if sx > -160 && sy > -160 && sx <= 800 && sy <= 1000 {
|
|
|
|
tile := v.regions[0].Region.DS1.Tiles[y][x]
|
|
|
|
for i := range tile.Floors {
|
|
|
|
if !tile.Floors[i].Hidden && tile.Floors[i].Prop1 != 0 {
|
|
|
|
v.regions[0].Region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, RegionLayerTypeFloors, i, target)
|
|
|
|
}
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
offX += 80
|
|
|
|
offY += 40
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for y := 0; y < int(v.regions[0].Region.TileHeight); y++ {
|
|
|
|
offX := -(y * 80)
|
|
|
|
offY := y * 40
|
|
|
|
for x := 0; x < int(v.regions[0].Region.TileWidth); x++ {
|
2019-11-01 22:41:49 -04:00
|
|
|
sx, sy := Common.IsoToScreen(x, y, int(v.OffsetX), int(v.OffsetY))
|
|
|
|
if sx > -160 && sy > -160 && sx <= 800 && sy <= 1000 {
|
|
|
|
tile := v.regions[0].Region.DS1.Tiles[y][x]
|
|
|
|
for i := range tile.Shadows {
|
|
|
|
if tile.Shadows[i].Hidden || tile.Shadows[i].Prop1 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.regions[0].Region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, RegionLayerTypeShadows, i, target)
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
|
|
|
offX += 80
|
|
|
|
offY += 40
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for y := 0; y < int(v.regions[0].Region.TileHeight); y++ {
|
|
|
|
offX := -(y * 80)
|
|
|
|
offY := y * 40
|
|
|
|
for x := 0; x < int(v.regions[0].Region.TileWidth); x++ {
|
2019-11-02 11:15:02 -04:00
|
|
|
tile := v.regions[0].Region.DS1.Tiles[y][x]
|
|
|
|
for i := range tile.Walls {
|
|
|
|
if tile.Walls[i].Hidden || tile.Walls[i].Orientation == 15 || tile.Walls[i].Orientation == 10 || tile.Walls[i].Orientation == 11 || tile.Walls[i].Orientation == 0 {
|
|
|
|
continue
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
2019-11-02 11:15:02 -04:00
|
|
|
v.regions[0].Region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, RegionLayerTypeWalls, i, target)
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
|
|
|
offX += 80
|
|
|
|
offY += 40
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for y := 0; y < int(v.regions[0].Region.TileHeight); y++ {
|
|
|
|
offX := -(y * 80)
|
|
|
|
offY := y * 40
|
|
|
|
for x := 0; x < int(v.regions[0].Region.TileWidth); x++ {
|
2019-11-02 11:15:02 -04:00
|
|
|
tile := v.regions[0].Region.DS1.Tiles[y][x]
|
|
|
|
for i := range tile.Walls {
|
|
|
|
if tile.Walls[i].Hidden || tile.Walls[i].Orientation != 15 {
|
|
|
|
continue
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
2019-11-02 11:15:02 -04:00
|
|
|
v.regions[0].Region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, RegionLayerTypeWalls, i, target)
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-01 14:12:23 -04:00
|
|
|
offX += 80
|
|
|
|
offY += 40
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|