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; |
11 const double _medAsteroidRadius = 20.0; | 11 const double _medAsteroidRadius = 20.0; |
12 const double _smlAsteroidRadius = 10.0; | 12 const double _smlAsteroidRadius = 10.0; |
13 const double _maxAsteroidSpeed = 1.0; | 13 const double _maxAsteroidSpeed = 1.0; |
14 | 14 |
15 const int _lifeTimeLaser = 50; | 15 const int _lifeTimeLaser = 50; |
16 | 16 |
| 17 const int _numStarsInStarField = 150; |
| 18 |
17 class GameDemoWorld extends NodeWithSize { | 19 class GameDemoWorld extends NodeWithSize { |
18 | 20 |
19 // Images | 21 // Images |
20 Image _imgBg; | 22 Image _imgBg; |
21 Image _imgAsteroid; | 23 Image _imgAsteroid; |
22 Image _imgShip; | 24 Image _imgShip; |
23 Image _imgLaser; | 25 Image _imgLaser; |
| 26 Image _imgStar; |
| 27 Image _imgNebula; |
24 | 28 |
25 // Inputs | 29 // Inputs |
26 double _joystickX = 0.0; | 30 double _joystickX = 0.0; |
27 double _joystickY = 0.0; | 31 double _joystickY = 0.0; |
28 bool _fire; | 32 bool _fire; |
29 | 33 |
| 34 Node _gameLayer; |
| 35 |
30 Ship _ship; | 36 Ship _ship; |
31 List<Asteroid> _asteroids = []; | 37 List<Asteroid> _asteroids = []; |
32 List<Laser> _lasers = []; | 38 List<Laser> _lasers = []; |
| 39 StarField _starField; |
| 40 Nebula _nebula; |
33 | 41 |
34 GameDemoWorld(ImageMap images) : super.withSize(new Size(_gameSizeWidth, _game
SizeHeight)) { | 42 GameDemoWorld(ImageMap images) : super.withSize(new Size(_gameSizeWidth, _game
SizeHeight)) { |
35 | 43 |
36 // Fetch images | 44 // Fetch images |
37 _imgBg = images["https://raw.githubusercontent.com/slembcke/GalacticGuardian
.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/resources-auto/Burn
Texture.png"]; | 45 _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"]; | 46 _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"]; | 47 _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"]; | 48 _imgLaser = images["https://raw.githubusercontent.com/slembcke/GalacticGuard
ian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resource
s-auto/laserBlue.png"]; |
| 49 _imgStar = images["https://raw.githubusercontent.com/slembcke/GalacticGuardi
an.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources
-auto/laserFlashPurple.png"]; |
| 50 _imgNebula = images["https://raw.githubusercontent.com/slembcke/GalacticGuar
dian.spritebuilder/GDC/Source/Resources/NebulaClouds.png"]; |
| 51 |
| 52 _gameLayer = new Node(); |
| 53 this.addChild(_gameLayer); |
41 | 54 |
42 // Add some asteroids to the game world | 55 // Add some asteroids to the game world |
43 for (int i = 0; i < 5; i++) { | 56 for (int i = 0; i < 5; i++) { |
44 addAsteroid(AsteroidSize.large); | 57 addAsteroid(AsteroidSize.large); |
45 } | 58 } |
46 for (int i = 0; i < 5; i++) { | 59 for (int i = 0; i < 5; i++) { |
47 addAsteroid(AsteroidSize.medium); | 60 addAsteroid(AsteroidSize.medium); |
48 } | 61 } |
49 | 62 |
50 // Add ship | 63 // Add ship |
51 addShip(); | 64 addShip(); |
| 65 |
| 66 // Add starfield |
| 67 _starField = new StarField.withImage(_imgStar, _numStarsInStarField); |
| 68 _starField.zPosition = -2.0; |
| 69 addChild(_starField); |
| 70 |
| 71 // Add nebula |
| 72 addNebula(); |
52 } | 73 } |
53 | 74 |
54 // Methods for adding game objects | 75 // Methods for adding game objects |
55 | 76 |
56 void addBackground() { | 77 void addBackground() { |
57 Sprite sprtBg = new Sprite.withImage(_imgBg); | 78 Sprite sprtBg = new Sprite.withImage(_imgBg); |
58 sprtBg.size = new Size(_gameSizeWidth, _gameSizeHeight); | 79 sprtBg.size = new Size(_gameSizeWidth, _gameSizeHeight); |
59 sprtBg.pivot = Point.origin; | 80 sprtBg.pivot = Point.origin; |
60 this.addChild(sprtBg); | 81 _gameLayer.addChild(sprtBg); |
61 } | 82 } |
62 | 83 |
63 void addAsteroid(AsteroidSize size, [Point pos]) { | 84 void addAsteroid(AsteroidSize size, [Point pos]) { |
64 Asteroid asteroid = new Asteroid.withImage(_imgAsteroid, size); | 85 Asteroid asteroid = new Asteroid.withImage(_imgAsteroid, size); |
65 asteroid.zPosition = 1.0; | 86 asteroid.zPosition = 1.0; |
66 if (pos != null) asteroid.position = pos; | 87 if (pos != null) asteroid.position = pos; |
67 this.addChild(asteroid); | 88 _gameLayer.addChild(asteroid); |
68 _asteroids.add(asteroid); | 89 _asteroids.add(asteroid); |
69 } | 90 } |
70 | 91 |
71 void addShip() { | 92 void addShip() { |
72 Ship ship = new Ship.withImage(_imgShip); | 93 Ship ship = new Ship.withImage(_imgShip); |
73 ship.zPosition = 10.0; | 94 ship.zPosition = 10.0; |
74 this.addChild(ship); | 95 _gameLayer.addChild(ship); |
75 _ship = ship; | 96 _ship = ship; |
76 } | 97 } |
77 | 98 |
78 void addLaser() { | 99 void addLaser() { |
79 Laser laser = new Laser.withImage(_imgLaser, _ship); | 100 Laser laser = new Laser.withImage(_imgLaser, _ship); |
80 laser.zPosition = 8.0; | 101 laser.zPosition = 8.0; |
81 _lasers.add(laser); | 102 _lasers.add(laser); |
82 this.addChild(laser); | 103 _gameLayer.addChild(laser); |
| 104 } |
| 105 |
| 106 void addNebula() { |
| 107 _nebula = new Nebula.withImage(_imgNebula); |
| 108 _gameLayer.addChild(_nebula); |
83 } | 109 } |
84 | 110 |
85 void update(double dt) { | 111 void update(double dt) { |
86 // Move asteroids | 112 // Move asteroids |
87 for (Asteroid asteroid in _asteroids) { | 113 for (Asteroid asteroid in _asteroids) { |
88 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector); | 114 asteroid.position = pointAdd(asteroid.position, asteroid._movementVector); |
89 } | 115 } |
90 | 116 |
91 // Move lasers and remove expired lasers | 117 // Move lasers and remove expired lasers |
92 for (int i = _lasers.length - 1; i >= 0; i--) { | 118 for (int i = _lasers.length - 1; i >= 0; i--) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // Move objects to center camera and warp objects around the edges | 166 // Move objects to center camera and warp objects around the edges |
141 centerCamera(); | 167 centerCamera(); |
142 warpObjects(); | 168 warpObjects(); |
143 } | 169 } |
144 | 170 |
145 void centerCamera() { | 171 void centerCamera() { |
146 const cameraDampening = 0.1; | 172 const cameraDampening = 0.1; |
147 Point delta = new Point(_gameSizeWidth/2 - _ship.position.x, _gameSizeHeight
/2 - _ship.position.y); | 173 Point delta = new Point(_gameSizeWidth/2 - _ship.position.x, _gameSizeHeight
/2 - _ship.position.y); |
148 delta = pointMult(delta, cameraDampening); | 174 delta = pointMult(delta, cameraDampening); |
149 | 175 |
150 for (Node child in children) { | 176 for (Node child in _gameLayer.children) { |
151 child.position = pointAdd(child.position, delta); | 177 child.position = pointAdd(child.position, delta); |
152 } | 178 } |
| 179 |
| 180 // Update starfield |
| 181 _starField.move(delta.x, delta.y); |
153 } | 182 } |
154 | 183 |
155 void warpObjects() { | 184 void warpObjects() { |
156 for (Node child in children) { | 185 for (Node child in _gameLayer.children) { |
157 if (child.position.x < 0) child.position = pointAdd(child.position, new Po
int(_gameSizeWidth, 0.0)); | 186 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)); | 187 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)); | 188 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)); | 189 if (child.position.y >= _gameSizeHeight) child.position = pointAdd(child.p
osition, new Point(0.0, -_gameSizeHeight)); |
161 } | 190 } |
162 } | 191 } |
163 | 192 |
164 // Handling controls | 193 // Handling controls |
165 | 194 |
166 void controlSteering(double x, double y) { | 195 void controlSteering(double x, double y) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 _movementVector = pointMult(new Point(Math.sin(rotRadians), -Math.cos(rotRad
ians)), 10.0); | 274 _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]); | 275 _movementVector = new Point(_movementVector.x + ship._movementVector[0], _mo
vementVector.y + ship._movementVector[1]); |
247 } | 276 } |
248 | 277 |
249 bool move() { | 278 bool move() { |
250 position = pointAdd(position, _movementVector); | 279 position = pointAdd(position, _movementVector); |
251 _frameCount++; | 280 _frameCount++; |
252 } | 281 } |
253 } | 282 } |
254 | 283 |
| 284 // Background starfield |
| 285 |
| 286 class StarField extends Node { |
| 287 Image _img; |
| 288 int _numStars; |
| 289 List<Point> _starPositions; |
| 290 List<double> _starScales; |
| 291 List<double> _opacity; |
| 292 |
| 293 StarField.withImage(Image this._img, int this._numStars) { |
| 294 _starPositions = []; |
| 295 _starScales = []; |
| 296 _opacity = []; |
| 297 |
| 298 for (int i = 0; i < _numStars; i++) { |
| 299 _starPositions.add(new Point(_rand.nextDouble() * _gameSizeWidth, _rand.ne
xtDouble() * _gameSizeHeight)); |
| 300 _starScales.add(_rand.nextDouble()); |
| 301 _opacity.add(_rand.nextDouble() * 0.5 + 0.5); |
| 302 } |
| 303 } |
| 304 |
| 305 void paint(PictureRecorder canvas) { |
| 306 // Setup paint object for opacity and transfer mode |
| 307 Paint paint = new Paint(); |
| 308 paint.setTransferMode(TransferMode.plusMode); |
| 309 |
| 310 double baseScaleX = 32.0/_img.width; |
| 311 double baseScaleY = 32.0/_img.height; |
| 312 |
| 313 // Draw each star |
| 314 for (int i = 0; i < _numStars; i++) { |
| 315 Point pos = _starPositions[i]; |
| 316 double scale = _starScales[i]; |
| 317 paint.color = new Color.fromARGB((255.0*_opacity[i]).toInt(), 255, 255, 25
5); |
| 318 |
| 319 canvas.save(); |
| 320 |
| 321 canvas.translate(pos.x, pos.y); |
| 322 canvas.scale(baseScaleX*scale, baseScaleY*scale); |
| 323 |
| 324 canvas.drawImage(_img, 0.0, 0.0, paint); |
| 325 |
| 326 canvas.restore(); |
| 327 } |
| 328 } |
| 329 |
| 330 void move(double dx, double dy) { |
| 331 for (int i = 0; i < _numStars; i++) { |
| 332 double xPos = _starPositions[i].x; |
| 333 double yPos = _starPositions[i].y; |
| 334 double scale = _starScales[i]; |
| 335 |
| 336 xPos += dx * scale; |
| 337 yPos += dy * scale; |
| 338 |
| 339 if (xPos >= _gameSizeWidth) xPos -= _gameSizeWidth; |
| 340 if (xPos < 0) xPos += _gameSizeWidth; |
| 341 if (yPos >= _gameSizeHeight) yPos -= _gameSizeHeight; |
| 342 if (yPos < 0) yPos += _gameSizeHeight; |
| 343 |
| 344 _starPositions[i] = new Point(xPos, yPos); |
| 345 } |
| 346 } |
| 347 } |
| 348 |
| 349 class Nebula extends Node { |
| 350 |
| 351 Nebula.withImage(Image img) { |
| 352 for (int i = 0; i < 2; i++) { |
| 353 for (int j = 0; j < 2; j++) { |
| 354 Sprite sprt = new Sprite.withImage(img); |
| 355 sprt.pivot = Point.origin; |
| 356 sprt.position = new Point(i * _gameSizeWidth - _gameSizeWidth, j * _game
SizeHeight - _gameSizeHeight); |
| 357 addChild(sprt); |
| 358 } |
| 359 } |
| 360 } |
| 361 } |
| 362 |
255 // Convenience methods | 363 // Convenience methods |
256 | 364 |
257 Point pointAdd(Point a, Point b) { | 365 Point pointAdd(Point a, Point b) { |
258 return new Point(a.x+ b.x, a.y + b.y); | 366 return new Point(a.x+ b.x, a.y + b.y); |
259 } | 367 } |
260 | 368 |
261 Point pointMult(Point a, double multiplier) { | 369 Point pointMult(Point a, double multiplier) { |
262 return new Point(a.x * multiplier, a.y * multiplier); | 370 return new Point(a.x * multiplier, a.y * multiplier); |
263 } | 371 } |
264 | 372 |
(...skipping 11 matching lines...) Expand all Loading... |
276 double dy = a.y - b.y; | 384 double dy = a.y - b.y; |
277 if (dx < 0.0) dx = -dx; | 385 if (dx < 0.0) dx = -dx; |
278 if (dy < 0.0) dy = -dy; | 386 if (dy < 0.0) dy = -dy; |
279 if (dx > dy) { | 387 if (dx > dy) { |
280 return dx + dy/2.0; | 388 return dx + dy/2.0; |
281 } | 389 } |
282 else { | 390 else { |
283 return dy + dx/2.0; | 391 return dy + dx/2.0; |
284 } | 392 } |
285 } | 393 } |
OLD | NEW |