doodle/dev-assets/doodads/trapdoors/trapdoor.js
Noah Petherbridge 08e65c32b5 Overhaul the Platformer Physics System
* Player character now experiences acceleration and friction when
  walking around the map!
* Actor position and movement had to be converted from int's
  (render.Point) to float64's to support fine-grained acceleration
  steps.
* Added "physics" package and physics.Vector to be a float64 counterpart
  for render.Point. Vector is used for uix.Actor.Position() for the sake
  of movement math. Vector is flattened back to a render.Point for
  collision purposes, since the levels and hitboxes are pixel-bound.
* Refactor the uix.Actor to no longer extend the doodads.Drawing (so it
  can have a Position that's a Vector instead of a Point). This broke
  some code that expected `.Doodad` to directly reference the
  Drawing.Doodad: now you had to refer to it as `a.Drawing.Doodad` which
  was ugly. Added convenience method .Doodad() for a shortcut.
* Moved functions like GetBoundingRect() from doodads package to
  collision, where it uses its own slimmer Actor interface for just the
  relevant methods it needs.
2020-04-04 21:00:32 -07:00

90 lines
2.1 KiB
JavaScript

function main() {
// What direction is the trapdoor facing?
var direction = Self.Doodad().Tag("direction");
console.log("Trapdoor(%s) initialized", direction);
var timer = 0;
// Set our hitbox based on our orientation.
var thickness = 6;
var doodadSize = 72;
if (direction === "left") {
Self.SetHitbox(48, 0, doodadSize, doodadSize);
} else if (direction === "right") {
Self.SetHitbox(0, 0, thickness, doodadSize);
} else if (direction === "up") {
Self.SetHitbox(0, doodadSize - thickness, doodadSize, doodadSize);
} else { // Down, default.
Self.SetHitbox(0, 0, doodadSize, thickness);
}
var animationSpeed = 100;
var opened = false;
// Register our animations.
var frames = [];
for (var i = 1; i <= 4; i++) {
frames.push(direction + i);
}
Self.AddAnimation("open", animationSpeed, frames);
frames.reverse();
Self.AddAnimation("close", animationSpeed, frames);
Events.OnCollide( function(e) {
if (opened) {
return;
}
// Is the actor colliding our solid part?
if (e.InHitbox) {
// Are they touching our opening side?
if (direction === "left") {
if (doodadSize - e.Overlap.X < thickness) {
// Touching the right edge, open the door.
opened = true;
Self.PlayAnimation("open", null);
return;
}
if (e.Overlap.W === doodadSize - thickness) {
return false;
}
} else if (direction === "right") {
if (e.Overlap.X > 0) {
return false;
} else if (e.Settled) {
opened = true;
Self.PlayAnimation("open", null);
}
} else if (direction === "up") {
if (doodadSize - e.Overlap.Y < thickness) {
// Touching the bottom edge, open the door.
opened = true;
Self.PlayAnimation("open", null);
return;
}
if (e.Overlap.H === doodadSize - thickness) {
return false;
}
} else if (direction === "down") {
if (e.Overlap.Y > 0) {
return false;
} else if (e.Settled) {
opened = true;
Self.PlayAnimation("open", null);
}
}
return true;
}
});
Events.OnLeave(function() {
if (opened) {
Self.PlayAnimation("close", function() {
opened = false;
});
}
})
}