mirror of
https://github.com/NohamR/Pocket-Bird.git
synced 2026-05-26 04:07:24 +00:00
Move anim class and stylesheet
This commit is contained in:
48
src/anim.js
Normal file
48
src/anim.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import Frame from "./frame.js";
|
||||
import { BirdType } from "./sprites";
|
||||
|
||||
class Anim {
|
||||
/**
|
||||
* @param {Frame[]} frames
|
||||
* @param {number[]} durations
|
||||
* @param {boolean} loop
|
||||
*/
|
||||
constructor(frames, durations, loop = true) {
|
||||
this.frames = frames;
|
||||
this.durations = durations;
|
||||
this.loop = loop;
|
||||
}
|
||||
|
||||
getAnimationDuration() {
|
||||
return this.durations.reduce((a, b) => a + b, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CanvasRenderingContext2D} ctx
|
||||
* @param {number} direction
|
||||
* @param {number} timeStart The start time of the animation in milliseconds
|
||||
* @param {number} canvasPixelSize The size of a canvas pixel in pixels
|
||||
* @param {BirdType} [species] The species to use for the animation
|
||||
* @returns {boolean} Whether the animation is complete
|
||||
*/
|
||||
draw(ctx, direction, timeStart, canvasPixelSize, species) {
|
||||
let time = Date.now() - timeStart;
|
||||
const duration = this.getAnimationDuration();
|
||||
if (this.loop) {
|
||||
time %= duration;
|
||||
}
|
||||
let totalDuration = 0;
|
||||
for (let i = 0; i < this.durations.length; i++) {
|
||||
totalDuration += this.durations[i];
|
||||
if (time < totalDuration) {
|
||||
this.frames[i].draw(ctx, direction, canvasPixelSize, species);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Draw the last frame if the animation is complete
|
||||
this.frames[this.frames.length - 1].draw(ctx, direction, canvasPixelSize, species);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export default Anim;
|
||||
Reference in New Issue
Block a user