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
RevLine 
[159eef8]1package com.example.helloandroid;
2
[9ef6f68]3import java.util.ArrayList;
4
[61c4fbc]5import android.graphics.Canvas;
[95509e1]6import android.graphics.Color;
[61c4fbc]7import android.graphics.Paint;
8import android.graphics.Path;
9import android.util.Log;
10
[159eef8]11public class Fleet {
[96857ee]12 private int x;
13 private int y;
[5a03a06]14 private double dblX;
15 private double dblY;
[96857ee]16 private double slope, xIntercept;
17 private double direction;
[6ebf60f]18 private Planet destination, nearPlanet;
[96857ee]19 private int numShips;
20 private int faction;
[9ef6f68]21 private boolean isNextToAPlanet;
[6ebf60f]22 private boolean isClockwise;
[0986844]23
[69f6f01]24 /* Optimizing: pre-calculate paths */
[159eef8]25 public Fleet(Planet source, Planet destination, int numShips, int faction) {
[95509e1]26 source.setNumShips(source.getNumShips()-numShips);
27
[96857ee]28 //Calculate initial coordinates and direction
[5a03a06]29 if((destination.getX() - source.getX()) != 0){
[0986844]30 //line formula
31 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
[5a03a06]32
[0986844]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
[96857ee]45 } else {
[1291908]46 if((destination.getY() - source.getY()) > 0 ){
[1a91f0d]47 direction = Math.PI/2;
[5a03a06]48 dblX = destination.getX();
49 dblY = source.getY() + source.radius + 10;
[1a91f0d]50 } else {
51 direction = 3*Math.PI/2;
[5a03a06]52 dblX = destination.getX();
53 dblY = source.getY() - source.radius - 10;
[1a91f0d]54 }
[0986844]55 xIntercept = destination.getX();
[96857ee]56 }
[0986844]57
[5a03a06]58 x = (int)dblX;
59 y = (int)dblY;
[0986844]60
[159eef8]61 this.numShips = numShips;
62 this.faction = faction;
63 this.destination = destination;
[9ef6f68]64 this.isNextToAPlanet = false;
[730d808]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;
[159eef8]72 }
[0986844]73
74
[96857ee]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
[95509e1]145 public void draw(Canvas canvas, Paint linePaint) {
[61c4fbc]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
[95509e1]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
[61c4fbc]177 canvas.drawPath(p, linePaint);
[95509e1]178
179 linePaint.setColor(prevC);
[61c4fbc]180 }
[96857ee]181
[9ef6f68]182 public void update(ArrayList<Planet> planets) {
[6ebf60f]183 int speed = 3; //pixels per move
[730d808]184 double distance, tangentAngle, angle;
[6ebf60f]185
[9ef6f68]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){
[6ebf60f]194 nearPlanet = p;
[9ef6f68]195 break;
196 }
197 }
198 //if temp planet is not picked move along the direction by #speed
[6ebf60f]199 if(nearPlanet == null) {
[9ef6f68]200 dblY += (Math.sin(direction)*speed);
201 dblX += (Math.cos(direction)*speed);
[0986844]202
[9ef6f68]203 x = (int)dblX;
204 y = (int)dblY;
[61c4fbc]205 }else {
[6ebf60f]206 if(nearPlanet == destination){
207 attack();
208 return;
209 }
210
[647a312]211 tangentAngle = Math.atan(getSlope(nearPlanet.getX(),nearPlanet.getY(),dblX,dblY));
[6ebf60f]212
[647a312]213 if(dblX < nearPlanet.getX())
214 tangentAngle += Math.PI;
[61c4fbc]215
[647a312]216 tangentAngle += Math.PI/2;
[61c4fbc]217
[730d808]218 if(Math.abs(tangentAngle-direction) > Math.PI/2) {
[0986844]219 isClockwise = false;
[730d808]220 direction = tangentAngle-Math.PI;
221 }else {
[0986844]222 isClockwise = true;
[730d808]223 direction = tangentAngle;
224 }
[6ebf60f]225
[730d808]226 Log.d("Galcon", "isClockwise: "+isClockwise);
[61c4fbc]227
[6ebf60f]228 xIntercept = dblY - (dblX*Math.tan(direction));
229
230 isNextToAPlanet = true;
[9ef6f68]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
[730d808]235 //otherwise continue moving along the circumference
[0986844]236
[730d808]237 double dist = Math.abs(destination.getY() - (Math.tan(direction)*destination.getX()) - xIntercept)/(double)Math.sqrt(Math.pow(direction,2)+1);
[012e702]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){
[6ebf60f]244 dblY += (Math.sin(direction)*speed);
245 dblX += (Math.cos(direction)*speed);
246
247 x = (int)dblX;
248 y = (int)dblY;
[0986844]249
[6ebf60f]250 if (getDistanceBetween(dblX,dblY,nearPlanet.getX(),nearPlanet.getY()) > nearPlanet.radius){
251 isNextToAPlanet = false;
252 nearPlanet = null;
[012e702]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;
[6ebf60f]264 }
[0986844]265 } else {
[04a9a00]266 angle = speed/(double)nearPlanet.radius;
[730d808]267
[0986844]268 if(isClockwise){
[730d808]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;
[0986844]272 } else {
[730d808]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;
[0986844]276 }
[730d808]277
[6ebf60f]278 x = (int)dblX;
279 y = (int)dblY;
280 xIntercept = dblY - (dblX*Math.tan(direction));
[0986844]281 }
[9ef6f68]282 }
[159eef8]283 }
[0986844]284
[159eef8]285 // attack the destination planet
[5a03a06]286 //after the method is called the fleet needs to removed
[159eef8]287 public void attack() {
[6ebf60f]288 if(this.faction == destination.getFaction())
289 destination.setNumShips(destination.getNumShips() + numShips);
290 else if(numShips <= destination.getNumShips()){
[1291908]291 destination.setNumShips(destination.getNumShips() - numShips);
292 } else {
293 destination.setNumShips(numShips - destination.getNumShips());
294 destination.setFaction(this.faction);
[1a91f0d]295 }
[6ebf60f]296 this.numShips = 0;
[159eef8]297 }
[0986844]298
[9ef6f68]299 //helper functions
[61c4fbc]300 private double getDistanceBetween(double x1, double y1, double x2, double y2) {
[9ef6f68]301 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
302 }
[0986844]303
[61c4fbc]304 private double getSlope(double x1, double y1, double x2, double y2) {
305 return ((y2 - y1)/(double)(x2 - x1));
[9ef6f68]306 }
[6ebf60f]307}
Note: See TracBrowser for help on using the repository browser.