Mercurial > projects > openmelee
annotate ships/ship.d @ 30:1cd0d4c7258e
misc changes
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Mon, 30 Mar 2009 16:16:58 -0400 |
parents | da11cc30423f |
children | 741313c302ed |
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.ships.ship; |
0 | 32 |
33 import tango.io.Stdout: Stdout; | |
26 | 34 import tango.math.Math : PI; |
0 | 35 |
36 import tango.util.container.LinkedList : LinkedList; | |
37 import blaze.dynamics.bzBody : bzBody; | |
26 | 38 import blaze.collision.shapes.bzShape : bzShape, bzShapeDef, bzMassData; |
39 import blaze.collision.shapes.bzShapeType; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
40 import blaze.common.bzMath: bzVec2, bzCross, bzClamp; |
6 | 41 import blaze.bzWorld : bzWorld; |
42 import blaze.dynamics.forces.bzAttractor: bzAttractor; | |
26 | 43 import blaze.dynamics.bzBodyDef : bzBodyDef; |
44 import blaze.collision.shapes.bzPolygon : bzPolyDef, bzPolygon; | |
45 import blaze.collision.shapes.bzCircle : bzCircleDef; | |
0 | 46 |
30 | 47 import openmelee.ai.utilities : randomRange; |
48 //import openmelee.test.wrap; | |
0 | 49 |
30 | 50 alias LinkedList!(bzShape) ShapeList; |
26 | 51 |
24 | 52 struct State |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
53 { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
54 bzVec2 position; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
55 bzVec2 velocity; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
56 bzVec2 up; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
57 bzVec2 side; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
58 bzVec2 forward; |
12 | 59 bzVec2 target; |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
60 float speed = 0; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
61 float maxForce = 0; |
13 | 62 bool turn; |
22 | 63 float radius = 0.0f; |
64 float enemyAngle = 0.0f; | |
65 bzVec2 avoid; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
66 |
24 | 67 Ship enemy; |
68 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
69 bzVec2 predictFuturePosition(float dt) { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
70 return (position + velocity * dt); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
71 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
72 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
73 |
0 | 74 abstract class Ship |
75 { | |
6 | 76 bzWorld world; |
0 | 77 bzBody rBody; |
78 ShapeList shapes; | |
79 bzVec2 engineForce; | |
80 bzVec2 turnForce; | |
81 bzVec2 leftTurnPoint; | |
82 bzVec2 rightTurnPoint; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
83 State state; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
84 |
22 | 85 float battery = 0.0f; |
86 float crew = 0.0f; | |
15 | 87 |
88 float maxLinVel = 50; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
89 float maxAngVel = 2; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
90 |
6 | 91 this(bzWorld world) { |
92 this.world = world; | |
0 | 93 shapes = new ShapeList; |
94 } | |
95 | |
96 void thrust() { | |
97 rBody.force += engineForce.rotate(rBody.angle); | |
98 } | |
99 | |
5 | 100 void turnLeft() { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
101 |
5 | 102 rBody.torque += bzCross(leftTurnPoint.rotate(rBody.angle), |
0 | 103 turnForce.rotate(rBody.angle)); |
5 | 104 } |
105 | |
106 void turnRight() { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
107 |
5 | 108 rBody.torque += bzCross(rightTurnPoint.rotate(rBody.angle), |
0 | 109 turnForce.rotate(rBody.angle)); |
110 } | |
6 | 111 |
14 | 112 void setPlanetGravity() { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
113 float minRadius = 0.1; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
114 float maxRadius = 10; |
26 | 115 float strength = 0.75; |
6 | 116 bzVec2 center = bzVec2(0,0); |
117 auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius); | |
118 world.addForce(attractor); | |
119 } | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
120 |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
121 void limitVelocity() { |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
122 float vx = rBody.linearVelocity.x; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
123 float vy = rBody.linearVelocity.y; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
124 float av = rBody.angularVelocity; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
125 rBody.linearVelocity.x = bzClamp(vx, -maxLinVel, maxLinVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
126 rBody.linearVelocity.y = bzClamp(vy, -maxLinVel, maxLinVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
127 rBody.angularVelocity = bzClamp(av, -maxAngVel, maxAngVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
128 } |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
129 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
130 void updateState() { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
131 state.velocity = rBody.linearVelocity; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
132 state.speed = state.velocity.length; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
133 state.position = rBody.position; |
17 | 134 state.forward = engineForce.rotate(rBody.angle); |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
135 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
136 |
22 | 137 void calcRadius() { |
138 for (bzShape s = rBody.shapeList; s; s = s.next) { | |
139 if(s.sweepRadius > state.radius) { | |
140 state.radius = s.sweepRadius; | |
141 } | |
142 } | |
143 } | |
26 | 144 |
145 void explode() { | |
146 for(bzShape shape = rBody.shapeList; shape; shape = shape.next) { | |
147 auto bodyDef = new bzBodyDef; | |
148 switch(shape.type) { | |
149 case bzShapeType.POLYGON: | |
150 auto s = cast(bzPolygon) shape; | |
151 auto shapeDef = new bzPolyDef; | |
152 shapeDef.vertices = s.vertices; | |
153 shapeDef.density = s.density; | |
154 bodyDef.position = s.worldCenter; | |
155 bodyDef.angle = randomRange(-PI, PI); | |
156 bodyDef.allowFreeze = false; | |
157 bodyDef.allowSleep = false; | |
158 auto shrapnel = world.createBody(bodyDef); | |
159 shrapnel.createShape(shapeDef); | |
160 bzMassData massData; | |
161 massData.mass = 5.0f; | |
162 shrapnel.setMass(massData); | |
163 float x = randomRange(-300, 300); | |
164 float y = randomRange(-300, 300); | |
165 shrapnel.linearVelocity = bzVec2(x, y); | |
166 break; | |
167 default: | |
168 break; | |
169 } | |
170 } | |
171 } | |
172 | |
0 | 173 } |