OLD | NEW |
| (Empty) |
1 part of game; | |
2 | |
3 class GameWorld extends Node { | |
4 | |
5 World world; | |
6 List<Body> bodies = []; | |
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; | |
16 | |
17 GameWorld(ImageMap images) { | |
18 this.width = 1024.0; | |
19 this.height = 1024.0; | |
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 | |
27 world = new World.withGravity(new Vector2(0.0, 0.0)); | |
28 | |
29 // Add a background | |
30 addBackground(); | |
31 | |
32 // Add some asteroids to the game world | |
33 for (int i = 0; i < 50; i++) { | |
34 addAsteroid(10.0); | |
35 } | |
36 for (int i = 0; i < 50; i++) { | |
37 addAsteroid(20.0); | |
38 } | |
39 | |
40 // Add ship | |
41 addShip(); | |
42 } | |
43 | |
44 void addBackground() { | |
45 Sprite sprtBg = new Sprite.withImage(_imgBg); | |
46 sprtBg.width = width; | |
47 sprtBg.height = height; | |
48 sprtBg.pivot = new Vector2(0.0, 0.0); | |
49 this.addChild(sprtBg); | |
50 } | |
51 | |
52 void addAsteroid([double radius=20.0]) { | |
53 | |
54 // Create shape | |
55 final CircleShape shape = new CircleShape(); | |
56 shape.radius = radius; | |
57 | |
58 // Define fixture (links body and shape) | |
59 final FixtureDef activeFixtureDef = new FixtureDef(); | |
60 activeFixtureDef.restitution = 1.0; | |
61 activeFixtureDef.density = 0.05; | |
62 activeFixtureDef.shape = shape; | |
63 | |
64 // Define body | |
65 final BodyDef bodyDef = new BodyDef(); | |
66 bodyDef.type = BodyType.DYNAMIC; | |
67 bodyDef.position = new Vector2(0.0, 30.0); | |
68 bodyDef.linearDamping = 0.0; | |
69 bodyDef.angularDamping = 0.0; | |
70 | |
71 // Create body and fixture from definitions | |
72 final Body body = world.createBody(bodyDef); | |
73 body.createFixtureFromFixtureDef(activeFixtureDef); | |
74 | |
75 // Set position of object | |
76 Math.Random rand = new Math.Random(); | |
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); | |
79 | |
80 // Add to list | |
81 bodies.add(body); | |
82 | |
83 // Create sprite | |
84 Sprite sprt = new Sprite.withImage(_imgAsteroid); | |
85 sprt.width = radius*2; | |
86 sprt.height = radius*2; | |
87 // sprt.colorOverlay = new Color(0x33ff0000); | |
88 // sprt.transferMode = TransferMode.plusMode; | |
89 body.userData = sprt; | |
90 this.addChild(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 Sprite sprt = new Sprite.withImage(_imgShip); | |
126 sprt.width = radius*2; | |
127 sprt.height = radius*2; | |
128 sprt.position = new Vector2(width/2.0, height/2.0); | |
129 body.userData = sprt; | |
130 this.addChild(sprt); | |
131 } | |
132 | |
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 | |
142 world.stepDt(1.0/60.0, 10, 10); // Pass in dt | |
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 | |
154 bodies.forEach(updateBody); | |
155 } | |
156 | |
157 void updateBody(Body body) { | |
158 Sprite sprt = body.userData; | |
159 double rot = 0.0; //body.getRotation(); | |
160 | |
161 // Check bounds and warp objects | |
162 if (body.position[0] < -sprt.width/2) { | |
163 body.setTransform(new Vector2(body.position[0] + this.width + sprt.width,
body.position[1]), rot); | |
164 } | |
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); | |
167 } | |
168 if (body.position[1] < -sprt.height/2) { | |
169 body.setTransform(new Vector2(body.position[0], body.position[1] + this.he
ight + sprt.height), rot); | |
170 } | |
171 if (body.position[1] > this.height + sprt.height/2) { | |
172 body.setTransform(new Vector2(body.position[0], body.position[1] - (this.h
eight + sprt.height)), rot); | |
173 } | |
174 | |
175 // Update sprite | |
176 sprt.position = body.position; | |
177 sprt.rotation = body.getAngle(); | |
178 } | |
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 |