Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 part of game; | |
| 2 | |
| 3 // Random generator | |
| 4 Math.Random _rand = new Math.Random(); | |
| 5 | |
| 6 const double _gameSizeWidth = 1024.0; | |
| 7 const double _gameSizeHeight = 1024.0; | |
| 8 | |
| 9 const double _shipRadius = 30.0; | |
| 10 const double _lrgAsteroidRadius = 40.0; | |
| 11 const double _medAsteroidRadius = 20.0; | |
| 12 const double _smlAsteroidRadius = 10.0; | |
| 13 const double _maxAsteroidSpeed = 1.0; | |
| 14 | |
| 15 const int _lifeTimeLaser = 50; | |
| 16 | |
| 17 class GameDemoWorld extends NodeWithSize { | |
| 18 | |
| 19 // Images | |
| 20 Image _imgBg; | |
| 21 Image _imgAsteroid; | |
| 22 Image _imgShip; | |
| 23 Image _imgLaser; | |
| 24 | |
| 25 // Inputs | |
| 26 double _joystickX = 0.0; | |
| 27 double _joystickY = 0.0; | |
| 28 bool _fire; | |
| 29 | |
| 30 Ship _ship; | |
| 31 List<Asteroid> _asteroids = []; | |
| 32 List<Laser> _lasers = []; | |
| 33 | |
| 34 GameDemoWorld(ImageMap images) : super.withSize(new Size(_gameSizeWidth, _game SizeHeight)) { | |
| 35 | |
| 36 // Fetch images | |
| 37 _imgBg = images["https://raw.githubusercontent.com/slembcke/GalacticGuardian .spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/resources-auto/Burn Texture.png"]; | |
| 38 _imgAsteroid = images["https://raw.githubusercontent.com/slembcke/GalacticGu ardian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resou rces-auto/asteroid_big_002.png"]; | |
| 39 _imgShip = images["https://raw.githubusercontent.com/slembcke/GalacticGuardi an.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources -auto/GG_blueship_Lv3.png"]; | |
| 40 _imgLaser = images["https://raw.githubusercontent.com/slembcke/GalacticGuard ian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resource s-auto/laserBlue.png"]; | |
| 41 | |
| 42 // Add some asteroids to the game world | |
| 43 for (int i = 0; i < 5; i++) { | |
| 44 addAsteroid(AsteroidSize.large); | |
| 45 } | |
| 46 for (int i = 0; i < 5; i++) { | |
| 47 addAsteroid(AsteroidSize.medium); | |
| 48 } | |
| 49 | |
| 50 // Add ship | |
| 51 addShip(); | |
| 52 } | |
| 53 | |
| 54 // Methods for adding game objects | |
| 55 | |
| 56 void addBackground() { | |
| 57 Sprite sprtBg = new Sprite.withImage(_imgBg); | |
| 58 sprtBg.size = new Size(_gameSizeWidth, _gameSizeHeight); | |
| 59 sprtBg.pivot = Point.origin; | |
| 60 this.addChild(sprtBg); | |
| 61 } | |
| 62 | |
| 63 void addAsteroid(AsteroidSize size, [Point pos]) { | |
| 64 Asteroid asteroid = new Asteroid.withImage(_imgAsteroid, size); | |
| 65 asteroid.zPosition = 1.0; | |
| 66 if (pos != null) asteroid.position = pos; | |
| 67 this.addChild(asteroid); | |
| 68 _asteroids.add(asteroid); | |
| 69 } | |
| 70 | |
| 71 void addShip() { | |
| 72 Ship ship = new Ship.withImage(_imgShip); | |
| 73 ship.zPosition = 10.0; | |
| 74 this.addChild(ship); | |
| 75 _ship = ship; | |
| 76 } | |
| 77 | |
| 78 void addLaser() { | |
| 79 Laser laser = new Laser.withImage(_imgLaser, _ship); | |
| 80 laser.zPosition = 8.0; | |
| 81 _lasers.add(laser); | |
| 82 this.addChild(laser); | |
| 83 } | |
| 84 | |
| 85 void update(double dt) { | |
| 86 // Move asteroids | |
| 87 for (Asteroid asteroid in _asteroids) { | |
| 88 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector); | |
| 89 } | |
| 90 | |
| 91 // Move lasers and remove expired lasers | |
| 92 for (int i = _lasers.length - 1; i >= 0; i--) { | |
| 93 Laser laser = _lasers[i]; | |
| 94 laser.move(); | |
| 95 if (laser._frameCount > _lifeTimeLaser) { | |
| 96 laser.removeFromParent(); | |
| 97 _lasers.removeAt(i); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 // Apply thrust to ship | |
| 102 if (_joystickX != 0.0 || _joystickY != 0.0) { | |
| 103 _ship.thrust(_joystickX, _joystickY); | |
| 104 } | |
| 105 | |
| 106 // Move ship | |
| 107 _ship.move(); | |
| 108 | |
| 109 // Check collisions between asteroids and lasers | |
| 110 for (int i = _lasers.length -1; i >= 0; i--) { | |
| 111 // Iterate over all the lasers | |
| 112 Laser laser = _lasers[i]; | |
| 113 | |
| 114 for (int j = _asteroids.length - 1; j >= 0; j--) { | |
| 115 // Iterate over all the asteroids | |
| 116 Asteroid asteroid = _asteroids[j]; | |
| 117 | |
| 118 // Check for collision | |
| 119 if (pointQuickDist(laser.position, asteroid.position) < laser.radius + a steroid.radius) { | |
| 120 // Remove laser | |
| 121 laser.removeFromParent(); | |
| 122 _lasers.removeAt(i); | |
| 123 | |
| 124 // Add asteroids | |
| 125 if (asteroid._asteroidSize == AsteroidSize.large) { | |
| 126 for (int a = 0; a < 3; a++) addAsteroid(AsteroidSize.medium, asteroi d.position); | |
| 127 } | |
| 128 else if (asteroid._asteroidSize == AsteroidSize.medium) { | |
| 129 for (int a = 0; a < 5; a++) addAsteroid(AsteroidSize.small, asteroid .position); | |
| 130 } | |
| 131 | |
| 132 // Remove asteroid | |
| 133 asteroid.removeFromParent(); | |
| 134 _asteroids.removeAt(j); | |
| 135 break; | |
| 136 } | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 // Move objects to center camera and warp objects around the edges | |
| 141 centerCamera(); | |
| 142 warpObjects(); | |
| 143 } | |
| 144 | |
| 145 void centerCamera() { | |
| 146 const cameraDampening = 0.1; | |
| 147 Point delta = new Point(_gameSizeWidth/2 - _ship.position.x, _gameSizeHeight /2 - _ship.position.y); | |
| 148 delta = pointMult(delta, cameraDampening); | |
| 149 | |
| 150 for (Node child in children) { | |
| 151 child.position = pointAdd(child.position, delta); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 void warpObjects() { | |
| 156 for (Node child in children) { | |
| 157 if (child.position.x < 0) child.position = pointAdd(child.position, new Po int(_gameSizeWidth, 0.0)); | |
| 158 if (child.position.x >= _gameSizeWidth) child.position = pointAdd(child.po sition, new Point(-_gameSizeWidth, 0.0)); | |
| 159 if (child.position.y < 0) child.position = pointAdd(child.position, new Po int(0.0, _gameSizeHeight)); | |
| 160 if (child.position.y >= _gameSizeHeight) child.position = pointAdd(child.p osition, new Point(0.0, -_gameSizeHeight)); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 // Handling controls | |
| 165 | |
| 166 void controlSteering(double x, double y) { | |
| 167 _joystickX = x; | |
| 168 _joystickY = y; | |
| 169 } | |
| 170 | |
| 171 void controlFire() { | |
| 172 addLaser(); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 // Game objects | |
| 177 | |
| 178 enum AsteroidSize { | |
| 179 small, | |
| 180 medium, | |
| 181 large, | |
| 182 } | |
| 183 | |
| 184 class Asteroid extends Sprite { | |
| 185 Point _movementVector; | |
| 186 AsteroidSize _asteroidSize; | |
| 187 double _radius; | |
| 188 | |
| 189 double get radius { | |
| 190 if (_radius != null) return _radius; | |
| 191 if (_asteroidSize == AsteroidSize.small) _radius = _smlAsteroidRadius; | |
| 192 else if (_asteroidSize == AsteroidSize.medium) _radius = _medAsteroidRadius; | |
| 193 else if (_asteroidSize == AsteroidSize.large) _radius = _lrgAsteroidRadius; | |
| 194 return _radius; | |
| 195 } | |
| 196 | |
| 197 Asteroid.withImage(Image img, AsteroidSize this._asteroidSize) : super.withIma ge(img) { | |
| 198 size = new Size(radius * 2.0, radius * 2.0); | |
| 199 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight * _rand.nextDouble()); | |
| 200 rotation = 360.0 * _rand.nextDouble(); | |
| 201 | |
| 202 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed, | |
| 203 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma xAsteroidSpeed); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 class Ship extends Sprite { | |
| 208 Vector2 _movementVector; | |
| 209 double _rotationTarget; | |
| 210 | |
| 211 Ship.withImage(Image img) : super.withImage(img) { | |
| 212 _movementVector = new Vector2.zero(); | |
| 213 rotation = _rotationTarget = 270.0; | |
| 214 | |
| 215 // Create sprite | |
| 216 size = new Size(_shipRadius * 2.0, _shipRadius * 2.0); | |
| 217 position = new Point(_gameSizeWidth/2.0, _gameSizeHeight/2.0); | |
| 218 } | |
| 219 | |
| 220 void thrust(double x, double y) { | |
| 221 _rotationTarget = convertRadians2Degrees(Math.atan2(y, x)); | |
| 222 Vector2 directionVector = new Vector2(x, y).normalize(); | |
| 223 _movementVector.addScaled(directionVector, 1.0); | |
| 224 } | |
| 225 | |
| 226 void move() { | |
| 227 position = new Point(position.x + _movementVector[0], position.y + _movement Vector[1]); | |
| 228 _movementVector.scale(0.9); | |
| 229 | |
| 230 rotation = dampenRotation(rotation, _rotationTarget, 0.1); | |
| 231 } | |
| 232 } | |
| 233 | |
| 234 class Laser extends Sprite { | |
| 235 int _frameCount = 0; | |
| 236 Point _movementVector; | |
| 237 double radius = 10.0; | |
| 238 | |
| 239 Laser.withImage(Image img, Ship ship) : super.withImage(img) { | |
| 240 size = new Size(20.0, 20.0); | |
| 241 position = ship.position; | |
| 242 rotation = ship.rotation + 90.0; | |
| 243 transferMode = TransferMode.plusMode; | |
| 244 double rotRadians = convertDegrees2Radians(rotation); | |
| 245 _movementVector = pointMult(new Point(Math.sin(rotRadians), -Math.cos(rotRad ians)), 10.0); | |
| 246 _movementVector = new Point(_movementVector.x + ship._movementVector[0], _mo vementVector.y + ship._movementVector[1]); | |
| 247 } | |
| 248 | |
| 249 bool move() { | |
| 250 position = pointAdd(position, _movementVector); | |
| 251 _frameCount++; | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 // Convenience methods | |
| 256 | |
| 257 Point pointAdd(Point a, Point b) { | |
| 258 return new Point(a.x+ b.x, a.y + b.y); | |
| 259 } | |
|
abarth-chromium
2015/06/13 00:03:20
We have an operator+ for Point and Size. I wonder
| |
| 260 | |
| 261 Point pointMult(Point a, double multiplier) { | |
| 262 return new Point(a.x * multiplier, a.y * multiplier); | |
| 263 } | |
|
abarth-chromium
2015/06/13 00:03:20
We should just add this to Point.dart
| |
| 264 | |
| 265 double dampenRotation(double src, double dst, double dampening) { | |
| 266 double delta = dst - src; | |
| 267 while (delta > 180.0) delta -= 360; | |
| 268 while (delta < -180) delta += 360; | |
| 269 delta *= dampening; | |
| 270 | |
| 271 return src + delta; | |
| 272 } | |
| 273 | |
| 274 double pointQuickDist(Point a, Point b) { | |
| 275 double dx = a.x - b.x; | |
| 276 double dy = a.y - b.y; | |
| 277 if (dx < 0.0) dx = -dx; | |
| 278 if (dy < 0.0) dy = -dy; | |
| 279 if (dx > dy) { | |
| 280 return dx + dy/2.0; | |
| 281 } | |
| 282 else { | |
| 283 return dy + dx/2.0; | |
| 284 } | |
| 285 } | |
| OLD | NEW |