Skip to content

Commit

Permalink
Step 3.1: Create 'Sprite' class
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB committed Aug 29, 2018
1 parent 611c519 commit fb7197f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
58 changes: 58 additions & 0 deletions resources/scripts/engine/sprite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Engine.Sprite = class Sprite {
// An easy representation of a sprite on a canvas, with a set of convenient tools
// for alignment and coloring
constructor(texture) {
this.texture = texture;
this.x = 0;
this.y = 0;
this.width = texture.width;
this.height = texture.height;
this.pivot = { x: 0, y: 0 };
this.opacity = 1;
}

draw(context, offsetX = 0, offsetY = 0) {
context.save();
context.globalAlpha = this.opacity;

// The following switch-case can also be seen as a list of all possible
// alignment modes
switch (this.align) {
case "top-left": case "left-top": this.pivot = { x: 0, y: 0 }; break;
case "top-right": case "right-top": this.pivot = { x: this.width, y: 0 }; break;
case "bottom-left": case "left-bottom": this.pivot = { x: 0, y: this.height }; break;
case "bottom-right": case "right-bottom": this.pivot = { x: this.width, y: this.height }; break;
case "middle": case "center": this.pivot = { x: this.width / 2, y: this.height / 2 }; break;
case "left": this.pivot = { x: 0, y: this.height / 2 }; break;
case "top": this.pivot = { x: this.width / 2, y: 0 }; break;
case "right": this.pivot = { x: this.width, y: this.height / 2 }; break;
case "bottom": this.pivot = { x: this.width / 2, y: this.height }; break;
}

context.drawImage(
this.texture,
(this.x - this.pivot.x) + offsetX,
(this.y - this.pivot.y) + offsetY,
this.width,
this.height
);

context.restore();
}

// A sprite property (key) can also be resized based on a given percentage.
// The 'relative' argument represents the whole of which the percents are gonna be
// calculated from, and the 'adapters' argument is an array of property names which
// gonna adapt themselves based on the changes made in the given key.
// Usually 'width' goes along with ['height'] adapters, if we
// want to keep their original ratio
setPercentage(key, relative, percents, ...adapters) {
let oldVal = this[key];
let newVal = this[key] = (percents * relative) / 100;
let ratio = newVal / oldVal;

adapters.forEach(adapter => {
this[adapter] *= ratio;
});
}
};
1 change: 1 addition & 0 deletions views/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<!-- Scripts -->
<script type="text/javascript" src="/scripts/namespaces.js"></script>
<script type="text/javascript" src="/scripts/engine/sprite.js"></script>
<script type="text/javascript" src="/scripts/engine/key_states.js"></script>
<script type="text/javascript" src="/scripts/engine/layer.js"></script>
<script type="text/javascript" src="/scripts/engine/screen.js"></script>
Expand Down

0 comments on commit fb7197f

Please sign in to comment.