[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;
|
---|
| 18 | private Planet destination;
|
---|
| 19 | private int numShips;
|
---|
| 20 | private int faction;
|
---|
[9ef6f68] | 21 | private boolean isNextToAPlanet;
|
---|
[0986844] | 22 | private boolean dirChanged, isClockwise;
|
---|
| 23 |
|
---|
[96857ee] | 24 | /* Optimising: 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;
|
---|
[61c4fbc] | 65 | dirChanged = false;
|
---|
[159eef8] | 66 | }
|
---|
[0986844] | 67 |
|
---|
| 68 |
|
---|
[96857ee] | 69 | public int getX() {
|
---|
| 70 | return x;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | public void setX(int x) {
|
---|
| 76 | this.x = x;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | public int getY() {
|
---|
| 82 | return y;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | public void setY(int y) {
|
---|
| 88 | this.y = y;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | public double getDirection() {
|
---|
| 94 | return direction;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | public void setDirection(double direction) {
|
---|
| 100 | this.direction = direction;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | public Planet getDestination() {
|
---|
| 106 | return destination;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | public void setDestination(Planet destination) {
|
---|
| 112 | this.destination = destination;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | public int getNumShips() {
|
---|
| 118 | return numShips;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | public void setNumShips(int numShips) {
|
---|
| 124 | this.numShips = numShips;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | public int getFaction() {
|
---|
| 130 | return faction;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | public void setFaction(int faction) {
|
---|
| 136 | this.faction = faction;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[95509e1] | 139 | public void draw(Canvas canvas, Paint linePaint) {
|
---|
[61c4fbc] | 140 | Path p = new Path();
|
---|
| 141 |
|
---|
| 142 | p.moveTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
|
---|
| 143 | p.lineTo((float)(x+5*Math.cos(direction-Math.PI/2)), (float)(y+5*Math.sin(direction-Math.PI/2)));
|
---|
| 144 | p.lineTo((float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction)));
|
---|
| 145 | p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
|
---|
| 146 |
|
---|
[95509e1] | 147 | int c, prevC = linePaint.getColor();
|
---|
| 148 |
|
---|
| 149 | switch(faction) {
|
---|
| 150 | case 0:
|
---|
| 151 | c = Color.argb(255, 100, 100, 100);
|
---|
| 152 | break;
|
---|
| 153 | case 1:
|
---|
| 154 | c = Color.argb(255, 255, 0, 0);
|
---|
| 155 | break;
|
---|
| 156 | case 2:
|
---|
| 157 | c = Color.argb(255, 0, 180, 0);
|
---|
| 158 | break;
|
---|
| 159 | case 3:
|
---|
| 160 | c = Color.argb(255, 0, 0, 255);
|
---|
| 161 | break;
|
---|
| 162 | case 4:
|
---|
| 163 | c = Color.argb(255, 150, 150, 0);
|
---|
| 164 | break;
|
---|
| 165 | default:
|
---|
| 166 | c = prevC;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | linePaint.setColor(c);
|
---|
| 170 |
|
---|
[61c4fbc] | 171 | canvas.drawPath(p, linePaint);
|
---|
[95509e1] | 172 |
|
---|
| 173 | linePaint.setColor(prevC);
|
---|
[61c4fbc] | 174 | }
|
---|
[96857ee] | 175 |
|
---|
[9ef6f68] | 176 | public void update(ArrayList<Planet> planets) {
|
---|
[5a03a06] | 177 | int speed = 1; //pixels per move
|
---|
[0986844] | 178 | double distance, tangentDirection, angle;
|
---|
[9ef6f68] | 179 | Planet temp = null;
|
---|
| 180 | //is the ship going around a planet already
|
---|
| 181 | if(!isNextToAPlanet){
|
---|
| 182 | /*looks through all the planets to figure out if
|
---|
| 183 | the ship's path is about to intersect a planet*/
|
---|
| 184 | for(Planet p: planets){
|
---|
| 185 | //if two point of intersection are found save planet
|
---|
| 186 | distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY());
|
---|
| 187 | if(distance <= p.radius){
|
---|
| 188 | temp = p;
|
---|
| 189 | break;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | //if temp planet is not picked move along the direction by #speed
|
---|
[61c4fbc] | 193 | if(temp == null || dirChanged) {
|
---|
[9ef6f68] | 194 | dblY += (Math.sin(direction)*speed);
|
---|
| 195 | dblX += (Math.cos(direction)*speed);
|
---|
[0986844] | 196 |
|
---|
[9ef6f68] | 197 | x = (int)dblX;
|
---|
| 198 | y = (int)dblY;
|
---|
[61c4fbc] | 199 | }else {
|
---|
| 200 | double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY));
|
---|
[0986844] | 201 | //figure out which way to go clockwise or counter clockwise
|
---|
| 202 | tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
| 203 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
| 204 | if (angle <= Math.PI/2)
|
---|
| 205 | angle = Math.PI - angle;
|
---|
[61c4fbc] | 206 |
|
---|
| 207 | if(dblX < temp.getX())
|
---|
| 208 | radAngle += Math.PI;
|
---|
| 209 |
|
---|
[0986844] | 210 | angle = radAngle + Math.PI/2;
|
---|
[61c4fbc] | 211 |
|
---|
| 212 | double diff = direction-angle;
|
---|
| 213 |
|
---|
[0986844] | 214 | if(diff > 0)
|
---|
| 215 | isClockwise = false;
|
---|
| 216 | else
|
---|
| 217 | isClockwise = true;
|
---|
[61c4fbc] | 218 | if(Math.abs(diff)>Math.PI/2)
|
---|
| 219 | direction = angle-Math.PI;
|
---|
| 220 | else
|
---|
| 221 | direction = angle;
|
---|
[0986844] | 222 |
|
---|
[61c4fbc] | 223 | dirChanged = true;
|
---|
| 224 |
|
---|
[9ef6f68] | 225 | //figure out which way to go clockwise or counter clockwise
|
---|
[61c4fbc] | 226 | /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
[9ef6f68] | 227 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
| 228 | if (angle <= Math.PI/2)
|
---|
[61c4fbc] | 229 | angle = Math.PI - angle;*/
|
---|
[9ef6f68] | 230 | //get next point and the direction and set it
|
---|
| 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 circumferenceds4
|
---|
[0986844] | 236 |
|
---|
| 237 |
|
---|
| 238 | if(true){
|
---|
| 239 |
|
---|
| 240 | } else {
|
---|
| 241 | angle = speed/temp.radius;
|
---|
| 242 | if(isClockwise){
|
---|
| 243 | dblX = ((Math.cos(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getX()));
|
---|
| 244 | dblY = ((Math.sin(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getY()));
|
---|
| 245 | direction = direction - (Math.PI/2);
|
---|
| 246 | } else {
|
---|
| 247 | dblX = ((Math.cos(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getX()));
|
---|
| 248 | dblY = ((Math.sin(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getY()));
|
---|
| 249 | direction = direction + (Math.PI/2);
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
[9ef6f68] | 252 | }
|
---|
[159eef8] | 253 | }
|
---|
[0986844] | 254 |
|
---|
[159eef8] | 255 | // attack the destination planet
|
---|
[5a03a06] | 256 | //after the method is called the fleet needs to removed
|
---|
[159eef8] | 257 | public void attack() {
|
---|
[1291908] | 258 | if(numShips <= destination.getNumShips()){
|
---|
| 259 | destination.setNumShips(destination.getNumShips() - numShips);
|
---|
| 260 | } else {
|
---|
| 261 | destination.setNumShips(numShips - destination.getNumShips());
|
---|
| 262 | destination.setFaction(this.faction);
|
---|
[1a91f0d] | 263 | }
|
---|
[159eef8] | 264 | }
|
---|
[0986844] | 265 |
|
---|
[9ef6f68] | 266 | //helper functions
|
---|
[61c4fbc] | 267 | private double getDistanceBetween(double x1, double y1, double x2, double y2) {
|
---|
[9ef6f68] | 268 | return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
|
---|
| 269 | }
|
---|
[0986844] | 270 |
|
---|
[61c4fbc] | 271 | private double getSlope(double x1, double y1, double x2, double y2) {
|
---|
| 272 | return ((y2 - y1)/(double)(x2 - x1));
|
---|
[9ef6f68] | 273 | }
|
---|
[159eef8] | 274 | }
|
---|