source: galcon-client/src/com/example/helloandroid/Fleet.java@ 9ef6f68

Last change on this file since 9ef6f68 was 9ef6f68, checked in by Zero Cool <devnull@…>, 14 years ago

Chugging along with path finding

  • Property mode set to 100644
File size: 4.3 KB
Line 
1package com.example.helloandroid;
2
3import java.util.ArrayList;
4
5public class Fleet {
6 private int x;
7 private int y;
8 private double dblX;
9 private double dblY;
10 private double slope, xIntercept;
11 private double direction;
12 private Planet destination;
13 private int numShips;
14 private int faction;
15 private boolean isNextToAPlanet;
16
17 /* Optimising: pre-calculate paths */
18 public Fleet(Planet source, Planet destination, int numShips, int faction) {
19 //Calculate initial coordinates and direction
20 if((destination.getX() - source.getX()) != 0){
21 //line formula
22 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
23 xIntercept = destination.getY() - (slope*destination.getX());
24
25 //direction
26 direction = Math.atan(slope);
27
28 //coordinates for all 4 coordinates
29 if((destination.getX() - source.getX()) < 0 )
30 direction += Math.PI;
31
32 dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX()));
33 dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY()));
34
35 } else {
36 if((destination.getY() - source.getY()) > 0 ){
37 direction = Math.PI/2;
38 dblX = destination.getX();
39 dblY = source.getY() + source.radius + 10;
40 } else {
41 direction = 3*Math.PI/2;
42 dblX = destination.getX();
43 dblY = source.getY() - source.radius - 10;
44 }
45 }
46
47 x = (int)dblX;
48 y = (int)dblY;
49
50 this.numShips = numShips;
51 this.faction = faction;
52 this.destination = destination;
53 this.isNextToAPlanet = false;
54 }
55
56
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
129 public void update(ArrayList<Planet> planets) {
130 int speed = 1; //pixels per move
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
170 }
171
172 // attack the destination planet
173 //after the method is called the fleet needs to removed
174 public void attack() {
175 if(numShips <= destination.getNumShips()){
176 destination.setNumShips(destination.getNumShips() - numShips);
177 } else {
178 destination.setNumShips(numShips - destination.getNumShips());
179 destination.setFaction(this.faction);
180 }
181 }
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 }
193}
Note: See TracBrowser for help on using the repository browser.