[159eef8] | 1 | package com.example.helloandroid;
|
---|
| 2 |
|
---|
[9ef6f68] | 3 | import java.util.ArrayList;
|
---|
| 4 |
|
---|
[61c4fbc] | 5 | import android.graphics.Canvas;
|
---|
| 6 | import android.graphics.Paint;
|
---|
| 7 | import android.graphics.Path;
|
---|
| 8 | import android.util.Log;
|
---|
| 9 |
|
---|
[159eef8] | 10 | public class Fleet {
|
---|
[96857ee] | 11 | private int x;
|
---|
| 12 | private int y;
|
---|
[5a03a06] | 13 | private double dblX;
|
---|
| 14 | private double dblY;
|
---|
[96857ee] | 15 | private double slope, xIntercept;
|
---|
| 16 | private double direction;
|
---|
| 17 | private Planet destination;
|
---|
| 18 | private int numShips;
|
---|
| 19 | private int faction;
|
---|
[9ef6f68] | 20 | private boolean isNextToAPlanet;
|
---|
[0986844] | 21 | private boolean dirChanged, isClockwise;
|
---|
| 22 |
|
---|
[96857ee] | 23 | /* Optimising: pre-calculate paths */
|
---|
[159eef8] | 24 | public Fleet(Planet source, Planet destination, int numShips, int faction) {
|
---|
[96857ee] | 25 | //Calculate initial coordinates and direction
|
---|
[5a03a06] | 26 | if((destination.getX() - source.getX()) != 0){
|
---|
[0986844] | 27 | //line formula
|
---|
| 28 | slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
|
---|
[5a03a06] | 29 |
|
---|
[0986844] | 30 | xIntercept = destination.getY() - (slope*destination.getX());
|
---|
| 31 |
|
---|
| 32 | //direction
|
---|
| 33 | direction = Math.atan(slope);
|
---|
| 34 |
|
---|
| 35 | //coordinates for all 4 coordinates
|
---|
| 36 | if((destination.getX() - source.getX()) < 0 )
|
---|
| 37 | direction += Math.PI;
|
---|
| 38 |
|
---|
| 39 | dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX()));
|
---|
| 40 | dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY()));
|
---|
| 41 |
|
---|
[96857ee] | 42 | } else {
|
---|
[1291908] | 43 | if((destination.getY() - source.getY()) > 0 ){
|
---|
[1a91f0d] | 44 | direction = Math.PI/2;
|
---|
[5a03a06] | 45 | dblX = destination.getX();
|
---|
| 46 | dblY = source.getY() + source.radius + 10;
|
---|
[1a91f0d] | 47 | } else {
|
---|
| 48 | direction = 3*Math.PI/2;
|
---|
[5a03a06] | 49 | dblX = destination.getX();
|
---|
| 50 | dblY = source.getY() - source.radius - 10;
|
---|
[1a91f0d] | 51 | }
|
---|
[0986844] | 52 | xIntercept = destination.getX();
|
---|
[96857ee] | 53 | }
|
---|
[0986844] | 54 |
|
---|
[5a03a06] | 55 | x = (int)dblX;
|
---|
| 56 | y = (int)dblY;
|
---|
[0986844] | 57 |
|
---|
[159eef8] | 58 | this.numShips = numShips;
|
---|
| 59 | this.faction = faction;
|
---|
| 60 | this.destination = destination;
|
---|
[9ef6f68] | 61 | this.isNextToAPlanet = false;
|
---|
[61c4fbc] | 62 | dirChanged = false;
|
---|
[159eef8] | 63 | }
|
---|
[0986844] | 64 |
|
---|
| 65 |
|
---|
[96857ee] | 66 | public int getX() {
|
---|
| 67 | return x;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | public void setX(int x) {
|
---|
| 73 | this.x = x;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | public int getY() {
|
---|
| 79 | return y;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | public void setY(int y) {
|
---|
| 85 | this.y = y;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | public double getDirection() {
|
---|
| 91 | return direction;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | public void setDirection(double direction) {
|
---|
| 97 | this.direction = direction;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | public Planet getDestination() {
|
---|
| 103 | return destination;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | public void setDestination(Planet destination) {
|
---|
| 109 | this.destination = destination;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | public int getNumShips() {
|
---|
| 115 | return numShips;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | public void setNumShips(int numShips) {
|
---|
| 121 | this.numShips = numShips;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | public int getFaction() {
|
---|
| 127 | return faction;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | public void setFaction(int faction) {
|
---|
| 133 | this.faction = faction;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[61c4fbc] | 136 | public void draw(Canvas canvas, Paint linePaint) {
|
---|
| 137 | //Log.i("Gencon", "direction: "+direction);
|
---|
| 138 | canvas.drawLine((float)x, (float)y, (float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction)), linePaint);
|
---|
| 139 |
|
---|
| 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 |
|
---|
[0986844] | 147 |
|
---|
[61c4fbc] | 148 | canvas.drawPath(p, linePaint);
|
---|
| 149 | }
|
---|
[96857ee] | 150 |
|
---|
[9ef6f68] | 151 | public void update(ArrayList<Planet> planets) {
|
---|
[5a03a06] | 152 | int speed = 1; //pixels per move
|
---|
[0986844] | 153 | double distance, tangentDirection, angle;
|
---|
[9ef6f68] | 154 | Planet temp = null;
|
---|
| 155 | //is the ship going around a planet already
|
---|
| 156 | if(!isNextToAPlanet){
|
---|
| 157 | /*looks through all the planets to figure out if
|
---|
| 158 | the ship's path is about to intersect a planet*/
|
---|
| 159 | for(Planet p: planets){
|
---|
| 160 | //if two point of intersection are found save planet
|
---|
| 161 | distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY());
|
---|
| 162 | if(distance <= p.radius){
|
---|
| 163 | temp = p;
|
---|
| 164 | break;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | //if temp planet is not picked move along the direction by #speed
|
---|
[61c4fbc] | 168 | if(temp == null || dirChanged) {
|
---|
[9ef6f68] | 169 | dblY += (Math.sin(direction)*speed);
|
---|
| 170 | dblX += (Math.cos(direction)*speed);
|
---|
[0986844] | 171 |
|
---|
[9ef6f68] | 172 | x = (int)dblX;
|
---|
| 173 | y = (int)dblY;
|
---|
[61c4fbc] | 174 | }else {
|
---|
| 175 | double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY));
|
---|
[0986844] | 176 | //figure out which way to go clockwise or counter clockwise
|
---|
| 177 | tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
| 178 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
| 179 | if (angle <= Math.PI/2)
|
---|
| 180 | angle = Math.PI - angle;
|
---|
[61c4fbc] | 181 |
|
---|
| 182 | if(dblX < temp.getX())
|
---|
| 183 | radAngle += Math.PI;
|
---|
| 184 |
|
---|
[0986844] | 185 | angle = radAngle + Math.PI/2;
|
---|
[61c4fbc] | 186 |
|
---|
| 187 | double diff = direction-angle;
|
---|
| 188 |
|
---|
[0986844] | 189 | if(diff > 0)
|
---|
| 190 | isClockwise = false;
|
---|
| 191 | else
|
---|
| 192 | isClockwise = true;
|
---|
[61c4fbc] | 193 | if(Math.abs(diff)>Math.PI/2)
|
---|
| 194 | direction = angle-Math.PI;
|
---|
| 195 | else
|
---|
| 196 | direction = angle;
|
---|
[0986844] | 197 |
|
---|
[61c4fbc] | 198 | dirChanged = true;
|
---|
| 199 |
|
---|
[9ef6f68] | 200 | //figure out which way to go clockwise or counter clockwise
|
---|
[61c4fbc] | 201 | /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
[9ef6f68] | 202 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
| 203 | if (angle <= Math.PI/2)
|
---|
[61c4fbc] | 204 | angle = Math.PI - angle;*/
|
---|
[9ef6f68] | 205 | //get next point and the direction and set it
|
---|
| 206 | }
|
---|
| 207 | } else {
|
---|
| 208 | //can you reach the center of the planet by following this direction
|
---|
| 209 | //if so set isNextToAPlanet to false and move
|
---|
| 210 | //otherwise continue moving along the circumferenceds4
|
---|
[0986844] | 211 |
|
---|
| 212 |
|
---|
| 213 | if(true){
|
---|
| 214 |
|
---|
| 215 | } else {
|
---|
| 216 | angle = speed/temp.radius;
|
---|
| 217 | if(isClockwise){
|
---|
| 218 | dblX = ((Math.cos(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getX()));
|
---|
| 219 | dblY = ((Math.sin(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getY()));
|
---|
| 220 | direction = direction - (Math.PI/2);
|
---|
| 221 | } else {
|
---|
| 222 | dblX = ((Math.cos(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getX()));
|
---|
| 223 | dblY = ((Math.sin(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getY()));
|
---|
| 224 | direction = direction + (Math.PI/2);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
[9ef6f68] | 227 | }
|
---|
[159eef8] | 228 | }
|
---|
[0986844] | 229 |
|
---|
[159eef8] | 230 | // attack the destination planet
|
---|
[5a03a06] | 231 | //after the method is called the fleet needs to removed
|
---|
[159eef8] | 232 | public void attack() {
|
---|
[1291908] | 233 | if(numShips <= destination.getNumShips()){
|
---|
| 234 | destination.setNumShips(destination.getNumShips() - numShips);
|
---|
| 235 | } else {
|
---|
| 236 | destination.setNumShips(numShips - destination.getNumShips());
|
---|
| 237 | destination.setFaction(this.faction);
|
---|
[1a91f0d] | 238 | }
|
---|
[159eef8] | 239 | }
|
---|
[0986844] | 240 |
|
---|
[9ef6f68] | 241 | //helper functions
|
---|
[61c4fbc] | 242 | private double getDistanceBetween(double x1, double y1, double x2, double y2) {
|
---|
[9ef6f68] | 243 | return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
|
---|
| 244 | }
|
---|
[0986844] | 245 |
|
---|
[61c4fbc] | 246 | private double getSlope(double x1, double y1, double x2, double y2) {
|
---|
| 247 | return ((y2 - y1)/(double)(x2 - x1));
|
---|
[9ef6f68] | 248 | }
|
---|
[159eef8] | 249 | }
|
---|