From 8dfae5b8d86bd1ffe46c4fbb6dfe8884078262ca Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 14 Apr 2019 15:25:03 -0700 Subject: [PATCH] Play Mode: Fix Level Collision w/ Scrolling Fixes: * Move the call to CollidesWithGrid() inside the Canvas instead of outside in the PlayScene.movePlayer() so it can apply to all Actors in motion. * PlayScene.movePlayer() in turn just sets the player's Velocity so the Canvas.Loop() can move the actor itself. * When keeping the player inside the level boundaries: previously it was assuming the player Position was relative to the window, and was checking the WorldIndexAt and getting wrong results. * Canvas scrolling (loopFollowActor): check that the actor is getting close to the screen edge using the Viewport into the world, NOT the screen-relative coordinates of the Canvas bounding boxes. --- interface.go | 13 +++++++++++++ point.go | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/interface.go b/interface.go index 7d07503..092ff08 100644 --- a/interface.go +++ b/interface.go @@ -135,6 +135,19 @@ func (r Rect) AddPoint(other Point) Rect { } } +// SubtractPoint is the inverse of AddPoint. Use this only if you need to invert +// the Point being added. +// +// This does r.X - other.X, r.Y - other.Y and keeps the width/height the same. +func (r Rect) SubtractPoint(other Point) Rect { + return Rect{ + X: r.X - other.X, + Y: r.Y - other.Y, + W: r.W, + H: r.H, + } +} + // Text holds information for drawing text. type Text struct { Text string diff --git a/point.go b/point.go index 272238c..6ef757d 100644 --- a/point.go +++ b/point.go @@ -75,6 +75,12 @@ func (p *Point) Add(other Point) { p.Y += other.Y } +// Subtract the other point from your current point. +func (p *Point) Subtract(other Point) { + p.X -= other.X + p.Y -= other.Y +} + // MarshalText to convert the point into text so that a render.Point may be used // as a map key and serialized to JSON. func (p *Point) MarshalText() ([]byte, error) {