Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: sky/sdk/example/game/lib/game_demo_world.dart

Issue 1223543004: Adds better explosions, shield, and game over (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: git cl web Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/sdk/example/game/lib/game_demo.dart ('k') | sky/sdk/example/game/lib/node_with_size.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 part of game; 1 part of game;
2 2
3 const double _steeringThreshold = 0.0; 3 const double _steeringThreshold = 0.0;
4 const double _steeringMax = 150.0; 4 const double _steeringMax = 150.0;
5 5
6 // Random generator 6 // Random generator
7 Math.Random _rand = new Math.Random(); 7 Math.Random _rand = new Math.Random();
8 8
9 const double _gameSizeWidth = 1024.0; 9 const double _gameSizeWidth = 1024.0;
10 const double _gameSizeHeight = 1024.0; 10 const double _gameSizeHeight = 1024.0;
11 11
12 const double _shipRadius = 30.0; 12 const double _shipRadius = 30.0;
13 const double _lrgAsteroidRadius = 40.0; 13 const double _lrgAsteroidRadius = 40.0;
14 const double _medAsteroidRadius = 20.0; 14 const double _medAsteroidRadius = 20.0;
15 const double _smlAsteroidRadius = 10.0; 15 const double _smlAsteroidRadius = 10.0;
16 const double _maxAsteroidSpeed = 1.0; 16 const double _maxAsteroidSpeed = 1.0;
17 17
18 const int _lifeTimeLaser = 50; 18 const int _lifeTimeLaser = 50;
19 19
20 const int _numStarsInStarField = 150; 20 const int _numStarsInStarField = 150;
21 21
22 const int _numFramesShieldActive = 60 * 5;
23 const int _numFramesShieldFlickers = 60;
24
22 class GameDemoWorld extends NodeWithSize { 25 class GameDemoWorld extends NodeWithSize {
26 App _app;
23 27
24 // Images 28 // Images
25 Image _imgNebula; 29 Image _imgNebula;
26 30
27 SpriteSheet _spriteSheet; 31 SpriteSheet _spriteSheet;
28 32
29 // Inputs 33 // Inputs
30 double _joystickX = 0.0; 34 double _joystickX = 0.0;
31 double _joystickY = 0.0; 35 double _joystickY = 0.0;
32 bool _fire; 36 bool _fire;
33 37
34 Node _gameLayer; 38 Node _gameLayer;
35 39
36 Ship _ship; 40 Ship _ship;
41 Sprite _shield;
37 List<Asteroid> _asteroids = []; 42 List<Asteroid> _asteroids = [];
38 List<Laser> _lasers = []; 43 List<Laser> _lasers = [];
39 StarField _starField; 44 StarField _starField;
40 Nebula _nebula; 45 Nebula _nebula;
46
47 // Game state
48 int _numFrames = 0;
49 bool _isGameOver = false;
41 50
42 GameDemoWorld(ImageMap images, this._spriteSheet) : super(new Size(_gameSizeWi dth, _gameSizeHeight)) { 51 GameDemoWorld(this._app, ImageMap images, this._spriteSheet) : super(new Size( _gameSizeWidth, _gameSizeHeight)) {
43 52
44 // Fetch images 53 // Fetch images
45 _imgNebula = images["res/nebula.png"]; 54 _imgNebula = images["res/nebula.png"];
46 55
47 _gameLayer = new Node(); 56 _gameLayer = new Node();
48 this.addChild(_gameLayer); 57 this.addChild(_gameLayer);
49 58
50 // Add some asteroids to the game world 59 // Add some asteroids to the game world
51 for (int i = 0; i < 5; i++) { 60 for (int i = 0; i < 5; i++) {
52 addAsteroid(AsteroidSize.large); 61 addAsteroid(AsteroidSize.large);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 if (pos != null) asteroid.position = pos; 93 if (pos != null) asteroid.position = pos;
85 _gameLayer.addChild(asteroid); 94 _gameLayer.addChild(asteroid);
86 _asteroids.add(asteroid); 95 _asteroids.add(asteroid);
87 } 96 }
88 97
89 void addShip() { 98 void addShip() {
90 Ship ship = new Ship(_spriteSheet["ship.png"]); 99 Ship ship = new Ship(_spriteSheet["ship.png"]);
91 ship.zPosition = 10.0; 100 ship.zPosition = 10.0;
92 _gameLayer.addChild(ship); 101 _gameLayer.addChild(ship);
93 _ship = ship; 102 _ship = ship;
103
104 _shield = new Sprite(_spriteSheet["shield.png"]);
105 _shield.zPosition = 11.0;
106 _shield.scale = 0.5;
107 _shield.transferMode = TransferMode.plus;
108 _gameLayer.addChild(_shield);
109
110 Action rotate = new ActionRepeatForever(new ActionTween((a) => _shield.rotat ion = a, 0.0, 360.0, 1.0));
111 actions.run(rotate);
94 } 112 }
95 113
96 void addLaser() { 114 void addLaser() {
97 Laser laser = new Laser(_spriteSheet["laser.png"], _ship); 115 Laser laser = new Laser(_spriteSheet["laser.png"], _ship);
98 laser.zPosition = 8.0; 116 laser.zPosition = 8.0;
99 laser.constrainProportions = true; 117 laser.constrainProportions = true;
100 _lasers.add(laser); 118 _lasers.add(laser);
101 _gameLayer.addChild(laser); 119 _gameLayer.addChild(laser);
102 } 120 }
103 121
104 void addNebula() { 122 void addNebula() {
105 _nebula = new Nebula.withImage(_imgNebula); 123 _nebula = new Nebula.withImage(_imgNebula);
106 _gameLayer.addChild(_nebula); 124 _gameLayer.addChild(_nebula);
107 } 125 }
108 126
109 void addExplosion(AsteroidSize asteroidSize, Point position) { 127 void addExplosion(AsteroidSize asteroidSize, Point position) {
128 Node explosionNode = new Node();
129
110 // Add particles 130 // Add particles
111 ParticleSystem particles = new ParticleSystem(_spriteSheet["explosion_partic le.png"], rotateToMovement: true, 131 ParticleSystem particlesDebris = new ParticleSystem(
112 startRotation:90.0, startRotationVar: 0.0, endRotation: 90.0, startSize: 0.3 , startSizeVar: 0.1, endSize: 0.3, endSizeVar: 0.1, 132 _spriteSheet["explosion_particle.png"],
113 numParticlesToEmit: 25, emissionRate:1000.0, greenVar: 127, redVar: 127); 133 rotateToMovement: true,
114 particles.zPosition = 1010.0; 134 startRotation:90.0,
115 particles.position = position; 135 startRotationVar: 0.0,
116 _gameLayer.addChild(particles); 136 endRotation: 90.0,
137 startSize: 0.3,
138 startSizeVar: 0.1,
139 endSize: 0.3,
140 endSizeVar: 0.1,
141 numParticlesToEmit: 25,
142 emissionRate:1000.0,
143 greenVar: 127,
144 redVar: 127
145 );
146 particlesDebris.zPosition = 1010.0;
147 explosionNode.addChild(particlesDebris);
148
149 ParticleSystem particlesFire = new ParticleSystem(
150 _spriteSheet["fire_particle.png"],
151 colorSequence: new ColorSequence([new Color(0xffffff33), new Color(0xffff3 333), new Color(0x00ff3333)], [0.0, 0.5, 1.0]),
152 numParticlesToEmit: 25,
153 emissionRate: 1000.0,
154 startSize: 0.5,
155 startSizeVar: 0.1,
156 endSize: 0.5,
157 endSizeVar: 0.1,
158 posVar: new Point(10.0, 10.0),
159 speed: 10.0,
160 speedVar: 5.0
161 );
162 particlesFire.zPosition = 1011.0;
163 explosionNode.addChild(particlesFire);
164
117 165
118 // Add ring 166 // Add ring
119 Sprite sprtRing = new Sprite(_spriteSheet["explosion_ring.png"]); 167 Sprite sprtRing = new Sprite(_spriteSheet["explosion_ring.png"]);
120 sprtRing.position = position;
121 sprtRing.transferMode = TransferMode.plus; 168 sprtRing.transferMode = TransferMode.plus;
122 _gameLayer.addChild(sprtRing); 169 explosionNode.addChild(sprtRing);
123 170
124 Action scale = new ActionTween( (a) => sprtRing.scale = a, 0.2, 1.0, 1.5); 171 Action scale = new ActionTween( (a) => sprtRing.scale = a, 0.2, 1.0, 1.5);
125 Action scaleAndRemove = new ActionSequence([scale, new ActionRemoveFromParen t(sprtRing)]); 172 Action scaleAndRemove = new ActionSequence([scale, new ActionRemoveNode(sprt Ring)]);
126 Action fade = new ActionTween( (a) => sprtRing.opacity = a, 1.0, 0.0, 1.5); 173 Action fade = new ActionTween( (a) => sprtRing.opacity = a, 1.0, 0.0, 1.5);
127 actions.run(scaleAndRemove); 174 actions.run(scaleAndRemove);
128 actions.run(fade); 175 actions.run(fade);
129 176
130 // Add streaks 177 // Add streaks
131 for (int i = 0; i < 5; i++) { 178 for (int i = 0; i < 5; i++) {
132 Sprite sprtFlare = new Sprite(_spriteSheet["explosion_flare.png"]); 179 Sprite sprtFlare = new Sprite(_spriteSheet["explosion_flare.png"]);
133 sprtFlare.pivot = new Point(0.3, 1.0); 180 sprtFlare.pivot = new Point(0.3, 1.0);
134 sprtFlare.position = position;
135 sprtFlare.scaleX = 0.3; 181 sprtFlare.scaleX = 0.3;
136 sprtFlare.transferMode = TransferMode.plus; 182 sprtFlare.transferMode = TransferMode.plus;
137 sprtFlare.rotation = _rand.nextDouble() * 360.0; 183 sprtFlare.rotation = _rand.nextDouble() * 360.0;
138 _gameLayer.addChild(sprtFlare); 184 explosionNode.addChild(sprtFlare);
139 185
140 double multiplier = _rand.nextDouble() * 0.3 + 1.0; 186 double multiplier = _rand.nextDouble() * 0.3 + 1.0;
141 187
142 Action scale = new ActionTween( (a) => sprtFlare.scaleY = a, 0.3 * multipl ier, 0.8, 1.5 * multiplier); 188 Action scale = new ActionTween( (a) => sprtFlare.scaleY = a, 0.3 * multipl ier, 0.8, 1.5 * multiplier);
143 Action scaleAndRemove = new ActionSequence([scale, new ActionRemoveFromPar ent(sprtFlare)]); 189 Action scaleAndRemove = new ActionSequence([scale, new ActionRemoveNode(sp rtFlare)]);
144 Action fadeIn = new ActionTween( (a) => sprtFlare.opacity = a, 0.0, 1.0, 0 .5 * multiplier); 190 Action fadeIn = new ActionTween( (a) => sprtFlare.opacity = a, 0.0, 1.0, 0 .5 * multiplier);
145 Action fadeOut = new ActionTween( (a) => sprtFlare.opacity = a, 1.0, 0.0, 1.0 * multiplier); 191 Action fadeOut = new ActionTween( (a) => sprtFlare.opacity = a, 1.0, 0.0, 1.0 * multiplier);
146 Action fadeInOut = new ActionSequence([fadeIn, fadeOut]); 192 Action fadeInOut = new ActionSequence([fadeIn, fadeOut]);
147 actions.run(scaleAndRemove); 193 actions.run(scaleAndRemove);
148 actions.run(fadeInOut); 194 actions.run(fadeInOut);
149 } 195 }
196
197 explosionNode.position = position;
198 explosionNode.zPosition = 1010.0;
199
200 if (asteroidSize == AsteroidSize.large) {
201 explosionNode.scale = 1.5;
202 }
203
204 _gameLayer.addChild(explosionNode);
150 } 205 }
151 206
152 void update(double dt) { 207 void update(double dt) {
153 // Move asteroids 208 // Move asteroids
154 for (Asteroid asteroid in _asteroids) { 209 for (Asteroid asteroid in _asteroids) {
155 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector); 210 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector);
156 } 211 }
157 212
158 // Move lasers and remove expired lasers 213 // Move lasers and remove expired lasers
159 for (int i = _lasers.length - 1; i >= 0; i--) { 214 for (int i = _lasers.length - 1; i >= 0; i--) {
160 Laser laser = _lasers[i]; 215 Laser laser = _lasers[i];
161 laser.move(); 216 laser.move();
162 if (laser._frameCount > _lifeTimeLaser) { 217 if (laser._frameCount > _lifeTimeLaser) {
163 laser.removeFromParent(); 218 laser.removeFromParent();
164 _lasers.removeAt(i); 219 _lasers.removeAt(i);
165 } 220 }
166 } 221 }
167 222
168 // Apply thrust to ship 223 // Apply thrust to ship
169 if (_joystickX != 0.0 || _joystickY != 0.0) { 224 if (_joystickX != 0.0 || _joystickY != 0.0) {
170 _ship.thrust(_joystickX, _joystickY); 225 _ship.thrust(_joystickX, _joystickY);
171 } 226 }
172 227
173 // Move ship 228 // Move ship
174 _ship.move(); 229 _ship.move();
230 _shield.position = _ship.position;
175 231
176 // Check collisions between asteroids and lasers 232 // Check collisions between asteroids and lasers
177 for (int i = _lasers.length -1; i >= 0; i--) { 233 for (int i = _lasers.length -1; i >= 0; i--) {
178 // Iterate over all the lasers 234 // Iterate over all the lasers
179 Laser laser = _lasers[i]; 235 Laser laser = _lasers[i];
180 236
181 for (int j = _asteroids.length - 1; j >= 0; j--) { 237 for (int j = _asteroids.length - 1; j >= 0; j--) {
182 // Iterate over all the asteroids 238 // Iterate over all the asteroids
183 Asteroid asteroid = _asteroids[j]; 239 Asteroid asteroid = _asteroids[j];
184 240
(...skipping 14 matching lines...) Expand all
199 addExplosion(asteroid._asteroidSize, asteroid.position); 255 addExplosion(asteroid._asteroidSize, asteroid.position);
200 256
201 // Remove asteroid 257 // Remove asteroid
202 asteroid.removeFromParent(); 258 asteroid.removeFromParent();
203 _asteroids.removeAt(j); 259 _asteroids.removeAt(j);
204 break; 260 break;
205 } 261 }
206 } 262 }
207 } 263 }
208 264
265 // Check collisions between asteroids and ship
266 if (_numFrames > _numFramesShieldActive) {
267 // Shield is no longer active
268
269 for (int i = _asteroids.length - 1; i >= 0; i--) {
270 // Iterate over all the asteroids
271 Asteroid asteroid = _asteroids[i];
272
273 if (pointQuickDist(asteroid.position, _ship.position) < asteroid.radius + _ship.radius) {
274 killShip();
275 }
276 }
277 }
278
209 // Move objects to center camera and warp objects around the edges 279 // Move objects to center camera and warp objects around the edges
210 centerCamera(); 280 centerCamera();
211 warpObjects(); 281 warpObjects();
282
283 // Update shield
284 if (_numFrames > _numFramesShieldActive) _shield.visible = false;
285 else if (_numFrames > _numFramesShieldActive - _numFramesShieldFlickers) _sh ield.visible = !_shield.visible;
286
287 _numFrames++;
212 } 288 }
213 289
214 void centerCamera() { 290 void centerCamera() {
215 const cameraDampening = 0.1; 291 const cameraDampening = 0.1;
216 Point delta = new Point(_gameSizeWidth/2 - _ship.position.x, _gameSizeHeight /2 - _ship.position.y); 292 Point delta = new Point(_gameSizeWidth/2 - _ship.position.x, _gameSizeHeight /2 - _ship.position.y);
217 delta = pointMult(delta, cameraDampening); 293 delta = pointMult(delta, cameraDampening);
218 294
219 for (Node child in _gameLayer.children) { 295 for (Node child in _gameLayer.children) {
220 child.position = pointAdd(child.position, delta); 296 child.position = pointAdd(child.position, delta);
221 } 297 }
222 298
223 // Update starfield 299 // Update starfield
224 _starField.move(delta.x, delta.y); 300 _starField.move(delta.x, delta.y);
225 } 301 }
226 302
227 void warpObjects() { 303 void warpObjects() {
228 for (Node child in _gameLayer.children) { 304 for (Node child in _gameLayer.children) {
229 if (child.position.x < 0) child.position = pointAdd(child.position, new Po int(_gameSizeWidth, 0.0)); 305 if (child.position.x < 0) child.position = pointAdd(child.position, new Po int(_gameSizeWidth, 0.0));
230 if (child.position.x >= _gameSizeWidth) child.position = pointAdd(child.po sition, new Point(-_gameSizeWidth, 0.0)); 306 if (child.position.x >= _gameSizeWidth) child.position = pointAdd(child.po sition, new Point(-_gameSizeWidth, 0.0));
231 if (child.position.y < 0) child.position = pointAdd(child.position, new Po int(0.0, _gameSizeHeight)); 307 if (child.position.y < 0) child.position = pointAdd(child.position, new Po int(0.0, _gameSizeHeight));
232 if (child.position.y >= _gameSizeHeight) child.position = pointAdd(child.p osition, new Point(0.0, -_gameSizeHeight)); 308 if (child.position.y >= _gameSizeHeight) child.position = pointAdd(child.p osition, new Point(0.0, -_gameSizeHeight));
233 } 309 }
234 } 310 }
235 311
312 void killShip() {
313 if (_isGameOver) return;
314
315 // Set game over
316 _isGameOver = true;
317
318 // Remove the ship
319 _ship.visible = false;
320
321 // Add an explosion
322 addExplosion(AsteroidSize.large, _ship.position);
323 }
324
236 // Handling controls 325 // Handling controls
237 326
238 void controlSteering(double x, double y) { 327 void controlSteering(double x, double y) {
328 // Reset controls if it's game over
329 if (_isGameOver) {
330 x = y = 0.0;
331 }
332
239 _joystickX = x; 333 _joystickX = x;
240 _joystickY = y; 334 _joystickY = y;
241 } 335 }
242 336
243 void controlFire() { 337 void controlFire() {
338 // Don't shoot if it's game over
339 if (_isGameOver) return;
340
244 addLaser(); 341 addLaser();
245 } 342 }
246 343
247 // Handle pointer events 344 // Handle pointer events
248 345
249 int _firstPointer = -1; 346 int _firstPointer = -1;
250 int _secondPointer = -1; 347 int _secondPointer = -1;
251 Point _firstPointerDownPos; 348 Point _firstPointerDownPos;
252 349
253 bool handleEvent(SpriteBoxEvent event) { 350 bool handleEvent(SpriteBoxEvent event) {
351
254 Point pointerPos = convertPointToNodeSpace(event.boxPosition); 352 Point pointerPos = convertPointToNodeSpace(event.boxPosition);
255 int pointer = event.pointer; 353 int pointer = event.pointer;
256 354
257 switch (event.type) { 355 switch (event.type) {
258 case 'pointerdown': 356 case 'pointerdown':
259 if (_firstPointer == -1) { 357 if (_firstPointer == -1) {
260 // Assign the first pointer 358 // Assign the first pointer
261 _firstPointer = pointer; 359 _firstPointer = pointer;
262 _firstPointerDownPos = pointerPos; 360 _firstPointerDownPos = pointerPos;
263 } 361 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 449
352 // Rotate forever 450 // Rotate forever
353 double direction = (_rand.nextBool()) ? 360.0 : -360.0; 451 double direction = (_rand.nextBool()) ? 360.0 : -360.0;
354 ActionTween rot = new ActionTween( (a) => rotation = a, 0.0, direction, 2.0 * _rand.nextDouble() + 2.0); 452 ActionTween rot = new ActionTween( (a) => rotation = a, 0.0, direction, 2.0 * _rand.nextDouble() + 2.0);
355 ActionRepeatForever repeat = new ActionRepeatForever(rot); 453 ActionRepeatForever repeat = new ActionRepeatForever(rot);
356 actions.run(repeat); 454 actions.run(repeat);
357 } 455 }
358 456
359 bool handleEvent(SpriteBoxEvent event) { 457 bool handleEvent(SpriteBoxEvent event) {
360 if (event.type == "pointerdown") { 458 if (event.type == "pointerdown") {
361 colorOverlay = new Color(0x99ff0000); 459 actions.stopWithTag("fade");
460 colorOverlay = new Color(0x99ffffff);
362 } 461 }
363 else if (event.type == "pointerup") { 462 else if (event.type == "pointerup") {
364 colorOverlay = null; 463 // Fade out the color overlay
464 Action fadeOut = new ActionTween((a) => this.colorOverlay = a, new Color(0 x99ffffff), new Color(0x00ffffff), 1.0);
465 Action fadeOutAndRemove = new ActionSequence([fadeOut, new ActionCallFunct ion(() => this.colorOverlay = null)]);
466 actions.run(fadeOutAndRemove, "fade");
365 } 467 }
366 return false; 468 return false;
367 } 469 }
368 } 470 }
369 471
370 class Ship extends Sprite { 472 class Ship extends Sprite {
371 Vector2 _movementVector; 473 Vector2 _movementVector;
372 double _rotationTarget; 474 double _rotationTarget;
475 double radius = _shipRadius;
373 476
374 Ship(Texture img) : super(img) { 477 Ship(Texture img) : super(img) {
375 _movementVector = new Vector2.zero(); 478 _movementVector = new Vector2.zero();
376 rotation = _rotationTarget = 270.0; 479 rotation = _rotationTarget = 270.0;
377 480
378 // Create sprite 481 // Create sprite
379 size = new Size(_shipRadius * 2.0, _shipRadius * 2.0); 482 size = new Size(_shipRadius * 2.0, _shipRadius * 2.0);
380 position = new Point(_gameSizeWidth/2.0, _gameSizeHeight/2.0); 483 position = new Point(_gameSizeWidth/2.0, _gameSizeHeight/2.0);
381 } 484 }
382 485
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 double dy = a.y - b.y; 623 double dy = a.y - b.y;
521 if (dx < 0.0) dx = -dx; 624 if (dx < 0.0) dx = -dx;
522 if (dy < 0.0) dy = -dy; 625 if (dy < 0.0) dy = -dy;
523 if (dx > dy) { 626 if (dx > dy) {
524 return dx + dy/2.0; 627 return dx + dy/2.0;
525 } 628 }
526 else { 629 else {
527 return dy + dx/2.0; 630 return dy + dx/2.0;
528 } 631 }
529 } 632 }
OLDNEW
« no previous file with comments | « sky/sdk/example/game/lib/game_demo.dart ('k') | sky/sdk/example/game/lib/node_with_size.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698