2018-06-21 02:00:46 +00:00
|
|
|
package doodle
|
|
|
|
|
|
|
|
import (
|
2018-07-24 03:10:53 +00:00
|
|
|
"git.kirsle.net/apps/doodle/doodads"
|
2018-06-21 02:00:46 +00:00
|
|
|
"git.kirsle.net/apps/doodle/events"
|
|
|
|
"git.kirsle.net/apps/doodle/level"
|
2018-07-22 00:12:22 +00:00
|
|
|
"git.kirsle.net/apps/doodle/render"
|
2018-06-21 02:00:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PlayScene manages the "Edit Level" game mode.
|
|
|
|
type PlayScene struct {
|
2018-07-24 03:10:53 +00:00
|
|
|
// Configuration attributes.
|
|
|
|
Filename string
|
2018-08-17 03:37:19 +00:00
|
|
|
Canvas *level.Grid
|
2018-07-24 03:10:53 +00:00
|
|
|
|
|
|
|
// Private variables.
|
2018-08-17 03:37:19 +00:00
|
|
|
canvas *level.Grid
|
2018-06-21 02:00:46 +00:00
|
|
|
|
|
|
|
// Canvas size
|
|
|
|
width int32
|
|
|
|
height int32
|
|
|
|
|
2018-07-24 03:10:53 +00:00
|
|
|
// Player character
|
2018-07-26 02:38:54 +00:00
|
|
|
Player doodads.Doodad
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the scene.
|
|
|
|
func (s *PlayScene) Name() string {
|
|
|
|
return "Play"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the play scene.
|
|
|
|
func (s *PlayScene) Setup(d *Doodle) error {
|
2018-07-24 03:10:53 +00:00
|
|
|
// Given a filename or map data to play?
|
|
|
|
if s.Canvas != nil {
|
|
|
|
log.Debug("PlayScene.Setup: loading map from given canvas")
|
|
|
|
s.canvas = s.Canvas
|
|
|
|
|
|
|
|
} else if s.Filename != "" {
|
|
|
|
log.Debug("PlayScene.Setup: loading map from file %s", s.Filename)
|
|
|
|
s.LoadLevel(s.Filename)
|
|
|
|
s.Filename = ""
|
|
|
|
}
|
|
|
|
|
2018-07-26 02:38:54 +00:00
|
|
|
s.Player = doodads.NewPlayer()
|
2018-06-21 02:00:46 +00:00
|
|
|
|
|
|
|
if s.canvas == nil {
|
2018-07-24 03:10:53 +00:00
|
|
|
log.Debug("PlayScene.Setup: no grid given, initializing empty grid")
|
2018-08-17 03:37:19 +00:00
|
|
|
s.canvas = &level.Grid{}
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
2018-07-24 03:10:53 +00:00
|
|
|
|
2018-06-21 02:00:46 +00:00
|
|
|
s.width = d.width // TODO: canvas width = copy the window size
|
|
|
|
s.height = d.height
|
2018-07-24 03:10:53 +00:00
|
|
|
|
|
|
|
d.Flash("Entered Play Mode. Press 'E' to edit this map.")
|
|
|
|
|
2018-06-21 02:00:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop the editor scene.
|
2018-07-22 00:12:22 +00:00
|
|
|
func (s *PlayScene) Loop(d *Doodle, ev *events.State) error {
|
2018-07-24 03:10:53 +00:00
|
|
|
// Switching to Edit Mode?
|
|
|
|
if ev.KeyName.Read() == "e" {
|
|
|
|
log.Info("Edit Mode, Go!")
|
|
|
|
d.Goto(&EditorScene{
|
2018-09-23 22:20:45 +00:00
|
|
|
// Canvas: s.canvas,
|
2018-07-24 03:10:53 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
s.movePlayer(ev)
|
2018-07-22 03:43:01 +00:00
|
|
|
return nil
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the pixels on this frame.
|
|
|
|
func (s *PlayScene) Draw(d *Doodle) error {
|
|
|
|
// Clear the canvas and fill it with white.
|
2018-07-22 00:12:22 +00:00
|
|
|
d.Engine.Clear(render.White)
|
2018-06-21 02:00:46 +00:00
|
|
|
|
2018-07-24 03:10:53 +00:00
|
|
|
s.canvas.Draw(d.Engine)
|
2018-06-21 02:00:46 +00:00
|
|
|
|
|
|
|
// Draw our hero.
|
2018-07-26 02:38:54 +00:00
|
|
|
s.Player.Draw(d.Engine)
|
2018-06-21 02:00:46 +00:00
|
|
|
|
2018-07-25 03:57:22 +00:00
|
|
|
// Draw out bounding boxes.
|
2018-07-26 02:38:54 +00:00
|
|
|
d.DrawCollisionBox(s.Player)
|
2018-07-25 03:57:22 +00:00
|
|
|
|
2018-06-21 02:00:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
// movePlayer updates the player's X,Y coordinate based on key pressed.
|
|
|
|
func (s *PlayScene) movePlayer(ev *events.State) {
|
2018-07-26 02:38:54 +00:00
|
|
|
delta := s.Player.Position()
|
2018-07-24 03:10:53 +00:00
|
|
|
var playerSpeed int32 = 8
|
|
|
|
var gravity int32 = 2
|
|
|
|
|
2018-06-21 02:00:46 +00:00
|
|
|
if ev.Down.Now {
|
2018-07-24 03:10:53 +00:00
|
|
|
delta.Y += playerSpeed
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
|
|
|
if ev.Left.Now {
|
2018-07-24 03:10:53 +00:00
|
|
|
delta.X -= playerSpeed
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
|
|
|
if ev.Right.Now {
|
2018-07-24 03:10:53 +00:00
|
|
|
delta.X += playerSpeed
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
|
|
|
if ev.Up.Now {
|
2018-07-24 03:10:53 +00:00
|
|
|
delta.Y -= playerSpeed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply gravity.
|
2018-07-25 03:57:22 +00:00
|
|
|
// var onFloor bool
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
info, ok := doodads.CollidesWithGrid(s.Player, s.canvas, delta)
|
2018-07-25 03:57:22 +00:00
|
|
|
if ok {
|
|
|
|
// Collision happened with world.
|
|
|
|
}
|
|
|
|
delta = info.MoveTo
|
|
|
|
|
|
|
|
// Apply gravity if not grounded.
|
2018-07-26 02:38:54 +00:00
|
|
|
if !s.Player.Grounded() {
|
2018-07-25 05:26:27 +00:00
|
|
|
// Gravity has to pipe through the collision checker, too, so it
|
|
|
|
// can't give us a cheated downward boost.
|
2018-07-25 03:57:22 +00:00
|
|
|
delta.Y += gravity
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
2018-07-24 03:10:53 +00:00
|
|
|
|
2018-07-26 02:38:54 +00:00
|
|
|
s.Player.MoveTo(delta)
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadLevel loads a level from disk.
|
2018-07-21 22:11:00 +00:00
|
|
|
func (s *PlayScene) LoadLevel(filename string) error {
|
2018-08-17 03:37:19 +00:00
|
|
|
s.canvas = &level.Grid{}
|
2018-06-21 02:00:46 +00:00
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// m, err := level.LoadJSON(filename)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2018-06-21 02:00:46 +00:00
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// for _, pixel := range m.Pixels {
|
|
|
|
// // *s.canvas[pixel] = nil
|
|
|
|
// }
|
2018-06-21 02:00:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-24 03:10:53 +00:00
|
|
|
|
|
|
|
// Destroy the scene.
|
|
|
|
func (s *PlayScene) Destroy() error {
|
|
|
|
return nil
|
|
|
|
}
|