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