Added some animations

This commit is contained in:
Zakary Timson 2022-08-06 22:45:35 -04:00
parent 4b55f4799e
commit b28a154a92
4 changed files with 62 additions and 34 deletions

View File

@ -16,7 +16,7 @@ function createWindow() {
roundedCorners: false, roundedCorners: false,
resizable: false, resizable: false,
fullscreen: false, fullscreen: false,
alwaysOnTop: false, alwaysOnTop: true,
icon: path.join(__dirname, "../assets/logo.png"), icon: path.join(__dirname, "../assets/logo.png"),
title: 'Desktop Daemon', title: 'Desktop Daemon',
webPreferences: { webPreferences: {
@ -27,7 +27,7 @@ function createWindow() {
}); });
window.loadFile(path.join(__dirname, "../index.html")); window.loadFile(path.join(__dirname, "../index.html"));
window.webContents.openDevTools(); // window.webContents.openDevTools();
} }
// Start // Start

View File

@ -13,22 +13,21 @@ canvas.height = screenHeight;
function clearScreen() { ctx.clearRect(0, 0, screenWidth, screenHeight); } function clearScreen() { ctx.clearRect(0, 0, screenWidth, screenHeight); }
const file = new StaticSprite(ctx, './assets/files.png', [1, 0], 0.5);
const sprite = new UnitController(ctx, { const sprite = new UnitController(ctx, {
spritesheetSrc: './assets/shadow_dog.png', spritesheetSrc: './assets/shadow_dog.png',
spriteAnimations: ['idle'], spriteAnimations: [
{name: 'idle', y: 0, frames: 7},
{name: 'right', y: 3, frames: 9},
{name: 'left', y: 3, frames: 9, reverse: true},
],
spriteScale: 0.5, spriteScale: 0.5,
spriteWidth: 575, spriteWidth: 575,
spriteHeight: 523, spriteHeight: 523,
spriteFrames: 7,
}); });
(async () => { setInterval(() => {
sprite.vel = [20, 0]; requestAnimationFrame(() => {
while(true) {
clearScreen(); clearScreen();
file.render(25, 25);
sprite.tick(); sprite.tick();
await sleep(1000 / FRAME_RATE); })
} }, 1000 / FRAME_RATE);
})();

View File

@ -1,36 +1,45 @@
export type SpriteAnimation = {
name: string;
y: number;
frames: number;
reverse?: boolean;
}
export class AnimatedSprite { export class AnimatedSprite {
private animation = ''; private animation: any = null;
private animationIndex = -1; private animationName = '';
private frame = -1; private frame = -1;
private spriteSheet!: HTMLImageElement; private spriteSheet!: HTMLImageElement;
constructor(private ctx: CanvasRenderingContext2D, constructor(private ctx: CanvasRenderingContext2D,
public spritesheetSrc: string, public spritesheetSrc: string,
public spriteAnimations: string[], public spriteAnimations: SpriteAnimation[],
public spriteScale = 1, public spriteScale = 1,
public spriteWidth = 100, public spriteWidth = 100,
public spriteHeight = 100, public spriteHeight = 100,
public spriteFrames = 10,
) { ) {
this.spriteSheet = new Image(); this.spriteSheet = new Image();
this.spriteSheet.src = this.spritesheetSrc; this.spriteSheet.src = this.spritesheetSrc;
} }
render(x: number, y: number, animation: string = this.animation) { render(x: number, y: number, animation: string = this.animationName) {
if(this.animation != animation) { if(this.animationName != animation) {
this.frame = 0; this.frame = 0;
this.animation = animation; this.animation = this.spriteAnimations.find((a) => a.name == animation);
this.animationIndex = this.spriteAnimations.findIndex((a) => a == animation); this.animationName = this.animation.name;
} else { } else {
this.frame++; this.frame++;
if(this.frame >= this.spriteFrames) this.frame = 0; if(this.frame >= this.animation.frames) this.frame = 0;
} }
this.ctx.save();
this.ctx.translate(this.spriteWidth * this.spriteScale / 2 * (this.animation.reverse ? 1 : -1), 0);
this.ctx.scale(this.animation.reverse ? -1 : 1, 1);
this.ctx.drawImage(this.spriteSheet, this.ctx.drawImage(this.spriteSheet,
this.frame * this.spriteWidth, this.animationIndex * this.spriteHeight, this.frame * this.spriteWidth, this.animation.y * this.spriteHeight, this.spriteWidth, this.spriteHeight,
this.spriteWidth, this.spriteHeight, x * (this.animation.reverse ? -1 : 1), this.ctx.canvas.height - (this.spriteHeight * this.spriteScale + y),
x, this.ctx.canvas.height - (this.spriteHeight * this.spriteScale + y),
this.spriteWidth * this.spriteScale, this.spriteHeight * this.spriteScale); this.spriteWidth * this.spriteScale, this.spriteHeight * this.spriteScale);
this.ctx.restore();
} }
} }

View File

@ -1,20 +1,17 @@
// const {FRAME_RATE} = require('./dist/constants.js'); // const {FRAME_RATE} = require('./dist/constants.js');
const {AnimatedSprite} = require('./sprites.js'); const {AnimatedSprite} = require('./sprites.js');
// TODO - Move to import
const FRAME_RATE = 10;
export type SpriteOptions = { export type SpriteOptions = {
spritesheetSrc: string, spritesheetSrc: string,
spriteAnimations: string[], spriteAnimations: any[],
spriteScale?: number,
spriteWidth?: number, spriteWidth?: number,
spriteHeight?: number, spriteHeight?: number,
spriteFrames?: number,
scale?: number,
} }
export class UnitController { export class UnitController {
private sprite!: typeof AnimatedSprite; private sprite!: typeof AnimatedSprite;
private random = 0;
public pos = [0, 0]; public pos = [0, 0];
public vel = [0, 0]; public vel = [0, 0];
@ -22,14 +19,37 @@ export class UnitController {
constructor(private ctx: CanvasRenderingContext2D, constructor(private ctx: CanvasRenderingContext2D,
private spriteOptions: SpriteOptions, private spriteOptions: SpriteOptions,
) { ) {
this.sprite = new AnimatedSprite(ctx, ...Object.values(spriteOptions)); this.sprite = new AnimatedSprite(ctx, spriteOptions.spritesheetSrc, spriteOptions.spriteAnimations, spriteOptions.spriteScale, spriteOptions.spriteWidth, spriteOptions.spriteHeight);
} }
tick() { tick() {
this.pos = [this.pos[0] + (this.vel[0] / FRAME_RATE), this.pos[1] + (this.vel[1] / FRAME_RATE)]; // Randomly move the unit
if(this.random <= 0) {
const move = Math.random();
if(move < 0.333) this.vel = [0, 0];
else if(move < 0.666) this.vel = [-10, 0];
else this.vel = [10, 0];
this.random = Math.random() * 50;
}
this.random--;
// Calculate new position
this.pos = [this.pos[0] + this.vel[0], this.pos[1] + (this.vel[1] - 9.8)];
if(this.pos[0] < 0) {
this.pos[0] = 0;
this.vel[0] = 0;
} else if(this.pos[0] > this.ctx.canvas.width) {
this.pos[0] = this.ctx.canvas.width;
this.vel[0] = 0;
}
if(this.pos[1] < 0) this.pos[1] = 0;
// Decide on animation
let animation = 'idle'; let animation = 'idle';
// if(this.vel[0] < 0) animation = 'left'; if(this.vel[0] < 0) animation = 'left';
// if(this.vel[0] > 0) animation = 'right'; if(this.vel[0] > 0) animation = 'right';
// Draw
this.sprite.render(this.pos[0], this.pos[1], animation); this.sprite.render(this.pos[0], this.pos[1], animation);
} }
} }