2020-01-15 18:03:52 +00:00
|
|
|
package soko
|
|
|
|
|
|
|
|
import (
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Level struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
2020-01-15 18:51:57 +00:00
|
|
|
Tiles []TileType
|
2020-01-15 18:03:52 +00:00
|
|
|
Entities []EntityType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Level) IdxToPos(i int) geom.Point { return geom.Pt(i%l.Width, i/l.Width) }
|
|
|
|
|
|
|
|
func (l Level) PosToIdx(p geom.Point) int {
|
|
|
|
if p.X < 0 || p.Y < 0 || p.X >= l.Width || p.Y >= l.Height {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return p.Y*l.Width + p.X
|
|
|
|
}
|
2020-01-15 20:25:32 +00:00
|
|
|
|
|
|
|
func (l Level) State() State {
|
|
|
|
s := NewState()
|
|
|
|
for i, t := range l.Tiles {
|
|
|
|
pos := l.IdxToPos(i)
|
|
|
|
switch t {
|
|
|
|
case TileTypeBasic:
|
|
|
|
s.BasicTiles.Add(pos)
|
|
|
|
case TileTypeMagma:
|
|
|
|
s.MagmaTiles.Add(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, e := range l.Entities {
|
|
|
|
pos := l.IdxToPos(i)
|
|
|
|
switch e {
|
|
|
|
case EntityTypeBrick:
|
|
|
|
s.AddBrick(pos)
|
|
|
|
case EntityTypePlayer:
|
|
|
|
s.SetPlayer(pos)
|
|
|
|
case EntityTypeTarget:
|
|
|
|
s.SetTarget(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|