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

Last change on this file was 9d030cb, checked in by dportnoy <devnull@…>, 14 years ago

Merge with 422c8ef5772e988da2065d1c4b95e6d6ab6192e7

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