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