Changeset 61c4fbc in galcon-client for src/com/example/helloandroid
- Timestamp:
- Jun 5, 2010, 5:08:40 PM (14 years ago)
- Branches:
- master
- Children:
- 0986844, 95509e1
- Parents:
- 9ef6f68
- Location:
- src/com/example/helloandroid
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
r9ef6f68 r61c4fbc 2 2 3 3 import java.util.ArrayList; 4 5 import android.graphics.Canvas; 6 import android.graphics.Paint; 7 import android.graphics.Path; 8 import android.util.Log; 4 9 5 10 public class Fleet { … … 14 19 private int faction; 15 20 private boolean isNextToAPlanet; 21 private boolean dirChanged; 16 22 17 23 /* Optimising: pre-calculate paths */ … … 21 27 //line formula 22 28 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY()); 29 23 30 xIntercept = destination.getY() - (slope*destination.getX()); 24 31 … … 52 59 this.destination = destination; 53 60 this.isNextToAPlanet = false; 61 dirChanged = false; 54 62 } 55 63 … … 125 133 } 126 134 127 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 } 128 148 129 149 public void update(ArrayList<Planet> planets) { 130 150 int speed = 1; //pixels per move 131 double distance, tangentDirection , angle;151 double distance, tangentDirection; 132 152 Planet temp = null; 133 153 //is the ship going around a planet already … … 144 164 } 145 165 //if temp planet is not picked move along the direction by #speed 146 if(temp == null ){166 if(temp == null || dirChanged) { 147 167 dblY += (Math.sin(direction)*speed); 148 168 dblX += (Math.cos(direction)*speed); … … 150 170 x = (int)dblX; 151 171 y = (int)dblY; 152 } else 153 //otherwise 154 { 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 155 189 //figure out which way to go clockwise or counter clockwise 156 tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);190 /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2); 157 191 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction))); 158 192 if (angle <= Math.PI/2) 159 angle = Math.PI - angle; 193 angle = Math.PI - angle;*/ 160 194 //get next point and the direction and set it 161 195 } … … 182 216 183 217 //helper functions 184 private double getDistanceBetween(double x1, double y1, double x2, double y2) 185 { 218 private double getDistanceBetween(double x1, double y1, double x2, double y2) { 186 219 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2)); 187 220 } 188 221 189 private double getSlope(double x1, double y1, double x2, double y2) 190 { 191 return ((y1 - y1)/(x2 - x1)); 222 private double getSlope(double x1, double y1, double x2, double y2) { 223 return ((y2 - y1)/(double)(x2 - x1)); 192 224 } 193 225 } -
src/com/example/helloandroid/GameView.java
r9ef6f68 r61c4fbc 171 171 private double mY; 172 172 173 public Object planetsLock ;173 public Object planetsLock, fleetsLock; 174 174 175 175 public ArrayList<Planet> planets; 176 public ArrayList<Fleet> fleets; 176 177 public Planet planetSelected; 177 178 … … 221 222 222 223 planetsLock = new Object(); 224 fleetsLock = new Object(); 223 225 224 226 planets = new ArrayList<Planet>(); 225 227 planetSelected = null; 228 229 fleets = new ArrayList<Fleet>(); 226 230 } 227 231 … … 589 593 } 590 594 595 synchronized(fleetsLock) { 596 for(Fleet f : fleets) { 597 f.draw(canvas, mLinePaint); 598 } 599 } 600 591 601 int yTop = mCanvasHeight - ((int) mY + mLanderHeight / 2); 592 602 int xLeft = (int) mX - mLanderWidth / 2; … … 665 675 } 666 676 677 synchronized(fleetsLock) { 678 for(Fleet f : fleets) { 679 f.update(planets); 680 } 681 } 682 667 683 double dxOld = mDX; 668 684 double dyOld = mDY; … … 734 750 Log.i("Gencon", "Detected touch event"); 735 751 752 if(event.getAction() != MotionEvent.ACTION_DOWN) 753 return true; 754 736 755 synchronized(thread.planetsLock) { 737 756 if(thread.planetSelected != null) { 757 Planet target = null; 758 759 for(Planet p : thread.planets) { 760 if(p.contains((int)event.getX(), (int)event.getY())) { 761 target = p; 762 break; 763 } 764 } 765 766 if(target != null) { 767 synchronized(thread.fleetsLock) { 768 Fleet f = new Fleet(thread.planetSelected, target, 1, 1); 769 thread.fleets.add(f); 770 } 771 } 772 738 773 thread.planetSelected.unselect(); 739 774 thread.planetSelected = null; 775 }else { 776 for(Planet p : thread.planets) { 777 if(p.contains((int)event.getX(), (int)event.getY())) { 778 p.select(); 779 thread.planetSelected = p; 780 break; 781 } 782 } 740 783 } 741 742 for(Planet p : thread.planets) {743 if(p.contains((int)event.getX(), (int)event.getY())) {744 p.select();745 thread.planetSelected = p;746 break;747 }748 }749 784 } 750 785 -
src/com/example/helloandroid/Planet.java
r9ef6f68 r61c4fbc 101 101 break; 102 102 case 2: 103 c = Color.argb(255, 0, 255, 0);103 c = Color.argb(255, 0, 180, 0); 104 104 break; 105 105 case 3: … … 107 107 break; 108 108 case 4: 109 c = Color.argb(255, 255, 255, 0);109 c = Color.argb(255, 150, 150, 0); 110 110 break; 111 111 default:
Note:
See TracChangeset
for help on using the changeset viewer.