doodle/dev-assets/doodads/azulian/azulian-red.js
Noah Petherbridge d28745f89e Mobile Enemy Doodad Test
* Add a Red Azulian as a test for mobile enemies.
  * Its A.I. has it walk back and forth, changing directions when it
    comes up against an obstacle for a few moments.
  * It plays walking animations and can trigger collision events with
    other Doodads, such as the Electric Door and Trapdoor.
* Move Gravity responsibility to the doodad scripts themselves.
  * Call `Self.SetGravity(true)` to opt the Doodad in to gravity.
  * The canvas.Loop() adds gravity to any doodad that has it enabled.
2019-05-06 16:30:45 -07:00

46 lines
1.2 KiB
JavaScript

function main() {
log.Info("Azulian '%s' initialized!", Self.Doodad.Title);
var playerSpeed = 4;
var gravity = 4;
var Vx = Vy = 0;
var direction = "right";
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"]);
// var nextTurn = time.Add(time.Now(), 2500);
// 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 (time.Now().After(nextTurn)) {
// direction = direction === "right" ? "left" : "right";
// nextTurn = time.Add(time.Now(), 2500);
// }
if (sampleTick % sampleRate === 0) {
var curX = Self.Position().X;
var delta = Math.abs(curX - lastSampledX);
if (delta < 5) {
log.Error("flip red azulian");
direction = direction === "right" ? "left" : "right";
}
lastSampledX = curX;
}
sampleTick++;
var Vx = playerSpeed * (direction === "left" ? -1 : 1);
Self.SetVelocity(Point(Vx, 0));
if (!Self.IsAnimating()) {
Self.PlayAnimation("walk-"+direction, null);
}
}, 100);
}