/
spritesheet.js
242 lines (198 loc) · 6.92 KB
/
spritesheet.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
// We keep a global dictionary of loaded sprite-sheets,
// which are each an instance of our SpriteSheetClass
// below.
//
// This dictionary is indexed by the URL path that the
// atlas is located at. For example, calling:
//
// gSpriteSheets['grits_effects.png']
//
// would return the SpriteSheetClass object associated
// to that URL, assuming that it exists.
var gSpriteSheets = {};
//-----------------------------------------
SpriteSheetClass = Class.extend({
// We store in the SpriteSheetClass:
//
// The Image object that we created for our
// atlas.
img: null,
// The URL path that we grabbed our atlas
// from.
url: "",
// An array of all the sprites in our atlas.
sprites: [],
//-----------------------------------------
init: function () {},
//-----------------------------------------
// Load the atlas at the path 'imgName' into
// memory. This is similar to how we've
// loaded images in previous units.
load: function (imgName) {
// Store the URL of the spritesheet we want.
this.url = imgName;
// Create a new image whose source is at 'imgName'.
var img = new Image();
img.src = imgName;
// Store the Image object in the img parameter.
this.img = img;
// Store this SpriteSheetClass in our global
// dictionary gSpriteSheets defined above.
gSpriteSheets[imgName] = this;
},
//-----------------------------------------
// Define a sprite for this atlas
defSprite: function (name, x, y, w, h, cx, cy) {
// We create a new object with:
//
// The name of the sprite as a string
//
// The x and y coordinates of the sprite
// in the atlas.
//
// The width and height of the sprite in
// the atlas.
//
// The x and y coordinates of the center
// of the sprite in the atlas. This is
// so we don't have to do the calculations
// each time we need this. This might seem
// minimal, but it adds up!
var spt = {
"id": name,
"x": x,
"y": y,
"w": w,
"h": h,
"cx": cx === null ? 0 : cx,
"cy": cy === null ? 0 : cy
};
// We push this new object into
// our array of sprite objects,
// at the end of the array.
this.sprites.push(spt);
},
//-----------------------------------------
// Parse the JSON file passed in as 'atlasJSON'
// that is associated to this atlas.
parseAtlasDefinition: function (atlasJSON) {
// Parse the input 'atlasJSON' using the
// JSON.parse method and store it in a
// variable.
var parsed = JSON.parse(atlasJSON);
// For each sprite in the parsed JSON,
// 'chaingun.png', chaingun_impact.png',
// etc....
for(var key in parsed.frames) {
// Grab the sprite from the parsed JSON...
var sprite = parsed.frames[key];
// Define the center of the sprite as an offset
// (hence the negative).
// We don't want to have to calculate these
// values every single time we want to draw a
// sprite! It adds up!
var cx = -sprite.frame.w * 0.5;
var cy = -sprite.frame.h * 0.5;
// Define the sprite for this sheet by calling
// defSprite to store it into the 'sprites' Array.
this.defSprite(key, sprite.frame.x, sprite.frame.y, sprite.frame.w, sprite.frame.h, cx, cy);
}
},
//-----------------------------------------
// Walk through all the sprite definitions for this
// atlas, and find which one matches the name.
getStats: function (name) {
// For each sprite in the 'sprites' Array...
for(var i = 0; i < this.sprites.length; i++) {
// Check if the sprite's 'id' parameter
// equals the passed in name...
if(this.sprites[i].id === name) {
// and return that sprite if it does.
return this.sprites[i];
}
}
// If we don't find the sprite, return null.
return null;
}
});
//-----------------------------------------
// External-facing function for drawing sprites based
// on the sprite name (ie. "chaingun.png", and the
// position on the canvas to draw to.
function drawSprite(spritename, posX, posY) {
// Walk through all our spritesheets defined in
// 'gSpriteSheets' and for each sheet:
for(var sheetName in gSpriteSheets) {
var sheet = gSpriteSheets[sheetName];
// 1) Use the getStats method of the spritesheet
// to find if a sprite with name 'spritename'
// exists in that sheet.
var sprite = sheet.getStats(spritename);
if(sprite == null) continue;
//if hits a null value skips the next part
//this part is the internal function call with vars from above
//
// 2) If we find the appropriate sprite, call
// '__drawSpriteInternal' with parameters as
// described below.
_drawSpriteInternal(srpite,sheet, posX, posY)
}
//
for (items in gspritesheets array){
if getStats(spritename != ""){
parse json object and put into vars
_drawSpriteInternal(stuff from parse)
}
}
}
//-----------------------------------------
// External-facing function for drawing sprites based
// on the sprite object stored in the 'sprites Array,
// the 'SpriteSheetClass' object stored in the
// 'gSpriteSheets' dictionary, and the position on
// canvas to draw to.
function __drawSpriteInternal(spt, sheet, posX, posY) {
// First, check if the sprite or sheet objects are
// null.
//
// YOUR CODE HERE
contextname.drawImage(params from parse, new params)
// Call the drawImage method of our canvas context
// using the full drawImage API. drawImage takes,
// in order:
//
// 1) the Image object to draw, this is our entire
// spritesheet.
//
// 2) the x-coordinate we are drawing from in the
// spritesheet.
//
// 3) the y-coordinate we are drawing from in the
// spritesheet.
//
// 4) the width of the sprite we are drawing from
// our spritesheet.
//
// 5) the height of the sprite we are drawing from
// our spritesheet.
//
// 6) the x-coordinate we are drawing to in our
// canvas.
//
// 7) the y-coordinate we are drawing to in our
// canvas.
//
// 8) the width we are drawing in our canvas. This
// is in case we want to scale the image we are
// drawing to the canvas. In our case, we don't.
//
// 9) the height we are drawing in our canvas. This
// is in case we want to scale the image we are
// drawing to the canvas. In our case, we don't.
//
// Wow, that's a lot of parameters. Luckily, most
// of them are stored directly in the sprite object
// we want to draw.
//
// YOUR CODE HERE
}