| OLD | NEW |
| 1 part of game; | 1 part of game; |
| 2 | 2 |
| 3 class GameWorld extends TransformNode { | 3 class GameWorld extends Node { |
| 4 | 4 |
| 5 World world; | 5 World world; |
| 6 List<Body> bodies = []; | 6 List<Body> bodies = []; |
| 7 Body _bodyShip; | 7 Body _bodyShip; |
| 8 | 8 |
| 9 Image _imgBg; | 9 Image _imgBg; |
| 10 Image _imgAsteroid; | 10 Image _imgAsteroid; |
| 11 Image _imgShip; | 11 Image _imgShip; |
| 12 | 12 |
| 13 double _steeringInput; | 13 double _steeringInput; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 35 } | 35 } |
| 36 for (int i = 0; i < 50; i++) { | 36 for (int i = 0; i < 50; i++) { |
| 37 addAsteroid(20.0); | 37 addAsteroid(20.0); |
| 38 } | 38 } |
| 39 | 39 |
| 40 // Add ship | 40 // Add ship |
| 41 addShip(); | 41 addShip(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void addBackground() { | 44 void addBackground() { |
| 45 SpriteNode sprtBg = new SpriteNode.withImage(_imgBg); | 45 Sprite sprtBg = new Sprite.withImage(_imgBg); |
| 46 sprtBg.width = width; | 46 sprtBg.width = width; |
| 47 sprtBg.height = height; | 47 sprtBg.height = height; |
| 48 sprtBg.pivot = new Vector2(0.0, 0.0); | 48 sprtBg.pivot = new Vector2(0.0, 0.0); |
| 49 this.addChild(sprtBg); | 49 this.addChild(sprtBg); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void addAsteroid([double radius=20.0]) { | 52 void addAsteroid([double radius=20.0]) { |
| 53 | 53 |
| 54 // Create shape | 54 // Create shape |
| 55 final CircleShape shape = new CircleShape(); | 55 final CircleShape shape = new CircleShape(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 74 | 74 |
| 75 // Set position of object | 75 // Set position of object |
| 76 Math.Random rand = new Math.Random(); | 76 Math.Random rand = new Math.Random(); |
| 77 body.setTransform(new Vector2(rand.nextDouble() * this.width, rand.nextDoubl
e() * this.height), 0.0); | 77 body.setTransform(new Vector2(rand.nextDouble() * this.width, rand.nextDoubl
e() * this.height), 0.0); |
| 78 body.applyLinearImpulse(new Vector2(rand.nextDouble()*10000.0-5000.0, rand.n
extDouble()*10000.0-5000.0), new Vector2(0.0, 0.0), true); | 78 body.applyLinearImpulse(new Vector2(rand.nextDouble()*10000.0-5000.0, rand.n
extDouble()*10000.0-5000.0), new Vector2(0.0, 0.0), true); |
| 79 | 79 |
| 80 // Add to list | 80 // Add to list |
| 81 bodies.add(body); | 81 bodies.add(body); |
| 82 | 82 |
| 83 // Create sprite | 83 // Create sprite |
| 84 SpriteNode sprt = new SpriteNode.withImage(_imgAsteroid); | 84 Sprite sprt = new Sprite.withImage(_imgAsteroid); |
| 85 sprt.width = radius*2; | 85 sprt.width = radius*2; |
| 86 sprt.height = radius*2; | 86 sprt.height = radius*2; |
| 87 // sprt.colorOverlay = new Color(0x33ff0000); | 87 // sprt.colorOverlay = new Color(0x33ff0000); |
| 88 // sprt.transferMode = TransferMode.plusMode; | 88 // sprt.transferMode = TransferMode.plusMode; |
| 89 body.userData = sprt; | 89 body.userData = sprt; |
| 90 this.addChild(sprt); | 90 this.addChild(sprt); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void addShip() { | 93 void addShip() { |
| 94 double radius = 30.0; | 94 double radius = 30.0; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 115 body.createFixtureFromFixtureDef(activeFixtureDef); | 115 body.createFixtureFromFixtureDef(activeFixtureDef); |
| 116 | 116 |
| 117 // Center on screen | 117 // Center on screen |
| 118 body.setTransform(new Vector2(width/2.0, height/2.0), 90.0); | 118 body.setTransform(new Vector2(width/2.0, height/2.0), 90.0); |
| 119 | 119 |
| 120 // Add to list | 120 // Add to list |
| 121 bodies.add(body); | 121 bodies.add(body); |
| 122 _bodyShip = body; | 122 _bodyShip = body; |
| 123 | 123 |
| 124 // Create sprite | 124 // Create sprite |
| 125 SpriteNode sprt = new SpriteNode.withImage(_imgShip); | 125 Sprite sprt = new Sprite.withImage(_imgShip); |
| 126 sprt.width = radius*2; | 126 sprt.width = radius*2; |
| 127 sprt.height = radius*2; | 127 sprt.height = radius*2; |
| 128 sprt.position = new Vector2(width/2.0, height/2.0); | 128 sprt.position = new Vector2(width/2.0, height/2.0); |
| 129 body.userData = sprt; | 129 body.userData = sprt; |
| 130 this.addChild(sprt); | 130 this.addChild(sprt); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void update(double dt) { | 133 void update(double dt) { |
| 134 // Apply thrust | 134 // Apply thrust |
| 135 if (_thrustInput != null) { | 135 if (_thrustInput != null) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 148 else { | 148 else { |
| 149 _lastSteeringSpeed *= 0.75; | 149 _lastSteeringSpeed *= 0.75; |
| 150 } | 150 } |
| 151 _bodyShip.setTransform(_bodyShip.position, _bodyShip.getAngle() + _lastSteer
ingSpeed); | 151 _bodyShip.setTransform(_bodyShip.position, _bodyShip.getAngle() + _lastSteer
ingSpeed); |
| 152 | 152 |
| 153 // Update all sprites | 153 // Update all sprites |
| 154 bodies.forEach(updateBody); | 154 bodies.forEach(updateBody); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void updateBody(Body body) { | 157 void updateBody(Body body) { |
| 158 SpriteNode sprt = body.userData; | 158 Sprite sprt = body.userData; |
| 159 double rot = 0.0; //body.getRotation(); | 159 double rot = 0.0; //body.getRotation(); |
| 160 | 160 |
| 161 // Check bounds and warp objects | 161 // Check bounds and warp objects |
| 162 if (body.position[0] < -sprt.width/2) { | 162 if (body.position[0] < -sprt.width/2) { |
| 163 body.setTransform(new Vector2(body.position[0] + this.width + sprt.width,
body.position[1]), rot); | 163 body.setTransform(new Vector2(body.position[0] + this.width + sprt.width,
body.position[1]), rot); |
| 164 } | 164 } |
| 165 if (body.position[0] > this.width + sprt.width/2) { | 165 if (body.position[0] > this.width + sprt.width/2) { |
| 166 body.setTransform(new Vector2(body.position[0] - (this.width + sprt.width)
, body.position[1]), rot); | 166 body.setTransform(new Vector2(body.position[0] - (this.width + sprt.width)
, body.position[1]), rot); |
| 167 } | 167 } |
| 168 if (body.position[1] < -sprt.height/2) { | 168 if (body.position[1] < -sprt.height/2) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 182 } | 182 } |
| 183 | 183 |
| 184 void controlThrust(double input) { | 184 void controlThrust(double input) { |
| 185 _thrustInput = input; | 185 _thrustInput = input; |
| 186 } | 186 } |
| 187 | 187 |
| 188 void controlFire() { | 188 void controlFire() { |
| 189 | 189 |
| 190 } | 190 } |
| 191 } | 191 } |
| OLD | NEW |