OLD | NEW |
1 part of game; | 1 part of game; |
2 | 2 |
3 class GameWorld extends TransformNode { | 3 class GameWorld extends TransformNode { |
4 | 4 |
5 World world; | 5 World world; |
6 List<Body> bodies = []; | 6 List<Body> bodies = []; |
7 Image _image; | 7 Body _bodyShip; |
| 8 |
| 9 Image _imgBg; |
| 10 Image _imgAsteroid; |
| 11 Image _imgShip; |
| 12 |
| 13 double _steeringInput; |
| 14 double _thrustInput; |
| 15 double _lastSteeringSpeed = 0.0; |
8 | 16 |
9 GameWorld(ImageMap images) { | 17 GameWorld(ImageMap images) { |
10 this.width = 1024.0; | 18 this.width = 1024.0; |
11 this.height = 1024.0; | 19 this.height = 1024.0; |
12 | 20 |
| 21 // Fetch images |
| 22 _imgBg = images["https://raw.githubusercontent.com/slembcke/GalacticGuardian
.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/resources-auto/Burn
Texture.png"]; |
| 23 _imgAsteroid = images["https://raw.githubusercontent.com/slembcke/GalacticGu
ardian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resou
rces-auto/asteroid_big_002.png"]; |
| 24 _imgShip = images["https://raw.githubusercontent.com/slembcke/GalacticGuardi
an.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources
-auto/GG_blueship_Lv3.png"]; |
| 25 |
| 26 // Create the physics world |
13 world = new World.withGravity(new Vector2(0.0, 0.0)); | 27 world = new World.withGravity(new Vector2(0.0, 0.0)); |
14 | 28 |
15 // Load and add background | 29 // Add a background |
16 Image imgBg = images["https://raw.githubusercontent.com/slembcke/GalacticGua
rdian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/resources-auto
/BurnTexture.png"]; | 30 addBackground(); |
17 SpriteNode sprtBg = new SpriteNode.withImage(imgBg); | 31 |
| 32 // Add some asteroids to the game world |
| 33 for (int i = 0; i < 5; i++) { |
| 34 addAsteroid(10.0); |
| 35 } |
| 36 for (int i = 0; i < 5; i++) { |
| 37 addAsteroid(20.0); |
| 38 } |
| 39 |
| 40 // Add ship |
| 41 addShip(); |
| 42 } |
| 43 |
| 44 void addBackground() { |
| 45 SpriteNode sprtBg = new SpriteNode.withImage(_imgBg); |
18 sprtBg.width = width; | 46 sprtBg.width = width; |
19 sprtBg.height = height; | 47 sprtBg.height = height; |
20 sprtBg.pivot = new Vector2(0.0, 0.0); | 48 sprtBg.pivot = new Vector2(0.0, 0.0); |
21 this.children.add(sprtBg); | 49 this.children.add(sprtBg); |
22 | |
23 // Load asteroid image | |
24 _image = images["https://raw.githubusercontent.com/slembcke/GalacticGuardian
.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/resources-a
uto/asteroid_big_002.png"]; | |
25 | |
26 // Add some asteroids to the game world | |
27 for (int i = 0; i < 50; i++) { | |
28 addAsteroid(10.0); | |
29 } | |
30 for (int i = 0; i < 50; i++) { | |
31 addAsteroid(20.0); | |
32 } | |
33 } | 50 } |
34 | 51 |
35 void addAsteroid([double radius=20.0]) { | 52 void addAsteroid([double radius=20.0]) { |
36 | 53 |
37 // Create shape | 54 // Create shape |
38 final CircleShape shape = new CircleShape(); | 55 final CircleShape shape = new CircleShape(); |
39 shape.radius = radius; | 56 shape.radius = radius; |
40 | 57 |
41 // Define fixture (links body and shape) | 58 // Define fixture (links body and shape) |
42 final FixtureDef activeFixtureDef = new FixtureDef(); | 59 final FixtureDef activeFixtureDef = new FixtureDef(); |
(...skipping 12 matching lines...) Expand all Loading... |
55 final Body body = world.createBody(bodyDef); | 72 final Body body = world.createBody(bodyDef); |
56 body.createFixtureFromFixtureDef(activeFixtureDef); | 73 body.createFixtureFromFixtureDef(activeFixtureDef); |
57 | 74 |
58 // Set position of object | 75 // Set position of object |
59 Math.Random rand = new Math.Random(); | 76 Math.Random rand = new Math.Random(); |
60 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); |
61 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); |
62 | 79 |
63 // Add to list | 80 // Add to list |
64 bodies.add(body); | 81 bodies.add(body); |
65 | 82 |
66 SpriteNode sprt = new SpriteNode.withImage(_image); | 83 // Create sprite |
| 84 SpriteNode sprt = new SpriteNode.withImage(_imgAsteroid); |
67 sprt.width = radius*2; | 85 sprt.width = radius*2; |
68 sprt.height = radius*2; | 86 sprt.height = radius*2; |
69 sprt.colorOverlay = new Color(0x33ff0000); | 87 // sprt.colorOverlay = new Color(0x33ff0000); |
70 sprt.transferMode = TransferMode.plusMode; | 88 // sprt.transferMode = TransferMode.plusMode; |
| 89 body.userData = sprt; |
| 90 this.children.add(sprt); |
| 91 } |
| 92 |
| 93 void addShip() { |
| 94 double radius = 30.0; |
| 95 |
| 96 // Create shape |
| 97 final CircleShape shape = new CircleShape(); |
| 98 shape.radius = radius; |
| 99 |
| 100 // Define fixture (links body and shape) |
| 101 final FixtureDef activeFixtureDef = new FixtureDef(); |
| 102 activeFixtureDef.restitution = 1.0; |
| 103 activeFixtureDef.density = 0.05; |
| 104 activeFixtureDef.shape = shape; |
| 105 |
| 106 // Define body |
| 107 final BodyDef bodyDef = new BodyDef(); |
| 108 bodyDef.type = BodyType.DYNAMIC; |
| 109 bodyDef.position = new Vector2(0.0, 30.0); |
| 110 bodyDef.linearDamping = 0.0; |
| 111 bodyDef.angularDamping = 0.95; |
| 112 |
| 113 // Create body and fixture from definitions |
| 114 final Body body = world.createBody(bodyDef); |
| 115 body.createFixtureFromFixtureDef(activeFixtureDef); |
| 116 |
| 117 // Center on screen |
| 118 body.setTransform(new Vector2(width/2.0, height/2.0), 90.0); |
| 119 |
| 120 // Add to list |
| 121 bodies.add(body); |
| 122 _bodyShip = body; |
| 123 |
| 124 // Create sprite |
| 125 SpriteNode sprt = new SpriteNode.withImage(_imgShip); |
| 126 sprt.width = radius*2; |
| 127 sprt.height = radius*2; |
| 128 sprt.position = new Vector2(width/2.0, height/2.0); |
71 body.userData = sprt; | 129 body.userData = sprt; |
72 this.children.add(sprt); | 130 this.children.add(sprt); |
73 } | 131 } |
74 | 132 |
75 void update(double dt) { | 133 void update(double dt) { |
| 134 // Apply thrust |
| 135 if (_thrustInput != null) { |
| 136 double force = _thrustInput*10000.0; |
| 137 double rad = degrees2radians(_bodyShip.getAngle()); |
| 138 _bodyShip.applyLinearImpulse(new Vector2(Math.cos(rad)*force, Math.sin(rad
)*force), new Vector2(0.0, 0.0), true); |
| 139 } |
| 140 |
| 141 // Simulate world |
76 world.stepDt(1.0/60.0, 10, 10); // Pass in dt | 142 world.stepDt(1.0/60.0, 10, 10); // Pass in dt |
77 | 143 |
| 144 // Apply stearing |
| 145 if (_steeringInput != null) { |
| 146 _lastSteeringSpeed = _steeringInput * 4.0; |
| 147 } |
| 148 else { |
| 149 _lastSteeringSpeed *= 0.75; |
| 150 } |
| 151 _bodyShip.setTransform(_bodyShip.position, _bodyShip.getAngle() + _lastSteer
ingSpeed); |
| 152 |
| 153 // Update all sprites |
78 bodies.forEach(updateBody); | 154 bodies.forEach(updateBody); |
79 } | 155 } |
80 | 156 |
81 void updateBody(Body body) { | 157 void updateBody(Body body) { |
82 SpriteNode sprt = body.userData; | 158 SpriteNode sprt = body.userData; |
83 double rot = 0.0; //body.getRotation(); | 159 double rot = 0.0; //body.getRotation(); |
84 | 160 |
85 // Check bounds and warp objects | 161 // Check bounds and warp objects |
86 if (body.position[0] < -sprt.width/2) { | 162 if (body.position[0] < -sprt.width/2) { |
87 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); |
88 } | 164 } |
89 if (body.position[0] > this.width + sprt.width/2) { | 165 if (body.position[0] > this.width + sprt.width/2) { |
90 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); |
91 } | 167 } |
92 if (body.position[1] < -sprt.height/2) { | 168 if (body.position[1] < -sprt.height/2) { |
93 body.setTransform(new Vector2(body.position[0], body.position[1] + this.he
ight + sprt.height), rot); | 169 body.setTransform(new Vector2(body.position[0], body.position[1] + this.he
ight + sprt.height), rot); |
94 } | 170 } |
95 if (body.position[1] > this.height + sprt.height/2) { | 171 if (body.position[1] > this.height + sprt.height/2) { |
96 body.setTransform(new Vector2(body.position[0], body.position[1] - (this.h
eight + sprt.height)), rot); | 172 body.setTransform(new Vector2(body.position[0], body.position[1] - (this.h
eight + sprt.height)), rot); |
97 } | 173 } |
98 | 174 |
99 // Update sprite | 175 // Update sprite |
100 sprt.position = body.position; | 176 sprt.position = body.position; |
101 sprt.rotation = body.getAngle(); | 177 sprt.rotation = body.getAngle(); |
102 } | 178 } |
103 } | 179 |
| 180 void controlSteering(double input) { |
| 181 _steeringInput = input; |
| 182 } |
| 183 |
| 184 void controlThrust(double input) { |
| 185 _thrustInput = input; |
| 186 } |
| 187 |
| 188 void controlFire() { |
| 189 |
| 190 } |
| 191 } |
OLD | NEW |