0
|
1 /*
|
|
2 * Copyright (c) 2009, Mason Green (zzzzrrr)
|
|
3 *
|
|
4 * All rights reserved.
|
|
5 *
|
|
6 * Redistribution and use in source and binary forms, with or without modification,
|
|
7 * are permitted provided that the following conditions are met:
|
|
8 *
|
|
9 * * Redistributions of source code must retain the above copyright notice,
|
|
10 * this list of conditions and the following disclaimer.
|
|
11 * * Redistributions in binary form must reproduce the above copyright notice,
|
|
12 * this list of conditions and the following disclaimer in the documentation
|
|
13 * and/or other materials provided with the distribution.
|
|
14 * * Neither the name of the polygonal nor the names of its contributors may be
|
|
15 * used to endorse or promote products derived from this software without specific
|
|
16 * prior written permission.
|
|
17 *
|
|
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29 */
|
|
30 module melee.melee;
|
|
31
|
|
32 import Integer = tango.text.convert.Integer;
|
|
33 import tango.math.Math;
|
|
34 import tango.math.random.Kiss;
|
|
35
|
|
36 import xf.hybrid.Event;
|
|
37 import xf.input.KeySym;
|
|
38 import xf.omg.core.LinearAlgebra;
|
|
39
|
|
40 public import blaze.all;
|
|
41
|
|
42 import melee.boundaryListener;
|
|
43 import melee.contactListener;
|
|
44 import melee.ship;
|
|
45 import melee.urQuan;
|
|
46 import melee.orz;
|
|
47
|
|
48 // Cursor scale factor
|
|
49 const CURSORSIZE = 0.05f;
|
|
50
|
|
51 const INIT_SPAWN_SIZE = 0.5f;
|
|
52
|
|
53 // Dragging stuffs
|
|
54 const BUNGEE_K = 1.5f;
|
|
55 // Damping factor for dragging
|
|
56 const DRAGDAMP = 20.0f;
|
|
57
|
|
58 // Size of hinges
|
|
59 const HINGE_RADIUS = 0.05f;
|
|
60
|
|
61 // Smallest allowed dimension
|
|
62 const MIN_DIMENSION = 0.1;
|
|
63
|
|
64 // Water stuffs
|
|
65 const MAX_PARTICLES = 10000;
|
|
66 const WATER_BOUNCE = 0.01;
|
|
67 const WATER_FRICTION = 0.5;
|
|
68 const WATER_RADIUS = 0.05f;
|
|
69 const MAX_CIRCLE_RES = 32;
|
|
70
|
|
71 const k_maxContactPoints = 2048;
|
|
72
|
|
73 enum ContactState {
|
|
74 e_contactAdded,
|
|
75 e_contactPersisted,
|
|
76 e_contactRemoved
|
|
77 }
|
|
78
|
|
79 struct ContactPoint {
|
|
80 bzShape shape1;
|
|
81 bzShape shape2;
|
|
82 bzVec2 normal;
|
|
83 bzVec2 position;
|
|
84 bzVec2 velocity;
|
|
85 bzContactID id;
|
|
86 ContactState state;
|
|
87 }
|
|
88
|
|
89 // Melee settings. Some can be controlled in the GUI.
|
|
90 struct Settings {
|
|
91 float hz = 60;
|
|
92 int velocityIterations = 8;
|
|
93 int positionIterations = 2;
|
|
94 bool drawShapes = true;
|
|
95 bool drawJoints = true;
|
|
96 bool drawControllers;
|
|
97 bool drawCoreShapes;
|
|
98 bool drawAABBs;
|
|
99 bool drawOBBs;
|
|
100 bool drawPairs;
|
|
101 bool drawContactPoints;
|
|
102 bool drawContactNormals;
|
|
103 bool drawContactForces;
|
|
104 bool drawFrictionForces;
|
|
105 bool drawCOMs;
|
|
106 bool drawStats;
|
|
107 bool enableWarmStarting;
|
|
108 bool enableTOI;
|
|
109 }
|
|
110
|
|
111 // Dirty, dirty hack for communicating config changes to Main
|
|
112 // TODO: Harass h3 to add .changed to hybrid so this isn't necessary
|
|
113 struct ConfigChange (T)
|
|
114 {
|
|
115 protected
|
|
116 {
|
|
117 bool _pending = false;
|
|
118 T _value;
|
|
119 }
|
|
120
|
|
121 T value()
|
|
122 {
|
|
123 _pending = false;
|
|
124 return _value;
|
|
125 }
|
|
126
|
|
127 void value(T change)
|
|
128 {
|
|
129 _value = change;
|
|
130 _pending = true;
|
|
131 }
|
|
132
|
|
133 bool pending()
|
|
134 {
|
|
135 return _pending;
|
|
136 }
|
|
137
|
|
138 T opAssign(T change)
|
|
139 {
|
|
140 value(change);
|
|
141 return _value;
|
|
142 }
|
|
143
|
|
144 bool opEquals(T other)
|
|
145 {
|
|
146 return _value == other;
|
|
147 }
|
|
148 }
|
|
149
|
|
150 T randomRange(T = int) (T min, T max)
|
|
151 {
|
|
152 return min + Kiss.instance.natural() % (max + 1 - min);
|
|
153 }
|
|
154
|
|
155 class Melee
|
|
156 {
|
|
157
|
|
158 this(Settings *settings)
|
|
159 {
|
|
160 this.settings = settings;
|
|
161 init();
|
|
162 spawnRect = vec2(INIT_SPAWN_SIZE, INIT_SPAWN_SIZE);
|
|
163 // bzWorld boundary callback
|
|
164 m_boundaryListener = new BoundaryListener(this);
|
|
165 // bzContact callback
|
|
166 m_contactListener = new ContactListener(this);
|
|
167 }
|
|
168
|
|
169 void init() {
|
|
170 // Define world boundaries
|
|
171 bzAABB worldAABB;
|
|
172 worldAABB.lowerBound.set(-200.0f, -100.0f);
|
|
173 worldAABB.upperBound.set(200.0f, 200.0f);
|
|
174 world = new bzWorld(worldAABB, gravity, allowSleep);
|
|
175 world.boundaryListener = m_boundaryListener;
|
|
176 world.contactListener = m_contactListener;
|
|
177 viewCenter = vec2(10, 10);
|
|
178 ship1 = new Orz(world);
|
|
179 ship2 = new UrQuan(world);
|
|
180 }
|
|
181
|
|
182 void drag()
|
|
183 {
|
|
184
|
|
185 }
|
|
186
|
|
187 EventHandling onClick(MouseButtonEvent e)
|
|
188 {
|
|
189 return EventHandling.Stop;
|
|
190 }
|
|
191
|
|
192 EventHandling onKey(KeyboardEvent e)
|
|
193 {
|
|
194 if (e.unicode == '+') { // HACK: the windows input writer doesn't do plus correctly yet - h3
|
|
195 e.keySym = KeySym.plus;
|
|
196 }
|
|
197 if (e.down) {
|
|
198 switch (e.keySym) {
|
|
199 case KeySym.Escape:
|
|
200 quit = true;
|
|
201 break;
|
|
202 default:
|
|
203 char key = cast(char) e.keySym;
|
|
204 if(key == 'w') {
|
|
205 thrust = true;
|
|
206 break;
|
|
207 } else {
|
|
208 ship1.turn(key);
|
|
209 }
|
|
210 break;
|
|
211 }
|
|
212 // Key released
|
|
213 } else {
|
|
214 char key = cast(char) e.keySym;
|
|
215 if(key == 'w') {
|
|
216 thrust = false;
|
|
217 } else if (key == 'd' || key == 'a') {
|
|
218 ship1.rBody.angularVelocity = 0.0f;
|
|
219 }
|
|
220 }
|
|
221
|
|
222 return EventHandling.Stop;
|
|
223 }
|
|
224
|
|
225 // Mouse move
|
|
226 EventHandling onMove(MouseMoveEvent e)
|
|
227 {
|
|
228 return EventHandling.Stop;
|
|
229 }
|
|
230
|
|
231 EventHandling onDT(TimeUpdateEvent e)
|
|
232 {
|
|
233 return EventHandling.Continue;
|
|
234 }
|
|
235
|
|
236 EventHandling onMouseEnter(MouseEnterEvent e)
|
|
237 {
|
|
238 return EventHandling.Continue;
|
|
239 }
|
|
240
|
|
241 EventHandling onMouseLeave(MouseLeaveEvent e)
|
|
242 {
|
|
243 return EventHandling.Continue;
|
|
244 }
|
|
245
|
|
246 protected
|
|
247 {
|
|
248 const bzVec2 gravity = bzVec2(0.0f, 0.0f);
|
|
249 bool allowSleep = false;
|
|
250
|
|
251 vec2 spawnRect;
|
|
252
|
|
253 vec2[] drawing;
|
|
254
|
|
255 vec2i screenSize = vec2i.zero;
|
|
256 vec2 mousePos = vec2.zero;
|
|
257
|
|
258 bool scaling = false;
|
|
259 bool full = false;
|
|
260
|
|
261 vec2 spawnStart;
|
|
262
|
|
263 bool preserveBullet = false;
|
|
264
|
|
265 float waterDelta = 0;
|
|
266 }
|
|
267
|
|
268 void boundaryViolated(bzBody rBody)
|
|
269 {
|
|
270 uint key = rBody.toHash();
|
|
271 killList[key] = rBody;
|
|
272 }
|
|
273
|
|
274 bool quit;
|
|
275
|
|
276 // Ortho view zoom
|
|
277 float zoom = 40;
|
|
278 int pointCount;
|
|
279 vec2 viewCenter;
|
|
280 bzBody[uint] killList;
|
|
281
|
|
282 bzWorld world;
|
|
283 Settings *settings;
|
|
284
|
|
285 // bzWorld boundary listener. Destroy bodies that leave world bzAABB
|
|
286 bzBoundaryListener m_boundaryListener;
|
|
287 bzContactListener m_contactListener;
|
|
288 ContactPoint[k_maxContactPoints] points;
|
|
289
|
|
290 ConfigChange!(vec2) editChange;
|
|
291 bool thrust;
|
|
292
|
|
293 Ship ship1;
|
|
294 Ship ship2;
|
|
295 }
|
|
296
|
|
297 // Utility functions
|
|
298 bzVec2 toBlaze(vec2 vec)
|
|
299 {
|
|
300 auto ret = bzVec2(vec.x, vec.y);
|
|
301 return ret;
|
|
302 }
|
|
303
|
|
304 vec2 rotate(vec2 point, float rad)
|
|
305 {
|
|
306 return vec2(cos(rad) * point.x - sin(rad) * point.y, sin(rad) * point.x + cos(rad) * point.y);
|
|
307 }
|
|
308
|
|
309 class Select
|
|
310 {
|
|
311 bool select;
|
|
312 }
|