Mercurial > projects > openmelee
annotate melee/melee.d @ 22:4fce5596d1f6
ai work
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Thu, 26 Mar 2009 16:56:30 -0400 |
parents | cad384ad349e |
children | e79347dd38a3 |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright (c) 2009, Mason Green (zzzzrrr) | |
8 | 3 * http://www.dsource.org/projects/openmelee |
0 | 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 */ | |
18 | 31 module openmelee.melee.melee; |
0 | 32 |
18 | 33 import tango.io.Stdout; |
12 | 34 |
0 | 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 | |
18 | 44 import blaze.common.bzMath : bzVec2; |
45 import blaze.bzWorld : bzWorld; | |
46 import blaze.collision.bzCollision : bzAABB; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
5
diff
changeset
|
47 |
18 | 48 import openmelee.melee.boundaryListener; |
49 import openmelee.melee.contactListener; | |
50 import openmelee.render.render; | |
22 | 51 import openmelee.ai.ai : AI; |
52 import openmelee.ai.human : Human; | |
53 import openmelee.ships.urQuan : UrQuan; | |
54 import openmelee.ships.orz : Orz; | |
55 import openmelee.ships.planet : Planet; | |
56 import openmelee.ships.asteroids : Asteroid; | |
0 | 57 |
17 | 58 const ITERS_PER_SECOND = 100; |
18 | 59 const k_maxContactPoints = 100; |
0 | 60 |
18 | 61 // Melee settings |
62 struct Settings { | |
63 float hz = 60; | |
64 int velocityIterations = 3; | |
65 int positionIterations = 1; | |
66 bool drawShapes = true; | |
67 bool drawJoints = true; | |
68 bool drawControllers; | |
69 bool drawCoreShapes; | |
70 bool drawAABBs; | |
71 bool drawOBBs; | |
72 bool drawPairs; | |
73 bool drawContactPoints; | |
74 bool drawContactNormals; | |
75 bool drawContactForces; | |
76 bool drawFrictionForces; | |
77 bool drawCOMs; | |
78 bool drawStats; | |
79 bool enableWarmStarting; | |
80 bool enableTOI; | |
81 } | |
82 | |
21 | 83 struct ContactPoint { |
84 bzShape shape1; | |
85 bzShape shape2; | |
86 bzVec2 normal; | |
87 bzVec2 position; | |
88 bzVec2 velocity; | |
89 bzContactID id; | |
90 //ContactState state; | |
91 } | |
92 | |
18 | 93 class Melee { |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
94 |
18 | 95 Settings settings; |
96 float timeStep; | |
97 const bzVec2 gravity = bzVec2(0.0f, 0.0f); | |
98 bool allowSleep; | |
99 Render draw; | |
100 | |
101 AI ai; | |
102 Human human; | |
103 Ship ship1; | |
104 Ship ship2; | |
22 | 105 Planet planet; |
18 | 106 |
107 bool running; | |
108 | |
0 | 109 StopWatch timer; |
18 | 110 bzAABB worldAABB; |
111 bzWorld world; | |
112 bzBoundaryListener m_boundaryListener; | |
113 bzContactListener m_contactListener; | |
114 ContactPoint[k_maxContactPoints] points; | |
115 int pointCount; | |
116 | |
117 this() { | |
118 } | |
119 | |
120 void init() { | |
17 | 121 |
18 | 122 timeStep = settings.hz > 0.0f ? 1.0f / settings.hz : 0.0f; |
123 version(distrib) gui.vfs.mount(new ZipFolder("./gui.zip")); | |
124 scope cfg = loadHybridConfig("./gui.cfg"); | |
125 scope renderer = new Renderer; | |
126 | |
127 m_boundaryListener = new BoundaryListener(this); | |
128 m_contactListener = new ContactListener(this); | |
129 initWorld(); | |
130 running = true; | |
15 | 131 |
18 | 132 draw = new Render(world, ship1, ship2, settings); |
133 human = new Human(ship1); | |
22 | 134 ai = new AI(ship1, world); |
18 | 135 |
136 gui.begin(cfg).retained; | |
0 | 137 gui.push(`main`); |
18 | 138 GLViewport(`glview`).renderingHandler(&draw.draw) |
139 .addHandler(&human.onClick) | |
140 .addHandler(&human.onMove) | |
141 .addHandler(&human.onKey) | |
142 .addHandler(&human.onDT) | |
143 .addHandler(&human.onMouseEnter) | |
144 .addHandler(&human.onMouseLeave) | |
145 .grabKeyboardFocus; | |
146 gui.pop(); | |
147 gui.immediate.end; | |
148 | |
149 jobHub.addRepeatableJob( { | |
150 // Update physics | |
151 world.step(timeStep, settings.velocityIterations, settings.positionIterations); | |
152 }, ITERS_PER_SECOND); | |
153 | |
154 jobHub.addPreFrameJob( { | |
155 // Update AI | |
22 | 156 ai.move(ship2); |
18 | 157 }); |
2 | 158 |
18 | 159 jobHub.addPostFrameJob( { |
160 | |
161 // Limit velocity | |
162 ship1.limitVelocity(); | |
163 ship2.limitVelocity(); | |
164 ship1.updateState(); | |
165 ship2.updateState(); | |
166 | |
167 gui.begin(cfg); | |
168 gui.push(`main`); | |
169 if (gui().getProperty!(bool)("frame.closeClicked")) { | |
170 running = false; | |
171 } | |
0 | 172 |
18 | 173 if(human.thrust) { |
174 ship1.thrust(); | |
175 } | |
176 | |
177 gui().setProperty!(bool)("showCursor", true); | |
178 gui.pop(); | |
179 gui.end; | |
180 gui.render(renderer); | |
181 | |
182 }); | |
183 | |
184 while (running && !human.quit) { | |
185 float delta = timer.stop; | |
186 timer.start; | |
187 jobHub.update(delta); | |
188 } | |
189 } | |
0 | 190 |
18 | 191 void initWorld() { |
192 // Define world boundaries | |
22 | 193 worldAABB.lowerBound.set(-100.0f, -150.0f); |
194 worldAABB.upperBound.set(100.0f, 150.0f); | |
18 | 195 world = new bzWorld(worldAABB, gravity, allowSleep); |
196 world.boundaryListener = m_boundaryListener; | |
197 world.contactListener = m_contactListener; | |
198 ship1 = new UrQuan(world); | |
199 ship2 = new Orz(world); | |
200 ship2.rBody.angle = 3.14159265/4; | |
22 | 201 planet = new Planet(world); |
202 //auto asteroids = new Asteroid(world); | |
18 | 203 } |
204 | |
205 void boundaryViolated(bzBody rBody) | |
206 { | |
207 float x,y; | |
0 | 208 |
18 | 209 if(rBody.position.x > worldAABB.upperBound.x) { |
210 x = worldAABB.lowerBound.x + 5; | |
211 rBody.position = bzVec2(x, rBody.position.y); | |
212 } else if (rBody.position.x < worldAABB.lowerBound.x) { | |
213 x = worldAABB.upperBound.x - 5; | |
214 rBody.position = bzVec2(x, rBody.position.y); | |
215 } else if (rBody.position.y > worldAABB.upperBound.y) { | |
216 y = worldAABB.lowerBound.y + 5; | |
217 rBody.position = bzVec2(rBody.position.x, y); | |
218 } else if(rBody.position.y < worldAABB.lowerBound.y) { | |
219 y = worldAABB.upperBound.y - 5; | |
220 rBody.position = bzVec2(rBody.position.x, y); | |
221 } | |
222 } | |
0 | 223 } |