Files
LilGuy/internal/maps/map.go

80 lines
2.2 KiB
Go

package maps
import (
"image/color"
"github.com/atridad/LilGuy/internal/config"
"github.com/atridad/LilGuy/internal/portal"
"github.com/atridad/LilGuy/internal/world"
)
type Map struct {
ID string
Number int
Width float64
Height float64
World *world.World
Portals []*portal.Portal
BackgroundColor color.NRGBA
}
func NewMap(id string, number int, width, height float64) *Map {
return &Map{
ID: id,
Number: number,
Width: width,
Height: height,
World: world.NewWorld(),
Portals: make([]*portal.Portal, 0),
BackgroundColor: color.NRGBA{R: 135, G: 206, B: 235, A: 255},
}
}
func (m *Map) AddPortal(p *portal.Portal) {
m.Portals = append(m.Portals, p)
}
func (m *Map) GetPortalByID(id string) *portal.Portal {
for _, p := range m.Portals {
if p.ID == id {
return p
}
}
return nil
}
func CreateDefaultMaps(screenWidth, screenHeight float64) (*Map, *Map) {
map1 := createMapFromConfig(config.DefaultMap1(), screenWidth, screenHeight)
map2 := createMapFromConfig(config.DefaultMap2(), screenWidth, screenHeight)
addPortalFromConfig(map1, config.LeftPortalMap1(), portal.SideLeft, screenWidth, screenHeight)
addPortalFromConfig(map1, config.RightPortalMap1(), portal.SideRight, screenWidth, screenHeight)
addPortalFromConfig(map2, config.LeftPortalMap2(), portal.SideLeft, screenWidth, screenHeight)
addPortalFromConfig(map2, config.RightPortalMap2(), portal.SideRight, screenWidth, screenHeight)
return map1, map2
}
func createMapFromConfig(cfg config.MapConfig, width, height float64) *Map {
m := NewMap(cfg.ID, cfg.Number, width, height)
m.BackgroundColor = cfg.BackgroundColor
m.World.AddSurface(&world.Surface{
X: 0,
Y: height - config.GroundHeight,
Width: width,
Height: config.GroundHeight,
Tag: world.TagGround,
Color: cfg.GroundColor,
})
return m
}
func addPortalFromConfig(m *Map, cfg config.PortalConfig, side portal.PortalSide, screenWidth, screenHeight float64) {
p := portal.CreateSidePortal(cfg.ID, side, screenWidth, screenHeight)
p.DestinationMap = cfg.DestinationMap
p.DestinationPortal = cfg.DestinationPortal
p.Color = cfg.Color
p.GlowColor = cfg.GlowColor
m.AddPortal(p)
}