-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.js
284 lines (261 loc) · 8.66 KB
/
Renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { Transform } from "./Transform.js";
import { Vector2 } from "./Vectors.js";
import EnumeratedValue from "./EnumeratedValue.js";
export {Renderer};
const spriteSheets = {
Goblin : {
json : "./images/enemies/goblin.json",
img : "./images/enemies/goblin.png"
},
Slime : {
json : "./images/enemies/slime.json",
img : "./images/enemies/slime.png"
},
Dragon : {
json : "./images/enemies/whiteEyesBlueDragon.json",
img : "./images/enemies/whiteEyesBlueDragon.png"
},
Items : {
json : "./images/items/Items.json",
img : "./images/items/Items.png"
},
Tiles : {
json : "./images/tiles/tiles.json",
img : "./images/tiles/tiles.png"
},
LeatherArmor : {
json : "./images/armor/leatherArmor.json",
img : "./images/armor/leatherArmor.png"
},
IronArmor : {
json : "./images/armor/ironArmor.json",
img : "./images/armor/ironArmor.png"
},
ExitIndicator : {
json : "./images/levelEditor/exit_indicator.json",
img : "./images/levelEditor/exit_indicator.png"
}
};
/**
* Class representing a Renderer object, which draws sprites to the game canvas.
*
* Methods:
* - getAnimation() : Returns the animation name of the current animation
* - updateSpriteSheet(spriteSheetInfo) : Changes the current sprite information
*/
class Renderer
{
_SpriteSheet
_Anchor;
_Animation;
transform;
sprite;
spriteSheet;
spriteSheetInfo;
stage;
zIndexForce;
/**
* Constructs a new Renderer on the provided staage, with the provided spriteSheetInfo.
*
* @param {Object} spriteSheet An object with a "_Json" key and a "_Img" key, providing the animation
* and frame data and the base texture, respectively.
* @param {Stage} stage PIXI.Stage to be rendered to.
* @param {Transform} transform Transform defining position, rotation, and scale of the sprite.
*/
constructor(spriteSheet, stage, transform = new Transform(), animation='default', zIndexForce = null, anchor = new Vector2(0.5, 0.5))
{
if (stage == undefined)
throw new Error('Renderer stage undefined! Was the renderer initialized correctly?');
this._Animation = new EnumeratedValue({ 'default' : animation }, (anim) => this.setAnimation(anim), this);
this._SpriteSheet = new EnumeratedValue(spriteSheets, (sheet) => {
this.updateSpriteSheet(sheet).then(() => this.setAnimation('default'));
}, this);
this._SpriteSheet.selected = spriteSheet;
this.zIndexForce = zIndexForce;
this.anchor = anchor;
this.stage = stage;
this.transform = transform;
this.updateSpriteSheet(this._SpriteSheet.map[this._SpriteSheet.selected]).then(() => {
this.setAnimation(animation);
this.sprite.textures[0].baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
this.link();
});
}
get anchor() { return this._Anchor; }
set anchor(value) { this._Anchor = value; }
get currentAnimation() { return this._Animation.selected; }
set currentAnimation(value)
{
this._Animation.value = value;
}
get spriteSheetName() { return this._SpriteSheet.selected; }
getAnimation()
{
return this.currentAnimation;
}
isLoaded()
{
return this.sprite != undefined;
}
playAnimation(speed=1, loop=false)
{
if (this.sprite == undefined)
return;
if (!this.sprite.playing)
{
this.sprite.animationSpeed = speed;
this.sprite.loop = loop;
this.sprite.play();
}
}
/**
* Forces the sprite's zIndex (aka draw depth) to be set to a specific number
* @param {Number} zIndex
*/
forceZIndex(zIndex)
{
this.zIndexForce = zIndex;
}
/**
* Frees the sprite's zIndex (aka draw depth), causing it to be based on the renderer's position in the room
*/
unforceZIndex()
{
this.zIndexForce = null;
}
/**
* Links this renderer to the stage. This will allow the sprite to be rendered to the canvas
*/
link()
{
this.stage.addChild(this.sprite);
}
/**
* Unlinks the renderer from the stage, removing it from the render cycle.
*/
unlink()
{
this.stage.removeChild(this.sprite);
}
/**
* Unlinks and destroys the sprite associated with this renderer.
* @returns True if the renderer was successfully disposed, false otherwise.
*/
dispose()
{
if (this.sprite == undefined)
return false;
this.unlink();
//this.sprite.destroy(true);
return true;
}
/**
* So long as all your animations and frames are contained on a single sheet, you should be able
* to avoid using this method.
*
* If you use this without also changing the animation (changing to "default" should always work), the
* animation may end up referencing a non-existent animation.
*
* @param {Object} spriteSheetInfo An object with a "_Json" key and a "_Img" key, providing the animation
* and frame data and the base texture, respectively.
*/
async updateSpriteSheet(spriteSheetInfo)
{
this.spriteSheetInfo = spriteSheetInfo;
var data = await PIXI.Assets.load(spriteSheetInfo.json);
this.spriteSheet = new PIXI.Spritesheet(PIXI.Texture.from(spriteSheetInfo.img), data.data);
await this.spriteSheet.parse();
// Load animations for level builder use
let animations = {};
for (const anim in data.animations)
{
animations[anim] = anim;
}
this._Animation = new EnumeratedValue(animations, (anim) => this.setAnimation(anim), this);
}
/**
* Changes the animation of this Renderer. The animation must be defined by the current sprite
* sheet json structure.
*
* @param {String} animationId The name of the animation to play
* @param {number} speed The animation speed. Defaults to 1
* @param {boolean} loop Whether the animation should loop. Defaults to false
*/
setAnimation(animationId, speed=1, loop=false)
{
let anim;
try {
anim = this.spriteSheet.animations[animationId];
} catch {
throw new Error("Trying to change a renderer's animation, but the animation does not exist.");
}
this._Animation.selected = animationId;
this.unlink();
this.sprite = new PIXI.AnimatedSprite(anim);
this.link();
this.sprite.loop = loop;
this.sprite.animationSpeed = speed;
this.sprite.play();
this.update(0);
}
/**
* Updates the position & transform of object being renddered
* @param {*} delta
*/
update(delta)
{
if (this.sprite == undefined) return;
var t = this.transform;
this.sprite.zIndex = (this.zIndexForce == null) ? t.position.y : this.zIndexForce;
this.sprite.scale.set(t.scale.x, t.scale.y);
const w = this.sprite.width;
const h = this.sprite.height;
this.sprite.position.set(t.position.x + 8 * t.scale.x, t.position.y + 8 * t.scale.y);
this.sprite.rotation = t.rotation/180*Math.PI;
this.sprite.anchor.set(this.anchor.x, this.anchor.y);
}
/**
* Serializes the this renderer to a JSON object string
* @returns the JSON string representation
*/
serialize()
{
return '{ "type":"Renderer", "anchor":' + this.anchor.serialize() + ', "zIndex":' + this.zIndexForce + ',"spriteSheet": ' + this.spriteSheetName +
', "transform": ' + this.transform.serialize() +
', "animation": "'+this.getAnimation()+'"}';
}
/**
* Deserializes the JSON object to a renderer object
* @param {*} obj the JSON object
* @param {*} stage the current game stage
* @returns new renderer object
*/
static deserialize(obj, stage)
{
if (!('zIndex' in obj))
obj.zIndex = null;
if (!('anchor' in obj))
obj.anchor = new Vector2(0.5, 0.5);
return new Renderer(
obj.spriteSheet,
stage,
Transform.deserialize(obj.transform),
obj.animation,
obj.zIndex,
Vector2.deserialize(obj.anchor)
);
}
static loadAssets()
{
let assets = [];
for (const sheet in spriteSheets)
{
assets.push(sheet.json);
assets.push(sheet.img);
}
for (const asset of assets)
{
PIXI.Assets.backgroundLoad(asset);
}
}
}