Changeset 5053d90 in galcon-client for src/com/example/helloandroid/GameView.java
- Timestamp:
- May 30, 2010, 9:32:02 PM (15 years ago)
- Branches:
- master
- Children:
- c27abf4
- Parents:
- b6a9b5f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.
Note:
See TracChangeset
for help on using the changeset viewer.