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