Changeset 9b87c8d in galcon-client for src/com


Ignore:
Timestamp:
May 24, 2010, 9:46:26 PM (14 years ago)
Author:
dportnoy <devnull@…>
Branches:
master
Children:
8a4e64f
Parents:
76819ac
Message:

Planets get randomly generated and drawn.

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

Legend:

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

    r76819ac r9b87c8d  
    11package com.example.helloandroid;
     2
     3import java.util.ArrayList;
     4import java.util.Random;
    25
    36import android.content.Context;
     
    710import android.graphics.Canvas;
    811import android.graphics.Paint;
    9 import android.graphics.RectF;
    1012import android.graphics.drawable.Drawable;
    1113import android.os.Bundle;
     
    1315import android.os.Message;
    1416import android.util.AttributeSet;
     17import android.util.Log;
    1518import android.view.KeyEvent;
    1619import android.view.SurfaceHolder;
     
    9396        private Bitmap mBackgroundImage;
    9497
    95         /**
    96          * Current height of the surface/canvas.
    97          *
    98          * @see #setSurfaceSize
    99          */
    10098        private int mCanvasHeight = 1;
    101 
    102         /**
    103          * Current width of the surface/canvas.
    104          *
    105          * @see #setSurfaceSize
    106          */
    10799        private int mCanvasWidth = 1;
    108100
     
    110102        private Drawable mCrashedImage;
    111103
    112         /**
    113          * Current difficulty -- amount of fuel, allowed angle, etc. Default is
    114          * MEDIUM.
    115          */
     104        /** Default is MEDIUM. */
    116105        private int mDifficulty;
    117106
    118         /** Velocity dx. */
     107        /** Velocity */
    119108        private double mDX;
    120 
    121         /** Velocity dy. */
    122109        private double mDY;
    123110
     
    179166        private boolean mRun = false;
    180167
    181         /** Scratch rect object. */
    182         private RectF mScratchRect;
    183 
    184168        /** Handle to the surface manager object we interact with */
    185169        private SurfaceHolder mSurfaceHolder;
     
    188172        private int mWinsInARow;
    189173
    190         /** X of lander center. */
     174        /** lander center. */
    191175        private double mX;
    192 
    193         /** Y of lander center. */
    194176        private double mY;
     177       
     178        private ArrayList<Planet> planets;
    195179
    196180        public DrawingThread(SurfaceHolder surfaceHolder, Context context,
     
    203187            Resources res = context.getResources();
    204188            // cache handles to our key sprites & other drawables
    205             mLanderImage = context.getResources().getDrawable(
    206                     R.drawable.lander_plain);
    207             mFiringImage = context.getResources().getDrawable(
    208                     R.drawable.lander_firing);
    209             mCrashedImage = context.getResources().getDrawable(
    210                     R.drawable.lander_crashed);
     189            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);
    211192
    212193            // load background image as a Bitmap instead of a Drawable b/c
    213194            // we don't need to transform it and it's faster to draw this way
    214             mBackgroundImage = BitmapFactory.decodeResource(res,
    215                     R.drawable.earthrise);
     195            mBackgroundImage = BitmapFactory.decodeResource(res, R.drawable.earthrise);
    216196
    217197            // Use the regular lander image as the model size for all sprites
     
    227207            mLinePaintBad.setAntiAlias(true);
    228208            mLinePaintBad.setARGB(255, 120, 180, 0);
    229 
    230             mScratchRect = new RectF(0, 0, 0, 0);
    231209
    232210            mWinsInARow = 0;
     
    241219            mHeading = 0;
    242220            mEngineFiring = true;
     221           
     222            planets = new ArrayList<Planet>();
    243223        }
    244224
     
    490470                mCanvasWidth = width;
    491471                mCanvasHeight = height;
    492 
     472               
     473                Log.i(this.getClass().getName(), "width: "+mCanvasWidth+", height: "+mCanvasHeight);
     474               
     475                Random rand = new Random();
     476
     477                for(int x=0; x<6; x++) {
     478                        planets.add(new Planet(rand.nextInt(45)+5, rand.nextInt(mCanvasWidth), rand.nextInt(mCanvasHeight)));
     479                }
     480                       
    493481                // don't forget to resize the background image
    494482                mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, width, height, true);
     
    594582            // so this is like clearing the screen.
    595583            canvas.drawBitmap(mBackgroundImage, 0, 0, null);
    596 
     584           
     585            for(Planet p : planets) {
     586                canvas.drawCircle(p.getX(), p.getY(), p.getRadius(), mLinePaint);
     587            }
     588           
    597589            int yTop = mCanvasHeight - ((int) mY + mLanderHeight / 2);
    598590            int xLeft = (int) mX - mLanderWidth / 2;
    599 
    600             // Draw the fuel gauge
    601             int fuelWidth = (int) (UI_BAR * mFuel / PHYS_FUEL_MAX);
    602             mScratchRect.set(4, 4, 4 + fuelWidth, 4 + UI_BAR_HEIGHT);
    603             canvas.drawRect(mScratchRect, mLinePaint);
    604 
    605             // Draw the speed gauge, with a two-tone effect
    606             double speed = Math.sqrt(mDX * mDX + mDY * mDY);
    607             int speedWidth = (int) (UI_BAR * speed / PHYS_SPEED_MAX);
    608 
    609             if (speed <= mGoalSpeed) {
    610                 mScratchRect.set(4 + UI_BAR + 4, 4,
    611                         4 + UI_BAR + 4 + speedWidth, 4 + UI_BAR_HEIGHT);
    612                 canvas.drawRect(mScratchRect, mLinePaint);
    613             } else {
    614                 // Draw the bad color in back, with the good color in front of
    615                 // it
    616                 mScratchRect.set(4 + UI_BAR + 4, 4,
    617                         4 + UI_BAR + 4 + speedWidth, 4 + UI_BAR_HEIGHT);
    618                 canvas.drawRect(mScratchRect, mLinePaintBad);
    619                 int goalWidth = (UI_BAR * mGoalSpeed / PHYS_SPEED_MAX);
    620                 mScratchRect.set(4 + UI_BAR + 4, 4, 4 + UI_BAR + 4 + goalWidth,
    621                         4 + UI_BAR_HEIGHT);
    622                 canvas.drawRect(mScratchRect, mLinePaint);
    623             }
    624 
    625             // Draw the landing pad
    626             canvas.drawLine(mGoalX, 1 + mCanvasHeight - TARGET_PAD_HEIGHT,
    627                     mGoalX + mGoalWidth, 1 + mCanvasHeight - TARGET_PAD_HEIGHT,
    628                     mLinePaint);
    629 
    630591
    631592            // Draw the ship with its current rotation
  • src/com/example/helloandroid/Planet.java

    r76819ac r9b87c8d  
    44        int radius;
    55        int regenRate;  // ships per second
    6         int x;
    7         int y;
     6        private int x;
     7        private int y;
    88        int faction;
    99        int numShips;
     
    2727        }
    2828       
     29        public int getRadius() {
     30                return radius;
     31        }
     32       
    2933        public void update() {
    3034                //regen ships if not owned by faction 0
Note: See TracChangeset for help on using the changeset viewer.