/
Chapter 7.txt
296 lines (234 loc) · 7.24 KB
/
Chapter 7.txt
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
285
286
287
288
289
290
291
292
293
294
295
296
Listing 7-1. index.html Containing Plugin References
<html>
<head>
<title>Whack-A-Mole 3D</title>
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="lib/glMatrix-1.3.7.min.js"></script>
<script type="text/javascript" src="lib/enchant.js"></script>
<script type="text/javascript" src="lib/nineleap.enchant.js"></script>
<script type="text/javascript" src="lib/gl.enchant.js"></script>
<script type="text/javascript" src="lib/collada.gl.enchant.js"></script>
<script type="text/javascript" src="lib/primitive.gl.enchant.js"></script>
<script type="text/javascript" src="main.js"></script>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
</body>
</html>
Listing 7-2. Framework of the 3D Game
enchant();
var game;
window.onload = function(){
game = new Core(320, 320);
game.fps = 60;
game.onload = function(){
};
game.start();
};
Listing 7-3. Creating a Scene3D
scene = new Scene3D();
Listing 7-4. Creating a Directional Light
scene.setDirectionalLight(new DirectionalLight());
Listing 7-5. Creating a new Camera3D
camera = new Camera3D();
camera.y = 1.1;
camera.z = -1.65;
camera.centerZ = -10;
camera.upVectorZ = 10;
camera.upVectorY = 100;
Listing 7-6. Designating the Scene’s Camera
scene.setCamera(camera);
Listing 7-7. Preloading droid.dae
game.preload('images/droid.dae');
Listing 7-8. Creating the Droid Class
var Droid = Class.create(Sprite3D, {
initialize: function(x, y, z) {
}
});
Listing 7-9. Calling Sprite3D and Setting the Collada File
Sprite3D.call(this);
this.set(game.assets["images/droid.dae"]);
Listing 7-10. Scaling Droid
this.scaleX = 0.3;
this.scaleY = 0.3;
this.scaleZ = 0.3;
Listing 7-11. Assigning x, y, and z
this.x = x;
this.y = y;
this.z = z;
Listing 7-12. Adding a New Droid to Scene3D
scene.addChild(new Droid(0, 0, -8));
Listing 7-13. Creating a Random Number Function and Variables
function rand(n) {
return Math.floor(Math.random() * n);
}
//Variables to keep track of droids
var maxDroid = 100;
var hitDroid = 0;
var combo = 1;
Listing 7-14. Creating ScoreLabel
//Define ScoreLabel
var ScoreLabel = Class.create(Label, { //Extends Label class
initialize: function(x, y) {
enchant.Label.call(this, "SCORE:0"); //Call the Label class constructor
this.x = x;
this.y = y;
this.score = 0;
this.color = "#ffffff";
},
add: function(pts) { //Add the score
this.score += pts;
this.text = "SCORE:" + this.score; //Update display
}
});
Listing 7-15. Creating and Adding ScoreLabel
//Display score label
scoreLabel = new ScoreLabel(5, 5);
game.rootScene.addChild(scoreLabel);
Listing 7-16. Restructured Droid Class
//Define Droid class
var Droid = Class.create(Sprite3D, { //Extend Sprite3D class
initialize: function(x, y, z) {
Sprite3D.call(this); //Call the Sprite3D constructor
this.set(game.assets["images/droid.dae"]);
this.scaleX = 0.3;
this.scaleY = 0.3;
this.scaleZ = 0.3;
this.x = x;
this.y = y;
this.z = z;
this.a = 0.01;
this.addEventListener('enterframe', this.tick); //Define event listener
this.addEventListener('touchstart', this.hit); //Define event listener for hit
this.mode = 2; //Set initial Droid mode to wait, and then appear
this.nextMode = 0;
this.waitFor = game.rootScene.age + rand(100) + 75;
},
tick: function() { //Iterate Droid animation based on mode
},
hit: function() { //Hit Droid
}
});
Listing 7-17. The tick Function
if (game.rootScene.age === 0) {
this.waitFor++;
}
switch (this.mode) {
case 0: //Appear from hole
this.a *= 0.98;
this.y += this.a;
if (this.y >= -1.2) {
this.mode = 2; //Go to wait mode after fully appearing
this.nextMode = 1; //After wait mode, go to mode 1 (hide)
this.waitFor = game.rootScene.age + rand(100) + 10;
}
break;
case 1: //Hide in hole
this.y -= this.a;
this.a *= 0.98;
if (this.y <= -1.5) {
this.mode = 2; //After fully hiding, go to wait mode
this.nextMode = 0; //After waiting, go to mode 0 (appear)
this.waitFor = game.rootScene.age + rand(100);
//Reduce number of max droids (to control length of game)
maxDroid--;
//If number of max droids has been reached, and the given
//Droid is instructed to hide, block the hole
if (maxDroid <= 0) this.mode = 3;
}
break;
case 2: //Wait
if (this.y < -1.5) this.y += 0.05;
if (game.rootScene.age > this.waitFor) {
this.mode = this.nextMode;
this.a = 0.05;
}
break;
case 3: //Deactivated (no more Droids will appear from this hole)
break;
case 4: //Fly
this.y += this.a;
this.a *= 1.1;
if (game.rootScene.age > this.waitFor) {
this.nextMode = 0;
this.waitFor = game.rootScene.age+rand(50) + 30;
this.mode = 2;
this.y = -3;
this.a = 0.05;
}
break;
}
Listing 7-18. The hit Function
if (this.y >= -1.4) { //If Droid is over halfway out of hole
this.mode = 4; //Enter fly mode
this.a = 0.02;
this.waitFor = game.rootScene.age + 30; //Wait for 30 frames (0.5s)
scoreLabel.add(combo); //Add combo score
combo++;
hitDroid++;
} else {
//If Droid is whacked before appearing out of hole, reset combo
combo = 1;
}
Listing 7-19. Adding a Droid With a Different Position
scene.addChild(new Droid(0, -1.5, -9.2));
Listing 7-20. Preloading Images
game.preload('images/pit.png', 'images/sign.png', 'images/droid.dae');
Listing 7-21. Creating the Pit
var pit = new PlaneXZ();
pit.scaleX = 3.0;
pit.scaleY = 3.0;
pit.scaleZ = 3.0;
var texture = new Texture(game.assets['images/pit.png'], { flipY: false });
texture.ambient = [1.0, 1.0, 1.0, 1.0];
pit.mesh.texture = texture;
pit.y = -1;
pit.z = -10;
scene.addChild(pit);
Listing 7-22. Creating the base
var base = new PlaneXY();
base.z = -8.6;
base.y = -2.5;
base.scaleX = 3.0;
base.scaleY = 3.0;
base.scaleZ = 3.0;
base.mesh.setBaseColor('#ff0000');
scene.addChild(base);
Listing 7-23. Creating a Sign
var sign = new PlaneXY();
sign.scaleX = 2.0;
sign.scaleY = 2.0;
sign.scaleZ = 2.0;
var signTexture = new Texture(game.assets['images/sign.png']);
signTexture.ambient = [1.0, 1.0, 1.0, 1.0];
sign.mesh.texture = signTexture;
sign.y = 0.5;
sign.z = -8.6;
scene.addChild(sign);
Listing 7-24. Creating Multiple Droids
//Create all Droids
for (var j = 0; j < 3; j++) {
for (var i = 0; i < 4; i++) {
scene.addChild(new Droid(i * 0.75 - 1.1, -1.5, -9.2 - j * 0.9));
}
}
Listing 7-25. Ending the Game
game.rootScene.addEventListener('enterframe', function() {
if (maxDroid <= 0) {
game.end(scoreLabel.score, "You whacked " + hitDroid + " Droids for a score of " + scoreLabel.score + " points!");
}
});
Listing 7-26. Changing the Camera Position
camera.y = 14;
camera.z = -8.1;
Listing 7-27. Making the Camera Pan at the Beginning
//Pan camera down at beginning
if (game.rootScene.age < 130) {
camera.z += 0.05;
camera.y -= 0.1;
}