source: galcon-client/src/com/example/helloandroid/Fleet.java@ 61c4fbc

Last change on this file since 61c4fbc was 61c4fbc, checked in by dportnoy <devnull@…>, 15 years ago

The player can now send fleets by clicking on the source and destination planets.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1package com.example.helloandroid;
2
3import java.util.ArrayList;
4
5import android.graphics.Canvas;
6import android.graphics.Paint;
7import android.graphics.Path;
8import android.util.Log;
9
10public class Fleet {
11 private int x;
12 private int y;
13 private double dblX;
14 private double dblY;
15 private double slope, xIntercept;
16 private double direction;
17 private Planet destination;
18 private int numShips;
19 private int faction;
20 private boolean isNextToAPlanet;
21 private boolean dirChanged;
22
23 /* Optimising: pre-calculate paths */
24 public Fleet(Planet source, Planet destination, int numShips, int faction) {
25 //Calculate initial coordinates and direction
26 if((destination.getX() - source.getX()) != 0){
27 //line formula
28 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
29
30 xIntercept = destination.getY() - (slope*destination.getX());
31
32 //direction
33 direction = Math.atan(slope);
34
35 //coordinates for all 4 coordinates
36 if((destination.getX() - source.getX()) < 0 )
37 direction += Math.PI;
38
39 dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX()));
40 dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY()));
41
42 } else {
43 if((destination.getY() - source.getY()) > 0 ){
44 direction = Math.PI/2;
45 dblX = destination.getX();
46 dblY = source.getY() + source.radius + 10;
47 } else {
48 direction = 3*Math.PI/2;
49 dblX = destination.getX();
50 dblY = source.getY() - source.radius - 10;
51 }
52 }
53
54 x = (int)dblX;
55 y = (int)dblY;
56
57 this.numShips = numShips;
58 this.faction = faction;
59 this.destination = destination;
60 this.isNextToAPlanet = false;
61 dirChanged = false;
62 }
63
64
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
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 }
148
149 public void update(ArrayList<Planet> planets) {
150 int speed = 1; //pixels per move
151 double distance, tangentDirection;
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
166 if(temp == null || dirChanged) {
167 dblY += (Math.sin(direction)*speed);
168 dblX += (Math.cos(direction)*speed);
169
170 x = (int)dblX;
171 y = (int)dblY;
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
189 //figure out which way to go clockwise or counter clockwise
190 /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
191 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
192 if (angle <= Math.PI/2)
193 angle = Math.PI - angle;*/
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
204 }
205
206 // attack the destination planet
207 //after the method is called the fleet needs to removed
208 public void attack() {
209 if(numShips <= destination.getNumShips()){
210 destination.setNumShips(destination.getNumShips() - numShips);
211 } else {
212 destination.setNumShips(numShips - destination.getNumShips());
213 destination.setFaction(this.faction);
214 }
215 }
216
217 //helper functions
218 private double getDistanceBetween(double x1, double y1, double x2, double y2) {
219 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
220 }
221
222 private double getSlope(double x1, double y1, double x2, double y2) {
223 return ((y2 - y1)/(double)(x2 - x1));
224 }
225}
Note: See TracBrowser for help on using the repository browser.