-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameMain.js
259 lines (207 loc) · 6.64 KB
/
GameMain.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
(function(){
var c = document.getElementById('game'),
C = c.getContext('2d'),
FPS = 60,
CANVAS_WIDTH = 480,
CANVAS_HEIGHT = 640,
gameObjects = [],
loaded = false,
loadingPanel,
Resource = Class.extend({
ready: false, //when this changes to true our image is loaded
init: function(res){
var that = this;
this.data = new Image();
this.data.onload = function(){
//when our image is loaded we change ready to true
that.ready = true;
//we save the width
that.width = this.width;
//we save the height
that.height = this.height;
};
//start loading the image
this.data.src = res;
}
}),
// Here we load all images
resources = {
'logo' : new Resource('resources/logo.png')
},
loadingPercentage = function(){
var num = 0,
amountResources = Object.keys(resources).length;
for(var name in resources)
{
if(resources[name].ready)
{
num++; //count every resource that is ready
}
}
//return a percentage
return amountResources === 0 ? 100 : num / amountResources * 100;
},
clamp = function(x, min, max)
{
if(x > max) x = max;
if(x < min) x = min;
return x;
},
Vector2D = Class.extend({
init : function(x, y){
this.x = x || 0;
this.y = y || 0;
},
mulS : function(scalar){ return new Vector2D(this.x * scalar, this.y * scalar);},
mulV : function(vector){ return new Vector2D(this.x * vector.x, this.y * vector.y);},
divS : function(scalar){ return new Vector2D(this.x / scalar, this.y / scalar);},
divV : function(vector){ return new Vector2D(this.x / vector.x, this.y / vector.y);},
addS : function(scalar){ return new Vector2D(this.x + scalar, this.y + scalar);},
addV : function(vector){ return new Vector2D(this.x + vector.x, this.y + vector.y);},
subS : function(scalar){ return new Vector2D(this.x - scalar, this.y - scalar);},
subV : function(vector){ return new Vector2D(this.x - vector.x, this.y - vector.y);},
dot : function(vector){ return (this.x * vector.x + this.y * vector.y);},
abs : function(){ return new Vector2D(Math.abs(this.x), Math.abs(this.y));},
length : function(){ return Math.sqrt(this.dot(this));},
lengthSqrt : function(){ return this.dot(this);},
normalize : function(){
var len = this.length();
this.x /= len;
this.y /= len;
return this;
}
}),
GameObject = Class.extend({
init: function(x, y){
this.pos = new Vector2D(x || 0, y || 0);
this.velocity = new Vector2D(0,0);
},
update : function()
{
//update the position by adding an velocity vector to the position
this.pos = this.velocity.addV(this.pos);
},
//we can use intersect for knowing if a is GameObjects intersects with an object
interSect : function(obj){
return (Math.abs(this.pos.x - obj.pos.x) * 2 < (this.width + obj.width)) && (Math.abs(this.pos.y - obj.pos.y) * 2 < (this.height + obj.height));
},
draw : function(resource)
{
//update the height of the GameObject
this.height = resources[resource].height;
//update the width of the GameObject
this.width = resources[resource].width;
//draw the resource to the screen on its position
C.drawImage(resources[resource].data, this.pos.x, this.pos.y);
},
remove: function(){
//remove this GameObject from the gameObjects array
gameObjects.splice(gameObjects.indexOf(this), 1);
}
}),
TextPanel = GameObject.extend({
init: function(x, y, size, font, fillStyle, text){
this._super(x || 0, y || 0);
this.text = text || '';
this.size = size || 20;
this.font = font || 'arial';
this.fillStyle = fillStyle || 'black';
},
draw: function(){
//set fillStyle
C.fillStyle = this.fillStyle;
//set font size and font style
C.font = 'bold ' + this.size + 'px ' + this.font;
//draw the text to the canvas
C.fillText(this.text, this.pos.x, this.pos.y + this.size/2 + 10);
//save the width of the TextPanel
this.width = C.measureText(this.text).width;
}
}),
LoadingPanel = TextPanel.extend({
init: function(){
this._super();
},
update : function()
{
//get the loading percentage
var percentage = loadingPercentage();
//set the text of the loading panel
this.text = 'Loading resources: ' + percentage + '%';
//set loaded = true; when we hit 100%
loaded = (percentage === 100);
}
}),
GameObjectSample_logo = GameObject.extend({
init : function(){
this._super(0,0);
//on init we set the direction
this.velocity = new Vector2D(1, 0.5).mulS(4);
},
update:function(){
this._super();
//make sure this object will not fly out of the view
this.pos.x = clamp(this.pos.x);
this.pos.y = clamp(this.pos.y);
// Bouncing logo
if(this.pos.x > CANVAS_WIDTH - this.width || this.pos.x < 0)
{
//we flip the X axis
this.velocity = new Vector2D(-this.velocity.x, this.velocity.y);
}
if(this.pos.y > CANVAS_HEIGHT - this.height || this.pos.y < 0)
{
//we flip the Y axis
this.velocity = new Vector2D(this.velocity.x, -this.velocity.y);
}
},
draw: function()
{
//draw resources.logo to the screen
this._super("logo");
}
}),
init = function(){
//set the canvas element width
c.width = CANVAS_WIDTH;
//set the canvas element height
c.height = CANVAS_HEIGHT;
//make an instance of loadingPanel
loadingPanel = new LoadingPanel();
//add a sample GameObject
var sample = new GameObjectSample_logo();
gameObjects.push(sample);
//game loop
(function(){
setTimeout(arguments.callee, 1000/FPS);
//clear screen
C.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
//when we have all resources loaded into the game we can start drawing and updating our gameobjects
if(loaded)
{
for(var gameObject in gameObjects)
{
//update all gameObjects
if(gameObjects[gameObject])
{
gameObjects[gameObject].update();
}
//draw all gameObjects
if(gameObjects[gameObject])
{
gameObjects[gameObject].draw();
}
}
}
else
{
//update the loading panel
loadingPanel.update();
//draw the loading panel while the client is still busy with all resources
loadingPanel.draw();
}
})();
};
//start the init
init();
})();