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
var (
debug bool
edit bool
)
func init() {
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() {
@ -31,7 +33,11 @@ func main() {
app := doodle.New(debug)
if filename != "" {
app.LoadLevel(filename)
if edit {
app.EditLevel(filename)
} else {
app.PlayLevel(filename)
}
}
app.Run()
}

View File

@ -139,8 +139,8 @@ func (d *Doodle) Run() error {
return nil
}
// LoadLevel loads a map from JSON into the EditorScene.
func (d *Doodle) LoadLevel(filename string) error {
// EditLevel loads a map from JSON into the EditorScene.
func (d *Doodle) EditLevel(filename string) error {
log.Info("Loading level from file: %s", filename)
scene := &EditorScene{}
err := scene.LoadLevel(filename)
@ -151,6 +151,18 @@ func (d *Doodle) LoadLevel(filename string) error {
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
type Pixel struct {
start bool

View File

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

View File

@ -15,6 +15,10 @@ type State struct {
// Screenshot key.
ScreenshotKey *BoolTick
Up *BoolTick
Left *BoolTick
Right *BoolTick
Down *BoolTick
// Cursor positions.
CursorX *Int32Tick
@ -27,6 +31,10 @@ func New() *State {
Button1: &BoolTick{},
Button2: &BoolTick{},
ScreenshotKey: &BoolTick{},
Up: &BoolTick{},
Left: &BoolTick{},
Right: &BoolTick{},
Down: &BoolTick{},
CursorX: &Int32Tick{},
CursorY: &Int32Tick{},
}
@ -107,6 +115,14 @@ func (s *State) Poll(ticks uint64) (*State, error) {
switch t.Keysym.Scancode {
case sdl.SCANCODE_F12:
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(
"FPS: %d (%dms) (%d,%d) F12=screenshot",
"FPS: %d (%dms) (%d,%d) S:%s F12=screenshot",
fpsCurrent,
fpsSkipped,
d.events.CursorX.Now,
d.events.CursorY.Now,
d.scene.Name(),
)
render.StrokedText(render.TextConfig{
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
// state information.
type Scene interface {
Name() string
Setup(*Doodle) error
Loop(*Doodle) error
}