1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-06 18:27:20 -05:00
OpenDiablo2/d2core/d2map/engine.go

146 lines
3.7 KiB
Go
Raw Normal View History

package d2map
import (
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2gamestate"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2audio"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
)
type MapEntity interface {
Render(target d2render.Surface)
Advance(tickTime float64)
GetPosition() (float64, float64)
}
type MapEngine struct {
gameState *d2gamestate.GameState
debugVisLevel int
regions []*MapRegion
entities []MapEntity
viewport *Viewport
camera Camera
}
func CreateMapEngine(gameState *d2gamestate.GameState) *MapEngine {
engine := &MapEngine{
gameState: gameState,
viewport: NewViewport(0, 0, 800, 600),
}
engine.viewport.SetCamera(&engine.camera)
return engine
}
func (m *MapEngine) GetStartPosition() (float64, float64) {
var startX, startY float64
if len(m.regions) > 0 {
region := m.regions[0]
startX, startY = region.getStartTilePosition()
}
return startX, startY
}
func (m *MapEngine) GetCenterPosition() (float64, float64) {
var centerX, centerY float64
if len(m.regions) > 0 {
region := m.regions[0]
centerX = float64(region.tileRect.Left) + float64(region.tileRect.Width)/2
centerY = float64(region.tileRect.Top) + float64(region.tileRect.Height)/2
}
return centerX, centerY
}
func (m *MapEngine) MoveCameraTo(x, y float64) {
m.camera.MoveTo(x, y)
}
func (m *MapEngine) MoveCameraBy(x, y float64) {
m.camera.MoveBy(x, y)
}
func (m *MapEngine) ScreenToWorld(x, y int) (float64, float64) {
return m.viewport.ScreenToWorld(x, y)
}
func (m *MapEngine) ScreenToOrtho(x, y int) (float64, float64) {
return m.viewport.ScreenToOrtho(x, y)
}
func (m *MapEngine) WorldToOrtho(x, y float64) (float64, float64) {
return m.viewport.WorldToOrtho(x, y)
}
func (m *MapEngine) SetDebugVisLevel(debugVisLevel int) {
m.debugVisLevel = debugVisLevel
}
func (m *MapEngine) GenerateMap(regionType d2enum.RegionIdType, levelPreset int, fileIndex int) {
region, entities := loadRegion(m.gameState.Seed, 0, 0, regionType, levelPreset, fileIndex)
m.regions = append(m.regions, region)
m.entities = append(m.entities, entities...)
}
func (m *MapEngine) GenerateAct1Overworld() {
d2audio.PlayBGM("/data/global/music/Act1/town1.wav") // TODO: Temp stuff here
region, entities := loadRegion(m.gameState.Seed, 0, 0, d2enum.RegionAct1Town, 1, -1)
m.regions = append(m.regions, region)
m.entities = append(m.entities, entities...)
if strings.Contains(region.regionPath, "E1") {
region, entities := loadRegion(m.gameState.Seed, region.tileRect.Width-1, 0, d2enum.RegionAct1Town, 2, -1)
m.regions = append(m.regions, region)
m.entities = append(m.entities, entities...)
} else if strings.Contains(region.regionPath, "S1") {
region, entities := loadRegion(m.gameState.Seed, 0, region.tileRect.Height-1, d2enum.RegionAct1Town, 3, -1)
m.regions = append(m.regions, region)
m.entities = append(m.entities, entities...)
}
}
func (m *MapEngine) GetRegionAtTile(x, y int) *MapRegion {
for _, region := range m.regions {
if region.tileRect.IsInRect(x, y) {
return region
}
}
return nil
}
func (m *MapEngine) AddEntity(entity MapEntity) {
m.entities = append(m.entities, entity)
}
func (m *MapEngine) Advance(tickTime float64) {
for _, region := range m.regions {
if region.isVisbile(m.viewport) {
region.advance(tickTime)
}
}
for _, entity := range m.entities {
entity.Advance(tickTime)
}
}
func (m *MapEngine) Render(target d2render.Surface) {
for _, region := range m.regions {
if region.isVisbile(m.viewport) {
region.renderPass1(m.viewport, target)
region.renderDebug(m.debugVisLevel, m.viewport, target)
region.renderPass2(m.entities, m.viewport, target)
region.renderPass3(m.viewport, target)
}
}
}