2019-10-31 21:17:13 -04:00
|
|
|
package Map
|
|
|
|
|
|
|
|
import (
|
2019-11-06 18:25:19 -05:00
|
|
|
"math"
|
2019-11-01 14:12:23 -04:00
|
|
|
"math/rand"
|
2019-11-06 21:25:09 -05:00
|
|
|
"strings"
|
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 {
|
2019-11-06 18:25:19 -05:00
|
|
|
Rect Common.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{
|
2019-11-06 18:25:19 -05:00
|
|
|
Rect: Common.Rectangle{0, 0, int(region.TileWidth), int(region.TileHeight)},
|
2019-11-01 14:12:23 -04:00
|
|
|
Region: region,
|
|
|
|
})
|
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-06 21:25:09 -05:00
|
|
|
randomSource := rand.NewSource(v.gameState.Seed)
|
|
|
|
region := LoadRegion(randomSource, RegionAct1Town, 1, v.fileProvider)
|
|
|
|
v.regions = append(v.regions, EngineRegion{
|
|
|
|
Rect: Common.Rectangle{0, 0, int(region.TileWidth), int(region.TileHeight)},
|
|
|
|
Region: region,
|
|
|
|
})
|
|
|
|
if strings.Contains(region.RegionPath, "E1") {
|
|
|
|
region2 := LoadRegion(randomSource, RegionAct1Town, 2, v.fileProvider)
|
|
|
|
v.regions = append(v.regions, EngineRegion{
|
|
|
|
Rect: Common.Rectangle{int(region.TileWidth - 1), 0, int(region2.TileWidth), int(region2.TileHeight)},
|
|
|
|
Region: region2,
|
|
|
|
})
|
|
|
|
} else if strings.Contains(region.RegionPath, "S1") {
|
|
|
|
region2 := LoadRegion(randomSource, RegionAct1Town, 3, v.fileProvider)
|
|
|
|
v.regions = append(v.regions, EngineRegion{
|
|
|
|
Rect: Common.Rectangle{0, int(region.TileHeight - 1), int(region2.TileWidth), int(region2.TileHeight)},
|
|
|
|
Region: region2,
|
|
|
|
})
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) RenderRegion(region EngineRegion, target *ebiten.Image) {
|
|
|
|
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++ {
|
|
|
|
sx, sy := Common.IsoToScreen(x+region.Rect.Left, y+region.Rect.Top, int(v.OffsetX), int(v.OffsetY))
|
|
|
|
if sx > -160 && sy > -160 && sx <= 880 && sy <= 1000 {
|
|
|
|
v.RenderTile(region.Region, offX, offY, x, y, target)
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) RenderTile(region *Region, offX, offY, x, y int, target *ebiten.Image) {
|
|
|
|
tile := region.DS1.Tiles[y][x]
|
|
|
|
for i := range tile.Floors {
|
|
|
|
if tile.Floors[i].Hidden || tile.Floors[i].Prop1 == 0 {
|
|
|
|
continue
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-06 21:25:09 -05:00
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, RegionLayerTypeFloors, i, target)
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
2019-11-06 21:25:09 -05:00
|
|
|
for i := range tile.Shadows {
|
|
|
|
if tile.Shadows[i].Hidden || tile.Shadows[i].Prop1 == 0 {
|
|
|
|
continue
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-06 21:25:09 -05:00
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, RegionLayerTypeShadows, i, target)
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-06 21:25:09 -05:00
|
|
|
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-06 21:25:09 -05:00
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, RegionLayerTypeWalls, i, target)
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range tile.Walls {
|
|
|
|
if tile.Walls[i].Hidden || tile.Walls[i].Orientation != 15 {
|
|
|
|
continue
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
2019-11-06 21:25:09 -05:00
|
|
|
region.RenderTile(offX+int(v.OffsetX), offY+int(v.OffsetY), x, y, RegionLayerTypeWalls, i, target)
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|