2018-09-26 17:04:46 +00:00
|
|
|
package doodads
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.kirsle.net/apps/doodle/level"
|
|
|
|
"git.kirsle.net/apps/doodle/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Actor is a reusable run-time drawing component used in Doodle. Actors are an
|
|
|
|
// active instance of a Doodad which have a position, velocity, etc.
|
|
|
|
type Actor interface {
|
|
|
|
ID() string
|
|
|
|
|
|
|
|
// Position and velocity, not saved to disk.
|
|
|
|
Position() render.Point
|
|
|
|
Velocity() render.Point
|
Play: Autoscrolling and Bounded Level Support
Implement scrolling behavior in Play Mode by allowing the Canvas to
follow a specific actor and keep it in view. The Canvas has a
FollowActor property which holds an ID of the actor to follow (if blank,
no actor is being followed).
In Play Mode the Player is followed and when they get too close to the
left or right edges of the screen, the level will scroll to try and
catch them. If the player is moving very fast they can outrun the
camera.
The bounded levels are enforced in Play Mode and the camera won't scroll
to view pixels out-of-bounds and the Doodad actors inside the level
aren't allowed to exit its boundaries. This is global, not only for the
Player doodad but any Doodad that came with the level as well.
Other changes:
- Restructured Canvas widget code into many new files. The Canvas widget
is shaping up to be where most of the magic happens, which is okay
because it's close to the action and pulling the strings from outside
would be harder, even tho as a UI element you think it should be
lightweight.
- Debug Overlay: added room for Scenes to insert their own custom Debug
Overlay key/value pairs (the values are string pointers so the Scene
can update them freely):
- The core labels are FPS, Scene and Mouse. The Pixel (world
coordinate under cursor) is removed from the core labels.
- Edit Scene provides Pixel, Tool and Swatch
- Play Scene provides Pixel, Player, Viewport, Scroll
2018-10-29 00:33:24 +00:00
|
|
|
SetVelocity(render.Point)
|
2018-12-30 21:50:24 +00:00
|
|
|
Acceleration() int
|
|
|
|
SetAcceleration(int)
|
2018-09-26 17:04:46 +00:00
|
|
|
Size() render.Rect
|
|
|
|
Grounded() bool
|
|
|
|
SetGrounded(bool)
|
|
|
|
|
|
|
|
// Movement commands.
|
|
|
|
MoveBy(render.Point) // Add {X,Y} to current Position.
|
|
|
|
MoveTo(render.Point) // Set current Position to {X,Y}.
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBoundingRect computes the full pairs of points for the collision box
|
|
|
|
// around a doodad actor.
|
|
|
|
func GetBoundingRect(d Actor) render.Rect {
|
|
|
|
var (
|
|
|
|
P = d.Position()
|
|
|
|
S = d.Size()
|
|
|
|
)
|
|
|
|
return render.Rect{
|
|
|
|
X: P.X,
|
|
|
|
Y: P.Y,
|
|
|
|
W: S.W,
|
|
|
|
H: S.H,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanBoundingBox scans all of the pixels in a bounding box on the grid and
|
|
|
|
// returns if any of them intersect with level geometry.
|
|
|
|
func (c *Collide) ScanBoundingBox(box render.Rect, grid *level.Chunker) bool {
|
|
|
|
col := GetCollisionBox(box)
|
|
|
|
|
|
|
|
c.ScanGridLine(col.Top[0], col.Top[1], grid, Top)
|
|
|
|
c.ScanGridLine(col.Bottom[0], col.Bottom[1], grid, Bottom)
|
|
|
|
c.ScanGridLine(col.Left[0], col.Left[1], grid, Left)
|
|
|
|
c.ScanGridLine(col.Right[0], col.Right[1], grid, Right)
|
|
|
|
return c.IsColliding()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanGridLine scans all of the pixels between p1 and p2 on the grid and tests
|
|
|
|
// for any pixels to be set, implying a collision between level geometry and the
|
|
|
|
// bounding boxes of the doodad.
|
|
|
|
func (c *Collide) ScanGridLine(p1, p2 render.Point, grid *level.Chunker, side Side) {
|
|
|
|
for point := range render.IterLine2(p1, p2) {
|
|
|
|
if _, err := grid.Get(point); err == nil {
|
|
|
|
// A hit!
|
|
|
|
switch side {
|
|
|
|
case Top:
|
|
|
|
c.Top = true
|
|
|
|
c.TopPoint = point
|
|
|
|
case Bottom:
|
|
|
|
c.Bottom = true
|
|
|
|
c.BottomPoint = point
|
|
|
|
case Left:
|
|
|
|
c.Left = true
|
|
|
|
c.LeftPoint = point
|
|
|
|
case Right:
|
|
|
|
c.Right = true
|
|
|
|
c.RightPoint = point
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|