[159eef8] | 1 | package com.example.helloandroid;
|
---|
| 2 |
|
---|
[9ef6f68] | 3 | import java.util.ArrayList;
|
---|
| 4 |
|
---|
[61c4fbc] | 5 | import android.graphics.Canvas;
|
---|
[95509e1] | 6 | import android.graphics.Color;
|
---|
[61c4fbc] | 7 | import android.graphics.Paint;
|
---|
| 8 | import android.graphics.Path;
|
---|
| 9 | import android.util.Log;
|
---|
| 10 |
|
---|
[159eef8] | 11 | public 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;
|
---|
| 23 |
|
---|
[730d808] | 24 | private boolean done;
|
---|
[0986844] | 25 |
|
---|
[96857ee] | 26 | /* Optimising: pre-calculate paths */
|
---|
[159eef8] | 27 | public Fleet(Planet source, Planet destination, int numShips, int faction) {
|
---|
[730d808] | 28 | done = false;
|
---|
| 29 |
|
---|
[95509e1] | 30 | source.setNumShips(source.getNumShips()-numShips);
|
---|
| 31 |
|
---|
[96857ee] | 32 | //Calculate initial coordinates and direction
|
---|
[5a03a06] | 33 | if((destination.getX() - source.getX()) != 0){
|
---|
[0986844] | 34 | //line formula
|
---|
| 35 | slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
|
---|
[5a03a06] | 36 |
|
---|
[0986844] | 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 |
|
---|
[96857ee] | 49 | } else {
|
---|
[1291908] | 50 | if((destination.getY() - source.getY()) > 0 ){
|
---|
[1a91f0d] | 51 | direction = Math.PI/2;
|
---|
[5a03a06] | 52 | dblX = destination.getX();
|
---|
| 53 | dblY = source.getY() + source.radius + 10;
|
---|
[1a91f0d] | 54 | } else {
|
---|
| 55 | direction = 3*Math.PI/2;
|
---|
[5a03a06] | 56 | dblX = destination.getX();
|
---|
| 57 | dblY = source.getY() - source.radius - 10;
|
---|
[1a91f0d] | 58 | }
|
---|
[0986844] | 59 | xIntercept = destination.getX();
|
---|
[96857ee] | 60 | }
|
---|
[0986844] | 61 |
|
---|
[5a03a06] | 62 | x = (int)dblX;
|
---|
| 63 | y = (int)dblY;
|
---|
[0986844] | 64 |
|
---|
[159eef8] | 65 | this.numShips = numShips;
|
---|
| 66 | this.faction = faction;
|
---|
| 67 | this.destination = destination;
|
---|
[9ef6f68] | 68 | this.isNextToAPlanet = false;
|
---|
[730d808] | 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;
|
---|
[159eef8] | 76 | }
|
---|
[0986844] | 77 |
|
---|
| 78 |
|
---|
[96857ee] | 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 |
|
---|
[95509e1] | 149 | public void draw(Canvas canvas, Paint linePaint) {
|
---|
[61c4fbc] | 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 |
|
---|
[95509e1] | 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 |
|
---|
[61c4fbc] | 181 | canvas.drawPath(p, linePaint);
|
---|
[95509e1] | 182 |
|
---|
| 183 | linePaint.setColor(prevC);
|
---|
[61c4fbc] | 184 | }
|
---|
[96857ee] | 185 |
|
---|
[9ef6f68] | 186 | public void update(ArrayList<Planet> planets) {
|
---|
[6ebf60f] | 187 | int speed = 3; //pixels per move
|
---|
[730d808] | 188 | double distance, tangentAngle, angle;
|
---|
[6ebf60f] | 189 |
|
---|
[9ef6f68] | 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){
|
---|
[6ebf60f] | 198 | nearPlanet = p;
|
---|
[9ef6f68] | 199 | break;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | //if temp planet is not picked move along the direction by #speed
|
---|
[6ebf60f] | 203 | if(nearPlanet == null) {
|
---|
[9ef6f68] | 204 | dblY += (Math.sin(direction)*speed);
|
---|
| 205 | dblX += (Math.cos(direction)*speed);
|
---|
[0986844] | 206 |
|
---|
[9ef6f68] | 207 | x = (int)dblX;
|
---|
| 208 | y = (int)dblY;
|
---|
[61c4fbc] | 209 | }else {
|
---|
[6ebf60f] | 210 | if(nearPlanet == destination){
|
---|
| 211 | attack();
|
---|
| 212 | return;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[730d808] | 215 | //double radAngle = Math.atan(getSlope(destination.getX(), destination.getY(), dblX, dblY));
|
---|
[0986844] | 216 | //figure out which way to go clockwise or counter clockwise
|
---|
[730d808] | 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);
|
---|
[6ebf60f] | 222 |
|
---|
[0986844] | 223 | if (angle <= Math.PI/2)
|
---|
| 224 | angle = Math.PI - angle;
|
---|
[61c4fbc] | 225 |
|
---|
[730d808] | 226 | //double diff = direction-angle;
|
---|
[61c4fbc] | 227 |
|
---|
[730d808] | 228 | //if(diff > 0)
|
---|
| 229 | if(Math.abs(tangentAngle-direction) > Math.PI/2) {
|
---|
[0986844] | 230 | isClockwise = false;
|
---|
[730d808] | 231 | direction = tangentAngle-Math.PI;
|
---|
| 232 | }else {
|
---|
[0986844] | 233 | isClockwise = true;
|
---|
[730d808] | 234 | direction = tangentAngle;
|
---|
| 235 | }
|
---|
[6ebf60f] | 236 |
|
---|
[730d808] | 237 | Log.d("Galcon", "isClockwise: "+isClockwise);
|
---|
[61c4fbc] | 238 |
|
---|
[6ebf60f] | 239 | xIntercept = dblY - (dblX*Math.tan(direction));
|
---|
| 240 |
|
---|
| 241 | isNextToAPlanet = true;
|
---|
[9ef6f68] | 242 | //figure out which way to go clockwise or counter clockwise
|
---|
[61c4fbc] | 243 | /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
[9ef6f68] | 244 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
| 245 | if (angle <= Math.PI/2)
|
---|
[61c4fbc] | 246 | angle = Math.PI - angle;*/
|
---|
[9ef6f68] | 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
|
---|
[730d808] | 252 | //otherwise continue moving along the circumference
|
---|
[0986844] | 253 |
|
---|
[730d808] | 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){
|
---|
[6ebf60f] | 257 | dblY += (Math.sin(direction)*speed);
|
---|
| 258 | dblX += (Math.cos(direction)*speed);
|
---|
| 259 |
|
---|
| 260 | x = (int)dblX;
|
---|
| 261 | y = (int)dblY;
|
---|
[0986844] | 262 |
|
---|
[6ebf60f] | 263 | if (getDistanceBetween(dblX,dblY,nearPlanet.getX(),nearPlanet.getY()) > nearPlanet.radius){
|
---|
| 264 | isNextToAPlanet = false;
|
---|
| 265 | nearPlanet = null;
|
---|
| 266 | }
|
---|
[0986844] | 267 | } else {
|
---|
[730d808] | 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 |
|
---|
[0986844] | 275 | if(isClockwise){
|
---|
[730d808] | 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;
|
---|
[0986844] | 279 | } else {
|
---|
[730d808] | 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;
|
---|
[0986844] | 283 | }
|
---|
[730d808] | 284 |
|
---|
| 285 | if(!done)
|
---|
| 286 | Log.d("Galcon", "new: ("+dblX+", "+dblY+")");
|
---|
| 287 |
|
---|
| 288 | done = true;
|
---|
| 289 |
|
---|
[6ebf60f] | 290 | x = (int)dblX;
|
---|
| 291 | y = (int)dblY;
|
---|
| 292 | xIntercept = dblY - (dblX*Math.tan(direction));
|
---|
[0986844] | 293 | }
|
---|
[9ef6f68] | 294 | }
|
---|
[159eef8] | 295 | }
|
---|
[0986844] | 296 |
|
---|
[159eef8] | 297 | // attack the destination planet
|
---|
[5a03a06] | 298 | //after the method is called the fleet needs to removed
|
---|
[159eef8] | 299 | public void attack() {
|
---|
[6ebf60f] | 300 | if(this.faction == destination.getFaction())
|
---|
| 301 | destination.setNumShips(destination.getNumShips() + numShips);
|
---|
| 302 | else if(numShips <= destination.getNumShips()){
|
---|
[1291908] | 303 | destination.setNumShips(destination.getNumShips() - numShips);
|
---|
| 304 | } else {
|
---|
| 305 | destination.setNumShips(numShips - destination.getNumShips());
|
---|
| 306 | destination.setFaction(this.faction);
|
---|
[1a91f0d] | 307 | }
|
---|
[6ebf60f] | 308 | this.numShips = 0;
|
---|
[159eef8] | 309 | }
|
---|
[0986844] | 310 |
|
---|
[9ef6f68] | 311 | //helper functions
|
---|
[61c4fbc] | 312 | private double getDistanceBetween(double x1, double y1, double x2, double y2) {
|
---|
[9ef6f68] | 313 | return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
|
---|
| 314 | }
|
---|
[0986844] | 315 |
|
---|
[61c4fbc] | 316 | private double getSlope(double x1, double y1, double x2, double y2) {
|
---|
| 317 | return ((y2 - y1)/(double)(x2 - x1));
|
---|
[9ef6f68] | 318 | }
|
---|
[6ebf60f] | 319 | } |
---|