[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;
|
---|
[61c4fbc] | 22 | private boolean dirChanged;
|
---|
[159eef8] | 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){
|
---|
[96857ee] | 30 | //line formula
|
---|
[9ef6f68] | 31 | slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
|
---|
[61c4fbc] | 32 |
|
---|
[1291908] | 33 | xIntercept = destination.getY() - (slope*destination.getX());
|
---|
[96857ee] | 34 |
|
---|
| 35 | //direction
|
---|
[9ef6f68] | 36 | direction = Math.atan(slope);
|
---|
[96857ee] | 37 |
|
---|
| 38 | //coordinates for all 4 coordinates
|
---|
[5a03a06] | 39 | if((destination.getX() - source.getX()) < 0 )
|
---|
[1a91f0d] | 40 | direction += Math.PI;
|
---|
[159eef8] | 41 |
|
---|
[5a03a06] | 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 | }
|
---|
[96857ee] | 55 | }
|
---|
[1a91f0d] | 56 |
|
---|
[5a03a06] | 57 | x = (int)dblX;
|
---|
| 58 | y = (int)dblY;
|
---|
| 59 |
|
---|
[159eef8] | 60 | this.numShips = numShips;
|
---|
| 61 | this.faction = faction;
|
---|
| 62 | this.destination = destination;
|
---|
[9ef6f68] | 63 | this.isNextToAPlanet = false;
|
---|
[61c4fbc] | 64 | dirChanged = false;
|
---|
[159eef8] | 65 | }
|
---|
| 66 |
|
---|
| 67 |
|
---|
[96857ee] | 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 |
|
---|
[95509e1] | 138 | public void draw(Canvas canvas, Paint linePaint) {
|
---|
[61c4fbc] | 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 |
|
---|
[95509e1] | 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 |
|
---|
[61c4fbc] | 170 | canvas.drawPath(p, linePaint);
|
---|
[95509e1] | 171 |
|
---|
| 172 | linePaint.setColor(prevC);
|
---|
[61c4fbc] | 173 | }
|
---|
[96857ee] | 174 |
|
---|
[9ef6f68] | 175 | public void update(ArrayList<Planet> planets) {
|
---|
[5a03a06] | 176 | int speed = 1; //pixels per move
|
---|
[61c4fbc] | 177 | double distance, tangentDirection;
|
---|
[9ef6f68] | 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
|
---|
[61c4fbc] | 192 | if(temp == null || dirChanged) {
|
---|
[9ef6f68] | 193 | dblY += (Math.sin(direction)*speed);
|
---|
| 194 | dblX += (Math.cos(direction)*speed);
|
---|
| 195 |
|
---|
| 196 | x = (int)dblX;
|
---|
| 197 | y = (int)dblY;
|
---|
[61c4fbc] | 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 |
|
---|
[9ef6f68] | 215 | //figure out which way to go clockwise or counter clockwise
|
---|
[61c4fbc] | 216 | /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
[9ef6f68] | 217 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
| 218 | if (angle <= Math.PI/2)
|
---|
[61c4fbc] | 219 | angle = Math.PI - angle;*/
|
---|
[9ef6f68] | 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 |
|
---|
[159eef8] | 230 | }
|
---|
| 231 |
|
---|
| 232 | // attack the destination planet
|
---|
[5a03a06] | 233 | //after the method is called the fleet needs to removed
|
---|
[159eef8] | 234 | public void attack() {
|
---|
[1291908] | 235 | if(numShips <= destination.getNumShips()){
|
---|
| 236 | destination.setNumShips(destination.getNumShips() - numShips);
|
---|
| 237 | } else {
|
---|
| 238 | destination.setNumShips(numShips - destination.getNumShips());
|
---|
| 239 | destination.setFaction(this.faction);
|
---|
[1a91f0d] | 240 | }
|
---|
[159eef8] | 241 | }
|
---|
[9ef6f68] | 242 |
|
---|
| 243 | //helper functions
|
---|
[61c4fbc] | 244 | private double getDistanceBetween(double x1, double y1, double x2, double y2) {
|
---|
[9ef6f68] | 245 | return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[61c4fbc] | 248 | private double getSlope(double x1, double y1, double x2, double y2) {
|
---|
| 249 | return ((y2 - y1)/(double)(x2 - x1));
|
---|
[9ef6f68] | 250 | }
|
---|
[159eef8] | 251 | }
|
---|