-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·225 lines (191 loc) · 7.15 KB
/
main.js
File metadata and controls
executable file
·225 lines (191 loc) · 7.15 KB
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
/**
* Duy Huynh
* TCSS 491, Winter '16
* Assignment 1 - Animation
* main.js
*
* Main for Assignment 1. Animating Spaz from the Jazz Jackrabbit video game series by Epic MegaGames.
*
* Resources:
* Code: Based off of Chris Marriott's main.js
* Background Image: http://imgur.com/gallery/VZ9H2
* Spaz sprites: http://www.spriters-resource.com/pc_computer/jazzjackrabbit2thesecretfiles/sheet/22144/
*
*/
/**
* Animate a sprite sheet:
* @param spriteSheet
* @param startX
* @param startY
* @param frameWidth
* @param frameHeight
* @param frameDuration
* @param frames
* @param loop
* @param reverse
* @constructor
*/
function Animation(spriteSheet, startX, startY, frameWidth, frameHeight, frameDuration, frames, loop, reverse) {
this.spriteSheet = spriteSheet;
this.startX = startX;
this.startY = startY;
this.frameWidth = frameWidth;
this.frameDuration = frameDuration;
this.frameHeight = frameHeight;
this.frames = frames;
this.totalTime = frameDuration * frames;
this.elapsedTime = 0;
this.loop = loop;
this.reverse = reverse;
}
Animation.prototype.drawFrame = function (tick, ctx, x, y, scaleBy) {
var scaleBy = scaleBy || 1;
this.elapsedTime += tick;
if (this.loop) {
if (this.isDone()) {
this.elapsedTime = 0;
}
} else if (this.isDone()) {
return;
}
var index = this.reverse ? this.frames - this.currentFrame() - 1 : this.currentFrame();
var vindex = 0;
if ((index + 1) * this.frameWidth + this.startX > this.spriteSheet.width) {
index -= Math.floor((this.spriteSheet.width - this.startX) / this.frameWidth);
vindex++;
}
while ((index + 1) * this.frameWidth > this.spriteSheet.width) {
index -= Math.floor(this.spriteSheet.width / this.frameWidth);
vindex++;
}
var locX = x;
var locY = y;
var offset = vindex === 0 ? this.startX : 0;
ctx.drawImage(this.spriteSheet,
index * this.frameWidth + offset, vindex * this.frameHeight + this.startY, // source from sheet
this.frameWidth, this.frameHeight,
locX, locY,
this.frameWidth * scaleBy,
this.frameHeight * scaleBy);
}
/**
* Returns which frame the animation is on.
* @returns {number}
*/
Animation.prototype.currentFrame = function () {
return Math.floor(this.elapsedTime / this.frameDuration);
}
/**
* Returns whether or not the animation is finished.
* @returns {boolean}
*/
Animation.prototype.isDone = function () {
return (this.elapsedTime >= this.totalTime);
}
function Background(game) {
Entity.call(this, game, 0, 400); // What does "call" do? Constructor?
//this.radius = 200;
}
Background.prototype = new Entity();
Background.prototype.constructor = Background;
Background.prototype.update = function () {
}
Background.prototype.draw = function (ctx) {
ctx.fillStyle = "Black";
//ctx.fillRect(0, 500, 800, 300);
ctx.drawImage(ASSET_MANAGER.getAsset("./img/8bitbg.png"), 0, 0);
this.game.ctx.fillText("This is Spaz.", 300, 80);
this.game.ctx.fillText("He can DANCE! [idle]", 300, 110);
this.game.ctx.fillText("He can RUN! [← →]", 300, 140);
this.game.ctx.fillText("He can JUMP! [spacebar]", 300, 170);
this.game.ctx.fillText("He can't ESCAPE! [wall collisions]", 300, 200);
this.game.ctx.fillText("'d' to draw collision circles", 300, 230);
Entity.prototype.draw.call(this);
}
function Spaz(game) {
// Animations:
this.standAnimation = new Animation(ASSET_MANAGER.getAsset("./img/spaz_frames.png"), 0, 0, 56, 52, 0.1, 6, true, false);
this.idleAnimation = new Animation(ASSET_MANAGER.getAsset("./img/spaz_frames.png"), 336, 0, 56, 52, 0.1, 20, true, false);
this.runRightAnimation = new Animation(ASSET_MANAGER.getAsset("./img/spaz_frames.png"), 1456, 0, 56, 52, 0.1, 8, true, false);
this.runLeftAnimation = new Animation(ASSET_MANAGER.getAsset("./img/spaz_frames.png"), 1904, 0, 56, 52, 0.1, 8, true, false);
this.skidRightAnimation = new Animation(ASSET_MANAGER.getAsset("./img/spaz_frames.png"), 2352, 0, 56, 52, 0.1, 14, false, false);
this.skidLeftAnimation = new Animation(ASSET_MANAGER.getAsset("./img/spaz_frames.png"), 3136, 0, 56, 52, 0.1, 14, false, false);
this.jumpAnimation = new Animation(ASSET_MANAGER.getAsset("./img/spaz_frames.png"), 3920, 0, 56, 52, 0.05, 17, false, false);
// States:
this.standing = true;
this.idle = 0;
this.jumping = false;
this.running = false;
this.skidLeft = false;
this.skidRight = false;
// Entity properties:
this.radius = 50 / 2;
this.ground = 300;
this.speed = 5;
this.width = 50;
this.height = 50;
this.centerX = this.width / 2;
this.centerY = this.ground + (this.height / 2);
Entity.call(this, game, 0, this.ground);
}
Spaz.prototype = new Entity();
Spaz.prototype.constructor = Spaz;
Spaz.prototype.update = function () {
// Jumping:
if (this.game.space) this.jumping = true;
// If Spaz is set to jump:
if (this.jumping) {
// If Spaz is finished jumping:
if (this.jumpAnimation.isDone()) {
// Reset jump animation timer
this.jumpAnimation.elapsedTime = 0;
// Reset 'jump' state.
this.jumping = false;
}
var jumpDistance = this.jumpAnimation.elapsedTime / this.jumpAnimation.totalTime;
var totalHeight = 100;
if (jumpDistance > 0.5)
jumpDistance = 1 - jumpDistance;
//var height = jumpDistance * 2 * totalHeight;
var height = totalHeight * (-2 * (jumpDistance * jumpDistance - jumpDistance));
this.y = this.ground - height;
}
// Running right and left:
this.game.right || this.game.left ? this.running = true : this.running = false;
// Running and boundary collisions:
if (this.running) {
if (this.game.right && this.x < this.game.surfaceWidth - this.width) this.x += this.speed;
if (this.game.left && this.x > 0) this.x -= this.speed;
}
Entity.prototype.update.call(this);
}
Spaz.prototype.draw = function (ctx) {
if (this.jumping) {
this.jumpAnimation.drawFrame(this.game.clockTick, ctx, this.x, this.y);
}
else if (this.running) {
if (this.game.right) this.runRightAnimation.drawFrame(this.game.clockTick, ctx, this.x, this.y);
if (this.game.left) this.runLeftAnimation.drawFrame(this.game.clockTick, ctx, this.x, this.y);
}
else {
this.standAnimation.drawFrame(this.game.clockTick, ctx, this.x, this.y);
}
Entity.prototype.draw.call(this);
}
// the "main" code begins here
var ASSET_MANAGER = new AssetManager();
ASSET_MANAGER.queueDownload("./img/8bitbg.png");
ASSET_MANAGER.queueDownload("./img/spaz_frames.png");
ASSET_MANAGER.downloadAll(function () {
console.log("Starting asset downloads");
var canvas = document.getElementById('gameWorld');
var ctx = canvas.getContext('2d');
ctx.font = "25px Comic Sans MS";
var gameEngine = new GameEngine();
var bg = new Background(gameEngine);
var spaz = new Spaz(gameEngine);
gameEngine.addEntity(bg);
gameEngine.addEntity(spaz);
gameEngine.init(ctx);
gameEngine.start();
});