Changeset 5053d90 in galcon-client for src/com/example


Ignore:
Timestamp:
May 30, 2010, 9:32:02 PM (14 years ago)
Author:
dportnoy <devnull@…>
Branches:
master
Children:
c27abf4
Parents:
b6a9b5f
Message:

The game now responds to touchscreen events and the player can select planets.

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

Legend:

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

    rb6a9b5f r5053d90  
    66import android.view.Menu;
    77import android.view.MenuItem;
     8import android.view.MotionEvent;
    89import android.view.Window;
    910import android.widget.TextView;
  • src/com/example/helloandroid/GameView.java

    rb6a9b5f r5053d90  
    1717import android.util.Log;
    1818import android.view.KeyEvent;
     19import android.view.MotionEvent;
    1920import android.view.SurfaceHolder;
    2021import android.view.SurfaceView;
     
    170171        private double mY;
    171172       
    172         private ArrayList<Planet> planets;
     173        public Object planetsLock;
     174       
     175        public ArrayList<Planet> planets;
     176        public Planet planetSelected;
    173177
    174178        public DrawingThread(SurfaceHolder surfaceHolder, Context context,
     
    215219            mHeading = 0;
    216220            mEngineFiring = true;
     221       
     222            planetsLock = new Object();
    217223           
    218224            planets = new ArrayList<Planet>();
     225            planetSelected = null;
    219226        }
    220227
     
    463470            }
    464471        }
    465 
     472       
    466473        /* Callback invoked when the surface dimensions change. */
    467474        public void setSurfaceSize(int width, int height) {
     
    475482                Random rand = new Random();
    476483
    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                        }
    490499                }
    491500                       
     
    569578         */
    570579        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                }
    573586           
    574                 //Log.i("Gencon", "doDraw called");
     587                if(planetSelected != null) {
     588                        planetSelected.drawSelectionCircle(canvas);
     589                }
    575590               
    576                 canvas.drawBitmap(mBackgroundImage, 0, 0, null);
    577             for(Planet p : planets) {
    578                 p.draw(canvas, mLinePaint, mTextPaint);
    579             }
    580            
    581591            int yTop = mCanvasHeight - ((int) mY + mLanderHeight / 2);
    582592            int xLeft = (int) mX - mLanderWidth / 2;
     
    585595            canvas.save();
    586596            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               
    597601            canvas.restore();
    598602        }
     
    655659            }
    656660
    657             for(Planet p : planets) {
    658                 p.update();
     661            synchronized(planetsLock) {
     662                for(Planet p : planets) {
     663                        p.update();
     664                }
    659665            }
    660666           
     
    740746    }
    741747
     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   
    742769    /**
    743770     * Fetches the animation thread corresponding to this LunarView.
  • src/com/example/helloandroid/Planet.java

    rb6a9b5f r5053d90  
    33import java.util.ArrayList;
    44
     5import android.graphics.Bitmap;
    56import android.graphics.Canvas;
    67import android.graphics.Color;
    78import android.graphics.Paint;
    89import android.graphics.Paint.FontMetrics;
    9 import android.util.Log;
    1010
    1111public class Planet {
     
    1616        int faction;
    1717        int numShips;
    18 
     18        boolean selected;
     19        private Bitmap selection;
     20       
    1921        public Planet(int radius, int x, int y) {
    2022                this.radius = radius;
     
    2325                faction = 0;
    2426                numShips = 0;
     27                selected = false;
    2528               
    2629                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                }
    2752        }
    2853       
     
    4368        }
    4469       
     70        public boolean isSelected() {
     71                return selected;
     72        }
     73       
    4574        public void setNumShips(int num) {
    4675                numShips = num;
     
    5180        }
    5281       
     82        public void select() {
     83                selected = true;
     84        }
     85       
     86        public void unselect() {
     87                selected = false;
     88        }
     89       
    5390        public void draw(Canvas canvas, Paint linePaint, Paint textPaint) {
    5491                FontMetrics metrics = textPaint.getFontMetrics();
    5592               
    5693                int c, prevC = linePaint.getColor();
     94               
     95                if(selected) {
     96                        //drawSelectionCircle(canvas);
     97                }
    5798               
    5899                switch(faction) {
     
    84125        }
    85126       
     127        public void drawSelectionCircle(Canvas canvas) {
     128                int size = getRadius()+15;
     129               
     130                canvas.drawBitmap(selection, x-size, y-size, null);
     131        }
     132       
    86133        public void update() {
    87134                if(faction != 0)
     
    92139        public void sendFleet(Planet p, int numShips) {
    93140               
     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;
    94147        }
    95148       
Note: See TracChangeset for help on using the changeset viewer.