Add flying animation

This commit is contained in:
Idrees Hassan
2024-12-13 19:26:05 -05:00
parent bccec6e072
commit 90846b5c43

35
birb.js
View File

@@ -105,6 +105,20 @@ const sharedFrames = {
[0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0], [0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0],
[0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0]
]), ]),
wingsUp: new Frame([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 4, 4, 0, 0, 5, 5, 5, 0, 0],
[0, 0, 3, 4, 4, 4, 5, 5, 5, 5, 0, 0],
[0, 2, 3, 1, 4, 5, 5, 5, 5, 0, 0, 0],
[0, 0, 3, 3, 3, 2, 5, 5, 2, 0, 0, 0],
[0, 0, 0, 3, 3, 3, 2, 2, 3, 0, 0, 0],
[0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0],
[0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0]
]),
}; };
const Animations = { const Animations = {
@@ -116,6 +130,13 @@ const Animations = {
750, 750,
250 250
]), ]),
FLYING: new Anim([
sharedFrames.headDown,
sharedFrames.wingsUp
], [
100,
100
]),
}; };
@@ -167,7 +188,7 @@ const States = {
HOP: "hop", HOP: "hop",
}; };
const HOP_HEIGHT = CANVAS_PIXEL_SIZE * 2; const HOP_HEIGHT = CANVAS_PIXEL_SIZE * 3;
const MAX_HOP_TICKS = 24; const MAX_HOP_TICKS = 24;
let direction = Directions.RIGHT; let direction = Directions.RIGHT;
@@ -184,9 +205,7 @@ function update() {
modY = 0; modY = 0;
if (state === States.IDLE) { if (state === States.IDLE) {
if (Math.random() < 0.0025) { if (Math.random() < 0.0025) {
state = States.HOP; hop();
setAnimation(Animations.STILL);
console.log("Hopping");
} }
} else if (state === States.HOP) { } else if (state === States.HOP) {
hopTicks++; hopTicks++;
@@ -215,12 +234,16 @@ function draw() {
draw(); draw();
canvas.addEventListener("click", () => { function hop() {
if (state === States.IDLE) { if (state === States.IDLE) {
state = States.HOP; state = States.HOP;
setAnimation(Animations.STILL); setAnimation(Animations.FLYING);
hopTicks = 0; hopTicks = 0;
} }
}
canvas.addEventListener("click", () => {
hop();
}); });
/** /**