2019-11-10 08:51:02 -05:00
|
|
|
package d2mapengine
|
2019-10-31 21:17:13 -04:00
|
|
|
|
|
|
|
import (
|
2019-11-06 21:25:09 -05:00
|
|
|
"strings"
|
2019-10-31 21:17:13 -04:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2019-11-11 23:48:55 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2enum"
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2interface"
|
2019-11-17 08:09:54 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2audio"
|
2019-12-13 00:33:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core"
|
2019-10-31 21:17:13 -04:00
|
|
|
)
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
type MapEntity interface {
|
|
|
|
Render(target *ebiten.Image, screenX, screenY int)
|
|
|
|
GetPosition() (float64, float64)
|
|
|
|
Advance(tickTime float64)
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
type MapEngine struct {
|
2019-11-10 03:36:53 -05:00
|
|
|
soundManager *d2audio.Manager
|
|
|
|
gameState *d2core.GameState
|
|
|
|
fileProvider d2interface.FileProvider
|
2019-12-08 22:18:42 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
debugVisLevel int
|
|
|
|
|
|
|
|
regions []*MapRegion
|
|
|
|
entities []MapEntity
|
2019-12-08 22:18:42 -05:00
|
|
|
viewport *Viewport
|
|
|
|
camera Camera
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func CreateMapEngine(gameState *d2core.GameState, soundManager *d2audio.Manager, fileProvider d2interface.FileProvider) *MapEngine {
|
|
|
|
engine := &MapEngine{
|
2019-10-31 21:17:13 -04:00
|
|
|
gameState: gameState,
|
|
|
|
soundManager: soundManager,
|
|
|
|
fileProvider: fileProvider,
|
2019-12-08 22:18:42 -05:00
|
|
|
viewport: NewViewport(0, 0, 800, 600),
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
2019-12-08 22:18:42 -05:00
|
|
|
|
|
|
|
engine.viewport.SetCamera(&engine.camera)
|
|
|
|
return engine
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) GetStartPosition() (float64, float64) {
|
|
|
|
var startX, startY float64
|
|
|
|
if len(me.regions) > 0 {
|
|
|
|
region := me.regions[0]
|
|
|
|
startX, startY = region.getStartTilePosition()
|
|
|
|
}
|
2019-12-06 09:44:52 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
return startX, startY
|
2019-12-06 09:44:52 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) GetCenterPosition() (float64, float64) {
|
|
|
|
var centerX, centerY float64
|
|
|
|
if len(me.regions) > 0 {
|
|
|
|
region := me.regions[0]
|
|
|
|
centerX = float64(region.tileRect.Left) + float64(region.tileRect.Width)/2
|
|
|
|
centerY = float64(region.tileRect.Top) + float64(region.tileRect.Height)/2
|
|
|
|
}
|
2019-11-17 01:14:58 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
return centerX, centerY
|
2019-12-08 22:18:42 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) MoveCameraTo(x, y float64) {
|
|
|
|
me.camera.MoveTo(x, y)
|
2019-12-08 22:18:42 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) MoveCameraBy(x, y float64) {
|
|
|
|
me.camera.MoveBy(x, y)
|
2019-12-08 22:18:42 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) ScreenToWorld(x, y int) (float64, float64) {
|
|
|
|
return me.viewport.ScreenToWorld(x, y)
|
2019-11-17 01:14:58 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) ScreenToOrtho(x, y int) (float64, float64) {
|
|
|
|
return me.viewport.ScreenToOrtho(x, y)
|
2019-11-06 21:25:09 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) WorldToOrtho(x, y float64) (float64, float64) {
|
|
|
|
return me.viewport.WorldToOrtho(x, y)
|
2019-11-06 18:25:19 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) SetDebugVisLevel(debugVisLevel int) {
|
|
|
|
me.debugVisLevel = debugVisLevel
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) GenerateMap(regionType d2enum.RegionIdType, levelPreset int, fileIndex int) {
|
|
|
|
region, entities := loadRegion(me.gameState.Seed, 0, 0, regionType, levelPreset, me.fileProvider, fileIndex)
|
|
|
|
me.regions = append(me.regions, region)
|
|
|
|
me.entities = append(me.entities, entities...)
|
2019-11-06 21:25:09 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) GenerateAct1Overworld() {
|
|
|
|
me.soundManager.PlayBGM("/data/global/music/Act1/town1.wav") // TODO: Temp stuff here
|
2019-11-15 11:03:58 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
region, entities := loadRegion(me.gameState.Seed, 0, 0, d2enum.RegionAct1Town, 1, me.fileProvider, -1)
|
|
|
|
me.regions = append(me.regions, region)
|
|
|
|
me.entities = append(me.entities, entities...)
|
2019-11-06 21:25:09 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
if strings.Contains(region.regionPath, "E1") {
|
|
|
|
region, entities := loadRegion(me.gameState.Seed, int(region.tileRect.Width-1), 0, d2enum.RegionAct1Town, 2, me.fileProvider, -1)
|
|
|
|
me.regions = append(me.regions, region)
|
|
|
|
me.entities = append(me.entities, entities...)
|
|
|
|
} else if strings.Contains(region.regionPath, "S1") {
|
|
|
|
region, entities := loadRegion(me.gameState.Seed, 0, int(region.tileRect.Height-1), d2enum.RegionAct1Town, 3, me.fileProvider, -1)
|
|
|
|
me.regions = append(me.regions, region)
|
|
|
|
me.entities = append(me.entities, entities...)
|
2019-11-17 08:09:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) GetRegionAtTile(x, y int) *MapRegion {
|
|
|
|
for _, region := range me.regions {
|
|
|
|
if region.tileRect.IsInRect(x, y) {
|
|
|
|
return region
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-08 22:18:42 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
return nil
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) AddEntity(entity MapEntity) {
|
|
|
|
me.entities = append(me.entities, entity)
|
2019-11-15 11:03:58 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) Advance(tickTime float64) {
|
|
|
|
for _, region := range me.regions {
|
|
|
|
if region.isVisbile(me.viewport) {
|
|
|
|
region.advance(tickTime)
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
for _, entity := range me.entities {
|
|
|
|
entity.Advance(tickTime)
|
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-12-13 00:33:11 -05:00
|
|
|
func (me *MapEngine) Render(target *ebiten.Image) {
|
|
|
|
for _, region := range me.regions {
|
|
|
|
if region.isVisbile(me.viewport) {
|
|
|
|
region.renderPass1(me.viewport, target)
|
|
|
|
region.renderDebug(me.debugVisLevel, me.viewport, target)
|
|
|
|
region.renderPass2(me.entities, me.viewport, target)
|
|
|
|
region.renderPass3(me.viewport, target)
|
2019-11-15 09:04:27 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|