game_demo_world.dart 21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
part of game;

const double _steeringThreshold = 0.0;
const double _steeringMax = 150.0;

// Random generator
Math.Random _rand = new Math.Random();

const double _gameSizeWidth = 1024.0;
const double _gameSizeHeight = 1024.0;

const double _shipRadius = 30.0;
const double _lrgAsteroidRadius = 40.0;
const double _medAsteroidRadius = 20.0;
const double _smlAsteroidRadius = 10.0;
const double _maxAsteroidSpeed = 1.0;

const int _lifeTimeLaser = 50;

const int _numStarsInStarField = 150;

const int _numFramesShieldActive = 60 * 5;
const int _numFramesShieldFlickers = 60;

class GameDemoWorld extends NodeWithSize {
  // Images
  sky.Image _imgNebula;

  SpriteSheet _spriteSheet;
30
  SpriteSheet _spriteSheetUI;
31
  Navigator _navigator;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

  // Inputs
  double _joystickX = 0.0;
  double _joystickY = 0.0;

  Node _gameLayer;

  Ship _ship;
  Sprite _shield;
  List<Asteroid> _asteroids = [];
  List<Laser> _lasers = [];
  StarField _starField;
  Nebula _nebula;

  // Game state
  int _numFrames = 0;
  bool _isGameOver = false;
49
  int _gameOverFrame;
50
  int _currentLevel = 0;
51

52 53
  // Heads up display
  Hud _hud;
54

55 56 57
  Function _gameOverCallback;

  GameDemoWorld(App app, this._navigator, ImageMap images, this._spriteSheet, this._spriteSheetUI, this._gameOverCallback) : super(new Size(_gameSizeWidth, _gameSizeHeight)) {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    // Fetch images
    _imgNebula = images["assets/nebula.png"];

    _gameLayer = new Node();
    this.addChild(_gameLayer);
    // Add ship
    addShip();

    // Add background
    Sprite sprtBackground = new Sprite.fromImage(images["assets/starfield.png"]);
    sprtBackground.position = new Point(512.0, 512.0);
    sprtBackground.zPosition = -3.0;
    addChild(sprtBackground);

    // Add starfield
    _starField = new StarField(_spriteSheet, _numStarsInStarField);
    _starField.zPosition = -2.0;
    addChild(_starField);

    // Add nebula
    addNebula();

    userInteractionEnabled = true;
    handleMultiplePointers = true;
82 83 84 85

    _hud = new Hud(_spriteSheetUI);
    _hud.zPosition = 1000.0;
    addChild(_hud);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    // Setup level
    setupLevel(0);
  }

  void setupLevel(int level) {
    int numLargeAsteroids = 5 + level * 2;
    int numMediumAsteroids = 5 + level * 2;

    // Add some asteroids to the game world
    for (int i = 0; i < numLargeAsteroids; i++) {
      addAsteroid(AsteroidSize.large);
    }
    for (int i = 0; i < numMediumAsteroids; i++) {
      addAsteroid(AsteroidSize.medium);
    }

    _numFrames = 0;
    _shield.visible = true;
105 106 107 108 109 110 111 112 113 114
  }

  // Methods for adding game objects

  void addAsteroid(AsteroidSize size, [Point pos]) {
    Asteroid asteroid = new Asteroid(_spriteSheet, size);
    asteroid.zPosition = 1.0;
    if (pos != null) asteroid.position = pos;
    _gameLayer.addChild(asteroid);
    _asteroids.add(asteroid);
115 116 117 118

    // Animate asteroid into the scene
    Action action = new ActionTween((a) => asteroid.scale = a, 0.0, 1.0, 1.0, bounceOut);
    _gameLayer.actions.run(action);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  }

  void addShip() {
    Ship ship = new Ship(_spriteSheet["ship.png"]);
    ship.zPosition = 10.0;
    _gameLayer.addChild(ship);
    _ship = ship;

    _shield = new Sprite(_spriteSheet["shield.png"]);
    _shield.zPosition = 11.0;
    _shield.scale = 0.5;
    _shield.transferMode = sky.TransferMode.plus;
    _gameLayer.addChild(_shield);

    Action rotate = new ActionRepeatForever(new ActionTween((a) => _shield.rotation = a, 0.0, 360.0, 1.0));
    actions.run(rotate);
  }

  void addLaser() {
    Laser laser = new Laser(_spriteSheet["laser.png"], _ship);
    laser.zPosition = 8.0;
    laser.constrainProportions = true;
    _lasers.add(laser);
    _gameLayer.addChild(laser);
  }

  void addNebula() {
    _nebula = new Nebula.withImage(_imgNebula);
    _gameLayer.addChild(_nebula);
  }

  void addExplosion(AsteroidSize asteroidSize, Point position) {
    Node explosionNode = new Node();

    // Add particles
    ParticleSystem particlesDebris = new ParticleSystem(
        _spriteSheet["explosion_particle.png"],
        rotateToMovement: true,
        startRotation:90.0,
        startRotationVar: 0.0,
        endRotation: 90.0,
        startSize: 0.3,
        startSizeVar: 0.1,
        endSize: 0.3,
        endSizeVar: 0.1,
        numParticlesToEmit: 25,
        emissionRate:1000.0,
        greenVar: 127,
        redVar: 127
    );
    particlesDebris.zPosition = 1010.0;
    explosionNode.addChild(particlesDebris);

    ParticleSystem particlesFire = new ParticleSystem(
      _spriteSheet["fire_particle.png"],
      colorSequence: new ColorSequence([new Color(0xffffff33), new Color(0xffff3333), new Color(0x00ff3333)], [0.0, 0.5, 1.0]),
      numParticlesToEmit: 25,
      emissionRate: 1000.0,
      startSize: 0.5,
      startSizeVar: 0.1,
      endSize: 0.5,
      endSizeVar: 0.1,
      posVar: new Point(10.0, 10.0),
      speed: 10.0,
      speedVar: 5.0
    );
    particlesFire.zPosition = 1011.0;
    explosionNode.addChild(particlesFire);


    // Add ring
    Sprite sprtRing = new Sprite(_spriteSheet["explosion_ring.png"]);
    sprtRing.transferMode = sky.TransferMode.plus;
    explosionNode.addChild(sprtRing);

    Action scale = new ActionTween( (a) => sprtRing.scale = a, 0.2, 1.0, 1.5);
    Action scaleAndRemove = new ActionSequence([scale, new ActionRemoveNode(sprtRing)]);
    Action fade = new ActionTween( (a) => sprtRing.opacity = a, 1.0, 0.0, 1.5);
    actions.run(scaleAndRemove);
    actions.run(fade);

    // Add streaks
    for (int i = 0; i < 5; i++) {
      Sprite sprtFlare = new Sprite(_spriteSheet["explosion_flare.png"]);
      sprtFlare.pivot = new Point(0.3, 1.0);
      sprtFlare.scaleX = 0.3;
      sprtFlare.transferMode = sky.TransferMode.plus;
      sprtFlare.rotation = _rand.nextDouble() * 360.0;
      explosionNode.addChild(sprtFlare);

      double multiplier = _rand.nextDouble() * 0.3 + 1.0;

      Action scale = new ActionTween( (a) => sprtFlare.scaleY = a, 0.3 * multiplier, 0.8, 1.5 * multiplier);
      Action scaleAndRemove = new ActionSequence([scale, new ActionRemoveNode(sprtFlare)]);
      Action fadeIn = new ActionTween( (a) => sprtFlare.opacity = a, 0.0, 1.0, 0.5 * multiplier);
      Action fadeOut = new ActionTween( (a) => sprtFlare.opacity = a, 1.0, 0.0, 1.0 * multiplier);
      Action fadeInOut = new ActionSequence([fadeIn, fadeOut]);
      actions.run(scaleAndRemove);
      actions.run(fadeInOut);
    }

    explosionNode.position = position;
    explosionNode.zPosition = 1010.0;

    if (asteroidSize == AsteroidSize.large) {
      explosionNode.scale = 1.5;
    }

    _gameLayer.addChild(explosionNode);
  }

  void update(double dt) {
    // Move asteroids
    for (Asteroid asteroid in _asteroids) {
      asteroid.position = pointAdd(asteroid.position, asteroid._movementVector);
    }

    // Move lasers and remove expired lasers
    for (int i = _lasers.length - 1; i >= 0; i--) {
      Laser laser = _lasers[i];
      laser.move();
      if (laser._frameCount > _lifeTimeLaser) {
        laser.removeFromParent();
        _lasers.removeAt(i);
      }
    }

    // Apply thrust to ship
    if (_joystickX != 0.0 || _joystickY != 0.0) {
      _ship.thrust(_joystickX, _joystickY);
    }

    // Move ship
    _ship.move();
    _shield.position = _ship.position;

    // Check collisions between asteroids and lasers
    for (int i = _lasers.length -1; i >= 0; i--) {
      // Iterate over all the lasers
      Laser laser = _lasers[i];

      for (int j = _asteroids.length - 1; j >= 0; j--) {
        // Iterate over all the asteroids
        Asteroid asteroid = _asteroids[j];

        // Check for collision
        if (pointQuickDist(laser.position, asteroid.position) < laser.radius + asteroid.radius) {
          // Remove laser
          laser.removeFromParent();
          _lasers.removeAt(i);

          // Add asteroids and explosions
          if (asteroid._asteroidSize == AsteroidSize.large) {
            for (int a = 0; a < 3; a++) addAsteroid(AsteroidSize.medium, asteroid.position);
          }
          else if (asteroid._asteroidSize == AsteroidSize.medium) {
            for (int a = 0; a < 5; a++) addAsteroid(AsteroidSize.small, asteroid.position);
          }

          addExplosion(asteroid._asteroidSize, asteroid.position);

          // Remove asteroid
          asteroid.removeFromParent();
          _asteroids.removeAt(j);
283 284 285 286 287 288 289 290

          // Scoring
          if (asteroid._asteroidSize == AsteroidSize.large)
            addScore(100);
          else if (asteroid._asteroidSize == AsteroidSize.medium)
            addScore(50);
          else
            addScore(10);
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
          break;
        }
      }
    }

    // Check collisions between asteroids and ship
    if (_numFrames > _numFramesShieldActive) {
      // Shield is no longer active

      for (int i = _asteroids.length - 1; i >= 0; i--) {
        // Iterate over all the asteroids
        Asteroid asteroid = _asteroids[i];

        if (pointQuickDist(asteroid.position, _ship.position) < asteroid.radius + _ship.radius) {
          killShip();
        }
      }
    }

    // Move objects to center camera and warp objects around the edges
    centerCamera();
    warpObjects();

314 315 316 317 318 319
    // Check for level up
    if (_asteroids.length == 0) {
      _currentLevel++;
      setupLevel(_currentLevel);
    }

320 321 322 323
    // Update shield
    if (_numFrames > _numFramesShieldActive) _shield.visible = false;
    else if (_numFrames > _numFramesShieldActive - _numFramesShieldFlickers) _shield.visible = !_shield.visible;

324 325 326 327 328
    // Check for exit back to main screen
    if (_isGameOver && _numFrames - _gameOverFrame == 60) {
      _navigator.pop();
    }

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    _numFrames++;
  }

  void centerCamera() {
    const cameraDampening = 0.1;
    Point delta = new Point(_gameSizeWidth/2 - _ship.position.x, _gameSizeHeight/2 - _ship.position.y);
    delta = pointMult(delta, cameraDampening);

    for (Node child in _gameLayer.children) {
      child.position = pointAdd(child.position, delta);
    }

    // Update starfield
    _starField.move(delta.x, delta.y);
  }

  void warpObjects() {
    for (Node child in _gameLayer.children) {
      if (child.position.x < 0) child.position = pointAdd(child.position, new Point(_gameSizeWidth, 0.0));
      if (child.position.x >= _gameSizeWidth) child.position = pointAdd(child.position, new Point(-_gameSizeWidth, 0.0));
      if (child.position.y < 0) child.position = pointAdd(child.position, new Point(0.0, _gameSizeHeight));
      if (child.position.y >= _gameSizeHeight) child.position = pointAdd(child.position, new Point(0.0, -_gameSizeHeight));
    }
  }

  void killShip() {
    if (_isGameOver) return;

    // Set game over
    _isGameOver = true;
359
    _gameOverFrame = _numFrames;
360
    _gameOverCallback(_hud.score);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

    // Remove the ship
    _ship.visible = false;

    // Add an explosion
    addExplosion(AsteroidSize.large, _ship.position);
  }

  // Handling controls

  void controlSteering(double x, double y) {
    // Reset controls if it's game over
    if (_isGameOver) {
      x = y = 0.0;
    }

    _joystickX = x;
    _joystickY = y;
  }

  void controlFire() {
    // Don't shoot if it's game over
    if (_isGameOver) return;

    addLaser();
  }

  // Handle pointer events

  int _firstPointer = -1;
  int _secondPointer = -1;
  Point _firstPointerDownPos;

  bool handleEvent(SpriteBoxEvent event) {

    Point pointerPos = convertPointToNodeSpace(event.boxPosition);
    int pointer = event.pointer;

    switch (event.type) {
      case 'pointerdown':
        if (_firstPointer == -1) {
          // Assign the first pointer
          _firstPointer = pointer;
          _firstPointerDownPos = pointerPos;
        }
        else if (_secondPointer == -1) {
          // Assign second pointer
          _secondPointer = pointer;
          controlFire();
        }
        else {
          // There is a pointer used for steering, let's fire instead
          controlFire();
        }
        break;
      case 'pointermove':
        if (pointer == _firstPointer) {
          // Handle turning control
          double joystickX = 0.0;
          double deltaX = pointerPos.x - _firstPointerDownPos.x;
          if (deltaX > _steeringThreshold || deltaX < -_steeringThreshold) {
            joystickX = (deltaX - _steeringThreshold)/(_steeringMax - _steeringThreshold);
            if (joystickX > 1.0) joystickX = 1.0;
            if (joystickX < -1.0) joystickX = -1.0;
          }

          double joystickY = 0.0;
          double deltaY = pointerPos.y - _firstPointerDownPos.y;
          if (deltaY > _steeringThreshold || deltaY < -_steeringThreshold) {
            joystickY = (deltaY - _steeringThreshold)/(_steeringMax - _steeringThreshold);
            if (joystickY > 1.0) joystickY = 1.0;
            if (joystickY < -1.0) joystickY = -1.0;
          }

          controlSteering(joystickX, joystickY);
        }
        break;
      case 'pointerup':
      case 'pointercancel':
        if (pointer == _firstPointer) {
          // Un-assign the first pointer
          _firstPointer = -1;
          _firstPointerDownPos = null;
          controlSteering(0.0, 0.0);
        }
        else if (pointer == _secondPointer) {
          _secondPointer = -1;
        }
        break;
      default:
        break;
    }
    return true;
  }
455 456 457 458 459

  // Scoring and HUD
  void addScore(int score) {
    _hud.score += score;
  }
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
}

// Game objects

enum AsteroidSize {
  small,
  medium,
  large,
}

class Asteroid extends Sprite {
  Point _movementVector;
  AsteroidSize _asteroidSize;
  double _radius;

  double get radius {
    if (_radius != null) return _radius;
    if (_asteroidSize == AsteroidSize.small) _radius = _smlAsteroidRadius;
    else if (_asteroidSize == AsteroidSize.medium) _radius = _medAsteroidRadius;
    else if (_asteroidSize == AsteroidSize.large) _radius = _lrgAsteroidRadius;
    return _radius;
  }

  Asteroid(SpriteSheet spriteSheet, AsteroidSize this._asteroidSize) {
    size = new Size(radius * 2.0, radius * 2.0);
    position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight * _rand.nextDouble());
    rotation = 360.0 * _rand.nextDouble();

    if (_asteroidSize == AsteroidSize.small) {
      texture = spriteSheet["asteroid_small_${_rand.nextInt(2)}.png"];
    } else {
      texture = spriteSheet["asteroid_big_${_rand.nextInt(2)}.png"];
    }

    _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _maxAsteroidSpeed,
                                _rand.nextDouble() * _maxAsteroidSpeed * 2 - _maxAsteroidSpeed);

    userInteractionEnabled = true;

    // Rotate forever
    double direction = (_rand.nextBool()) ? 360.0 : -360.0;
    ActionTween rot = new ActionTween( (a) => rotation = a, 0.0, direction, 2.0 * _rand.nextDouble() + 2.0);
    ActionRepeatForever repeat = new ActionRepeatForever(rot);
    actions.run(repeat);
  }

  bool handleEvent(SpriteBoxEvent event) {
    if (event.type == "pointerdown") {
      actions.stopWithTag("fade");
      colorOverlay = new Color(0x99ffffff);
    }
    else if (event.type == "pointerup") {
      // Fade out the color overlay
      Action fadeOut = new ActionTween((a) => this.colorOverlay = a, new Color(0x99ffffff), new Color(0x00ffffff), 1.0);
      Action fadeOutAndRemove = new ActionSequence([fadeOut, new ActionCallFunction(() => this.colorOverlay = null)]);
      actions.run(fadeOutAndRemove, "fade");
    }
    return false;
  }
}

class Ship extends Sprite {
  Vector2 _movementVector;
  double _rotationTarget;
  double radius = _shipRadius;

  Ship(Texture img) : super(img) {
    _movementVector = new Vector2.zero();
    rotation = _rotationTarget = 270.0;

    // Create sprite
    size = new Size(_shipRadius * 2.0, _shipRadius * 2.0);
    position = new Point(_gameSizeWidth/2.0, _gameSizeHeight/2.0);
  }

  void thrust(double x, double y) {
    _rotationTarget = convertRadians2Degrees(Math.atan2(y, x));
    Vector2 directionVector = new Vector2(x, y).normalize();
    _movementVector.addScaled(directionVector, 1.0);
  }

  void move() {
    position = new Point(position.x + _movementVector[0], position.y + _movementVector[1]);
    _movementVector.scale(0.9);

    rotation = dampenRotation(rotation, _rotationTarget, 0.1);
  }
}

class Laser extends Sprite {
  int _frameCount = 0;
  Point _movementVector;
  double radius = 20.0;

  Laser(Texture img, Ship ship) : super(img) {
    size = new Size(30.0, 30.0);
    position = ship.position;
    rotation = ship.rotation + 90.0;
    transferMode = sky.TransferMode.plus;
    double rotRadians = convertDegrees2Radians(rotation);
    _movementVector = pointMult(new Point(Math.sin(rotRadians), -Math.cos(rotRadians)), 10.0);
    _movementVector = new Point(_movementVector.x + ship._movementVector[0], _movementVector.y + ship._movementVector[1]);
  }

  void move() {
    position = pointAdd(position, _movementVector);
    _frameCount++;
  }
}

// Background starfield

572 573
class StarField extends NodeWithSize {
  sky.Image _image;
574
  int _numStars;
575
  bool _autoScroll;
576 577
  List<Point> _starPositions;
  List<double> _starScales;
578 579 580 581 582 583
  List<Rect> _rects;
  List<Color> _colors;
  Paint _paint = new Paint()
    ..setFilterQuality(sky.FilterQuality.low)
    ..isAntiAlias = false
    ..setTransferMode(sky.TransferMode.plus);
584

585
  StarField(SpriteSheet spriteSheet, this._numStars, [this._autoScroll = false]) : super(new Size(1024.0, 1024.0)) {
586 587
    _starPositions = [];
    _starScales = [];
588 589
    _colors = [];
    _rects = [];
590 591 592 593

    for (int i  = 0; i < _numStars; i++) {
      _starPositions.add(new Point(_rand.nextDouble() * _gameSizeWidth, _rand.nextDouble() * _gameSizeHeight));
      _starScales.add(_rand.nextDouble());
594 595
      _colors.add(new Color.fromARGB((255.0 * (_rand.nextDouble() * 0.5 + 0.5)).toInt(), 255, 255, 255));
      _rects.add(spriteSheet["star_${_rand.nextInt(2)}.png"].frame);
596
    }
597 598

    _image = spriteSheet.image;
599 600 601
  }

  void paint(PaintingCanvas canvas) {
602 603
    // Create a transform for each star
    List<sky.RSTransform> transforms = [];
604
    for (int i = 0; i < _numStars; i++) {
605 606
      sky.RSTransform transform = new sky.RSTransform(_starScales[i], 0.0, _starPositions[i].x, _starPositions[i].y);
      transforms.add(transform);
607
    }
608 609 610

    // Draw the stars
    canvas.drawAtlas(_image, transforms, _rects, _colors, sky.TransferMode.modulate, null, _paint);
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
  }

  void move(double dx, double dy) {
    for (int i  = 0; i < _numStars; i++) {
      double xPos = _starPositions[i].x;
      double yPos = _starPositions[i].y;
      double scale = _starScales[i];

      xPos += dx * scale;
      yPos += dy * scale;

      if (xPos >= _gameSizeWidth) xPos -= _gameSizeWidth;
      if (xPos < 0) xPos += _gameSizeWidth;
      if (yPos >= _gameSizeHeight) yPos -= _gameSizeHeight;
      if (yPos < 0) yPos += _gameSizeHeight;

      _starPositions[i] = new Point(xPos, yPos);
    }
  }
630 631 632 633 634 635

  void update(double dt) {
    if (_autoScroll) {
      move(dt * 100.0, 0.0);
    }
  }
636 637
}

638 639 640 641
class Hud extends NodeWithSize {
  SpriteSheet spriteSheetUI;
  Sprite sprtBgScore;
  Sprite sprtBgShield;
642
  bool _dirtyScore = true;
643 644 645 646 647 648 649

  int _score = 0;

  int get score => _score;

  set score(int score) {
    _score = score;
650
    _dirtyScore = true;
651 652 653 654 655 656
  }

  Hud(this.spriteSheetUI) {
    pivot = Point.origin;

    sprtBgScore = new Sprite(spriteSheetUI["scoreboard.png"]);
657
    sprtBgScore.pivot = new Point(1.0, 0.0);
658 659 660 661
    sprtBgScore.scale = 0.6;
    addChild(sprtBgScore);

    sprtBgShield = new Sprite(spriteSheetUI["bar_shield.png"]);
662
    sprtBgShield.pivot = Point.origin;
663
    sprtBgShield.scale = 0.6;
664 665
    // TODO: Add shield
    //addChild(sprtBgShield);
666 667 668 669 670 671 672 673
  }

  void spriteBoxPerformedLayout() {
    // Set the size and position of HUD display
    position = spriteBox.visibleArea.topLeft;
    size = spriteBox.visibleArea.size;

    // Position hud objects
674 675
    sprtBgShield.position = new Point(20.0, 20.0);
    sprtBgScore.position = new Point(size.width - 20.0, 20.0);
676 677
  }

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
  void update(double dt) {
    // Update score
    if (_dirtyScore) {

      sprtBgScore.removeAllChildren();

      String scoreStr = _score.toString();
      double xPos = -50.0;
      for (int i = scoreStr.length - 1; i >= 0; i--) {
        String numStr = scoreStr.substring(i, i + 1);
        Sprite numSprt = new Sprite(spriteSheetUI["number_$numStr.png"]);
        numSprt.position = new Point(xPos, 49.0);
        sprtBgScore.addChild(numSprt);
        xPos -= 37.0;
      }
      _dirtyScore = false;
    }
    // Update power bar
696 697 698
  }
}

699 700 701 702 703 704
class Nebula extends Node {

  Nebula.withImage(sky.Image img) {
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        Sprite sprt = new Sprite.fromImage(img);
705
        sprt.transferMode = sky.TransferMode.plus;
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
        sprt.pivot = Point.origin;
        sprt.position = new Point(i * _gameSizeWidth - _gameSizeWidth, j * _gameSizeHeight - _gameSizeHeight);
        addChild(sprt);
      }
    }
  }
}

// Convenience methods

Point pointAdd(Point a, Point b) {
  return new Point(a.x+ b.x, a.y + b.y);
}

Point pointMult(Point a, double multiplier) {
  return new Point(a.x * multiplier, a.y * multiplier);
}

double dampenRotation(double src, double dst, double dampening) {
  double delta = dst - src;
  while (delta > 180.0) delta -= 360;
  while (delta < -180) delta += 360;
  delta *= dampening;

  return src + delta;
}

double pointQuickDist(Point a, Point b) {
  double dx = a.x - b.x;
  double dy = a.y - b.y;
  if (dx < 0.0) dx = -dx;
  if (dy < 0.0) dy = -dy;
  if (dx > dy) {
    return dx + dy/2.0;
  }
  else {
    return dy + dx/2.0;
  }
}