Added second (bigger) level.

Level is automatically scaled to available space.
This commit is contained in:
Sander Schobers 2019-12-28 18:40:40 +01:00
parent 3d4df7e956
commit efea350ae9
5 changed files with 46 additions and 14 deletions

View File

@ -151,7 +151,7 @@ func (g *game) loadAssets() error {
log.Printf("Loaded %d textures.\n", len(g.ctx.Textures))
log.Println("Loading levels...")
err = g.loadLevels("1")
err = g.loadLevels("1", "2")
if err != nil {
return err
}

View File

@ -13,7 +13,7 @@ type mainMenu struct {
func (m *mainMenu) Enter(ctx *Context) error {
m.ctx = ctx
m.Init()
m.Add("Play", func() { m.ctx.Navigation.playLevel("1") })
m.Add("Play", func() { m.ctx.Navigation.playLevel("2") })
m.Add("Settings", func() { m.ctx.Navigation.changeSettings() })
m.Add("Quit", func() { m.ctx.Navigation.quit() })
return nil

View File

@ -13,7 +13,8 @@ import (
type playLevel struct {
alui.ControlBase
ctx *Context
ctx *Context
init bool
menu *alui.Menu
showMenu bool
@ -84,16 +85,13 @@ func (l *playLevel) Enter(ctx *Context) error {
l.menu.Add("Quit to desktop", func() { l.ctx.Navigation.quit() })
l.menu.OnEscape = func() { l.showMenu = false }
l.init = true
l.state.Init(l.ctx, l.name)
return nil
}
func (l *playLevel) Leave() {}
func (l *playLevel) posToScreen(p geom.Point) geom.PointF32 {
return l.posToScreenF32(p.ToF32(), 0)
}
func (l *playLevel) posToScreenF32(p geom.PointF32, z float32) geom.PointF32 {
pos := l.posToCabinet(p.Add2D(.5, .5)).Add2D(0, z)
return pos.Mul(l.scale).Add(l.offset)
@ -118,13 +116,32 @@ func (l *playLevel) posToCabinet(p geom.PointF32) geom.PointF32 {
// Offset between vertical tiles: -25,40
func (l *playLevel) Layout(ctx *alui.Context, bounds geom.RectangleF32) {
l.scale = bounds.Dy() / 1080
l.offset = geom.PointF32{}
if l.init {
l.scale = 1
l.offset = geom.PointF32{}
level := l.state.Level()
tilesCenter := l.posToScreenF32(geom.PtF32(.5*float32(level.width), .5*float32(level.height)), 0)
center := bounds.Center()
l.offset = geom.PtF32(center.X-tilesCenter.X, center.Y-tilesCenter.Y)
level := l.state.Level()
var contentCenter = l.posToScreenF32(geom.PtF32(.5*float32(level.width), .5*float32(level.height)), 0)
var content = geom.RectF32(contentCenter.X, contentCenter.Y, contentCenter.X, contentCenter.Y)
for idx, tile := range l.state.Level().tiles {
if tile == tileNothing || tile == tileInvalid {
continue
}
pos := level.idxToPos(idx).ToF32()
bottomLeft := l.posToScreenF32(pos.Add2D(-1.5, 1.5), 100)
content.Min = geom.MinPtF32(content.Min, bottomLeft)
content.Max = geom.MaxPtF32(content.Max, bottomLeft)
topRight := l.posToScreenF32(pos.Add2D(1.5, -1.5), 0)
content.Min = geom.MinPtF32(content.Min, topRight)
content.Max = geom.MaxPtF32(content.Max, topRight)
}
l.scale = geom.Min32(bounds.Dx()/content.Dx(), bounds.Dy()/content.Dy())
contentCenter = content.Center().Mul(l.scale)
center := bounds.Center()
l.offset = geom.PtF32(center.X-contentCenter.X, center.Y-contentCenter.Y)
l.init = false
}
l.state.Tick(l.ctx.Tick)
}

View File

@ -43,7 +43,9 @@ type playLevelState struct {
func (s *playLevelState) Entities() []*entity {
var entities []*entity
entities = append(entities, s.player)
entities = append(entities, s.villain)
if s.villain != nil {
entities = append(entities, s.villain)
}
entities = append(entities, s.bricks...)
return entities
}

View File

@ -0,0 +1,13 @@
level:
._._._._._._._._._._._._._._._._._._._._
._._._._._#_#_#_._._._._._._._._._._._._
._._._._._#B#_#_._._._._._._._._._._._._
._._._._._#_#_#B._._._._._._._._._._._._
._._._#_#_#B#_#B#_._._._._._._._._._._._
._._._#_._#_._._#_._._._._#X#_~_~_~_#_._
._#_#_#_._#_._._#_._._._._._._._~_~_#_._
._#_#B#_#_#B#_#_#_#_#_#_#_#_#_#_~_#_#_._
._._._._._#_._._._#_._#@._._._._#_#_._._
._._._._._#_#_#_#_#_._._._._._._._._._._
._._._._._._._._._._._._._._._._._._._._
:level