- Timestamp:
- May 30, 2010, 9:32:02 PM (14 years ago)
- Branches:
- master
- Children:
- c27abf4
- Parents:
- b6a9b5f
- Location:
- src/com/example/helloandroid
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Game.java
rb6a9b5f r5053d90 6 6 import android.view.Menu; 7 7 import android.view.MenuItem; 8 import android.view.MotionEvent; 8 9 import android.view.Window; 9 10 import android.widget.TextView; -
src/com/example/helloandroid/GameView.java
rb6a9b5f r5053d90 17 17 import android.util.Log; 18 18 import android.view.KeyEvent; 19 import android.view.MotionEvent; 19 20 import android.view.SurfaceHolder; 20 21 import android.view.SurfaceView; … … 170 171 private double mY; 171 172 172 private ArrayList<Planet> planets; 173 public Object planetsLock; 174 175 public ArrayList<Planet> planets; 176 public Planet planetSelected; 173 177 174 178 public DrawingThread(SurfaceHolder surfaceHolder, Context context, … … 215 219 mHeading = 0; 216 220 mEngineFiring = true; 221 222 planetsLock = new Object(); 217 223 218 224 planets = new ArrayList<Planet>(); 225 planetSelected = null; 219 226 } 220 227 … … 463 470 } 464 471 } 465 472 466 473 /* Callback invoked when the surface dimensions change. */ 467 474 public void setSurfaceSize(int width, int height) { … … 475 482 Random rand = new Random(); 476 483 477 for(int x=0; x<15; x++) { 478 Planet p = new Planet(rand.nextInt(45)+5, rand.nextInt(mCanvasWidth), rand.nextInt(mCanvasHeight)); 479 480 if(Planet.collisionDetected(p, planets)) { 481 x--; 482 }else if(p.getX()-p.getRadius() < 0 || mCanvasWidth<=p.getX()+p.getRadius() || 483 p.getY()-p.getRadius() < 0 || mCanvasHeight<=p.getY()+p.getRadius()) { 484 x--; 485 }else { 486 p.setNumShips(rand.nextInt(150)); 487 p.setFaction(rand.nextInt(5)); 488 planets.add(p); 489 } 484 synchronized(planetsLock) { 485 for(int x=0; x<15; x++) { 486 Planet p = new Planet(rand.nextInt(45)+5, rand.nextInt(mCanvasWidth), rand.nextInt(mCanvasHeight)); 487 488 if(Planet.collisionDetected(p, planets)) { 489 x--; 490 }else if(p.getX()-p.getRadius() < 0 || mCanvasWidth<=p.getX()+p.getRadius() || 491 p.getY()-p.getRadius() < 0 || mCanvasHeight<=p.getY()+p.getRadius()) { 492 x--; 493 }else { 494 p.setNumShips(rand.nextInt(150)); 495 p.setFaction(rand.nextInt(5)); 496 planets.add(p); 497 } 498 } 490 499 } 491 500 … … 569 578 */ 570 579 private void doDraw(Canvas canvas) { 571 // Draw the background image. Operations on the Canvas accumulate 572 // so this is like clearing the screen. 580 canvas.drawBitmap(mBackgroundImage, 0, 0, null); 581 synchronized(planetsLock) { 582 for(Planet p : planets) { 583 p.draw(canvas, mLinePaint, mTextPaint); 584 } 585 } 573 586 574 //Log.i("Gencon", "doDraw called"); 587 if(planetSelected != null) { 588 planetSelected.drawSelectionCircle(canvas); 589 } 575 590 576 canvas.drawBitmap(mBackgroundImage, 0, 0, null);577 for(Planet p : planets) {578 p.draw(canvas, mLinePaint, mTextPaint);579 }580 581 591 int yTop = mCanvasHeight - ((int) mY + mLanderHeight / 2); 582 592 int xLeft = (int) mX - mLanderWidth / 2; … … 585 595 canvas.save(); 586 596 canvas.rotate((float)mHeading, (float)mX, mCanvasHeight - (float)mY); 587 if (mMode == STATE_LOSE) { 588 //mCrashedImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); 589 //mCrashedImage.draw(canvas); 590 } else if (mEngineFiring) { 591 //mFiringImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); 592 //mFiringImage.draw(canvas); 593 } else { 594 mLanderImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); 595 mLanderImage.draw(canvas); 596 } 597 598 mLanderImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); 599 mLanderImage.draw(canvas); 600 597 601 canvas.restore(); 598 602 } … … 655 659 } 656 660 657 for(Planet p : planets) { 658 p.update(); 661 synchronized(planetsLock) { 662 for(Planet p : planets) { 663 p.update(); 664 } 659 665 } 660 666 … … 740 746 } 741 747 748 @Override public boolean onTouchEvent(MotionEvent event) { 749 Log.i("Gencon", "Detected touch event"); 750 751 synchronized(thread.planetsLock) { 752 if(thread.planetSelected != null) { 753 thread.planetSelected.unselect(); 754 thread.planetSelected = null; 755 } 756 757 for(Planet p : thread.planets) { 758 if(p.contains((int)event.getX(), (int)event.getY())) { 759 p.select(); 760 thread.planetSelected = p; 761 break; 762 } 763 } 764 } 765 766 return true; 767 } 768 742 769 /** 743 770 * Fetches the animation thread corresponding to this LunarView. -
src/com/example/helloandroid/Planet.java
rb6a9b5f r5053d90 3 3 import java.util.ArrayList; 4 4 5 import android.graphics.Bitmap; 5 6 import android.graphics.Canvas; 6 7 import android.graphics.Color; 7 8 import android.graphics.Paint; 8 9 import android.graphics.Paint.FontMetrics; 9 import android.util.Log;10 10 11 11 public class Planet { … … 16 16 int faction; 17 17 int numShips; 18 18 boolean selected; 19 private Bitmap selection; 20 19 21 public Planet(int radius, int x, int y) { 20 22 this.radius = radius; … … 23 25 faction = 0; 24 26 numShips = 0; 27 selected = false; 25 28 26 29 regenRate = 0; //change this to some expression / funcion call 30 31 int size = getRadius()+15; 32 33 selection = Bitmap.createBitmap(size*2, size*2, Bitmap.Config.ARGB_8888); 34 Canvas c = new Canvas(selection); 35 c.drawColor(Color.argb(0, 0, 0, 0)); 36 37 Paint p = new Paint(); 38 p.setAntiAlias(true); 39 40 p.setColor(Color.argb(255, 255, 255, 255)); 41 c.drawCircle(size, size, getRadius()+11, p); 42 43 p.setColor(Color.argb(255, 100, 100, 100)); 44 c.drawCircle(size, size, getRadius()+5, p); 45 46 for(int i=0; i<size*2; i++) { 47 for(int j=0; j<size*2; j++) { 48 if(selection.getPixel(i,j) == Color.argb(255, 100, 100, 100)) 49 selection.setPixel(i, j, Color.argb(0, 0, 0, 0)); 50 } 51 } 27 52 } 28 53 … … 43 68 } 44 69 70 public boolean isSelected() { 71 return selected; 72 } 73 45 74 public void setNumShips(int num) { 46 75 numShips = num; … … 51 80 } 52 81 82 public void select() { 83 selected = true; 84 } 85 86 public void unselect() { 87 selected = false; 88 } 89 53 90 public void draw(Canvas canvas, Paint linePaint, Paint textPaint) { 54 91 FontMetrics metrics = textPaint.getFontMetrics(); 55 92 56 93 int c, prevC = linePaint.getColor(); 94 95 if(selected) { 96 //drawSelectionCircle(canvas); 97 } 57 98 58 99 switch(faction) { … … 84 125 } 85 126 127 public void drawSelectionCircle(Canvas canvas) { 128 int size = getRadius()+15; 129 130 canvas.drawBitmap(selection, x-size, y-size, null); 131 } 132 86 133 public void update() { 87 134 if(faction != 0) … … 92 139 public void sendFleet(Planet p, int numShips) { 93 140 141 } 142 143 public boolean contains(int x, int y) { 144 double dist = Math.sqrt(Math.pow(this.x-x, 2) + Math.pow(this.y-y, 2)); 145 146 return dist <= this.radius; 94 147 } 95 148
Note:
See TracChangeset
for help on using the changeset viewer.