This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Engine]: update sprite to work with sprite sheets #45
- Loading branch information
1 parent
71b04af
commit c64fadd
Showing
5 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters