| OLD | NEW |
| 1 part of game; | 1 part of game; |
| 2 | 2 |
| 3 // Random generator | 3 // Random generator |
| 4 Math.Random _rand = new Math.Random(); | 4 Math.Random _rand = new Math.Random(); |
| 5 | 5 |
| 6 const double _gameSizeWidth = 1024.0; | 6 const double _gameSizeWidth = 1024.0; |
| 7 const double _gameSizeHeight = 1024.0; | 7 const double _gameSizeHeight = 1024.0; |
| 8 | 8 |
| 9 const double _shipRadius = 30.0; | 9 const double _shipRadius = 30.0; |
| 10 const double _lrgAsteroidRadius = 40.0; | 10 const double _lrgAsteroidRadius = 40.0; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // Add ship | 63 // Add ship |
| 64 addShip(); | 64 addShip(); |
| 65 | 65 |
| 66 // Add starfield | 66 // Add starfield |
| 67 _starField = new StarField.withImage(_imgStar, _numStarsInStarField); | 67 _starField = new StarField.withImage(_imgStar, _numStarsInStarField); |
| 68 _starField.zPosition = -2.0; | 68 _starField.zPosition = -2.0; |
| 69 addChild(_starField); | 69 addChild(_starField); |
| 70 | 70 |
| 71 // Add nebula | 71 // Add nebula |
| 72 addNebula(); | 72 addNebula(); |
| 73 |
| 74 userInteractionEnabled = true; |
| 75 handleMultiplePointers = true; |
| 73 } | 76 } |
| 74 | 77 |
| 75 // Methods for adding game objects | 78 // Methods for adding game objects |
| 76 | 79 |
| 77 void addBackground() { | 80 void addBackground() { |
| 78 Sprite sprtBg = new Sprite.withImage(_imgBg); | 81 Sprite sprtBg = new Sprite.withImage(_imgBg); |
| 79 sprtBg.size = new Size(_gameSizeWidth, _gameSizeHeight); | 82 sprtBg.size = new Size(_gameSizeWidth, _gameSizeHeight); |
| 80 sprtBg.pivot = Point.origin; | 83 sprtBg.pivot = Point.origin; |
| 81 _gameLayer.addChild(sprtBg); | 84 _gameLayer.addChild(sprtBg); |
| 82 } | 85 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 // Handling controls | 196 // Handling controls |
| 194 | 197 |
| 195 void controlSteering(double x, double y) { | 198 void controlSteering(double x, double y) { |
| 196 _joystickX = x; | 199 _joystickX = x; |
| 197 _joystickY = y; | 200 _joystickY = y; |
| 198 } | 201 } |
| 199 | 202 |
| 200 void controlFire() { | 203 void controlFire() { |
| 201 addLaser(); | 204 addLaser(); |
| 202 } | 205 } |
| 206 |
| 207 // Handle pointer events |
| 208 |
| 209 int _firstPointer = -1; |
| 210 int _secondPointer = -1; |
| 211 Point _firstPointerDownPos; |
| 212 |
| 213 bool handleEvent(SpriteBoxEvent event) { |
| 214 Point pointerPos = convertPointToNodeSpace(event.boxPosition); |
| 215 int pointer = event.pointer; |
| 216 |
| 217 switch (event.type) { |
| 218 case 'pointerdown': |
| 219 if (_firstPointer == -1) { |
| 220 // Assign the first pointer |
| 221 _firstPointer = pointer; |
| 222 _firstPointerDownPos = pointerPos; |
| 223 } |
| 224 else if (_secondPointer == -1) { |
| 225 // Assign second pointer |
| 226 _secondPointer = pointer; |
| 227 controlFire(); |
| 228 } |
| 229 else { |
| 230 // There is a pointer used for steering, let's fire instead |
| 231 controlFire(); |
| 232 } |
| 233 break; |
| 234 case 'pointermove': |
| 235 if (pointer == _firstPointer) { |
| 236 // Handle turning control |
| 237 double joystickX = 0.0; |
| 238 double deltaX = pointerPos.x - _firstPointerDownPos.x; |
| 239 if (deltaX > _steeringThreshold || deltaX < -_steeringThreshold) { |
| 240 joystickX = (deltaX - _steeringThreshold)/(_steeringMax - _steeringT
hreshold); |
| 241 if (joystickX > 1.0) joystickX = 1.0; |
| 242 if (joystickX < -1.0) joystickX = -1.0; |
| 243 } |
| 244 |
| 245 double joystickY = 0.0; |
| 246 double deltaY = pointerPos.y - _firstPointerDownPos.y; |
| 247 if (deltaY > _steeringThreshold || deltaY < -_steeringThreshold) { |
| 248 joystickY = (deltaY - _steeringThreshold)/(_steeringMax - _steeringT
hreshold); |
| 249 if (joystickY > 1.0) joystickY = 1.0; |
| 250 if (joystickY < -1.0) joystickY = -1.0; |
| 251 } |
| 252 |
| 253 controlSteering(joystickX, joystickY); |
| 254 } |
| 255 break; |
| 256 case 'pointerup': |
| 257 case 'pointercancel': |
| 258 if (pointer == _firstPointer) { |
| 259 // Un-assign the first pointer |
| 260 _firstPointer = -1; |
| 261 _firstPointerDownPos = null; |
| 262 controlSteering(0.0, 0.0); |
| 263 } |
| 264 else if (pointer == _secondPointer) { |
| 265 _secondPointer = -1; |
| 266 } |
| 267 break; |
| 268 default: |
| 269 break; |
| 270 } |
| 271 return true; |
| 272 } |
| 203 } | 273 } |
| 204 | 274 |
| 205 // Game objects | 275 // Game objects |
| 206 | 276 |
| 207 enum AsteroidSize { | 277 enum AsteroidSize { |
| 208 small, | 278 small, |
| 209 medium, | 279 medium, |
| 210 large, | 280 large, |
| 211 } | 281 } |
| 212 | 282 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 223 return _radius; | 293 return _radius; |
| 224 } | 294 } |
| 225 | 295 |
| 226 Asteroid.withImage(Image img, AsteroidSize this._asteroidSize) : super.withIma
ge(img) { | 296 Asteroid.withImage(Image img, AsteroidSize this._asteroidSize) : super.withIma
ge(img) { |
| 227 size = new Size(radius * 2.0, radius * 2.0); | 297 size = new Size(radius * 2.0, radius * 2.0); |
| 228 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight *
_rand.nextDouble()); | 298 position = new Point(_gameSizeWidth * _rand.nextDouble(), _gameSizeHeight *
_rand.nextDouble()); |
| 229 rotation = 360.0 * _rand.nextDouble(); | 299 rotation = 360.0 * _rand.nextDouble(); |
| 230 | 300 |
| 231 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma
xAsteroidSpeed, | 301 _movementVector = new Point(_rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma
xAsteroidSpeed, |
| 232 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma
xAsteroidSpeed); | 302 _rand.nextDouble() * _maxAsteroidSpeed * 2 - _ma
xAsteroidSpeed); |
| 303 |
| 304 userInteractionEnabled = true; |
| 305 } |
| 306 |
| 307 bool handleEvent(SpriteBoxEvent event) { |
| 308 if (event.type == "pointerdown") { |
| 309 colorOverlay = new Color(0x99ff0000); |
| 310 } |
| 311 else if (event.type == "pointerup") { |
| 312 colorOverlay = null; |
| 313 } |
| 314 return false; |
| 233 } | 315 } |
| 234 } | 316 } |
| 235 | 317 |
| 236 class Ship extends Sprite { | 318 class Ship extends Sprite { |
| 237 Vector2 _movementVector; | 319 Vector2 _movementVector; |
| 238 double _rotationTarget; | 320 double _rotationTarget; |
| 239 | 321 |
| 240 Ship.withImage(Image img) : super.withImage(img) { | 322 Ship.withImage(Image img) : super.withImage(img) { |
| 241 _movementVector = new Vector2.zero(); | 323 _movementVector = new Vector2.zero(); |
| 242 rotation = _rotationTarget = 270.0; | 324 rotation = _rotationTarget = 270.0; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 double dy = a.y - b.y; | 466 double dy = a.y - b.y; |
| 385 if (dx < 0.0) dx = -dx; | 467 if (dx < 0.0) dx = -dx; |
| 386 if (dy < 0.0) dy = -dy; | 468 if (dy < 0.0) dy = -dy; |
| 387 if (dx > dy) { | 469 if (dx > dy) { |
| 388 return dx + dy/2.0; | 470 return dx + dy/2.0; |
| 389 } | 471 } |
| 390 else { | 472 else { |
| 391 return dy + dx/2.0; | 473 return dy + dx/2.0; |
| 392 } | 474 } |
| 393 } | 475 } |
| OLD | NEW |