Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
[Engine]: update sprite to work with sprite sheets #45
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Mar 29, 2022
1 parent 71b04af commit c64fadd
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/GameObjects/GameObjectBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class GameObject {
return this;
}

_render() {
_render(deltaTime) {
if (!this.visible) return;

CanvasStateInstance.context.save();
Expand All @@ -154,7 +154,7 @@ export default class GameObject {
CanvasStateInstance.context.fillStyle = this.fillColor;
CanvasStateInstance.context.strokeStyle = this.strokeColor;
CanvasStateInstance.context.lineWidth = this.strokeWidth;
this._renderType();
this._renderType(deltaTime);

CanvasStateInstance.context.restore();
}
Expand Down
59 changes: 59 additions & 0 deletions src/GameObjects/Sprite/Animation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export default class Animation {
constructor(parent, name, numberOfFrames, speed, loop = true) {
this._parent = parent;
this.name = name;
this.numberOfFrames = numberOfFrames;
this.speed = speed;
this.loop = loop;
this.reversed = false; // TODO: Not Working
this.yoyo = false; // TODO: Not Working

this._currentFrame = 0;
this._currentTime = 0;
}

getFrame() { return this.numberOfFrames[this._currentFrame]; }

reset() {
this._currentFrame = 0;
this._currentTime = 0;
return this;
}

setName(name) {
this.name = name;
return this;
}
setSpeed(speed) {
this.speed = speed;
return this;
}
setNumberOfFrames(numberOfFrames) {
this.numberOfFrames = numberOfFrames;
return this;
}
setLoop(loop) {
this.loop = loop;
return this;
}

// Private
_update(deltaTime) {
this._currentTime += deltaTime * 100;

if (this._currentTime >= this.speed) {
this._currentTime = 0;
this._currentFrame++;

if (this._currentFrame >= this.numberOfFrames) {
if (this.loop) this._currentFrame = 0;
else {
this._currentFrame = this.numberOfFrames - 1;
this._parent.animations.pause();
}
}
}

this._parent.setFrame(this._currentFrame);
}
}
50 changes: 50 additions & 0 deletions src/GameObjects/Sprite/Animations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Animation from "./Animation.js";

export default class Animations {
constructor(parent) {
this._parent = parent;
this.animations = {};
this.currentAnimation = null;
this.paused = true;
}

get(name) { return this.animations[name]; }

add(name, numberOfFrames, speed, loop = true) {
this.animations[name] = new Animation(this._parent, name, numberOfFrames, speed, loop);
return this.get(name);
}

reset(name) {
this.animations[name].reset();
return this.get(this.currentAnimation);
}

play(name) {
if (this.currentAnimation) this.currentAnimation.reset();
this.currentAnimation = this.animations[name];
this.resume();
return this.get(this.currentAnimation);
}

pause() {
this.paused = true;
return this.get(this.currentAnimation);
}

resume() {
this.paused = false;
return this.get(this.currentAnimation);
}

setName(name, newName) { return this.animations[name].setName(newName); }
setSpeed(name, speed) { return this.animations[name].setSpeed(speed); }
setNumberOfFrames(name, numberOfFrames) { return this.animations[name].setNumberOfFrames(numberOfFrames); }
setLoop(name, loop) { return this.animations[name].setLoop(loop); }

// Private
_update(deltaTime) {
if (this.paused) return;
if (this.currentAnimation) this.currentAnimation._update(deltaTime);
}
}
15 changes: 14 additions & 1 deletion src/GameObjects/Sprite/Sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { CanvasStateInstance } from "../../State/CanvasState.js";
import Rectangle from "../Rectangle/Rectangle.js";
import Types from "../Types.js";

import Animations from "./Animations.js";

export default class Sprite extends Rectangle {
constructor(x, y, key, frame = 0, width = 0, height = 0) {
super(x, y, "#ffffff", "#000000");
Expand All @@ -11,13 +13,24 @@ export default class Sprite extends Rectangle {
this.width = width || AssetsManagerInstance.getSpriteSize(key).width;
this.height = height || AssetsManagerInstance.getSpriteSize(key).height;

this.animations = new Animations(this);

this._type = Types.sprite;
}

get x() { return this._x - this.width * this.origin.x * this.scale.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y * this.scale.y; } // Get the position Y relative to the origin

_renderType() {
setFrame(frame) {
this.frame = frame;
return this;
}

getNumFramesByWidth() { return Math.floor(this.texture.width / this.width); }

// Private
_renderType(deltaTime) {
this.animations._update(deltaTime);
CanvasStateInstance.context.drawImage(
this.texture, // Image
this.frame * this.width, 0, // Source X, Source Y
Expand Down
2 changes: 1 addition & 1 deletion src/Scenes/SceneManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class SceneManager {

const zSortedChildren = this.currentScene.children.sort((a, b) => a.z - b.z);
zSortedChildren.forEach(child => {
if (child._render) child._render();
if (child._render) child._render(this.deltaTime);
if (GlobalStateManagerInstance.debug && child._debug) child._debug();
});

Expand Down

0 comments on commit c64fadd

Please sign in to comment.