Example doodads from Sketchy Maze.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.1 KiB

// Playable Character
//
// If not controlled by the player, its AI is to move left and
// right and is able to pick up keys, press buttons and generally
// get up to mischief.
var playerSpeed = 4,
direction = "right";
function main() {
Self.SetMobile(true); // Needed to activate most objects
Self.SetInventory(true); // Can pick up keys and items (automatic)
Self.SetGravity(true); // Is pulled down by gravity
Self.SetHitbox(0, 0, 32, 52);
setupAnimations();
// Are we the player character?
if (Self.IsPlayer()) {
return playerControls();
}
return ai()
}
function setupAnimations() {
Self.AddAnimation("walk-left", 200, ["stand-left", "walk-left-1", "walk-left-2", "walk-left-3", "walk-left-2", "walk-left-1"]);
Self.AddAnimation("walk-right", 200, ["stand-right", "walk-right-1", "walk-right-2", "walk-right-3", "walk-right-2", "walk-right-1"]);
}
// Player controls
function playerControls() {
Events.OnKeypress(function (ev) {
if (ev.Right) {
if (!Self.IsAnimating()) {
Self.PlayAnimation("walk-right", null);
}
} else if (ev.Left) {
if (!Self.IsAnimating()) {
Self.PlayAnimation("walk-left", null);
}
} else {
Self.StopAnimation();
}
})
}
// AI controls. The AI is very simple:
// walk to the right until you hit an obstacle,
// then walk to the left until you hit an obstacle,
// and repeat.
// It will automatically pickup keys and interact with things
// as it passes because it HasInventory and IsMobile.
function ai() {
// 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++;
var Vx = parseFloat(playerSpeed * (direction === "left" ? -1 : 1));
Self.SetVelocity(Vector(Vx, 0.0));
if (!Self.IsAnimating()) {
Self.PlayAnimation("walk-" + direction, null);
}
}, 100);
}