OLD | NEW |
(Empty) | |
| 1 part of game; |
| 2 |
| 3 Math.Random _rand; |
| 4 |
| 5 class GameTests extends TransformNode{ |
| 6 |
| 7 Image _imgAsteroid; |
| 8 Image _imgBg; |
| 9 Image _imgShip; |
| 10 |
| 11 GameTests(ImageMap images) { |
| 12 // Setup random number generator |
| 13 _rand = new Math.Random(); |
| 14 |
| 15 // Fetch images |
| 16 _imgBg = images["https://raw.githubusercontent.com/slembcke/GalacticGuardian
.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/resources-auto/Burn
Texture.png"]; |
| 17 _imgAsteroid = images["https://raw.githubusercontent.com/slembcke/GalacticGu
ardian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resou
rces-auto/asteroid_big_002.png"]; |
| 18 _imgShip = images["https://raw.githubusercontent.com/slembcke/GalacticGuardi
an.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources
-auto/GG_blueship_Lv3.png"]; |
| 19 |
| 20 for (int i = 0; i < 100; i++) { |
| 21 addSprite(i/100.0); |
| 22 } |
| 23 } |
| 24 |
| 25 void addSprite([double scale = null]) { |
| 26 TestAsteroidSprite sprt = new TestAsteroidSprite.withImage(_imgAsteroid); |
| 27 sprt.width = 64.0; |
| 28 sprt.height = 64.0; |
| 29 |
| 30 if (scale == null) { |
| 31 scale = _rand.nextDouble(); |
| 32 } |
| 33 |
| 34 sprt.zPosition = scale; |
| 35 sprt.scale = scale; |
| 36 sprt.position = new Vector2(_rand.nextDouble()*1024.0, _rand.nextDouble()*10
24.0); |
| 37 |
| 38 this.addChild(sprt); |
| 39 } |
| 40 |
| 41 void update(double dt) { |
| 42 for (TransformNode child in children) { |
| 43 child.update(dt); |
| 44 } |
| 45 } |
| 46 } |
| 47 |
| 48 class TestAsteroidSprite extends SpriteNode { |
| 49 |
| 50 Vector2 _movementVector; |
| 51 double _rotationalSpeed; |
| 52 |
| 53 TestAsteroidSprite.withImage(Image img) : super.withImage(img) { |
| 54 _movementVector = new Vector2(_rand.nextDouble() * 4.0 - 2.0, _rand.nextDoub
le() * 4.0 - 2.0); |
| 55 _rotationalSpeed = _rand.nextDouble() * 2.0 - 1.0; |
| 56 } |
| 57 |
| 58 void update(double dt) { |
| 59 position = position + _movementVector * scale; |
| 60 |
| 61 // Bounce at edges |
| 62 if (position[0] < 0 || position[0] > 1024.0) _movementVector[0] = -_movement
Vector[0]; |
| 63 if (position[1] < 0 || position[1] > 1024.0) _movementVector[1] = -_movement
Vector[1]; |
| 64 |
| 65 rotation += _rotationalSpeed; |
| 66 } |
| 67 } |
OLD | NEW |