| OLD | NEW |
| 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 29 matching lines...) Expand all Loading... |
| 40 Ship _ship; | 40 Ship _ship; |
| 41 Sprite _shield; | 41 Sprite _shield; |
| 42 List<Asteroid> _asteroids = []; | 42 List<Asteroid> _asteroids = []; |
| 43 List<Laser> _lasers = []; | 43 List<Laser> _lasers = []; |
| 44 StarField _starField; | 44 StarField _starField; |
| 45 Nebula _nebula; | 45 Nebula _nebula; |
| 46 | 46 |
| 47 // Game state | 47 // Game state |
| 48 int _numFrames = 0; | 48 int _numFrames = 0; |
| 49 bool _isGameOver = false; | 49 bool _isGameOver = false; |
| 50 | 50 |
| 51 GameDemoWorld(this._app, ImageMap images, this._spriteSheet) : super(new Size(
_gameSizeWidth, _gameSizeHeight)) { | 51 GameDemoWorld(this._app, ImageMap images, this._spriteSheet) : super(new Size(
_gameSizeWidth, _gameSizeHeight)) { |
| 52 | 52 |
| 53 // Fetch images | 53 // Fetch images |
| 54 _imgNebula = images["res/nebula.png"]; | 54 _imgNebula = images["res/nebula.png"]; |
| 55 | 55 |
| 56 _gameLayer = new Node(); | 56 _gameLayer = new Node(); |
| 57 this.addChild(_gameLayer); | 57 this.addChild(_gameLayer); |
| 58 | 58 |
| 59 // Add some asteroids to the game world | 59 // Add some asteroids to the game world |
| 60 for (int i = 0; i < 5; i++) { | 60 for (int i = 0; i < 5; i++) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 79 addChild(_starField); | 79 addChild(_starField); |
| 80 | 80 |
| 81 // Add nebula | 81 // Add nebula |
| 82 addNebula(); | 82 addNebula(); |
| 83 | 83 |
| 84 userInteractionEnabled = true; | 84 userInteractionEnabled = true; |
| 85 handleMultiplePointers = true; | 85 handleMultiplePointers = true; |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Methods for adding game objects | 88 // Methods for adding game objects |
| 89 | 89 |
| 90 void addAsteroid(AsteroidSize size, [Point pos]) { | 90 void addAsteroid(AsteroidSize size, [Point pos]) { |
| 91 Asteroid asteroid = new Asteroid(_spriteSheet, size); | 91 Asteroid asteroid = new Asteroid(_spriteSheet, 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); |
| 95 _asteroids.add(asteroid); | 95 _asteroids.add(asteroid); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void addShip() { | 98 void addShip() { |
| 99 Ship ship = new Ship(_spriteSheet["ship.png"]); | 99 Ship ship = new Ship(_spriteSheet["ship.png"]); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 explosionNode.position = position; | 197 explosionNode.position = position; |
| 198 explosionNode.zPosition = 1010.0; | 198 explosionNode.zPosition = 1010.0; |
| 199 | 199 |
| 200 if (asteroidSize == AsteroidSize.large) { | 200 if (asteroidSize == AsteroidSize.large) { |
| 201 explosionNode.scale = 1.5; | 201 explosionNode.scale = 1.5; |
| 202 } | 202 } |
| 203 | 203 |
| 204 _gameLayer.addChild(explosionNode); | 204 _gameLayer.addChild(explosionNode); |
| 205 } | 205 } |
| 206 | 206 |
| 207 @override |
| 207 void update(double dt) { | 208 void update(double dt) { |
| 208 // Move asteroids | 209 // Move asteroids |
| 209 for (Asteroid asteroid in _asteroids) { | 210 for (Asteroid asteroid in _asteroids) { |
| 210 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector); | 211 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector); |
| 211 } | 212 } |
| 212 | 213 |
| 213 // Move lasers and remove expired lasers | 214 // Move lasers and remove expired lasers |
| 214 for (int i = _lasers.length - 1; i >= 0; i--) { | 215 for (int i = _lasers.length - 1; i >= 0; i--) { |
| 215 Laser laser = _lasers[i]; | 216 Laser laser = _lasers[i]; |
| 216 laser.move(); | 217 laser.move(); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 | 341 |
| 341 addLaser(); | 342 addLaser(); |
| 342 } | 343 } |
| 343 | 344 |
| 344 // Handle pointer events | 345 // Handle pointer events |
| 345 | 346 |
| 346 int _firstPointer = -1; | 347 int _firstPointer = -1; |
| 347 int _secondPointer = -1; | 348 int _secondPointer = -1; |
| 348 Point _firstPointerDownPos; | 349 Point _firstPointerDownPos; |
| 349 | 350 |
| 351 @override |
| 350 bool handleEvent(SpriteBoxEvent event) { | 352 bool handleEvent(SpriteBoxEvent event) { |
| 351 | 353 |
| 352 Point pointerPos = convertPointToNodeSpace(event.boxPosition); | 354 Point pointerPos = convertPointToNodeSpace(event.boxPosition); |
| 353 int pointer = event.pointer; | 355 int pointer = event.pointer; |
| 354 | 356 |
| 355 switch (event.type) { | 357 switch (event.type) { |
| 356 case 'pointerdown': | 358 case 'pointerdown': |
| 357 if (_firstPointer == -1) { | 359 if (_firstPointer == -1) { |
| 358 // Assign the first pointer | 360 // Assign the first pointer |
| 359 _firstPointer = pointer; | 361 _firstPointer = pointer; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 | 449 |
| 448 userInteractionEnabled = true; | 450 userInteractionEnabled = true; |
| 449 | 451 |
| 450 // Rotate forever | 452 // Rotate forever |
| 451 double direction = (_rand.nextBool()) ? 360.0 : -360.0; | 453 double direction = (_rand.nextBool()) ? 360.0 : -360.0; |
| 452 ActionTween rot = new ActionTween( (a) => rotation = a, 0.0, direction, 2.0
* _rand.nextDouble() + 2.0); | 454 ActionTween rot = new ActionTween( (a) => rotation = a, 0.0, direction, 2.0
* _rand.nextDouble() + 2.0); |
| 453 ActionRepeatForever repeat = new ActionRepeatForever(rot); | 455 ActionRepeatForever repeat = new ActionRepeatForever(rot); |
| 454 actions.run(repeat); | 456 actions.run(repeat); |
| 455 } | 457 } |
| 456 | 458 |
| 459 @override |
| 457 bool handleEvent(SpriteBoxEvent event) { | 460 bool handleEvent(SpriteBoxEvent event) { |
| 458 if (event.type == "pointerdown") { | 461 if (event.type == "pointerdown") { |
| 459 actions.stopWithTag("fade"); | 462 actions.stopWithTag("fade"); |
| 460 colorOverlay = new Color(0x99ffffff); | 463 colorOverlay = new Color(0x99ffffff); |
| 461 } | 464 } |
| 462 else if (event.type == "pointerup") { | 465 else if (event.type == "pointerup") { |
| 463 // Fade out the color overlay | 466 // Fade out the color overlay |
| 464 Action fadeOut = new ActionTween((a) => this.colorOverlay = a, new Color(0
x99ffffff), new Color(0x00ffffff), 1.0); | 467 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)]); | 468 Action fadeOutAndRemove = new ActionSequence([fadeOut, new ActionCallFunct
ion(() => this.colorOverlay = null)]); |
| 466 actions.run(fadeOutAndRemove, "fade"); | 469 actions.run(fadeOutAndRemove, "fade"); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 double dy = a.y - b.y; | 626 double dy = a.y - b.y; |
| 624 if (dx < 0.0) dx = -dx; | 627 if (dx < 0.0) dx = -dx; |
| 625 if (dy < 0.0) dy = -dy; | 628 if (dy < 0.0) dy = -dy; |
| 626 if (dx > dy) { | 629 if (dx > dy) { |
| 627 return dx + dy/2.0; | 630 return dx + dy/2.0; |
| 628 } | 631 } |
| 629 else { | 632 else { |
| 630 return dy + dx/2.0; | 633 return dy + dx/2.0; |
| 631 } | 634 } |
| 632 } | 635 } |
| OLD | NEW |