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