comparison melee/melee.d @ 18:7f74e064dad5

refactored code
author zzzzrrr <mason.green@gmail.com>
date Wed, 25 Mar 2009 11:28:25 -0400
parents main.d@82efafc87d54
children 08ddf9e71b88
comparison
equal deleted inserted replaced
17:82efafc87d54 18:7f74e064dad5
1 /*
2 * Copyright (c) 2009, Mason Green (zzzzrrr)
3 * http://www.dsource.org/projects/openmelee
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * * Neither the name of the polygonal nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without specific
17 * prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 module openmelee.melee.melee;
32
33 import tango.io.Stdout;
34
35 version(distrib) import tango.io.vfs.ZipFolder;
36 import tango.time.StopWatch;
37 import fc = tango.text.convert.Float : toString;
38 import tango.util.log.Trace;
39
40 import xf.core.JobHub;
41 import xf.hybrid.Hybrid;
42 import xf.hybrid.backend.GL;
43
44 import blaze.common.bzMath : bzVec2;
45 import blaze.bzWorld : bzWorld;
46 import blaze.collision.bzCollision : bzAABB;
47
48 import openmelee.melee.boundaryListener;
49 import openmelee.melee.contactListener;
50 import openmelee.render.render;
51 import openmelee.ai.ai;
52 import openmelee.ai.human;
53 import openmelee.ships.urQuan;
54 import openmelee.ships.orz;
55 import openmelee.ships.planet;
56
57 const ITERS_PER_SECOND = 100;
58 const k_maxContactPoints = 100;
59
60 // Melee settings
61 struct Settings {
62 float hz = 60;
63 int velocityIterations = 3;
64 int positionIterations = 1;
65 bool drawShapes = true;
66 bool drawJoints = true;
67 bool drawControllers;
68 bool drawCoreShapes;
69 bool drawAABBs;
70 bool drawOBBs;
71 bool drawPairs;
72 bool drawContactPoints;
73 bool drawContactNormals;
74 bool drawContactForces;
75 bool drawFrictionForces;
76 bool drawCOMs;
77 bool drawStats;
78 bool enableWarmStarting;
79 bool enableTOI;
80 }
81
82 class Melee {
83
84 Settings settings;
85 float timeStep;
86 const bzVec2 gravity = bzVec2(0.0f, 0.0f);
87 bool allowSleep;
88 Render draw;
89
90 AI ai;
91 Human human;
92 Ship ship1;
93 Ship ship2;
94
95 bool running;
96
97 StopWatch timer;
98 bzAABB worldAABB;
99 bzWorld world;
100 bzBoundaryListener m_boundaryListener;
101 bzContactListener m_contactListener;
102 ContactPoint[k_maxContactPoints] points;
103 int pointCount;
104
105 this() {
106 }
107
108 void init() {
109
110 timeStep = settings.hz > 0.0f ? 1.0f / settings.hz : 0.0f;
111 version(distrib) gui.vfs.mount(new ZipFolder("./gui.zip"));
112 scope cfg = loadHybridConfig("./gui.cfg");
113 scope renderer = new Renderer;
114
115 m_boundaryListener = new BoundaryListener(this);
116 m_contactListener = new ContactListener(this);
117 initWorld();
118 running = true;
119
120 draw = new Render(world, ship1, ship2, settings);
121 human = new Human(ship1);
122 ai = new AI(ship2);
123
124 gui.begin(cfg).retained;
125 gui.push(`main`);
126 GLViewport(`glview`).renderingHandler(&draw.draw)
127 .addHandler(&human.onClick)
128 .addHandler(&human.onMove)
129 .addHandler(&human.onKey)
130 .addHandler(&human.onDT)
131 .addHandler(&human.onMouseEnter)
132 .addHandler(&human.onMouseLeave)
133 .grabKeyboardFocus;
134 gui.pop();
135 gui.immediate.end;
136
137 jobHub.addRepeatableJob( {
138 // Update physics
139 world.step(timeStep, settings.velocityIterations, settings.positionIterations);
140 }, ITERS_PER_SECOND);
141
142 jobHub.addPreFrameJob( {
143 // Update AI
144 ai.move(ship1);
145 });
146
147 jobHub.addPostFrameJob( {
148
149 // Limit velocity
150 ship1.limitVelocity();
151 ship2.limitVelocity();
152 ship1.updateState();
153 ship2.updateState();
154
155 gui.begin(cfg);
156 gui.push(`main`);
157 if (gui().getProperty!(bool)("frame.closeClicked")) {
158 running = false;
159 }
160
161 if(human.thrust) {
162 ship1.thrust();
163 }
164
165 gui().setProperty!(bool)("showCursor", true);
166 gui.pop();
167 gui.end;
168 gui.render(renderer);
169
170 });
171
172 while (running && !human.quit) {
173 float delta = timer.stop;
174 timer.start;
175 jobHub.update(delta);
176 }
177 }
178
179 void initWorld() {
180 // Define world boundaries
181 worldAABB.lowerBound.set(-400.0f, -250.0f);
182 worldAABB.upperBound.set(400.0f, 250.0f);
183 world = new bzWorld(worldAABB, gravity, allowSleep);
184 world.boundaryListener = m_boundaryListener;
185 world.contactListener = m_contactListener;
186 ship1 = new UrQuan(world);
187 ship2 = new Orz(world);
188 ship2.rBody.angle = 3.14159265/4;
189 auto planet = new Planet(world);
190 }
191
192 void boundaryViolated(bzBody rBody)
193 {
194 float x,y;
195
196 if(rBody.position.x > worldAABB.upperBound.x) {
197 x = worldAABB.lowerBound.x + 5;
198 rBody.position = bzVec2(x, rBody.position.y);
199 } else if (rBody.position.x < worldAABB.lowerBound.x) {
200 x = worldAABB.upperBound.x - 5;
201 rBody.position = bzVec2(x, rBody.position.y);
202 } else if (rBody.position.y > worldAABB.upperBound.y) {
203 y = worldAABB.lowerBound.y + 5;
204 rBody.position = bzVec2(rBody.position.x, y);
205 } else if(rBody.position.y < worldAABB.lowerBound.y) {
206 y = worldAABB.upperBound.y - 5;
207 rBody.position = bzVec2(rBody.position.x, y);
208 }
209 }
210 }