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.
45
dev-assets/doodads/azulian/azulian-red.js
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
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);
|
||||||
|
}
|
|
@ -9,44 +9,29 @@ function main() {
|
||||||
var animStart = animEnd = 0;
|
var animStart = animEnd = 0;
|
||||||
var animFrame = animStart;
|
var animFrame = animStart;
|
||||||
|
|
||||||
setInterval(function() {
|
Self.SetGravity(true);
|
||||||
if (animating) {
|
Self.AddAnimation("walk-left", 100, ["blu-wl1", "blu-wl2", "blu-wl3", "blu-wl4"]);
|
||||||
if (animFrame < animStart || animFrame > animEnd) {
|
Self.AddAnimation("walk-right", 100, ["blu-wr1", "blu-wr2", "blu-wr3", "blu-wr4"]);
|
||||||
animFrame = animStart;
|
|
||||||
}
|
|
||||||
|
|
||||||
animFrame++;
|
|
||||||
if (animFrame === animEnd) {
|
|
||||||
animFrame = animStart;
|
|
||||||
}
|
|
||||||
Self.ShowLayer(animFrame);
|
|
||||||
} else {
|
|
||||||
Self.ShowLayer(animStart);
|
|
||||||
}
|
|
||||||
}, 100);
|
|
||||||
|
|
||||||
Events.OnKeypress(function(ev) {
|
Events.OnKeypress(function(ev) {
|
||||||
Vx = 0;
|
Vx = 0;
|
||||||
Vy = 0;
|
Vy = 0;
|
||||||
|
|
||||||
if (ev.Right.Now) {
|
if (ev.Right.Now) {
|
||||||
animStart = 2;
|
if (!Self.IsAnimating()) {
|
||||||
animEnd = animStart+4;
|
Self.PlayAnimation("walk-right", null);
|
||||||
animating = true;
|
}
|
||||||
Vx = playerSpeed;
|
Vx = playerSpeed;
|
||||||
} else if (ev.Left.Now) {
|
} else if (ev.Left.Now) {
|
||||||
animStart = 6;
|
if (!Self.IsAnimating()) {
|
||||||
animEnd = animStart+4;
|
Self.PlayAnimation("walk-left", null);
|
||||||
animating = true;
|
}
|
||||||
Vx = -playerSpeed;
|
Vx = -playerSpeed;
|
||||||
} else {
|
} else {
|
||||||
|
Self.StopAnimation();
|
||||||
animating = false;
|
animating = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Self.Grounded()) {
|
|
||||||
Vy += gravity;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Self.SetVelocity(Point(Vx, Vy));
|
// Self.SetVelocity(Point(Vx, Vy));
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
BIN
dev-assets/doodads/azulian/red-back.png
Normal file
After Width: | Height: | Size: 826 B |
BIN
dev-assets/doodads/azulian/red-front.png
Normal file
After Width: | Height: | Size: 839 B |
BIN
dev-assets/doodads/azulian/red-wl1.png
Normal file
After Width: | Height: | Size: 803 B |
BIN
dev-assets/doodads/azulian/red-wl2.png
Normal file
After Width: | Height: | Size: 816 B |
BIN
dev-assets/doodads/azulian/red-wl3.png
Normal file
After Width: | Height: | Size: 800 B |
BIN
dev-assets/doodads/azulian/red-wl4.png
Normal file
After Width: | Height: | Size: 819 B |
BIN
dev-assets/doodads/azulian/red-wr1.png
Normal file
After Width: | Height: | Size: 829 B |
BIN
dev-assets/doodads/azulian/red-wr2.png
Normal file
After Width: | Height: | Size: 834 B |
BIN
dev-assets/doodads/azulian/red-wr3.png
Normal file
After Width: | Height: | Size: 815 B |
BIN
dev-assets/doodads/azulian/red-wr4.png
Normal file
After Width: | Height: | Size: 838 B |