Noah Petherbridge
d14eaf7df2
* The F4 key to draw collision boxes works reliably again: it draws the player's hitbox in world-space using the canvas.DrawStrokes() function, rather than in screen-space so it follows the player reliably. * The F4 key also draws hitboxes for ALL other actors in the level: buttons, enemies, doors, etc. * The level geometry collision function is updated to respect a doodad's declared Hitbox from their script, which may result in a smaller box than their raw Canvas size. The result is tighter collision between doodads, and Boy's sprite is rather narrow for its square Canvas so collision on rightward geometry is tighter for the player character. * Collision checks between actors also respect the actor's declared hitboxes now, allowing for Boy to get even closer to a locked door before being blocked.
40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
function main() {
|
|
var playerSpeed = 4;
|
|
var gravity = 4;
|
|
var Vx = Vy = 0;
|
|
|
|
var direction = "right";
|
|
|
|
Self.SetHitbox(0, 0, 32, 32)
|
|
Self.SetMobile(true);
|
|
Self.SetGravity(true);
|
|
Self.AddAnimation("walk-left", 100, ["red-wl1", "red-wl2", "red-wl3", "red-wl4"]);
|
|
Self.AddAnimation("walk-right", 100, ["red-wr1", "red-wr2", "red-wr3", "red-wr4"]);
|
|
|
|
// Sample our X position every few frames and detect if we've hit a solid wall.
|
|
var sampleTick = 0;
|
|
var sampleRate = 5;
|
|
var lastSampledX = 0;
|
|
|
|
setInterval(function() {
|
|
if (sampleTick % sampleRate === 0) {
|
|
var curX = Self.Position().X;
|
|
var delta = Math.abs(curX - lastSampledX);
|
|
if (delta < 5) {
|
|
direction = direction === "right" ? "left" : "right";
|
|
}
|
|
lastSampledX = curX;
|
|
}
|
|
sampleTick++;
|
|
|
|
// TODO: Vector() requires floats, pain in the butt for JS,
|
|
// the JS API should be friendlier and custom...
|
|
var Vx = parseFloat(playerSpeed * (direction === "left" ? -1 : 1));
|
|
Self.SetVelocity(Vector(Vx, 0.0));
|
|
|
|
if (!Self.IsAnimating()) {
|
|
Self.PlayAnimation("walk-"+direction, null);
|
|
}
|
|
}, 100);
|
|
}
|