Improve OnCollide Doodad Script Handling

* Events.OnCollide now receives a CollideEvent object, which makes
  available the .Actor who collided and the .Overlap rect which is
  zero-relative to the target actor. Doodad scripts can use the .Overlap
  to see WHERE in their own box the other actor has intruded.
  * Update the LockedDoor and ElectricDoor doodads to detect when the
    player has entered their inner rect (since their doors are narrower
    than their doodad size)
  * Update the Button doodads to only press in when the player actually
    touches them (because their sizes are shorter than their doodad
    height)
  * Update the Trapdoor to only trigger its animation when the board
    along its top has been touched, not when the empty space below was
    touched from the bottom.
* Events.OnLeave now implemented and fires when an actor who was
  previously intersecting your doodad has left.
* The engine detects when an event JS callback returns false.
  Eventually, the OnCollide can return false to signify the collision is
  not accepted and the actor should be bumped away as if they hit solid
  geometry.
This commit is contained in:
Noah 2019-05-06 22:57:32 -07:00
parent 21fcbbfa74
commit 13d40423cc
5 changed files with 78 additions and 19 deletions

View File

@ -3,7 +3,15 @@ function main() {
var timer = 0; var timer = 0;
Events.OnCollide( function() { Events.OnCollide(function(e) {
// Verify they've touched the button.
if (e.Overlap.Y + e.Overlap.H < 24) {
Self.Canvas.SetBackground(RGBA(0, 255, 0, 153));
return;
}
Self.Canvas.SetBackground(RGBA(255, 255, 0, 153));
if (timer > 0) { if (timer > 0) {
clearTimeout(timer); clearTimeout(timer);
} }
@ -13,5 +21,10 @@ function main() {
Self.ShowLayer(0); Self.ShowLayer(0);
timer = 0; timer = 0;
}, 200); }, 200);
});
Events.OnLeave(function(e) {
console.log("%s has stopped touching %s", e, Self.Doodad.Title)
Self.Canvas.SetBackground(RGBA(0, 0, 1, 0));
}) })
} }

View File

@ -1,7 +1,19 @@
function main() { function main() {
console.log("%s initialized!", Self.Doodad.Title); console.log("%s initialized!", Self.Doodad.Title);
Events.OnCollide( function() { var pressed = false;
Events.OnCollide(function(e) {
if (pressed) {
return;
}
// Verify they've touched the button.
if (e.Overlap.Y + e.Overlap.H < 24) {
return;
}
Self.ShowLayer(1); Self.ShowLayer(1);
}) pressed = true;
});
} }

View File

@ -1,16 +1,29 @@
function main() { function main() {
console.log("%s initialized!", Self.Doodad.Title); console.log("%s initialized!", Self.Doodad.Title);
var err = Self.AddAnimation("open", 100, [0, 1, 2, 3]); Self.AddAnimation("open", 100, [0, 1, 2, 3]);
console.error("door error: %s", err) Self.AddAnimation("close", 100, [3, 2, 1, 0]);
var animating = false; var animating = false;
var opened = false;
Events.OnCollide(function() { Events.OnCollide(function(e) {
if (animating) { if (animating || opened) {
return; return;
} }
if (e.Overlap.X + e.Overlap.W >= 16 && e.Overlap.X < 48) {
animating = true; animating = true;
Self.PlayAnimation("open", null); Self.PlayAnimation("open", function() {
opened = true;
animating = false;
}); });
}
});
Events.OnLeave(function() {
if (opened) {
Self.PlayAnimation("close", function() {
opened = false;
});
}
})
} }

View File

@ -3,6 +3,18 @@ function main() {
var unlocked = false; var unlocked = false;
Events.OnCollide(function(e) { Events.OnCollide(function(e) {
console.log("%s was touched by %s!", Self.Doodad.Title, e.Actor.ID());
console.log("my box: %+v and theirs: %+v", Self.GetBoundingRect(), e.Actor.GetBoundingRect());
console.warn("But the overlap is: %+v", e.Overlap);
console.log(Object.keys(e));
if (e.Overlap.X + e.Overlap.W >= 16 && e.Overlap.X < 48) {
Self.Canvas.SetBackground(RGBA(255, 0, 0, 153));
} else {
Self.Canvas.SetBackground(RGBA(0, 255, 0, 153));
return;
}
if (unlocked) { if (unlocked) {
return; return;
} }
@ -10,4 +22,8 @@ function main() {
unlocked = true; unlocked = true;
Self.PlayAnimation("open", null); Self.PlayAnimation("open", null);
}); });
Events.OnLeave(function(e) {
console.log("%s has stopped touching %s", e, Self.Doodad.Title)
Self.Canvas.SetBackground(RGBA(0, 0, 1, 0));
})
} }

View File

@ -4,22 +4,27 @@ function main() {
var timer = 0; var timer = 0;
var animationSpeed = 100; var animationSpeed = 100;
var animating = false; var opened = false;
Self.AddAnimation("open", animationSpeed, ["down1", "down2", "down3", "down4"]); Self.AddAnimation("open", animationSpeed, ["down1", "down2", "down3", "down4"]);
Self.AddAnimation("close", animationSpeed, ["down4", "down3", "down2", "down1"]); Self.AddAnimation("close", animationSpeed, ["down4", "down3", "down2", "down1"]);
Events.OnCollide( function() { Events.OnCollide( function(e) {
if (animating) { if (opened) {
return; return;
} }
animating = true; // Not touching the top of the door means door doesn't open.
if (e.Overlap.Y > 9) {
return;
}
opened = true;
Self.PlayAnimation("open", function() { Self.PlayAnimation("open", function() {
setTimeout(function() { });
});
Events.OnLeave(function() {
Self.PlayAnimation("close", function() { Self.PlayAnimation("close", function() {
animating = false; opened = false;
}); });
}, 3000);
}) })
});
} }