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

Side by Side Diff: sky/examples/game/lib/game_demo_world.dart

Issue 1201983004: Adds API documentation for SkyGames (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 6 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 | « no previous file | sky/examples/game/lib/node.dart » ('j') | sky/examples/game/lib/node.dart » ('J')
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;
(...skipping 24 matching lines...) Expand all
35 bool _fire; 35 bool _fire;
36 36
37 Node _gameLayer; 37 Node _gameLayer;
38 38
39 Ship _ship; 39 Ship _ship;
40 List<Asteroid> _asteroids = []; 40 List<Asteroid> _asteroids = [];
41 List<Laser> _lasers = []; 41 List<Laser> _lasers = [];
42 StarField _starField; 42 StarField _starField;
43 Nebula _nebula; 43 Nebula _nebula;
44 44
45 GameDemoWorld(ImageMap images) : super.withSize(new Size(_gameSizeWidth, _game SizeHeight)) { 45 GameDemoWorld(ImageMap images) : super(new Size(_gameSizeWidth, _gameSizeHeigh t)) {
46 46
47 // Fetch images 47 // Fetch images
48 _imgBg = images["https://raw.githubusercontent.com/slembcke/GalacticGuardian .spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/resources-auto/Burn Texture.png"]; 48 _imgBg = images["https://raw.githubusercontent.com/slembcke/GalacticGuardian .spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/resources-auto/Burn Texture.png"];
49 _imgAsteroid = images["https://raw.githubusercontent.com/slembcke/GalacticGu ardian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resou rces-auto/asteroid_big_002.png"]; 49 _imgAsteroid = images["https://raw.githubusercontent.com/slembcke/GalacticGu ardian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resou rces-auto/asteroid_big_002.png"];
50 _imgShip = images["https://raw.githubusercontent.com/slembcke/GalacticGuardi an.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources -auto/GG_blueship_Lv3.png"]; 50 _imgShip = images["https://raw.githubusercontent.com/slembcke/GalacticGuardi an.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources -auto/GG_blueship_Lv3.png"];
51 _imgLaser = images["https://raw.githubusercontent.com/slembcke/GalacticGuard ian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resource s-auto/laserBlue.png"]; 51 _imgLaser = images["https://raw.githubusercontent.com/slembcke/GalacticGuard ian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resource s-auto/laserBlue.png"];
52 _imgStar = images["https://raw.githubusercontent.com/slembcke/GalacticGuardi an.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources -auto/laserFlashPurple.png"]; 52 _imgStar = images["https://raw.githubusercontent.com/slembcke/GalacticGuardi an.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources -auto/laserFlashPurple.png"];
53 _imgNebula = images["https://raw.githubusercontent.com/slembcke/GalacticGuar dian.spritebuilder/GDC/Source/Resources/NebulaClouds.png"]; 53 _imgNebula = images["https://raw.githubusercontent.com/slembcke/GalacticGuar dian.spritebuilder/GDC/Source/Resources/NebulaClouds.png"];
54 54
55 _gameLayer = new Node(); 55 _gameLayer = new Node();
(...skipping 18 matching lines...) Expand all
74 // Add nebula 74 // Add nebula
75 addNebula(); 75 addNebula();
76 76
77 userInteractionEnabled = true; 77 userInteractionEnabled = true;
78 handleMultiplePointers = true; 78 handleMultiplePointers = true;
79 } 79 }
80 80
81 // Methods for adding game objects 81 // Methods for adding game objects
82 82
83 void addBackground() { 83 void addBackground() {
84 Sprite sprtBg = new Sprite.withImage(_imgBg); 84 Sprite sprtBg = new Sprite(_imgBg);
85 sprtBg.size = new Size(_gameSizeWidth, _gameSizeHeight); 85 sprtBg.size = new Size(_gameSizeWidth, _gameSizeHeight);
86 sprtBg.pivot = Point.origin; 86 sprtBg.pivot = Point.origin;
87 _gameLayer.addChild(sprtBg); 87 _gameLayer.addChild(sprtBg);
88 } 88 }
89 89
90 void addAsteroid(AsteroidSize size, [Point pos]) { 90 void addAsteroid(AsteroidSize size, [Point pos]) {
91 Asteroid asteroid = new Asteroid.withImage(_imgAsteroid, size); 91 Asteroid asteroid = new Asteroid.withImage(_imgAsteroid, size);
92 asteroid.zPosition = 1.0; 92 asteroid.zPosition = 1.0;
93 if (pos != null) asteroid.position = pos; 93 if (pos != null) asteroid.position = pos;
94 _gameLayer.addChild(asteroid); 94 _gameLayer.addChild(asteroid);
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 double _radius; 289 double _radius;
290 290
291 double get radius { 291 double get radius {
292 if (_radius != null) return _radius; 292 if (_radius != null) return _radius;
293 if (_asteroidSize == AsteroidSize.small) _radius = _smlAsteroidRadius; 293 if (_asteroidSize == AsteroidSize.small) _radius = _smlAsteroidRadius;
294 else if (_asteroidSize == AsteroidSize.medium) _radius = _medAsteroidRadius; 294 else if (_asteroidSize == AsteroidSize.medium) _radius = _medAsteroidRadius;
295 else if (_asteroidSize == AsteroidSize.large) _radius = _lrgAsteroidRadius; 295 else if (_asteroidSize == AsteroidSize.large) _radius = _lrgAsteroidRadius;
296 return _radius; 296 return _radius;
297 } 297 }
298 298
299 Asteroid.withImage(Image img, AsteroidSize this._asteroidSize) : super.withIma ge(img) { 299 Asteroid.withImage(Image img, AsteroidSize this._asteroidSize) : super(img) {
300 size = new Size(radius * 2.0, radius * 2.0); 300 size = new Size(radius * 2.0, radius * 2.0);
301 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight * _rand.nextDouble()); 301 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight * _rand.nextDouble());
302 rotation = 360.0 * _rand.nextDouble(); 302 rotation = 360.0 * _rand.nextDouble();
303 303
304 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed, 304 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed,
305 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed); 305 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed);
306 306
307 userInteractionEnabled = true; 307 userInteractionEnabled = true;
308 } 308 }
309 309
310 bool handleEvent(SpriteBoxEvent event) { 310 bool handleEvent(SpriteBoxEvent event) {
311 if (event.type == "pointerdown") { 311 if (event.type == "pointerdown") {
312 colorOverlay = new Color(0x99ff0000); 312 colorOverlay = new Color(0x99ff0000);
313 } 313 }
314 else if (event.type == "pointerup") { 314 else if (event.type == "pointerup") {
315 colorOverlay = null; 315 colorOverlay = null;
316 } 316 }
317 return false; 317 return false;
318 } 318 }
319 } 319 }
320 320
321 class Ship extends Sprite { 321 class Ship extends Sprite {
322 Vector2 _movementVector; 322 Vector2 _movementVector;
323 double _rotationTarget; 323 double _rotationTarget;
324 324
325 Ship.withImage(Image img) : super.withImage(img) { 325 Ship.withImage(Image img) : super(img) {
326 _movementVector = new Vector2.zero(); 326 _movementVector = new Vector2.zero();
327 rotation = _rotationTarget = 270.0; 327 rotation = _rotationTarget = 270.0;
328 328
329 // Create sprite 329 // Create sprite
330 size = new Size(_shipRadius * 2.0, _shipRadius * 2.0); 330 size = new Size(_shipRadius * 2.0, _shipRadius * 2.0);
331 position = new Point(_gameSizeWidth/2.0, _gameSizeHeight/2.0); 331 position = new Point(_gameSizeWidth/2.0, _gameSizeHeight/2.0);
332 } 332 }
333 333
334 void thrust(double x, double y) { 334 void thrust(double x, double y) {
335 _rotationTarget = convertRadians2Degrees(Math.atan2(y, x)); 335 _rotationTarget = convertRadians2Degrees(Math.atan2(y, x));
336 Vector2 directionVector = new Vector2(x, y).normalize(); 336 Vector2 directionVector = new Vector2(x, y).normalize();
337 _movementVector.addScaled(directionVector, 1.0); 337 _movementVector.addScaled(directionVector, 1.0);
338 } 338 }
339 339
340 void move() { 340 void move() {
341 position = new Point(position.x + _movementVector[0], position.y + _movement Vector[1]); 341 position = new Point(position.x + _movementVector[0], position.y + _movement Vector[1]);
342 _movementVector.scale(0.9); 342 _movementVector.scale(0.9);
343 343
344 rotation = dampenRotation(rotation, _rotationTarget, 0.1); 344 rotation = dampenRotation(rotation, _rotationTarget, 0.1);
345 } 345 }
346 } 346 }
347 347
348 class Laser extends Sprite { 348 class Laser extends Sprite {
349 int _frameCount = 0; 349 int _frameCount = 0;
350 Point _movementVector; 350 Point _movementVector;
351 double radius = 10.0; 351 double radius = 10.0;
352 352
353 Laser.withImage(Image img, Ship ship) : super.withImage(img) { 353 Laser.withImage(Image img, Ship ship) : super(img) {
354 size = new Size(20.0, 20.0); 354 size = new Size(20.0, 20.0);
355 position = ship.position; 355 position = ship.position;
356 rotation = ship.rotation + 90.0; 356 rotation = ship.rotation + 90.0;
357 transferMode = TransferMode.plus; 357 transferMode = TransferMode.plus;
358 double rotRadians = convertDegrees2Radians(rotation); 358 double rotRadians = convertDegrees2Radians(rotation);
359 _movementVector = pointMult(new Point(Math.sin(rotRadians), -Math.cos(rotRad ians)), 10.0); 359 _movementVector = pointMult(new Point(Math.sin(rotRadians), -Math.cos(rotRad ians)), 10.0);
360 _movementVector = new Point(_movementVector.x + ship._movementVector[0], _mo vementVector.y + ship._movementVector[1]); 360 _movementVector = new Point(_movementVector.x + ship._movementVector[0], _mo vementVector.y + ship._movementVector[1]);
361 } 361 }
362 362
363 void move() { 363 void move() {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 _starPositions[i] = new Point(xPos, yPos); 429 _starPositions[i] = new Point(xPos, yPos);
430 } 430 }
431 } 431 }
432 } 432 }
433 433
434 class Nebula extends Node { 434 class Nebula extends Node {
435 435
436 Nebula.withImage(Image img) { 436 Nebula.withImage(Image img) {
437 for (int i = 0; i < 2; i++) { 437 for (int i = 0; i < 2; i++) {
438 for (int j = 0; j < 2; j++) { 438 for (int j = 0; j < 2; j++) {
439 Sprite sprt = new Sprite.withImage(img); 439 Sprite sprt = new Sprite(img);
440 sprt.pivot = Point.origin; 440 sprt.pivot = Point.origin;
441 sprt.position = new Point(i * _gameSizeWidth - _gameSizeWidth, j * _game SizeHeight - _gameSizeHeight); 441 sprt.position = new Point(i * _gameSizeWidth - _gameSizeWidth, j * _game SizeHeight - _gameSizeHeight);
442 addChild(sprt); 442 addChild(sprt);
443 } 443 }
444 } 444 }
445 } 445 }
446 } 446 }
447 447
448 // Convenience methods 448 // Convenience methods
449 449
(...skipping 19 matching lines...) Expand all
469 double dy = a.y - b.y; 469 double dy = a.y - b.y;
470 if (dx < 0.0) dx = -dx; 470 if (dx < 0.0) dx = -dx;
471 if (dy < 0.0) dy = -dy; 471 if (dy < 0.0) dy = -dy;
472 if (dx > dy) { 472 if (dx > dy) {
473 return dx + dy/2.0; 473 return dx + dy/2.0;
474 } 474 }
475 else { 475 else {
476 return dy + dx/2.0; 476 return dy + dx/2.0;
477 } 477 }
478 } 478 }
OLDNEW
« no previous file with comments | « no previous file | sky/examples/game/lib/node.dart » ('j') | sky/examples/game/lib/node.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698