Changeset 8a4e64f in galcon-client


Ignore:
Timestamp:
May 25, 2010, 2:46:37 PM (14 years ago)
Author:
dportnoy <devnull@…>
Branches:
master
Children:
3e9f39e
Parents:
9b87c8d
Message:

Generated planets are guaranteed not to collide or or be off the screen. Number of ships on each planet is also displayed.

Location:
src/com/example/helloandroid
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/com/example/helloandroid/GameView.java

    r9b87c8d r8a4e64f  
    9999        private int mCanvasWidth = 1;
    100100
    101         /** What to draw for the Lander when it has crashed */
    102         private Drawable mCrashedImage;
    103 
    104101        /** Default is MEDIUM. */
    105102        private int mDifficulty;
     
    112109        private boolean mEngineFiring;
    113110
    114         /** What to draw for the Lander when the engine is firing */
    115         private Drawable mFiringImage;
    116 
    117111        /** Fuel remaining */
    118112        private double mFuel;
     
    152146
    153147        /** Paint to draw the lines on screen. */
    154         private Paint mLinePaint;
     148        private Paint mLinePaint, mTextPaint;
    155149
    156150        /** "Bad" speed-too-high variant of the line color. */
     
    188182            // cache handles to our key sprites & other drawables
    189183            mLanderImage = context.getResources().getDrawable(R.drawable.lander_plain);
    190             mFiringImage = context.getResources().getDrawable(R.drawable.lander_firing);
    191             mCrashedImage = context.getResources().getDrawable(R.drawable.lander_crashed);
    192184
    193185            // load background image as a Bitmap instead of a Drawable b/c
     
    207199            mLinePaintBad.setAntiAlias(true);
    208200            mLinePaintBad.setARGB(255, 120, 180, 0);
     201           
     202            mTextPaint = new Paint();
     203            mTextPaint.setAntiAlias(true);
     204            mTextPaint.setARGB(255, 255, 0, 0);
    209205
    210206            mWinsInARow = 0;
     
    422418             * thread, which updates the user-text View.
    423419             */
     420                boolean test = true;
     421                if(test)
     422                        return;
    424423            synchronized (mSurfaceHolder) {
    425424                mMode = mode;
     
    475474                Random rand = new Random();
    476475
    477                 for(int x=0; x<6; x++) {
    478                         planets.add(new Planet(rand.nextInt(45)+5, rand.nextInt(mCanvasWidth), rand.nextInt(mCanvasHeight)));
     476                for(int x=0; x<10; x++) {
     477                        Planet p = new Planet(rand.nextInt(45)+5, rand.nextInt(mCanvasWidth), rand.nextInt(mCanvasHeight));
     478                        p.setNumShips(rand.nextInt(150));
     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                                planets.add(p);
     487                        }
    479488                }
    480489                       
     
    519528                    return true;
    520529                } else if (mMode == STATE_RUNNING) {
    521                     // center/space -> fire
    522                     if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
    523                             || keyCode == KeyEvent.KEYCODE_SPACE) {
    524                         setFiring(true);
    525                         return true;
    526                         // left/q -> left
    527                     } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT
    528                             || keyCode == KeyEvent.KEYCODE_Q) {
    529                         mRotating = -1;
    530                         return true;
    531                         // right/w -> right
    532                     } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
    533                             || keyCode == KeyEvent.KEYCODE_W) {
    534                         mRotating = 1;
    535                         return true;
    536                         // up -> pause
    537                     } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
    538                         pause();
    539                         return true;
    540                     }
     530                    return true;
    541531                }
    542532
     
    559549                    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
    560550                            || keyCode == KeyEvent.KEYCODE_SPACE) {
    561                         setFiring(false);
    562551                        handled = true;
    563552                    } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT
     
    565554                            || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
    566555                            || keyCode == KeyEvent.KEYCODE_W) {
    567                         mRotating = 0;
    568556                        handled = true;
    569557                    }
     
    585573            for(Planet p : planets) {
    586574                canvas.drawCircle(p.getX(), p.getY(), p.getRadius(), mLinePaint);
     575                canvas.drawText(Integer.toString(p.getNumShips()), p.getX(), p.getY(), mTextPaint);
    587576            }
    588577           
     
    592581            // Draw the ship with its current rotation
    593582            canvas.save();
    594             canvas.rotate((float) mHeading, (float) mX, mCanvasHeight
    595                     - (float) mY);
     583            canvas.rotate((float)mHeading, (float)mX, mCanvasHeight - (float)mY);
    596584            if (mMode == STATE_LOSE) {
    597                 mCrashedImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop
    598                         + mLanderHeight);
    599                 mCrashedImage.draw(canvas);
     585                //mCrashedImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight);
     586                //mCrashedImage.draw(canvas);
    600587            } else if (mEngineFiring) {
    601                 mFiringImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop
    602                         + mLanderHeight);
    603                 mFiringImage.draw(canvas);
     588                //mFiringImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight);
     589                //mFiringImage.draw(canvas);
    604590            } else {
    605                 mLanderImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop
    606                         + mLanderHeight);
     591                mLanderImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight);
    607592                mLanderImage.draw(canvas);
    608593            }
     
    665650            }
    666651
     652            for(Planet p : planets) {
     653                p.update();
     654            }
     655           
    667656            double dxOld = mDX;
    668657            double dyOld = mDY;
  • src/com/example/helloandroid/Planet.java

    r9b87c8d r8a4e64f  
    11package com.example.helloandroid;
     2
     3import java.util.ArrayList;
    24
    35public class Planet {
     
    3133        }
    3234       
     35        public int getNumShips() {
     36                return numShips;
     37        }
     38       
     39        public void setNumShips(int num) {
     40                numShips = num;
     41        }
     42       
    3343        public void update() {
    3444                //regen ships if not owned by faction 0
     45                numShips++;
    3546        }
    3647       
     
    3849               
    3950        }
     51       
     52        public boolean collides(Planet p) {
     53                double dist = Math.sqrt(Math.pow(this.x-p.x, 2) + Math.pow(this.y-p.y, 2));
     54               
     55                return dist <= this.radius + p.radius;
     56        }
     57       
     58        public static boolean collisionDetected(Planet p, ArrayList<Planet> curPlanets) {
     59                for(Planet p2 : curPlanets) {
     60                        if(p.collides(p2))
     61                                return true;
     62                }
     63               
     64                return false;
     65        }
    4066}
Note: See TracChangeset for help on using the changeset viewer.