Chromium Code Reviews| 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; |
| 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 class GameDemoWorld extends NodeWithSize { | 22 class GameDemoWorld extends NodeWithSize { |
| 23 | 23 |
| 24 // Images | 24 // Images |
| 25 Image _imgBg; | |
| 26 Image _imgAsteroid; | |
| 27 Image _imgShip; | |
| 28 Image _imgLaser; | |
| 29 Image _imgStar; | |
| 30 Image _imgNebula; | 25 Image _imgNebula; |
| 31 | 26 |
| 27 SpriteSheet _spriteSheet; | |
| 28 | |
| 32 // Inputs | 29 // Inputs |
| 33 double _joystickX = 0.0; | 30 double _joystickX = 0.0; |
| 34 double _joystickY = 0.0; | 31 double _joystickY = 0.0; |
| 35 bool _fire; | 32 bool _fire; |
| 36 | 33 |
| 37 Node _gameLayer; | 34 Node _gameLayer; |
| 38 | 35 |
| 39 Ship _ship; | 36 Ship _ship; |
| 40 List<Asteroid> _asteroids = []; | 37 List<Asteroid> _asteroids = []; |
| 41 List<Laser> _lasers = []; | 38 List<Laser> _lasers = []; |
| 42 StarField _starField; | 39 StarField _starField; |
| 43 Nebula _nebula; | 40 Nebula _nebula; |
| 44 | 41 |
| 45 GameDemoWorld(ImageMap images) : super(new Size(_gameSizeWidth, _gameSizeHeigh t)) { | 42 GameDemoWorld(ImageMap images, this._spriteSheet) : super(new Size(_gameSizeWi dth, _gameSizeHeight)) { |
| 46 | 43 |
| 47 // Fetch images | 44 // Fetch images |
| 48 _imgBg = images["https://raw.githubusercontent.com/slembcke/GalacticGuardian .spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/resources-auto/Burn Texture.png"]; | 45 _imgNebula = images["res/nebula.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"]; | |
| 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"]; | |
| 53 _imgNebula = images["https://raw.githubusercontent.com/slembcke/GalacticGuar dian.spritebuilder/GDC/Source/Resources/NebulaClouds.png"]; | |
| 54 | 46 |
| 55 _gameLayer = new Node(); | 47 _gameLayer = new Node(); |
| 56 this.addChild(_gameLayer); | 48 this.addChild(_gameLayer); |
| 57 | 49 |
| 58 // Add some asteroids to the game world | 50 // Add some asteroids to the game world |
| 59 for (int i = 0; i < 5; i++) { | 51 for (int i = 0; i < 5; i++) { |
| 60 addAsteroid(AsteroidSize.large); | 52 addAsteroid(AsteroidSize.large); |
| 61 } | 53 } |
| 62 for (int i = 0; i < 5; i++) { | 54 for (int i = 0; i < 5; i++) { |
| 63 addAsteroid(AsteroidSize.medium); | 55 addAsteroid(AsteroidSize.medium); |
| 64 } | 56 } |
| 65 | 57 |
| 66 // Add ship | 58 // Add ship |
| 67 addShip(); | 59 addShip(); |
| 68 | 60 |
| 69 // Add starfield | 61 // Add starfield |
| 70 _starField = new StarField.withImage(_imgStar, _numStarsInStarField); | 62 _starField = new StarField(_spriteSheet["star.png"], _numStarsInStarField); |
| 71 _starField.zPosition = -2.0; | 63 _starField.zPosition = -2.0; |
| 72 addChild(_starField); | 64 addChild(_starField); |
| 73 | 65 |
| 74 // Add nebula | 66 // Add nebula |
| 75 addNebula(); | 67 addNebula(); |
| 76 | 68 |
| 77 userInteractionEnabled = true; | 69 userInteractionEnabled = true; |
| 78 handleMultiplePointers = true; | 70 handleMultiplePointers = true; |
| 79 } | 71 } |
| 80 | 72 |
| 81 // Methods for adding game objects | 73 // Methods for adding game objects |
| 82 | |
| 83 void addBackground() { | |
| 84 Sprite sprtBg = new Sprite(_imgBg); | |
| 85 sprtBg.size = new Size(_gameSizeWidth, _gameSizeHeight); | |
| 86 sprtBg.pivot = Point.origin; | |
| 87 _gameLayer.addChild(sprtBg); | |
| 88 } | |
| 89 | 74 |
| 90 void addAsteroid(AsteroidSize size, [Point pos]) { | 75 void addAsteroid(AsteroidSize size, [Point pos]) { |
| 91 Asteroid asteroid = new Asteroid.withImage(_imgAsteroid, size); | 76 Asteroid asteroid = new Asteroid(_spriteSheet["asteroid_big_1.png"], size); |
| 92 asteroid.zPosition = 1.0; | 77 asteroid.zPosition = 1.0; |
| 93 if (pos != null) asteroid.position = pos; | 78 if (pos != null) asteroid.position = pos; |
| 94 _gameLayer.addChild(asteroid); | 79 _gameLayer.addChild(asteroid); |
| 95 _asteroids.add(asteroid); | 80 _asteroids.add(asteroid); |
| 96 } | 81 } |
| 97 | 82 |
| 98 void addShip() { | 83 void addShip() { |
| 99 Ship ship = new Ship.withImage(_imgShip); | 84 Ship ship = new Ship(_spriteSheet["ship.png"]); |
| 100 ship.zPosition = 10.0; | 85 ship.zPosition = 10.0; |
| 101 _gameLayer.addChild(ship); | 86 _gameLayer.addChild(ship); |
| 102 _ship = ship; | 87 _ship = ship; |
| 103 } | 88 } |
| 104 | 89 |
| 105 void addLaser() { | 90 void addLaser() { |
| 106 Laser laser = new Laser.withImage(_imgLaser, _ship); | 91 Laser laser = new Laser(_spriteSheet["laser.png"], _ship); |
| 107 laser.zPosition = 8.0; | 92 laser.zPosition = 8.0; |
| 108 _lasers.add(laser); | 93 _lasers.add(laser); |
| 109 _gameLayer.addChild(laser); | 94 _gameLayer.addChild(laser); |
| 110 } | 95 } |
| 111 | 96 |
| 112 void addNebula() { | 97 void addNebula() { |
| 113 _nebula = new Nebula.withImage(_imgNebula); | 98 _nebula = new Nebula.withImage(_imgNebula); |
| 114 _gameLayer.addChild(_nebula); | 99 _gameLayer.addChild(_nebula); |
| 115 } | 100 } |
| 116 | 101 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 double _radius; | 274 double _radius; |
| 290 | 275 |
| 291 double get radius { | 276 double get radius { |
| 292 if (_radius != null) return _radius; | 277 if (_radius != null) return _radius; |
| 293 if (_asteroidSize == AsteroidSize.small) _radius = _smlAsteroidRadius; | 278 if (_asteroidSize == AsteroidSize.small) _radius = _smlAsteroidRadius; |
| 294 else if (_asteroidSize == AsteroidSize.medium) _radius = _medAsteroidRadius; | 279 else if (_asteroidSize == AsteroidSize.medium) _radius = _medAsteroidRadius; |
| 295 else if (_asteroidSize == AsteroidSize.large) _radius = _lrgAsteroidRadius; | 280 else if (_asteroidSize == AsteroidSize.large) _radius = _lrgAsteroidRadius; |
| 296 return _radius; | 281 return _radius; |
| 297 } | 282 } |
| 298 | 283 |
| 299 Asteroid.withImage(Image img, AsteroidSize this._asteroidSize) : super(img) { | 284 Asteroid(Texture img, AsteroidSize this._asteroidSize) : super(img) { |
| 300 size = new Size(radius * 2.0, radius * 2.0); | 285 size = new Size(radius * 2.0, radius * 2.0); |
| 301 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight * _rand.nextDouble()); | 286 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight * _rand.nextDouble()); |
| 302 rotation = 360.0 * _rand.nextDouble(); | 287 rotation = 360.0 * _rand.nextDouble(); |
| 303 | 288 |
| 304 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed, | 289 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed, |
| 305 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed); | 290 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed); |
| 306 | 291 |
| 307 userInteractionEnabled = true; | 292 userInteractionEnabled = true; |
| 308 } | 293 } |
| 309 | 294 |
| 310 bool handleEvent(SpriteBoxEvent event) { | 295 bool handleEvent(SpriteBoxEvent event) { |
| 311 if (event.type == "pointerdown") { | 296 if (event.type == "pointerdown") { |
| 312 colorOverlay = new Color(0x99ff0000); | 297 colorOverlay = new Color(0x99ff0000); |
| 313 } | 298 } |
| 314 else if (event.type == "pointerup") { | 299 else if (event.type == "pointerup") { |
| 315 colorOverlay = null; | 300 colorOverlay = null; |
| 316 } | 301 } |
| 317 return false; | 302 return false; |
| 318 } | 303 } |
| 319 } | 304 } |
| 320 | 305 |
| 321 class Ship extends Sprite { | 306 class Ship extends Sprite { |
| 322 Vector2 _movementVector; | 307 Vector2 _movementVector; |
| 323 double _rotationTarget; | 308 double _rotationTarget; |
| 324 | 309 |
| 325 Ship.withImage(Image img) : super(img) { | 310 Ship(Texture img) : super(img) { |
| 326 _movementVector = new Vector2.zero(); | 311 _movementVector = new Vector2.zero(); |
| 327 rotation = _rotationTarget = 270.0; | 312 rotation = _rotationTarget = 270.0; |
| 328 | 313 |
| 329 // Create sprite | 314 // Create sprite |
| 330 size = new Size(_shipRadius * 2.0, _shipRadius * 2.0); | 315 size = new Size(_shipRadius * 2.0, _shipRadius * 2.0); |
| 331 position = new Point(_gameSizeWidth/2.0, _gameSizeHeight/2.0); | 316 position = new Point(_gameSizeWidth/2.0, _gameSizeHeight/2.0); |
| 332 } | 317 } |
| 333 | 318 |
| 334 void thrust(double x, double y) { | 319 void thrust(double x, double y) { |
| 335 _rotationTarget = convertRadians2Degrees(Math.atan2(y, x)); | 320 _rotationTarget = convertRadians2Degrees(Math.atan2(y, x)); |
| 336 Vector2 directionVector = new Vector2(x, y).normalize(); | 321 Vector2 directionVector = new Vector2(x, y).normalize(); |
| 337 _movementVector.addScaled(directionVector, 1.0); | 322 _movementVector.addScaled(directionVector, 1.0); |
| 338 } | 323 } |
| 339 | 324 |
| 340 void move() { | 325 void move() { |
| 341 position = new Point(position.x + _movementVector[0], position.y + _movement Vector[1]); | 326 position = new Point(position.x + _movementVector[0], position.y + _movement Vector[1]); |
| 342 _movementVector.scale(0.9); | 327 _movementVector.scale(0.9); |
| 343 | 328 |
| 344 rotation = dampenRotation(rotation, _rotationTarget, 0.1); | 329 rotation = dampenRotation(rotation, _rotationTarget, 0.1); |
| 345 } | 330 } |
| 346 } | 331 } |
| 347 | 332 |
| 348 class Laser extends Sprite { | 333 class Laser extends Sprite { |
| 349 int _frameCount = 0; | 334 int _frameCount = 0; |
| 350 Point _movementVector; | 335 Point _movementVector; |
| 351 double radius = 10.0; | 336 double radius = 10.0; |
| 352 | 337 |
| 353 Laser.withImage(Image img, Ship ship) : super(img) { | 338 Laser(Texture img, Ship ship) : super(img) { |
| 354 size = new Size(20.0, 20.0); | 339 size = new Size(20.0, 20.0); |
| 355 position = ship.position; | 340 position = ship.position; |
| 356 rotation = ship.rotation + 90.0; | 341 rotation = ship.rotation + 90.0; |
| 357 transferMode = TransferMode.plus; | 342 transferMode = TransferMode.plus; |
| 358 double rotRadians = convertDegrees2Radians(rotation); | 343 double rotRadians = convertDegrees2Radians(rotation); |
| 359 _movementVector = pointMult(new Point(Math.sin(rotRadians), -Math.cos(rotRad ians)), 10.0); | 344 _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]); | 345 _movementVector = new Point(_movementVector.x + ship._movementVector[0], _mo vementVector.y + ship._movementVector[1]); |
| 361 } | 346 } |
| 362 | 347 |
| 363 void move() { | 348 void move() { |
| 364 position = pointAdd(position, _movementVector); | 349 position = pointAdd(position, _movementVector); |
| 365 _frameCount++; | 350 _frameCount++; |
| 366 } | 351 } |
| 367 } | 352 } |
| 368 | 353 |
| 369 // Background starfield | 354 // Background starfield |
| 370 | 355 |
| 371 class StarField extends Node { | 356 class StarField extends Node { |
| 372 Image _img; | 357 Texture _img; |
| 373 int _numStars; | 358 int _numStars; |
| 374 List<Point> _starPositions; | 359 List<Point> _starPositions; |
| 375 List<double> _starScales; | 360 List<double> _starScales; |
| 376 List<double> _opacity; | 361 List<double> _opacity; |
| 377 | 362 |
| 378 StarField.withImage(Image this._img, int this._numStars) { | 363 StarField(this._img, this._numStars) { |
| 379 _starPositions = []; | 364 _starPositions = []; |
| 380 _starScales = []; | 365 _starScales = []; |
| 381 _opacity = []; | 366 _opacity = []; |
| 382 | 367 |
| 383 for (int i = 0; i < _numStars; i++) { | 368 for (int i = 0; i < _numStars; i++) { |
| 384 _starPositions.add(new Point(_rand.nextDouble() * _gameSizeWidth, _rand.ne xtDouble() * _gameSizeHeight)); | 369 _starPositions.add(new Point(_rand.nextDouble() * _gameSizeWidth, _rand.ne xtDouble() * _gameSizeHeight)); |
| 385 _starScales.add(_rand.nextDouble()); | 370 _starScales.add(_rand.nextDouble()); |
| 386 _opacity.add(_rand.nextDouble() * 0.5 + 0.5); | 371 _opacity.add(_rand.nextDouble() * 0.5 + 0.5); |
| 387 } | 372 } |
| 388 } | 373 } |
| 389 | 374 |
| 390 void paint(PictureRecorder canvas) { | 375 void paint(PictureRecorder canvas) { |
| 391 // Setup paint object for opacity and transfer mode | 376 // Setup paint object for opacity and transfer mode |
| 392 Paint paint = new Paint(); | 377 Paint paint = new Paint(); |
| 393 paint.setTransferMode(TransferMode.plus); | 378 paint.setTransferMode(TransferMode.plus); |
| 394 | 379 |
| 395 double baseScaleX = 32.0/_img.width; | 380 double baseScaleX = 32.0/_img.size.width; |
|
eseidel
2015/06/23 23:22:31
I thought our style was to have spaces around oper
| |
| 396 double baseScaleY = 32.0/_img.height; | 381 double baseScaleY = 32.0/_img.size.height; |
| 397 | 382 |
| 398 // Draw each star | 383 // Draw each star |
| 399 for (int i = 0; i < _numStars; i++) { | 384 for (int i = 0; i < _numStars; i++) { |
| 400 Point pos = _starPositions[i]; | 385 Point pos = _starPositions[i]; |
| 401 double scale = _starScales[i]; | 386 double scale = _starScales[i]; |
| 402 paint.color = new Color.fromARGB((255.0*_opacity[i]).toInt(), 255, 255, 25 5); | 387 paint.color = new Color.fromARGB((255.0*_opacity[i]).toInt(), 255, 255, 25 5); |
| 403 | 388 |
| 404 canvas.save(); | 389 canvas.save(); |
| 405 | 390 |
| 406 canvas.translate(pos.x, pos.y); | 391 canvas.translate(pos.x, pos.y); |
| 407 canvas.scale(baseScaleX*scale, baseScaleY*scale); | 392 canvas.scale(baseScaleX*scale, baseScaleY*scale); |
| 408 | 393 |
| 409 canvas.drawImage(_img, 0.0, 0.0, paint); | 394 canvas.drawImageRect(_img.image, _img.frame, _img.spriteSourceSize, paint) ; |
| 410 | 395 |
| 411 canvas.restore(); | 396 canvas.restore(); |
| 412 } | 397 } |
| 413 } | 398 } |
| 414 | 399 |
| 415 void move(double dx, double dy) { | 400 void move(double dx, double dy) { |
| 416 for (int i = 0; i < _numStars; i++) { | 401 for (int i = 0; i < _numStars; i++) { |
| 417 double xPos = _starPositions[i].x; | 402 double xPos = _starPositions[i].x; |
| 418 double yPos = _starPositions[i].y; | 403 double yPos = _starPositions[i].y; |
| 419 double scale = _starScales[i]; | 404 double scale = _starScales[i]; |
| 420 | 405 |
| 421 xPos += dx * scale; | 406 xPos += dx * scale; |
| 422 yPos += dy * scale; | 407 yPos += dy * scale; |
| 423 | 408 |
| 424 if (xPos >= _gameSizeWidth) xPos -= _gameSizeWidth; | 409 if (xPos >= _gameSizeWidth) xPos -= _gameSizeWidth; |
| 425 if (xPos < 0) xPos += _gameSizeWidth; | 410 if (xPos < 0) xPos += _gameSizeWidth; |
| 426 if (yPos >= _gameSizeHeight) yPos -= _gameSizeHeight; | 411 if (yPos >= _gameSizeHeight) yPos -= _gameSizeHeight; |
| 427 if (yPos < 0) yPos += _gameSizeHeight; | 412 if (yPos < 0) yPos += _gameSizeHeight; |
| 428 | 413 |
| 429 _starPositions[i] = new Point(xPos, yPos); | 414 _starPositions[i] = new Point(xPos, yPos); |
| 430 } | 415 } |
| 431 } | 416 } |
| 432 } | 417 } |
| 433 | 418 |
| 434 class Nebula extends Node { | 419 class Nebula extends Node { |
| 435 | 420 |
| 436 Nebula.withImage(Image img) { | 421 Nebula.withImage(Image img) { |
| 437 for (int i = 0; i < 2; i++) { | 422 for (int i = 0; i < 2; i++) { |
| 438 for (int j = 0; j < 2; j++) { | 423 for (int j = 0; j < 2; j++) { |
| 439 Sprite sprt = new Sprite(img); | 424 Sprite sprt = new Sprite.fromImage(img); |
| 440 sprt.pivot = Point.origin; | 425 sprt.pivot = Point.origin; |
| 441 sprt.position = new Point(i * _gameSizeWidth - _gameSizeWidth, j * _game SizeHeight - _gameSizeHeight); | 426 sprt.position = new Point(i * _gameSizeWidth - _gameSizeWidth, j * _game SizeHeight - _gameSizeHeight); |
| 442 addChild(sprt); | 427 addChild(sprt); |
| 443 } | 428 } |
| 444 } | 429 } |
| 445 } | 430 } |
| 446 } | 431 } |
| 447 | 432 |
| 448 // Convenience methods | 433 // Convenience methods |
| 449 | 434 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 469 double dy = a.y - b.y; | 454 double dy = a.y - b.y; |
| 470 if (dx < 0.0) dx = -dx; | 455 if (dx < 0.0) dx = -dx; |
| 471 if (dy < 0.0) dy = -dy; | 456 if (dy < 0.0) dy = -dy; |
| 472 if (dx > dy) { | 457 if (dx > dy) { |
| 473 return dx + dy/2.0; | 458 return dx + dy/2.0; |
| 474 } | 459 } |
| 475 else { | 460 else { |
| 476 return dy + dx/2.0; | 461 return dy + dx/2.0; |
| 477 } | 462 } |
| 478 } | 463 } |
| OLD | NEW |