Add initial Play scene

This commit is contained in:
Noah 2018-06-20 19:00:46 -07:00
parent ede3d58e1d
commit 90f1704886
7 changed files with 157 additions and 4 deletions

View File

@ -13,10 +13,12 @@ var Build string
// Command line args // Command line args
var ( var (
debug bool debug bool
edit bool
) )
func init() { func init() {
flag.BoolVar(&debug, "debug", false, "Debug mode") flag.BoolVar(&debug, "debug", false, "Debug mode")
flag.BoolVar(&edit, "edit", false, "Edit the map given on the command line. Default is to play the map.")
} }
func main() { func main() {
@ -31,7 +33,11 @@ func main() {
app := doodle.New(debug) app := doodle.New(debug)
if filename != "" { if filename != "" {
app.LoadLevel(filename) if edit {
app.EditLevel(filename)
} else {
app.PlayLevel(filename)
}
} }
app.Run() app.Run()
} }

View File

@ -139,8 +139,8 @@ func (d *Doodle) Run() error {
return nil return nil
} }
// LoadLevel loads a map from JSON into the EditorScene. // EditLevel loads a map from JSON into the EditorScene.
func (d *Doodle) LoadLevel(filename string) error { func (d *Doodle) EditLevel(filename string) error {
log.Info("Loading level from file: %s", filename) log.Info("Loading level from file: %s", filename)
scene := &EditorScene{} scene := &EditorScene{}
err := scene.LoadLevel(filename) err := scene.LoadLevel(filename)
@ -151,6 +151,18 @@ func (d *Doodle) LoadLevel(filename string) error {
return nil return nil
} }
// PlayLevel loads a map from JSON into the PlayScene.
func (d *Doodle) PlayLevel(filename string) error {
log.Info("Loading level from file: %s", filename)
scene := &PlayScene{}
err := scene.LoadLevel(filename)
if err != nil {
return err
}
d.Goto(scene)
return nil
}
// TODO: not a global // TODO: not a global
type Pixel struct { type Pixel struct {
start bool start bool

View File

@ -23,6 +23,11 @@ type EditorScene struct {
height int32 height int32
} }
// Name of the scene.
func (s *EditorScene) Name() string {
return "Edit"
}
// Setup the editor scene. // Setup the editor scene.
func (e *EditorScene) Setup(d *Doodle) error { func (e *EditorScene) Setup(d *Doodle) error {
if e.pixelHistory == nil { if e.pixelHistory == nil {

View File

@ -15,6 +15,10 @@ type State struct {
// Screenshot key. // Screenshot key.
ScreenshotKey *BoolTick ScreenshotKey *BoolTick
Up *BoolTick
Left *BoolTick
Right *BoolTick
Down *BoolTick
// Cursor positions. // Cursor positions.
CursorX *Int32Tick CursorX *Int32Tick
@ -27,6 +31,10 @@ func New() *State {
Button1: &BoolTick{}, Button1: &BoolTick{},
Button2: &BoolTick{}, Button2: &BoolTick{},
ScreenshotKey: &BoolTick{}, ScreenshotKey: &BoolTick{},
Up: &BoolTick{},
Left: &BoolTick{},
Right: &BoolTick{},
Down: &BoolTick{},
CursorX: &Int32Tick{}, CursorX: &Int32Tick{},
CursorY: &Int32Tick{}, CursorY: &Int32Tick{},
} }
@ -107,6 +115,14 @@ func (s *State) Poll(ticks uint64) (*State, error) {
switch t.Keysym.Scancode { switch t.Keysym.Scancode {
case sdl.SCANCODE_F12: case sdl.SCANCODE_F12:
s.ScreenshotKey.Push(t.State == 1) s.ScreenshotKey.Push(t.State == 1)
case sdl.SCANCODE_UP:
s.Up.Push(t.State == 1)
case sdl.SCANCODE_LEFT:
s.Left.Push(t.State == 1)
case sdl.SCANCODE_RIGHT:
s.Right.Push(t.State == 1)
case sdl.SCANCODE_DOWN:
s.Down.Push(t.State == 1)
} }
} }
} }

3
fps.go
View File

@ -27,11 +27,12 @@ func (d *Doodle) DrawDebugOverlay() {
} }
text := fmt.Sprintf( text := fmt.Sprintf(
"FPS: %d (%dms) (%d,%d) F12=screenshot", "FPS: %d (%dms) (%d,%d) S:%s F12=screenshot",
fpsCurrent, fpsCurrent,
fpsSkipped, fpsSkipped,
d.events.CursorX.Now, d.events.CursorX.Now,
d.events.CursorY.Now, d.events.CursorY.Now,
d.scene.Name(),
) )
render.StrokedText(render.TextConfig{ render.StrokedText(render.TextConfig{
Text: text, Text: text,

112
play_scene.go Normal file
View File

@ -0,0 +1,112 @@
package doodle
import (
"git.kirsle.net/apps/doodle/events"
"git.kirsle.net/apps/doodle/level"
"github.com/veandco/go-sdl2/sdl"
)
// PlayScene manages the "Edit Level" game mode.
type PlayScene struct {
canvas Grid
// Canvas size
width int32
height int32
// Player position and velocity.
x int32
y int32
vx int32
vy int32
}
// Name of the scene.
func (s *PlayScene) Name() string {
return "Play"
}
// Setup the play scene.
func (s *PlayScene) Setup(d *Doodle) error {
s.x = 10
s.y = 10
if s.canvas == nil {
s.canvas = Grid{}
}
s.width = d.width // TODO: canvas width = copy the window size
s.height = d.height
return nil
}
// Loop the editor scene.
func (s *PlayScene) Loop(d *Doodle) error {
s.PollEvents(d.events)
// Apply gravity.
return s.Draw(d)
}
// Draw the pixels on this frame.
func (s *PlayScene) Draw(d *Doodle) error {
// Clear the canvas and fill it with white.
d.renderer.SetDrawColor(255, 255, 255, 255)
d.renderer.Clear()
d.renderer.SetDrawColor(0, 0, 0, 255)
for pixel := range s.canvas {
d.renderer.DrawPoint(pixel.x, pixel.y)
}
// Draw our hero.
d.renderer.SetDrawColor(0, 0, 255, 255)
d.renderer.DrawRect(&sdl.Rect{
X: s.x,
Y: s.y,
W: 16,
H: 16,
})
// Draw the FPS.
d.DrawDebugOverlay()
d.renderer.Present()
return nil
}
// PollEvents checks the event state and updates variables.
func (s *PlayScene) PollEvents(ev *events.State) {
if ev.Down.Now {
s.y += 4
}
if ev.Left.Now {
s.x -= 4
}
if ev.Right.Now {
s.x += 4
}
if ev.Up.Now {
s.y -= 4
}
}
// LoadLevel loads a level from disk.
func (e *PlayScene) LoadLevel(filename string) error {
e.canvas = Grid{}
m, err := level.LoadJSON(filename)
if err != nil {
return err
}
for _, point := range m.Pixels {
pixel := Pixel{
x: point.X,
y: point.Y,
}
e.canvas[pixel] = nil
}
return nil
}

View File

@ -4,6 +4,7 @@ package doodle
// scene at a time and that scene has control over the main loop, and its own // scene at a time and that scene has control over the main loop, and its own
// state information. // state information.
type Scene interface { type Scene interface {
Name() string
Setup(*Doodle) error Setup(*Doodle) error
Loop(*Doodle) error Loop(*Doodle) error
} }