source: galcon-client/src/com/example/helloandroid/Fleet.java@ 730d808

Last change on this file since 730d808 was 730d808, checked in by dportnoy <devnull@…>, 14 years ago

Currently debugging fleet pathing.

  • Property mode set to 100644
File size: 8.4 KB
Line 
1package com.example.helloandroid;
2
3import java.util.ArrayList;
4
5import android.graphics.Canvas;
6import android.graphics.Color;
7import android.graphics.Paint;
8import android.graphics.Path;
9import android.util.Log;
10
11public class Fleet {
12 private int x;
13 private int y;
14 private double dblX;
15 private double dblY;
16 private double slope, xIntercept;
17 private double direction;
18 private Planet destination, nearPlanet;
19 private int numShips;
20 private int faction;
21 private boolean isNextToAPlanet;
22 private boolean isClockwise;
23
24 private boolean done;
25
26 /* Optimising: pre-calculate paths */
27 public Fleet(Planet source, Planet destination, int numShips, int faction) {
28 done = false;
29
30 source.setNumShips(source.getNumShips()-numShips);
31
32 //Calculate initial coordinates and direction
33 if((destination.getX() - source.getX()) != 0){
34 //line formula
35 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
36
37 xIntercept = destination.getY() - (slope*destination.getX());
38
39 //direction
40 direction = Math.atan(slope);
41
42 //coordinates for all 4 coordinates
43 if((destination.getX() - source.getX()) < 0 )
44 direction += Math.PI;
45
46 dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX()));
47 dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY()));
48
49 } else {
50 if((destination.getY() - source.getY()) > 0 ){
51 direction = Math.PI/2;
52 dblX = destination.getX();
53 dblY = source.getY() + source.radius + 10;
54 } else {
55 direction = 3*Math.PI/2;
56 dblX = destination.getX();
57 dblY = source.getY() - source.radius - 10;
58 }
59 xIntercept = destination.getX();
60 }
61
62 x = (int)dblX;
63 y = (int)dblY;
64
65 this.numShips = numShips;
66 this.faction = faction;
67 this.destination = destination;
68 this.isNextToAPlanet = false;
69
70 Log.d("Galcon", "Source: ("+source.getX()+", "+source.getY()+")");
71 Log.d("Galcon", "Destination: ("+destination.getX()+", "+destination.getY()+")");
72 Log.d("Galcon", "Initial direction: "+direction);
73
74 if(direction < 0)
75 direction += Math.PI*2;
76 }
77
78
79 public int getX() {
80 return x;
81 }
82
83
84
85 public void setX(int x) {
86 this.x = x;
87 }
88
89
90
91 public int getY() {
92 return y;
93 }
94
95
96
97 public void setY(int y) {
98 this.y = y;
99 }
100
101
102
103 public double getDirection() {
104 return direction;
105 }
106
107
108
109 public void setDirection(double direction) {
110 this.direction = direction;
111 }
112
113
114
115 public Planet getDestination() {
116 return destination;
117 }
118
119
120
121 public void setDestination(Planet destination) {
122 this.destination = destination;
123 }
124
125
126
127 public int getNumShips() {
128 return numShips;
129 }
130
131
132
133 public void setNumShips(int numShips) {
134 this.numShips = numShips;
135 }
136
137
138
139 public int getFaction() {
140 return faction;
141 }
142
143
144
145 public void setFaction(int faction) {
146 this.faction = faction;
147 }
148
149 public void draw(Canvas canvas, Paint linePaint) {
150 Path p = new Path();
151
152 p.moveTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
153 p.lineTo((float)(x+5*Math.cos(direction-Math.PI/2)), (float)(y+5*Math.sin(direction-Math.PI/2)));
154 p.lineTo((float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction)));
155 p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
156
157 int c, prevC = linePaint.getColor();
158
159 switch(faction) {
160 case 0:
161 c = Color.argb(255, 100, 100, 100);
162 break;
163 case 1:
164 c = Color.argb(255, 255, 0, 0);
165 break;
166 case 2:
167 c = Color.argb(255, 0, 180, 0);
168 break;
169 case 3:
170 c = Color.argb(255, 0, 0, 255);
171 break;
172 case 4:
173 c = Color.argb(255, 150, 150, 0);
174 break;
175 default:
176 c = prevC;
177 }
178
179 linePaint.setColor(c);
180
181 canvas.drawPath(p, linePaint);
182
183 linePaint.setColor(prevC);
184 }
185
186 public void update(ArrayList<Planet> planets) {
187 int speed = 3; //pixels per move
188 double distance, tangentAngle, angle;
189
190 //is the ship going around a planet already
191 if(!isNextToAPlanet){
192 /*looks through all the planets to figure out if
193 the ship's path is about to intersect a planet*/
194 for(Planet p: planets){
195 //if two point of intersection are found save planet
196 distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY());
197 if(distance <= p.radius){
198 nearPlanet = p;
199 break;
200 }
201 }
202 //if temp planet is not picked move along the direction by #speed
203 if(nearPlanet == null) {
204 dblY += (Math.sin(direction)*speed);
205 dblX += (Math.cos(direction)*speed);
206
207 x = (int)dblX;
208 y = (int)dblY;
209 }else {
210 if(nearPlanet == destination){
211 attack();
212 return;
213 }
214
215 //double radAngle = Math.atan(getSlope(destination.getX(), destination.getY(), dblX, dblY));
216 //figure out which way to go clockwise or counter clockwise
217 tangentAngle = (Math.atan(getSlope(nearPlanet.getX(),nearPlanet.getY(),dblX,dblY))) + (Math.PI/2);
218 angle = Math.atan((Math.tan(tangentAngle) - Math.tan(direction))/(1 + Math.tan(tangentAngle)*Math.tan(direction)));
219
220 Log.d("Galcon", "tangentAngle: "+tangentAngle);
221 Log.d("Galcon", "direction: "+direction+", angle: "+angle);
222
223 if (angle <= Math.PI/2)
224 angle = Math.PI - angle;
225
226 //double diff = direction-angle;
227
228 //if(diff > 0)
229 if(Math.abs(tangentAngle-direction) > Math.PI/2) {
230 isClockwise = false;
231 direction = tangentAngle-Math.PI;
232 }else {
233 isClockwise = true;
234 direction = tangentAngle;
235 }
236
237 Log.d("Galcon", "isClockwise: "+isClockwise);
238
239 xIntercept = dblY - (dblX*Math.tan(direction));
240
241 isNextToAPlanet = true;
242 //figure out which way to go clockwise or counter clockwise
243 /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
244 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
245 if (angle <= Math.PI/2)
246 angle = Math.PI - angle;*/
247 //get next point and the direction and set it
248 }
249 } else {
250 //can you reach the center of the planet by following this direction
251 //if so set isNextToAPlanet to false and move
252 //otherwise continue moving along the circumference
253
254 double dist = Math.abs(destination.getY() - (Math.tan(direction)*destination.getX()) - xIntercept)/(double)Math.sqrt(Math.pow(direction,2)+1);
255 //Log.d("Galcon", "dist: "+dist);
256 if(dist < 5 && dist == 1){
257 dblY += (Math.sin(direction)*speed);
258 dblX += (Math.cos(direction)*speed);
259
260 x = (int)dblX;
261 y = (int)dblY;
262
263 if (getDistanceBetween(dblX,dblY,nearPlanet.getX(),nearPlanet.getY()) > nearPlanet.radius){
264 isNextToAPlanet = false;
265 nearPlanet = null;
266 }
267 } else {
268 angle = speed/(double)nearPlanet.radius*.1;
269
270 if(!done) {
271 Log.d("Galcon", "direction: "+direction+", angle: "+angle);
272 Log.d("Galcon", "original: ("+dblX+", "+dblY+")");
273 }
274
275 if(isClockwise){
276 dblX = Math.cos(direction - (Math.PI/2) + angle)*nearPlanet.radius + nearPlanet.getX();
277 dblY = Math.sin(direction - (Math.PI/2) + angle)*nearPlanet.radius + nearPlanet.getY();
278 direction = direction + angle;
279 } else {
280 dblX = Math.cos(direction + (Math.PI/2) - angle)*nearPlanet.radius + nearPlanet.getX();
281 dblY = Math.sin(direction + (Math.PI/2) - angle)*nearPlanet.radius + nearPlanet.getY();
282 direction = direction - angle;
283 }
284
285 if(!done)
286 Log.d("Galcon", "new: ("+dblX+", "+dblY+")");
287
288 done = true;
289
290 x = (int)dblX;
291 y = (int)dblY;
292 xIntercept = dblY - (dblX*Math.tan(direction));
293 }
294 }
295 }
296
297 // attack the destination planet
298 //after the method is called the fleet needs to removed
299 public void attack() {
300 if(this.faction == destination.getFaction())
301 destination.setNumShips(destination.getNumShips() + numShips);
302 else if(numShips <= destination.getNumShips()){
303 destination.setNumShips(destination.getNumShips() - numShips);
304 } else {
305 destination.setNumShips(numShips - destination.getNumShips());
306 destination.setFaction(this.faction);
307 }
308 this.numShips = 0;
309 }
310
311 //helper functions
312 private double getDistanceBetween(double x1, double y1, double x2, double y2) {
313 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
314 }
315
316 private double getSlope(double x1, double y1, double x2, double y2) {
317 return ((y2 - y1)/(double)(x2 - x1));
318 }
319}
Note: See TracBrowser for help on using the repository browser.