More optimizations
This commit is contained in:
56
internal/maps/manager.go
Normal file
56
internal/maps/manager.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package maps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/atridad/LilGuy/internal/portal"
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
maps map[string]*Map
|
||||
currentMap *Map
|
||||
}
|
||||
|
||||
func NewManager() *Manager {
|
||||
return &Manager{
|
||||
maps: make(map[string]*Map),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) RegisterMap(mp *Map) {
|
||||
m.maps[mp.ID] = mp
|
||||
}
|
||||
|
||||
func (m *Manager) GetMap(id string) *Map {
|
||||
return m.maps[id]
|
||||
}
|
||||
|
||||
func (m *Manager) SetCurrentMap(id string) error {
|
||||
mp, ok := m.maps[id]
|
||||
if !ok {
|
||||
return fmt.Errorf("map not found: %s", id)
|
||||
}
|
||||
m.currentMap = mp
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) CurrentMap() *Map {
|
||||
return m.currentMap
|
||||
}
|
||||
|
||||
func (m *Manager) ResolveTransition(event portal.TransitionEvent) (*Map, *portal.Portal) {
|
||||
destMap := m.GetMap(event.DestinationMap)
|
||||
if destMap == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
destPortal := destMap.GetPortalByID(event.DestinationPortal)
|
||||
return destMap, destPortal
|
||||
}
|
||||
|
||||
func (m *Manager) Reset() {
|
||||
for _, mp := range m.maps {
|
||||
mp.Reset()
|
||||
}
|
||||
// Reset to default map if needed, or let the caller do it
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package maps
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/atridad/LilGuy/internal/portal"
|
||||
"github.com/atridad/LilGuy/internal/world"
|
||||
)
|
||||
@@ -16,6 +17,8 @@ type Map struct {
|
||||
World *world.World
|
||||
Portals []*portal.Portal
|
||||
BackgroundColor color.NRGBA
|
||||
|
||||
bakedImage *ebiten.Image
|
||||
}
|
||||
|
||||
func NewMap(id string, number int, displayName string, width, height float64) *Map {
|
||||
@@ -44,6 +47,38 @@ func (m *Map) GetPortalByID(id string) *portal.Portal {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Renders the static world geometry to an offscreen image
|
||||
func (m *Map) Bake() {
|
||||
if m.Width <= 0 || m.Height <= 0 {
|
||||
return
|
||||
}
|
||||
m.bakedImage = ebiten.NewImage(int(m.Width), int(m.Height))
|
||||
m.bakedImage.Fill(m.BackgroundColor)
|
||||
m.World.Draw(m.bakedImage)
|
||||
}
|
||||
|
||||
func (m *Map) Draw(screen *ebiten.Image) {
|
||||
if m.bakedImage != nil {
|
||||
screen.DrawImage(m.bakedImage, nil)
|
||||
} else {
|
||||
screen.Fill(m.BackgroundColor)
|
||||
m.World.Draw(screen)
|
||||
}
|
||||
|
||||
// Draw portals
|
||||
for _, p := range m.Portals {
|
||||
p.Draw(screen)
|
||||
}
|
||||
}
|
||||
|
||||
// Resets the map state
|
||||
func (m *Map) Reset() {
|
||||
for _, p := range m.Portals {
|
||||
p.Enabled = true
|
||||
p.Visible = true
|
||||
}
|
||||
}
|
||||
|
||||
func CreateDefaultMaps(screenWidth, screenHeight float64) (*Map, *Map) {
|
||||
plains := CreatePlains(screenWidth, screenHeight)
|
||||
desert := CreateDesert(screenWidth, screenHeight)
|
||||
|
||||
Reference in New Issue
Block a user