2019-05-29 04:43:30 +00:00
|
|
|
package uix
|
|
|
|
|
|
|
|
import (
|
2019-07-05 22:02:22 +00:00
|
|
|
"errors"
|
2019-05-29 04:43:30 +00:00
|
|
|
"time"
|
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/collision"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/physics"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/scripting"
|
2019-12-28 03:16:34 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2022-01-17 04:09:27 +00:00
|
|
|
"github.com/dop251/goja"
|
2019-05-29 04:43:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// loopActorCollision is the Loop function that checks if pairs of
|
|
|
|
// actors are colliding with each other, and handles their scripting
|
|
|
|
// responses to such collisions.
|
|
|
|
func (w *Canvas) loopActorCollision() error {
|
2019-07-05 22:02:22 +00:00
|
|
|
if w.scripting == nil {
|
|
|
|
return errors.New("Canvas.loopActorCollision: scripting engine not attached to Canvas")
|
|
|
|
}
|
|
|
|
|
2019-05-29 04:43:30 +00:00
|
|
|
var (
|
|
|
|
// Current time of this tick so we can advance animations.
|
|
|
|
now = time.Now()
|
|
|
|
|
|
|
|
// As we iterate over all actors below to process their movement, track
|
|
|
|
// their bounding rectangles so we can later see if any pair of actors
|
|
|
|
// intersect each other. Also, in case of actor scripts protesting a
|
|
|
|
// collision later, store each actor's original position before the move.
|
|
|
|
boxes = make([]render.Rect, len(w.actors))
|
|
|
|
originalPositions = map[string]render.Point{}
|
2024-05-29 05:36:53 +00:00
|
|
|
originalHitboxes = map[string]render.Rect{} // original world hitboxes
|
2019-05-29 04:43:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Loop over all the actors in parallel, processing their movement and
|
|
|
|
// checking collision data against the level geometry.
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
// NOTE: parallelism wasn't good for race conditions like the Thief
|
|
|
|
// trying to take your inventory.
|
|
|
|
// var wg sync.WaitGroup
|
2019-05-29 04:43:30 +00:00
|
|
|
for i, a := range w.actors {
|
2022-01-19 05:24:36 +00:00
|
|
|
if a.IsFrozen() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
// wg.Add(1)
|
|
|
|
//go
|
|
|
|
func(i int, a *Actor) {
|
|
|
|
// defer wg.Done()
|
2019-05-29 04:43:30 +00:00
|
|
|
originalPositions[a.ID()] = a.Position()
|
2024-05-29 05:36:53 +00:00
|
|
|
originalHitboxes[a.ID()] = collision.GetBoundingRectHitbox(a, a.Hitbox())
|
2019-05-29 04:43:30 +00:00
|
|
|
|
|
|
|
// Advance any animations for this actor.
|
2024-07-08 05:45:21 +00:00
|
|
|
// TODO: wallclock time here, should be set by FPS for consistency.
|
2019-05-29 04:43:30 +00:00
|
|
|
if a.activeAnimation != nil && a.activeAnimation.nextFrameAt.Before(now) {
|
|
|
|
if done := a.TickAnimation(a.activeAnimation); done {
|
2019-12-31 02:13:28 +00:00
|
|
|
// Animation has finished, get the callback function.
|
|
|
|
callback := a.animationCallback
|
2019-05-29 04:43:30 +00:00
|
|
|
|
2019-12-31 02:13:28 +00:00
|
|
|
// Clean up the animation state, in case the callback wants
|
|
|
|
// to immediately play another animation.
|
2019-05-29 04:43:30 +00:00
|
|
|
a.StopAnimation()
|
2019-12-31 02:13:28 +00:00
|
|
|
|
|
|
|
// Call the callback function.
|
2022-01-17 04:09:27 +00:00
|
|
|
if function, ok := goja.AssertFunction(callback); ok {
|
|
|
|
function(goja.Undefined())
|
2019-12-31 02:13:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the actor's velocity to see if it's moving this tick.
|
|
|
|
v := a.Velocity()
|
2020-04-05 04:00:32 +00:00
|
|
|
|
|
|
|
// Apply gravity to the actor's velocity.
|
|
|
|
if a.hasGravity && !a.Grounded() { //v.Y >= 0 {
|
|
|
|
if !a.Grounded() {
|
2024-02-07 03:04:47 +00:00
|
|
|
var (
|
|
|
|
gravity = balance.GravityMaximum
|
|
|
|
acceleration = balance.GravityAcceleration
|
|
|
|
)
|
2022-05-06 04:35:32 +00:00
|
|
|
if a.IsWet() {
|
|
|
|
gravity = balance.SwimGravity
|
|
|
|
}
|
2024-02-07 03:04:47 +00:00
|
|
|
|
|
|
|
// If the actor is jumping/moving upwards, apply softer gravity.
|
|
|
|
if v.Y < 0 {
|
|
|
|
acceleration = balance.GravityJumpAcceleration
|
|
|
|
}
|
|
|
|
|
2020-04-05 04:00:32 +00:00
|
|
|
v.Y = physics.Lerp(
|
2022-05-06 04:35:32 +00:00
|
|
|
v.Y, // current speed
|
|
|
|
gravity, // target max gravity falling downwards
|
2024-02-07 03:04:47 +00:00
|
|
|
acceleration,
|
2020-04-05 04:00:32 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
v.Y = 0
|
|
|
|
}
|
|
|
|
a.SetVelocity(v)
|
|
|
|
// v.Y += balance.Gravity
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If not moving, grab the bounding box right now.
|
2020-04-05 04:00:32 +00:00
|
|
|
if v.IsZero() {
|
|
|
|
boxes[i] = collision.GetBoundingRect(a)
|
2019-05-29 04:43:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a delta point from their current location to where they
|
|
|
|
// want to move to this tick.
|
2020-04-05 04:00:32 +00:00
|
|
|
delta := physics.VectorFromPoint(a.Position())
|
2019-05-29 04:43:30 +00:00
|
|
|
delta.Add(v)
|
|
|
|
|
|
|
|
// Check collision with level geometry.
|
2020-04-05 04:00:32 +00:00
|
|
|
chkPoint := delta.ToPoint()
|
2022-05-06 04:35:32 +00:00
|
|
|
info, _ := collision.CollidesWithGrid(a, w.chunks, chkPoint)
|
|
|
|
|
|
|
|
// Inform the caller about the collision state every tick
|
|
|
|
if w.OnLevelCollision != nil {
|
|
|
|
w.OnLevelCollision(a, info)
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
2020-01-03 04:23:27 +00:00
|
|
|
|
|
|
|
// Move us back where the collision check put us
|
|
|
|
if !a.noclip {
|
2020-04-05 04:00:32 +00:00
|
|
|
delta = physics.VectorFromPoint(info.MoveTo)
|
2020-01-03 04:23:27 +00:00
|
|
|
}
|
2019-05-29 04:43:30 +00:00
|
|
|
|
|
|
|
// Move the actor's World Position to the new location.
|
2020-04-05 04:00:32 +00:00
|
|
|
a.MoveTo(delta.ToPoint())
|
2019-05-29 04:43:30 +00:00
|
|
|
|
|
|
|
// Keep the actor from leaving the world borders of bounded maps.
|
|
|
|
w.loopContainActorsInsideLevel(a)
|
|
|
|
|
|
|
|
// Store this actor's bounding box after they've moved.
|
2024-05-29 05:36:53 +00:00
|
|
|
boxes[i] = collision.GetBoundingRect(a)
|
2019-05-29 04:43:30 +00:00
|
|
|
}(i, a)
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
// wg.Wait()
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 05:36:53 +00:00
|
|
|
// log.Warn("== BEGIN BetweenBoxes")
|
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
// Check pairs of all our Actor boxes for overlap and running their OnCollide
|
|
|
|
// scripts for mobile actors.
|
|
|
|
var collidingActors = ActorCollisionMap{}
|
2019-05-29 04:43:30 +00:00
|
|
|
for tuple := range collision.BetweenBoxes(boxes) {
|
2024-07-08 05:45:21 +00:00
|
|
|
|
|
|
|
// Give the A, B tuple of boxes names: their order doesn't matter.
|
|
|
|
// Example: stable could be the Button and mover is the Player walking onto it.
|
|
|
|
// Or: stable could be the Player and mover is a Key that they walked onto.
|
|
|
|
stable, mover := w.actors[tuple.A], w.actors[tuple.B]
|
2020-01-03 01:58:22 +00:00
|
|
|
|
|
|
|
// If neither actor is mobile, don't run collision handlers.
|
2024-07-08 05:45:21 +00:00
|
|
|
if !(stable.IsMobile() || mover.IsMobile()) {
|
2020-01-03 01:58:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
collidingActors.Set(stable, mover)
|
2019-05-29 04:43:30 +00:00
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
log.Error("between boxes: %+v A=<%s> B=<%s>", tuple, stable.ID(), mover.ID())
|
2020-04-05 04:00:32 +00:00
|
|
|
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
// Call the OnCollide handler for A informing them of B's intersection.
|
2019-05-29 04:43:30 +00:00
|
|
|
if w.scripting != nil {
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
var (
|
2024-07-08 05:45:21 +00:00
|
|
|
rect = collision.GetBoundingRectHitbox(mover, mover.Hitbox())
|
2024-05-29 05:36:53 +00:00
|
|
|
// lastGoodBox = rect
|
2019-12-31 02:13:28 +00:00
|
|
|
lastGoodBox = render.Rect{
|
2024-05-29 05:36:53 +00:00
|
|
|
// Level Positions of the doodad is based on the top left
|
|
|
|
// of its graphical sprite, not its (possibly offset) hitbox.
|
2024-07-08 05:45:21 +00:00
|
|
|
X: originalPositions[mover.ID()].X,
|
|
|
|
Y: originalPositions[mover.ID()].Y,
|
2019-12-31 02:13:28 +00:00
|
|
|
W: boxes[tuple.B].W,
|
|
|
|
H: boxes[tuple.B].H,
|
|
|
|
}
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
)
|
|
|
|
|
2020-01-03 06:05:49 +00:00
|
|
|
// HACK: below, when we determine the moving actor is "onTop" of
|
|
|
|
// the doodad's solid hitbox, we lockY their movement so they don't
|
|
|
|
// fall down further; but sometimes there's an off-by-one error if
|
|
|
|
// the actor fell a distance before landing, and so the final
|
|
|
|
// Settled collision check doesn't fire (i.e. if they fell onto a
|
|
|
|
// Crumbly Floor which should begin shaking when walked on).
|
|
|
|
//
|
|
|
|
// When we decide they're onTop, record the Y position, and then
|
|
|
|
// use it for collision-check purposes but DON'T physically move
|
|
|
|
// the character by it (moving the character may clip them thru
|
|
|
|
// other solid hitboxes like the upside-down trapdoor)
|
2024-07-08 05:45:21 +00:00
|
|
|
// var onTopY int
|
2020-01-03 06:05:49 +00:00
|
|
|
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
// Firstly we want to make sure B isn't able to clip through A's
|
|
|
|
// solid hitbox if A protests the movement. Trace a vector from
|
|
|
|
// B's original position to their current one and ping A's
|
|
|
|
// OnCollide handler for each step, with Settled=false. A should
|
|
|
|
// only return false if it protests the movement, but not trigger
|
|
|
|
// any actions (such as emit messages to linked doodads) until
|
|
|
|
// Settled=true.
|
2024-07-08 05:45:21 +00:00
|
|
|
if origHitbox, ok := originalHitboxes[mover.ID()]; ok {
|
2019-12-31 02:13:28 +00:00
|
|
|
|
2020-01-03 06:05:49 +00:00
|
|
|
var (
|
2024-07-08 05:45:21 +00:00
|
|
|
// Special case for when a mobile actor lands ON TOP OF a solid
|
|
|
|
// actor. We want to stop their Y movement downwards, but allow
|
|
|
|
// horizontal movement on the X axis.
|
|
|
|
// Touching the solid actor from the side is already fine.
|
|
|
|
onTop bool
|
|
|
|
onBottom bool // they hit the bottom instead
|
|
|
|
// onSide bool // they hit a side, maybe allow Y movement
|
|
|
|
|
|
|
|
// If we lock their movement coordinate.
|
|
|
|
lockX *int
|
|
|
|
lockY *int
|
2020-01-03 06:05:49 +00:00
|
|
|
)
|
|
|
|
|
2024-05-29 05:36:53 +00:00
|
|
|
// If their original hitbox is offset from their sprite corner,
|
|
|
|
// gather the offset now.
|
|
|
|
var (
|
2024-07-08 05:45:21 +00:00
|
|
|
origPosition = originalPositions[mover.ID()]
|
2024-05-29 05:36:53 +00:00
|
|
|
hitboxPadding = render.Point{
|
|
|
|
X: render.AbsInt(origHitbox.X - origPosition.X),
|
|
|
|
Y: render.AbsInt(origHitbox.Y - origPosition.Y),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
// Trace a vector back from the mover's current position
|
|
|
|
// to where they originated from. If A protests B's position at
|
|
|
|
// ANY time, we ?mark didProtest=true? and continue backscanning
|
|
|
|
// B's movement. The next time A does NOT protest, that is to be
|
|
|
|
// B's new position.
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
for point := range render.IterLine(
|
2024-05-29 05:36:53 +00:00
|
|
|
origHitbox.Point(),
|
2024-07-08 05:45:21 +00:00
|
|
|
mover.Position(), // TODO: verify non 0,0 hitbox doodads work
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
) {
|
2020-01-03 06:05:49 +00:00
|
|
|
point := point
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
test := render.Rect{
|
|
|
|
X: point.X,
|
|
|
|
Y: point.Y,
|
|
|
|
W: rect.W,
|
|
|
|
H: rect.H,
|
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := collision.CompareBoxes(boxes[tuple.A], test); err == nil {
|
2024-05-29 05:36:53 +00:00
|
|
|
// A and B have their drawings overlapping on the page. Get each
|
|
|
|
// of their declared hitboxes (if smaller) to see if their hitboxes
|
|
|
|
// intersect as well.
|
|
|
|
var (
|
2024-07-08 05:45:21 +00:00
|
|
|
stableHitbox = collision.GetBoundingRectHitbox(stable, stable.Hitbox())
|
|
|
|
moverHitbox = collision.GetBoundingRectHitbox(mover, mover.Hitbox())
|
2024-05-29 05:36:53 +00:00
|
|
|
)
|
|
|
|
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
// B is overlapping A's box, call its OnCollide handler
|
|
|
|
// with Settled=false and see if it protests the overlap.
|
2024-07-08 05:45:21 +00:00
|
|
|
err := w.scripting.To(stable.ID()).Events.RunCollide(&CollideEvent{
|
|
|
|
Actor: mover,
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
Overlap: info.Overlap,
|
2024-07-08 05:45:21 +00:00
|
|
|
InHitbox: stableHitbox.Intersects(moverHitbox),
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
Settled: false,
|
|
|
|
})
|
|
|
|
|
2024-05-29 05:36:53 +00:00
|
|
|
// log.Warn("ActorCollision: CompareBoxes info was %+v", info)
|
|
|
|
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
// Did A protest?
|
|
|
|
if err == scripting.ErrReturnFalse {
|
2019-12-31 02:13:28 +00:00
|
|
|
// Are they on top?
|
2024-05-29 05:36:53 +00:00
|
|
|
var (
|
2024-07-08 05:45:21 +00:00
|
|
|
stableTop = stableHitbox.Y
|
|
|
|
stableBottom = stableHitbox.Y + stableHitbox.H
|
|
|
|
moverTop = test.Y
|
|
|
|
moverBottom = test.Y + test.H // bottom of falling actor
|
2024-05-29 05:36:53 +00:00
|
|
|
)
|
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
// Is the colliding actor on top? (e.g. mover=player character)
|
|
|
|
if render.AbsInt(moverBottom-stableTop) < balance.OnTopThreshold {
|
2019-12-31 02:13:28 +00:00
|
|
|
onTop = true
|
2024-07-08 05:45:21 +00:00
|
|
|
// onTopY = stableHitbox.Y
|
2024-05-29 05:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Or are they hitting from below?
|
2024-07-08 05:45:21 +00:00
|
|
|
if render.AbsInt(stableBottom-moverTop) < balance.OnTopThreshold {
|
2024-05-29 05:36:53 +00:00
|
|
|
onBottom = true
|
2019-12-31 02:13:28 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
if onTop || onBottom {
|
|
|
|
log.Error("onTop=%+v onBottom=%+v", onTop, onBottom)
|
|
|
|
}
|
|
|
|
|
2019-12-31 02:13:28 +00:00
|
|
|
// What direction were we moving?
|
|
|
|
if test.Y != lastGoodBox.Y {
|
2024-07-08 05:45:21 +00:00
|
|
|
|
|
|
|
// If we are hitting the top or bottom, lock our Y coordinate here.
|
|
|
|
if onTop || onBottom {
|
|
|
|
|
|
|
|
// First Y coordinate before the protested collision.
|
|
|
|
if lockY == nil {
|
|
|
|
lockY = new(int)
|
|
|
|
*lockY = lastGoodBox.Y
|
|
|
|
if onBottom {
|
|
|
|
*lockY -= hitboxPadding.Y
|
|
|
|
}
|
2024-05-29 05:36:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
// If on top, set the mover to Grounded here.
|
|
|
|
if onTop {
|
|
|
|
mover.SetGrounded(true)
|
|
|
|
}
|
2020-01-03 06:05:49 +00:00
|
|
|
}
|
2024-07-08 05:45:21 +00:00
|
|
|
|
2019-12-31 02:13:28 +00:00
|
|
|
}
|
|
|
|
if test.X != lastGoodBox.X {
|
2024-07-08 05:45:21 +00:00
|
|
|
if lockX == nil && !(onTop || onBottom) {
|
|
|
|
lockX = new(int)
|
|
|
|
*lockX = lastGoodBox.X
|
2019-12-31 02:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 06:05:49 +00:00
|
|
|
// Move them back to the last good box.
|
2024-05-29 05:36:53 +00:00
|
|
|
lastGoodBox = render.Rect{
|
2024-07-08 05:45:21 +00:00
|
|
|
X: test.X, // - hitboxPadding.X, // note: this is in World Coordinates
|
|
|
|
Y: test.Y, // - hitboxPadding.Y,
|
2024-05-29 05:36:53 +00:00
|
|
|
W: test.W,
|
|
|
|
H: test.H,
|
|
|
|
}
|
2024-07-08 05:45:21 +00:00
|
|
|
if lockX != nil {
|
|
|
|
lastGoodBox.X = *lockX - hitboxPadding.X
|
2019-12-31 02:13:28 +00:00
|
|
|
}
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
} else {
|
2022-01-19 05:24:36 +00:00
|
|
|
if err != nil {
|
2024-07-08 05:45:21 +00:00
|
|
|
log.Error("RunCollide on %s (%s) errored: %s", stable.ID(), stable.Actor.Filename, err)
|
2022-01-19 05:24:36 +00:00
|
|
|
}
|
2020-01-03 06:05:49 +00:00
|
|
|
// Move them back to the last good box.
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
lastGoodBox = test
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
2020-01-03 06:05:49 +00:00
|
|
|
} else {
|
|
|
|
// No collision between boxes, increment the lastGoodBox
|
|
|
|
lastGoodBox = test
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 06:05:49 +00:00
|
|
|
// Did we lock their X or Y coordinate from moving further?
|
2024-07-08 05:45:21 +00:00
|
|
|
if lockY != nil {
|
|
|
|
lastGoodBox.Y = *lockY
|
2020-01-03 06:05:49 +00:00
|
|
|
}
|
2024-07-08 05:45:21 +00:00
|
|
|
if lockX != nil {
|
|
|
|
lastGoodBox.X = *lockX
|
2020-01-03 06:05:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
if !mover.noclip {
|
2024-05-29 05:36:53 +00:00
|
|
|
log.Error("Move B to: %s", lastGoodBox.Point())
|
|
|
|
|
|
|
|
// The stationary doodad should move the moving one only.
|
2024-07-08 05:45:21 +00:00
|
|
|
mover.MoveTo(lastGoodBox.Point())
|
2020-01-03 04:23:27 +00:00
|
|
|
}
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
} else {
|
|
|
|
log.Error(
|
|
|
|
"ERROR: Actors %s and %s overlap and the script returned false,"+
|
|
|
|
"but I didn't store %s original position earlier??",
|
2024-07-08 05:45:21 +00:00
|
|
|
stable.Doodad().Title, mover.Doodad().Title, mover.Doodad().Title,
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-08 05:45:21 +00:00
|
|
|
// TODO: onTopY != nil
|
|
|
|
// if onTopY != 0 && lastGoodBox.Y-onTopY <= 1 {
|
|
|
|
// lastGoodBox.Y = onTopY
|
|
|
|
// }
|
2020-01-03 06:05:49 +00:00
|
|
|
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
// Movement has been settled. Check if B's point is still invading
|
|
|
|
// A's box and call its OnCollide handler one last time in
|
|
|
|
// Settled=true mode so it can run its actions.
|
|
|
|
if info, err := collision.CompareBoxes(boxes[tuple.A], lastGoodBox); err == nil {
|
2024-07-08 05:45:21 +00:00
|
|
|
if err := w.scripting.To(stable.ID()).Events.RunCollide(&CollideEvent{
|
|
|
|
Actor: mover,
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
Overlap: info.Overlap,
|
2024-07-08 05:45:21 +00:00
|
|
|
InHitbox: info.Overlap.Intersects(stable.Hitbox()),
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
Settled: true,
|
|
|
|
}); err != nil && err != scripting.ErrReturnFalse {
|
2024-07-08 05:45:21 +00:00
|
|
|
log.Error("VM(%s).RunCollide: %s", stable.ID(), err.Error())
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
2021-01-03 23:19:21 +00:00
|
|
|
|
|
|
|
// If the (player) is pressing the Use key, call the colliding
|
|
|
|
// actor's OnUse event.
|
2024-07-08 05:45:21 +00:00
|
|
|
if mover.flagUsing {
|
|
|
|
if err := w.scripting.To(stable.ID()).Events.RunUse(&UseEvent{
|
|
|
|
Actor: mover,
|
2021-01-03 23:19:21 +00:00
|
|
|
}); err != nil {
|
2024-07-08 05:45:21 +00:00
|
|
|
log.Error("VM(%s).RunUse: %s", stable.ID(), err.Error())
|
2021-01-03 23:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 05:36:53 +00:00
|
|
|
log.Warn("-- END BetweenBoxes")
|
|
|
|
|
2019-05-29 04:43:30 +00:00
|
|
|
// Check for lacks of collisions since last frame.
|
2024-07-08 05:45:21 +00:00
|
|
|
// Note: w.collidingActors is "last frame's" map of colliding actor boxes.
|
|
|
|
w.collidingActors.Iter(func(stable, mover *Actor) {
|
|
|
|
|
|
|
|
// Are these not colliding this frame?
|
|
|
|
// TODO: does this work with three-way actor collisions?
|
|
|
|
if !collidingActors.Exists(stable, mover) {
|
|
|
|
w.scripting.To(stable.ID()).Events.RunLeave(&CollideEvent{
|
|
|
|
Actor: mover,
|
Thief and Inventory APIs
This commit adds the Thief character with starter graphics
(no animations).
The Thief walks back and forth and will steal items from other
doodads, including the player. For singleton items that have no
quantity, like the Colored Keys, the Thief will only steal one
if he does not already have it. Quantitied items like the
Small Key are always stolen.
Flexibility in the playable character is introduced: Boy,
Azulian, Bird, and Thief all respond to playable controls.
There is not currently a method to enable these apart from
modifying balance.PlayerCharacterDoodad at compile time.
New and Changed Doodads
* Thief: new doodad that walks back and forth and will steal
items from other characters inventory.
* Bird: has no inventory and cannot pick up items, unless player
controlled. Its hitbox has also been fixed so it collides with
floors correctly - not something normally seen in the Bird.
* Boy: opts in to have inventory.
* Keys (all): only gives themselves to actors having inventories.
JavaScript API - New functions available
* Self.IsPlayer() - returns if the current actor IS the player.
* Self.SetInventory(bool) - doodads must opt-in to having an
inventory. Keys should only give themselves to doodads having
an inventory.
* Self.HasInventory() bool
* Self.AddItem(filename, qty)
* Self.RemoveItem(filename, qty)
* Self.HasItem(filename)
* Self.Inventory() - returns map[string]int
* Self.ClearInventory()
* Self.OnLeave(func(e)) now receives a CollideEvent as parameter
instead of the useless actor ID. Notably, e.Actor is the
leaving actor and e.Settled is always true.
Other Changes
* Play Mode: if playing as a character which doesn't obey gravity,
such as the bird, antigravity controls are enabled by default.
If you `import antigravity` you can turn gravity back on.
* Doodad collision scripts are no longer run in parallel
goroutines. It made the Thief's job difficult trying to steal
items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
|
|
|
Settled: true,
|
|
|
|
})
|
2019-05-29 04:43:30 +00:00
|
|
|
}
|
2024-07-08 05:45:21 +00:00
|
|
|
})
|
2019-05-29 04:43:30 +00:00
|
|
|
|
|
|
|
// Store this frame's colliding actors for next frame.
|
|
|
|
w.collidingActors = collidingActors
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-08 05:45:21 +00:00
|
|
|
|
|
|
|
// ActorCollisionMap keeps a cache of collision box overlaps between
|
|
|
|
// an Actor and one or more other Actors.
|
|
|
|
type ActorCollisionMap map[*Actor]map[*Actor]interface{}
|
|
|
|
|
|
|
|
// Set a collision to the other actor.
|
|
|
|
func (m ActorCollisionMap) Set(stable, mover *Actor) {
|
|
|
|
if m[stable] == nil {
|
|
|
|
m[stable] = map[*Actor]interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
m[stable][mover] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists checks if the actor is colliding with the other.
|
|
|
|
func (m ActorCollisionMap) Exists(stable, mover *Actor) bool {
|
|
|
|
if m[stable] == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := m[stable][mover]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iter the collision data.
|
|
|
|
func (m ActorCollisionMap) Iter(fn func(stable, mover *Actor)) {
|
|
|
|
for stable, moverMap := range m {
|
|
|
|
for mover := range moverMap {
|
|
|
|
fn(stable, mover)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|