Mercurial > projects > openmelee
annotate ship.d @ 13:e1004697cae1
tracking
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Mon, 23 Mar 2009 08:02:23 -0400 |
parents | 2ecd16840900 |
children | af1e8620f027 |
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 */ | |
2 | 31 module openmelee.ship; |
0 | 32 |
33 import tango.io.Stdout: Stdout; | |
34 | |
35 import tango.util.container.LinkedList : LinkedList; | |
36 import blaze.dynamics.bzBody : bzBody; | |
37 import blaze.collision.shapes.bzShape : bzShape; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
38 import blaze.common.bzMath: bzVec2, bzCross, bzClamp; |
6 | 39 import blaze.bzWorld : bzWorld; |
40 import blaze.dynamics.forces.bzAttractor: bzAttractor; | |
0 | 41 |
42 alias LinkedList!(bzShape) ShapeList; | |
43 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
44 struct State |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
45 { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
46 bzVec2 position; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
47 bzVec2 velocity; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
48 bzVec2 up; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
49 bzVec2 side; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
50 bzVec2 forward; |
12 | 51 bzVec2 target; |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
52 float speed = 0; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
53 float maxForce = 0; |
13 | 54 bool turn; |
55 | |
56 float enemyAngle; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
57 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
58 bzVec2 predictFuturePosition(float dt) { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
59 return (position + velocity * dt); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
60 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
61 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
62 |
0 | 63 abstract class Ship |
64 { | |
6 | 65 bzWorld world; |
0 | 66 bzBody rBody; |
67 ShapeList shapes; | |
68 bzVec2 engineForce; | |
69 bzVec2 turnForce; | |
70 bzVec2 leftTurnPoint; | |
71 bzVec2 rightTurnPoint; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
72 State state; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
73 |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
74 float maxLinVel = 100; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
75 float maxAngVel = 2; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
76 |
6 | 77 this(bzWorld world) { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
78 |
6 | 79 this.world = world; |
0 | 80 shapes = new ShapeList; |
81 } | |
82 | |
83 void thrust() { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
84 |
0 | 85 rBody.force += engineForce.rotate(rBody.angle); |
86 } | |
87 | |
5 | 88 void turnLeft() { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
89 |
5 | 90 rBody.torque += bzCross(leftTurnPoint.rotate(rBody.angle), |
0 | 91 turnForce.rotate(rBody.angle)); |
5 | 92 } |
93 | |
94 void turnRight() { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
95 |
5 | 96 rBody.torque += bzCross(rightTurnPoint.rotate(rBody.angle), |
0 | 97 turnForce.rotate(rBody.angle)); |
98 } | |
6 | 99 |
100 void setGravity() { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
101 float minRadius = 0.1; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
102 float maxRadius = 10; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
103 float strength = 3.5; |
6 | 104 bzVec2 center = bzVec2(0,0); |
105 auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius); | |
106 world.addForce(attractor); | |
107 } | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
108 |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
109 void limitVelocity() { |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
110 float vx = rBody.linearVelocity.x; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
111 float vy = rBody.linearVelocity.y; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
112 float av = rBody.angularVelocity; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
113 rBody.linearVelocity.x = bzClamp(vx, -maxLinVel, maxLinVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
114 rBody.linearVelocity.y = bzClamp(vy, -maxLinVel, maxLinVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
115 rBody.angularVelocity = bzClamp(av, -maxAngVel, maxAngVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
116 } |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
117 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
118 void updateState() { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
119 state.velocity = rBody.linearVelocity; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
120 state.speed = state.velocity.length; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
121 state.position = rBody.position; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
122 bzVec2 heading = engineForce.rotate(rBody.angle); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
123 heading.normalize(); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
124 state.forward = heading; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
125 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
126 |
0 | 127 } |