| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 addAsteroid(AsteroidSize.large); | 52 addAsteroid(AsteroidSize.large); |
| 53 } | 53 } |
| 54 for (int i = 0; i < 5; i++) { | 54 for (int i = 0; i < 5; i++) { |
| 55 addAsteroid(AsteroidSize.medium); | 55 addAsteroid(AsteroidSize.medium); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Add ship | 58 // Add ship |
| 59 addShip(); | 59 addShip(); |
| 60 | 60 |
| 61 // Add starfield | 61 // Add starfield |
| 62 _starField = new StarField(_spriteSheet["star.png"], _numStarsInStarField); | 62 _starField = new StarField(_spriteSheet, _numStarsInStarField); |
| 63 _starField.zPosition = -2.0; | 63 _starField.zPosition = -2.0; |
| 64 addChild(_starField); | 64 addChild(_starField); |
| 65 | 65 |
| 66 // Add nebula | 66 // Add nebula |
| 67 addNebula(); | 67 addNebula(); |
| 68 | 68 |
| 69 userInteractionEnabled = true; | 69 userInteractionEnabled = true; |
| 70 handleMultiplePointers = true; | 70 handleMultiplePointers = true; |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Methods for adding game objects | 73 // Methods for adding game objects |
| 74 | 74 |
| 75 void addAsteroid(AsteroidSize size, [Point pos]) { | 75 void addAsteroid(AsteroidSize size, [Point pos]) { |
| 76 Asteroid asteroid = new Asteroid(_spriteSheet["asteroid_big_1.png"], size); | 76 Asteroid asteroid = new Asteroid(_spriteSheet, size); |
| 77 asteroid.zPosition = 1.0; | 77 asteroid.zPosition = 1.0; |
| 78 if (pos != null) asteroid.position = pos; | 78 if (pos != null) asteroid.position = pos; |
| 79 _gameLayer.addChild(asteroid); | 79 _gameLayer.addChild(asteroid); |
| 80 _asteroids.add(asteroid); | 80 _asteroids.add(asteroid); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void addShip() { | 83 void addShip() { |
| 84 Ship ship = new Ship(_spriteSheet["ship.png"]); | 84 Ship ship = new Ship(_spriteSheet["ship.png"]); |
| 85 ship.zPosition = 10.0; | 85 ship.zPosition = 10.0; |
| 86 _gameLayer.addChild(ship); | 86 _gameLayer.addChild(ship); |
| 87 _ship = ship; | 87 _ship = ship; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void addLaser() { | 90 void addLaser() { |
| 91 Laser laser = new Laser(_spriteSheet["laser.png"], _ship); | 91 Laser laser = new Laser(_spriteSheet["laser.png"], _ship); |
| 92 laser.zPosition = 8.0; | 92 laser.zPosition = 8.0; |
| 93 laser.constrainProportions = true; | 93 laser.constrainProportions = true; |
| 94 _lasers.add(laser); | 94 _lasers.add(laser); |
| 95 _gameLayer.addChild(laser); | 95 _gameLayer.addChild(laser); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void addNebula() { | 98 void addNebula() { |
| 99 _nebula = new Nebula.withImage(_imgNebula); | 99 _nebula = new Nebula.withImage(_imgNebula); |
| 100 _gameLayer.addChild(_nebula); | 100 _gameLayer.addChild(_nebula); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void addExplosion(AsteroidSize asteroidSize, Point position) { | 103 void addExplosion(AsteroidSize asteroidSize, Point position) { |
| 104 // Add particles | 104 // Add particles |
| 105 ParticleSystem particles = new ParticleSystem(_spriteSheet["laser.png"], rot
ateToMovement: true, | 105 ParticleSystem particles = new ParticleSystem(_spriteSheet["explosion_partic
le.png"], rotateToMovement: true, |
| 106 startRotation:90.0, startRotationVar: 0.0, endRotation: 90.0, startSize: 0.2
, startSizeVar: 0.1, endSize: 0.2, endSizeVar: 0.1, | 106 startRotation:90.0, startRotationVar: 0.0, endRotation: 90.0, startSize: 0.3
, startSizeVar: 0.1, endSize: 0.3, endSizeVar: 0.1, |
| 107 numParticlesToEmit: 25, emissionRate:1000.0, blueVar: 127); | 107 numParticlesToEmit: 25, emissionRate:1000.0, greenVar: 127, redVar: 127); |
| 108 particles.zPosition = 1010.0; | 108 particles.zPosition = 1010.0; |
| 109 particles.position = position; | 109 particles.position = position; |
| 110 _gameLayer.addChild(particles); | 110 _gameLayer.addChild(particles); |
| 111 |
| 112 // Add ring |
| 113 Sprite sprtRing = new Sprite(_spriteSheet["explosion_ring.png"]); |
| 114 sprtRing.position = position; |
| 115 sprtRing.transferMode = TransferMode.plus; |
| 116 _gameLayer.addChild(sprtRing); |
| 117 |
| 118 Action scale = new ActionTween( (a) => sprtRing.scale = a, 0.2, 1.0, 1.5); |
| 119 Action scaleAndRemove = new ActionSequence([scale, new ActionRemoveFromParen
t(sprtRing)]); |
| 120 Action fade = new ActionTween( (a) => sprtRing.opacity = a, 1.0, 0.0, 1.5); |
| 121 actions.run(scaleAndRemove); |
| 122 actions.run(fade); |
| 123 |
| 124 // Add streaks |
| 125 for (int i = 0; i < 5; i++) { |
| 126 Sprite sprtFlare = new Sprite(_spriteSheet["explosion_flare.png"]); |
| 127 sprtFlare.pivot = new Point(0.3, 1.0); |
| 128 sprtFlare.position = position; |
| 129 sprtFlare.scaleX = 0.3; |
| 130 sprtFlare.transferMode = TransferMode.plus; |
| 131 sprtFlare.rotation = _rand.nextDouble() * 360.0; |
| 132 _gameLayer.addChild(sprtFlare); |
| 133 |
| 134 double multiplier = _rand.nextDouble() * 0.3 + 1.0; |
| 135 |
| 136 Action scale = new ActionTween( (a) => sprtFlare.scaleY = a, 0.3 * multipl
ier, 0.8, 1.5 * multiplier); |
| 137 Action scaleAndRemove = new ActionSequence([scale, new ActionRemoveFromPar
ent(sprtFlare)]); |
| 138 Action fadeIn = new ActionTween( (a) => sprtFlare.opacity = a, 0.0, 1.0, 0
.5 * multiplier); |
| 139 Action fadeOut = new ActionTween( (a) => sprtFlare.opacity = a, 1.0, 0.0,
1.0 * multiplier); |
| 140 Action fadeInOut = new ActionSequence([fadeIn, fadeOut]); |
| 141 actions.run(scaleAndRemove); |
| 142 actions.run(fadeInOut); |
| 143 } |
| 111 } | 144 } |
| 112 | 145 |
| 113 void update(double dt) { | 146 void update(double dt) { |
| 114 // Move asteroids | 147 // Move asteroids |
| 115 for (Asteroid asteroid in _asteroids) { | 148 for (Asteroid asteroid in _asteroids) { |
| 116 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector); | 149 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector); |
| 117 } | 150 } |
| 118 | 151 |
| 119 // Move lasers and remove expired lasers | 152 // Move lasers and remove expired lasers |
| 120 for (int i = _lasers.length - 1; i >= 0; i--) { | 153 for (int i = _lasers.length - 1; i >= 0; i--) { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 double _radius; | 320 double _radius; |
| 288 | 321 |
| 289 double get radius { | 322 double get radius { |
| 290 if (_radius != null) return _radius; | 323 if (_radius != null) return _radius; |
| 291 if (_asteroidSize == AsteroidSize.small) _radius = _smlAsteroidRadius; | 324 if (_asteroidSize == AsteroidSize.small) _radius = _smlAsteroidRadius; |
| 292 else if (_asteroidSize == AsteroidSize.medium) _radius = _medAsteroidRadius; | 325 else if (_asteroidSize == AsteroidSize.medium) _radius = _medAsteroidRadius; |
| 293 else if (_asteroidSize == AsteroidSize.large) _radius = _lrgAsteroidRadius; | 326 else if (_asteroidSize == AsteroidSize.large) _radius = _lrgAsteroidRadius; |
| 294 return _radius; | 327 return _radius; |
| 295 } | 328 } |
| 296 | 329 |
| 297 Asteroid(Texture img, AsteroidSize this._asteroidSize) : super(img) { | 330 Asteroid(SpriteSheet spriteSheet, AsteroidSize this._asteroidSize) { |
| 298 size = new Size(radius * 2.0, radius * 2.0); | 331 size = new Size(radius * 2.0, radius * 2.0); |
| 299 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight *
_rand.nextDouble()); | 332 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight *
_rand.nextDouble()); |
| 300 rotation = 360.0 * _rand.nextDouble(); | 333 rotation = 360.0 * _rand.nextDouble(); |
| 301 | 334 |
| 335 if (_asteroidSize == AsteroidSize.small) { |
| 336 texture = spriteSheet["asteroid_small_${_rand.nextInt(2)}.png"]; |
| 337 } else { |
| 338 texture = spriteSheet["asteroid_big_${_rand.nextInt(2)}.png"]; |
| 339 } |
| 340 |
| 302 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma
xAsteroidSpeed, | 341 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma
xAsteroidSpeed, |
| 303 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma
xAsteroidSpeed); | 342 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma
xAsteroidSpeed); |
| 304 | 343 |
| 305 userInteractionEnabled = true; | 344 userInteractionEnabled = true; |
| 306 | 345 |
| 307 // Rotate forever | 346 // Rotate forever |
| 308 double direction = (_rand.nextBool()) ? 360.0 : -360.0; | 347 double direction = (_rand.nextBool()) ? 360.0 : -360.0; |
| 309 ActionTween rot = new ActionTween( (a) => rotation = a, 0.0, direction, 2.0
* _rand.nextDouble() + 2.0); | 348 ActionTween rot = new ActionTween( (a) => rotation = a, 0.0, direction, 2.0
* _rand.nextDouble() + 2.0); |
| 310 ActionRepeatForever repeat = new ActionRepeatForever(rot); | 349 ActionRepeatForever repeat = new ActionRepeatForever(rot); |
| 311 actions.run(repeat); | 350 actions.run(repeat); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 | 405 |
| 367 void move() { | 406 void move() { |
| 368 position = pointAdd(position, _movementVector); | 407 position = pointAdd(position, _movementVector); |
| 369 _frameCount++; | 408 _frameCount++; |
| 370 } | 409 } |
| 371 } | 410 } |
| 372 | 411 |
| 373 // Background starfield | 412 // Background starfield |
| 374 | 413 |
| 375 class StarField extends Node { | 414 class StarField extends Node { |
| 376 Texture _img; | |
| 377 int _numStars; | 415 int _numStars; |
| 378 List<Point> _starPositions; | 416 List<Point> _starPositions; |
| 379 List<double> _starScales; | 417 List<double> _starScales; |
| 380 List<double> _opacity; | 418 List<double> _opacity; |
| 419 List<Texture> _textures; |
| 381 | 420 |
| 382 StarField(this._img, this._numStars) { | 421 StarField(SpriteSheet spriteSheet, this._numStars) { |
| 383 _starPositions = []; | 422 _starPositions = []; |
| 384 _starScales = []; | 423 _starScales = []; |
| 385 _opacity = []; | 424 _opacity = []; |
| 425 _textures = []; |
| 386 | 426 |
| 387 for (int i = 0; i < _numStars; i++) { | 427 for (int i = 0; i < _numStars; i++) { |
| 388 _starPositions.add(new Point(_rand.nextDouble() * _gameSizeWidth, _rand.ne
xtDouble() * _gameSizeHeight)); | 428 _starPositions.add(new Point(_rand.nextDouble() * _gameSizeWidth, _rand.ne
xtDouble() * _gameSizeHeight)); |
| 389 _starScales.add(_rand.nextDouble()); | 429 _starScales.add(_rand.nextDouble()); |
| 390 _opacity.add(_rand.nextDouble() * 0.5 + 0.5); | 430 _opacity.add(_rand.nextDouble() * 0.5 + 0.5); |
| 431 _textures.add(spriteSheet["star_${_rand.nextInt(2)}.png"]); |
| 391 } | 432 } |
| 392 } | 433 } |
| 393 | 434 |
| 394 void paint(PaintingCanvas canvas) { | 435 void paint(PaintingCanvas canvas) { |
| 395 // Setup paint object for opacity and transfer mode | 436 // Setup paint object for opacity and transfer mode |
| 396 Paint paint = new Paint(); | 437 Paint paint = new Paint(); |
| 397 paint.setTransferMode(TransferMode.plus); | 438 paint.setTransferMode(TransferMode.plus); |
| 398 | 439 |
| 399 double baseScaleX = 32.0 / _img.size.width; | 440 double baseScaleX = 64.0 / _textures[0].size.width; |
| 400 double baseScaleY = 32.0 / _img.size.height; | 441 double baseScaleY = 64.0 / _textures[0].size.height; |
| 401 | 442 |
| 402 // Draw each star | 443 // Draw each star |
| 403 for (int i = 0; i < _numStars; i++) { | 444 for (int i = 0; i < _numStars; i++) { |
| 404 Point pos = _starPositions[i]; | 445 Point pos = _starPositions[i]; |
| 405 double scale = _starScales[i]; | 446 double scale = _starScales[i]; |
| 406 paint.color = new Color.fromARGB((255.0*_opacity[i]).toInt(), 255, 255, 25
5); | 447 paint.color = new Color.fromARGB((255.0*_opacity[i]).toInt(), 255, 255, 25
5); |
| 407 | 448 |
| 408 canvas.save(); | 449 canvas.save(); |
| 409 | 450 |
| 410 canvas.translate(pos.x, pos.y); | 451 canvas.translate(pos.x, pos.y); |
| 411 canvas.scale(baseScaleX * scale, baseScaleY * scale); | 452 canvas.scale(baseScaleX * scale, baseScaleY * scale); |
| 412 | 453 |
| 413 canvas.drawImageRect(_img.image, _img.frame, _img.spriteSourceSize, paint)
; | 454 canvas.drawImageRect(_textures[i].image, _textures[i].frame, _textures[i].
spriteSourceSize, paint); |
| 414 | 455 |
| 415 canvas.restore(); | 456 canvas.restore(); |
| 416 } | 457 } |
| 417 } | 458 } |
| 418 | 459 |
| 419 void move(double dx, double dy) { | 460 void move(double dx, double dy) { |
| 420 for (int i = 0; i < _numStars; i++) { | 461 for (int i = 0; i < _numStars; i++) { |
| 421 double xPos = _starPositions[i].x; | 462 double xPos = _starPositions[i].x; |
| 422 double yPos = _starPositions[i].y; | 463 double yPos = _starPositions[i].y; |
| 423 double scale = _starScales[i]; | 464 double scale = _starScales[i]; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 double dy = a.y - b.y; | 514 double dy = a.y - b.y; |
| 474 if (dx < 0.0) dx = -dx; | 515 if (dx < 0.0) dx = -dx; |
| 475 if (dy < 0.0) dy = -dy; | 516 if (dy < 0.0) dy = -dy; |
| 476 if (dx > dy) { | 517 if (dx > dy) { |
| 477 return dx + dy/2.0; | 518 return dx + dy/2.0; |
| 478 } | 519 } |
| 479 else { | 520 else { |
| 480 return dy + dx/2.0; | 521 return dy + dx/2.0; |
| 481 } | 522 } |
| 482 } | 523 } |
| OLD | NEW |