1 | ;
|
2 |
|
3 | var Entity = require("./entity");
|
4 |
|
5 | /**
|
6 | * An upgraded {@link Splat.Entity} that knows how to draw and advance an {@link Animation}.
|
7 | * @constructor
|
8 | * @augments Splat.Entity
|
9 | * @alias Splat.AnimatedEntity
|
10 | * @param {number} x The top-left x coordinate. See {@link Splat.Entity#x}
|
11 | * @param {number} y The top-left y coordinate. See {@link Splat.Entity#y}
|
12 | * @param {number} width The width on the x-axis. See {@link Splat.Entity#width}
|
13 | * @param {number} height The height on the y-axis. See {@link Splat.Entity#height}
|
14 | * @param {Animation|external:image} sprite The Animation or image to draw
|
15 | * @param {number} spriteOffsetX How much to offset {@link Splat.AnimatedEntity#sprite} from the {@link Splat.Entity#x} when drawing
|
16 | * @param {number} spriteOffsetY How much to offset {@link Splat.AnimatedEntity#sprite} from the {@link Splat.Entity#y} when drawing
|
17 | */
|
18 | function AnimatedEntity(x, y, width, height, sprite, spriteOffsetX, spriteOffsetY) {
|
19 | /**
|
20 | * The Animation or image to draw
|
21 | * @member {Animation|external:image}
|
22 | */
|
23 | this.sprite = sprite;
|
24 | /**
|
25 | * How much to offset {@link Splat.AnimatedEntity#sprite} from {@link Splat.Entity#x} when drawing.
|
26 | * @member {number}
|
27 | */
|
28 | this.spriteOffsetX = spriteOffsetX;
|
29 | /**
|
30 | * How much to offset {@link Splat.AnimatedEntity#sprite} from {@link Splat.Entity#y} when drawing.
|
31 | * @member {number}
|
32 | */
|
33 | this.spriteOffsetY = spriteOffsetY;
|
34 | Entity.call(this, x, y, width, height);
|
35 | }
|
36 | AnimatedEntity.prototype = Object.create(Entity.prototype);
|
37 | /**
|
38 | * Simulate movement since the last frame, changing {@link Splat.Entity#x}, {@link Splat.Entity#y}, and calling {@link Animation#move} if applicable.
|
39 | * @param {number} elapsedMillis The number of milliseconds since the last frame.
|
40 | */
|
41 | AnimatedEntity.prototype.move = function(elapsedMillis) {
|
42 | Entity.prototype.move.call(this, elapsedMillis);
|
43 | if (typeof this.sprite.move === "function") {
|
44 | this.sprite.move(elapsedMillis);
|
45 | }
|
46 | };
|
47 | /**
|
48 | * Draw the {@link Splat.AnimatedEntity#sprite}.
|
49 | * @param {external:CanvasRenderingContext2D} context The drawing context.
|
50 | */
|
51 | AnimatedEntity.prototype.draw = function(context) {
|
52 | if (typeof this.sprite.draw === "function") {
|
53 | this.sprite.draw(context, this.x + this.spriteOffsetX, this.y + this.spriteOffsetY);
|
54 | } else {
|
55 | context.drawImage(this.sprite, this.x + this.spriteOffsetX, this.y + this.spriteOffsetY);
|
56 | }
|
57 | // draw bounding boxes
|
58 | // context.strokeStyle = "#ff0000";
|
59 | // context.strokeRect(this.x, this.y, this.width, this.height);
|
60 | };
|
61 | /**
|
62 | * Make a copy of this AnimatedEntity. The {@link Splat.AnimatedEntity#sprite} is not copied, so both entities will share it.
|
63 | * @returns {Splat.AnimatedEntity}
|
64 | */
|
65 | AnimatedEntity.prototype.copy = function() {
|
66 | return new AnimatedEntity(this.x, this.y, this.width, this.height, this.sprite, this.spriteOffsetX, this.spriteOffsetY);
|
67 | };
|
68 |
|
69 | module.exports = AnimatedEntity;
|