Changeset 4666fae in advance-wars for src/com/medievaltech/advancewars


Ignore:
Timestamp:
Oct 20, 2011, 3:25:50 PM (13 years ago)
Author:
dportnoy <devnull@…>
Branches:
master
Children:
bb2fa26
Parents:
331d180
Message:

Moved many widely used variables to the Static class.

Location:
src/com/medievaltech/advancewars
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/com/medievaltech/advancewars/Game.java

    r331d180 r4666fae  
    99import android.view.MenuItem;
    1010import android.view.Window;
    11 import android.widget.TextView;
    1211
    13 import com.medievaltech.advancewars.GameView.*;
    1412import com.medievaltech.advancewars.Enum.*;
    1513
    1614public class Game extends Activity {
    17     public DrawingThread mThread;
    18     private GameView mGameView;
    19    
     15       
    2016    /**
    2117     * Invoked during init to give the Activity a chance to set up its Menu.
     
    4743       
    4844        if(i == MenuOption.END_TURN.ordinal()) {
    49                 mThread.mTurn = Turn.ENEMY_TURN;
     45                Static.turn = Turn.ENEMY_TURN;
    5046        }else if(i == MenuOption.SAVE.ordinal()) {
    5147                try {
     
    5753                }
    5854        }else if(i == MenuOption.MAIN.ordinal()) {
    59                 mThread.mGameState = GameState.MAIN_MENU;
     55                Static.gameState = GameState.MAIN_MENU;
    6056        }else if(i == MenuOption.EXIT.ordinal()) {
    6157                finish();
     
    8581        setContentView(R.layout.main);
    8682
    87         mGameView = (GameView) findViewById(R.id.lunar);
    88         mThread = mGameView.getThread();
    89         mGameView.mGame = this;
    90 
    91         mGameView.setTextView((TextView) findViewById(R.id.text));
     83        Static.game = this;
    9284
    9385        if (savedInstanceState == null) {       // we were just launched: set up a new game
     
    9688            Log.w("AdvanceWars", "SIS is nonnull");
    9789           
    98             mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");
     90            Static.gameState = (GameState)savedInstanceState.getSerializable("gameState");
    9991        }
    10092    }
     
    108100    @Override
    109101    protected void onSaveInstanceState(Bundle outState) {
    110         outState.putSerializable("gameState", mGameView.getThread().mGameState);
     102        outState.putSerializable("gameState", Static.gameState);
    111103       
    112104        super.onSaveInstanceState(outState);
  • src/com/medievaltech/advancewars/GameView.java

    r331d180 r4666fae  
    1414import android.util.AttributeSet;
    1515import android.util.Log;
    16 import android.widget.TextView;
    1716
    1817class GameView extends SurfaceView implements SurfaceHolder.Callback {
    1918       
    20     class DrawingThread extends Thread {       
    21         public GameState mGameState;
    22        
    23         //maybe make this private and make an accessor for it
    24         //public Map mMap;
    25        
    26         public Tile grassTile, oceanTile;
    27        
    28         public Turn mTurn;
    29        
    30         public Player enemy;
     19    class DrawingThread extends Thread {
     20       
    3121        private Unit selectedUnit;
    32        
    33         private int mCanvasHeight = 1;
    34         private int mCanvasWidth = 1;
    3522
    3623        private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1,
    3724                                mTilePaint2, mSelectionPaint, mUnitPaint1, mUnitPaint2;
    3825
    39         /** Indicate whether the surface has been created & is ready to draw */
    40         private boolean mRun = false;
    41 
    4226        /** Handle to the surface manager object we interact with */
    4327        private SurfaceHolder mSurfaceHolder;
    4428       
    45         private com.medievaltech.gui.Window wndMainMenu;
    46        
    4729        public DrawingThread(SurfaceHolder surfaceHolder, Context context, Handler handler) {
    4830            mSurfaceHolder = surfaceHolder;
    4931           
    5032            mLinePaint = new Paint();
    51             mLinePaint.setAntiAlias(true);
    52             mLinePaint.setARGB(255, 0, 255, 0);
     33            initPaint(mLinePaint, 0, 255, 0);
    5334
    5435            mTextPaint = new Paint();
    55             mTextPaint.setAntiAlias(true);
    56             mTextPaint.setARGB(255, 255, 255, 255);
    5736            mTextPaint.setTextSize(12);
    5837            mTextPaint.setTextAlign(Paint.Align.CENTER);
     38            initPaint(mTextPaint, 255, 255, 255);
    5939           
    6040            mButtonPaint = new Paint();
    61             mButtonPaint.setAntiAlias(true);
    62             mButtonPaint.setARGB(255, 0, 0, 0);
    6341            mButtonPaint.setTextSize(20);
    6442            mButtonPaint.setTextAlign(Paint.Align.CENTER);
     43            initPaint(mButtonPaint, 0, 0, 0);
    6544           
    6645            mTilePaint1 = new Paint();
    67             mTilePaint1.setAntiAlias(true);
    68             mTilePaint1.setARGB(255, 0, 255, 0);
     46            initPaint(mTilePaint1, 0, 255, 0);
    6947           
    7048            mTilePaint2 = new Paint();
    71             mTilePaint2.setAntiAlias(true);
    72             mTilePaint2.setARGB(255, 0, 0, 255);
     49            initPaint(mTilePaint2, 0, 0, 255);
    7350           
    7451            mUnitPaint1 = new Paint();
    75             mUnitPaint1.setAntiAlias(true);
    76             mUnitPaint1.setARGB(255, 255, 0, 0);
     52            initPaint(mUnitPaint1, 255, 0, 0);
    7753           
    7854            mUnitPaint2 = new Paint();
    79             mUnitPaint2.setAntiAlias(true);
    80             mUnitPaint2.setARGB(255, 160, 160, 255);
     55            initPaint(mUnitPaint2, 160, 160, 255);
    8156           
    8257            mSelectionPaint = new Paint();
    83             mSelectionPaint.setAntiAlias(true);
    84             mSelectionPaint.setARGB(255, 255, 127, 0);
     58            initPaint(mSelectionPaint, 255, 127, 0);
    8559           
    8660            Player.neutralColor = new Paint();
    87             Player.neutralColor.setAntiAlias(true);
    88             Player.neutralColor.setARGB(255, 127, 127, 127);
    89            
    90             wndMainMenu = new com.medievaltech.gui.Window(0, 0, 320, 450);;
    91             wndMainMenu.addGUIObject("txtTitle", new Text("Main Menu", 100, 30, 120, 20, mTextPaint));
    92             wndMainMenu.addGUIObject("btnNewGame", new Button("New Game", 100, 90, 120, 20, mLinePaint, mButtonPaint));
    93             wndMainMenu.addGUIObject("btnLoadGame", new Button("Load Game", 100, 125, 120, 20, mLinePaint, mButtonPaint));
    94             wndMainMenu.addGUIObject("btnMapEditor", new Button("Map Editor", 100, 160, 120, 20, mLinePaint, mButtonPaint));
    95             wndMainMenu.addGUIObject("btnQuit", new Button("Quit", 100, 195, 120, 20, mLinePaint, mButtonPaint));
    96            
    97             grassTile = new Tile(mTilePaint1, TerrainType.LAND);
    98             oceanTile = new Tile(mTilePaint2, TerrainType.SEA);
    99            
    100             Static.map = new Map(grassTile, 6, 8, new Point(10, 25));
     61            initPaint(Player.neutralColor, 127, 127, 127);
     62           
     63            Static.wndMainMenu = new com.medievaltech.gui.Window(0, 0, 320, 450);
     64            Static.wndMainMenu.addGUIObject("txtTitle", new Text("Main Menu", 100, 30, 120, 20, mTextPaint));
     65            Static.wndMainMenu.addGUIObject("btnNewGame", new Button("New Game", 100, 90, 120, 20, mLinePaint, mButtonPaint));
     66            Static.wndMainMenu.addGUIObject("btnLoadGame", new Button("Load Game", 100, 125, 120, 20, mLinePaint, mButtonPaint));
     67            Static.wndMainMenu.addGUIObject("btnMapEditor", new Button("Map Editor", 100, 160, 120, 20, mLinePaint, mButtonPaint));
     68            Static.wndMainMenu.addGUIObject("btnQuit", new Button("Quit", 100, 195, 120, 20, mLinePaint, mButtonPaint));
     69           
     70            Static.grassTile = new Tile(mTilePaint1, TerrainType.LAND);
     71            Static.oceanTile = new Tile(mTilePaint2, TerrainType.SEA);
     72           
     73            Static.map = new Map(Static.grassTile, 6, 8, new Point(10, 25));
    10174           
    10275            boolean land = true;
     
    10578                        for(int y=0; y<Static.map.getHeight(); y++) {
    10679                                if(land)
    107                                         Static.map.setTile(x, y, new Tile(grassTile, new Point(x, y)));
     80                                        Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(x, y)));
    10881                                else
    109                                         Static.map.setTile(x, y, new Tile(oceanTile, new Point(x, y)));
     82                                        Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(x, y)));
    11083                                land = !land;
    11184                        }
     
    11386            }
    11487           
    115             Player humanPlayer = new Player("Human", mUnitPaint1);
    116             enemy = new Player("Comp", mUnitPaint2);
    117            
    118             Static.map.getTile(0, 0).addUnit(new Soldier(enemy));
    119             Static.map.getTile(2, 3).addUnit(new Soldier(humanPlayer));
    120             Static.map.getTile(5, 6).addUnit(new Soldier(humanPlayer));
     88            Static.human = new Player("Human", mUnitPaint1);
     89            Static.enemy = new Player("Comp", mUnitPaint2);
     90           
     91            Static.map.getTile(0, 0).addUnit(new Soldier(Static.enemy));
     92            Static.map.getTile(2, 3).addUnit(new Soldier(Static.human));
     93            Static.map.getTile(5, 6).addUnit(new Soldier(Static.human));
    12194           
    12295            Static.map.getTile(4, 4).addBuilding(new City());
    12396           
    124             mTurn = Turn.YOUR_TURN;
    125            
    126             mGameState = GameState.MAIN_MENU;
     97            Static.turn = Turn.YOUR_TURN;
     98           
     99            Static.gameState = GameState.MAIN_MENU;
     100        }
     101       
     102        public void initPaint(Paint p, int r, int g, int b) {
     103            p.setAntiAlias(true);
     104            p.setARGB(255, r, g, b);
    127105        }
    128106       
     
    134112            synchronized (mSurfaceHolder) {
    135113                Log.i("AdvanceWars", "Player's turn starting now");
    136                 mGameState = GameState.MAIN_MENU;
     114                Static.gameState = GameState.MAIN_MENU;
    137115            }
    138116        }
     
    140118        @Override
    141119        public void run() {
    142             while (mRun) {
     120            while (Static.run) {
    143121                Canvas c = null;
    144122                try {
     
    158136            }
    159137        }
    160 
    161         /**
    162          * Used to signal the thread whether it should be running or not.
    163          * Passing true allows the thread to run; passing false will shut it
    164          * down if it's already running. Calling start() after this was most
    165          * recently called with false will result in an immediate shutdown.
    166          *
    167          * @param b true to run, false to shut down
    168          */
    169         public void setRunning(boolean b) {
    170             mRun = b;
    171         }
    172138       
    173139        public void setGameState(GameState state) {
    174140            synchronized (mSurfaceHolder) {
    175                 mGameState = state;
     141                Static.gameState = state;
    176142            }
    177143        }
     
    179145        /* Callback invoked when the surface dimensions change. */
    180146        public void setSurfaceSize(int width, int height) {
    181             // synchronized to make sure these all change atomically
    182147            synchronized (mSurfaceHolder) {
    183                 mCanvasWidth = width;
    184                 mCanvasHeight = height;
    185                
    186                 Log.i("AdvanceWars", "width: "+mCanvasWidth+", height: "+mCanvasHeight);
     148                Log.i("AdvanceWars", "width: "+width+", height: "+height);
    187149            }
    188150        }
    189151       
    190152        private void doLogic() {
    191                 if(mTurn == Turn.YOUR_TURN)
     153                if(Static.turn == Turn.YOUR_TURN)
    192154                        return;
    193155               
    194                 switch(mGameState) {
     156                switch(Static.gameState) {
    195157                case BATTLE_MAP:
    196                         Iterator<Unit> iter = enemy.getControlledUnits().iterator();
     158                        Iterator<Unit> iter = Static.enemy.getControlledUnits().iterator();
    197159                        Unit cur;
    198160                        int x, y;
     
    213175                       
    214176                               
    215                                 mTurn = Turn.YOUR_TURN;
     177                        Static.turn = Turn.YOUR_TURN;
    216178                        break;
    217179                }
     
    225187                canvas.drawColor(Color.BLACK);
    226188               
    227                 switch(mGameState) {
     189                switch(Static.gameState) {
    228190                case MAIN_MENU:
    229                 wndMainMenu.draw(canvas);
     191                Static.wndMainMenu.draw(canvas);
    230192                        break;
    231193                case BATTLE_MAP:
     
    249211
    250212    /** Pointer to the text view to display "Paused.." etc. */
    251     private TextView mStatusText;
    252 
    253     /** The thread that actually draws the animation */
    254     private DrawingThread thread;
    255    
    256     public Game mGame;
     213    //private TextView mStatusText;
    257214
    258215    public GameView(Context context, AttributeSet attrs) {
     
    264221
    265222        // create thread only; it's started in surfaceCreated()
    266         thread = new DrawingThread(holder, context, new Handler() {
    267             @Override
    268             public void handleMessage(Message m) {
    269                 mStatusText.setVisibility(m.getData().getInt("viz"));
    270                 mStatusText.setText(m.getData().getString("text"));
    271             }
    272         });
     223        Static.thread = new DrawingThread(holder, context, new Handler() {});
    273224
    274225        setFocusable(true); // make sure we get key events
     
    280231        if(event.getAction() == MotionEvent.ACTION_UP) {
    281232                Log.i("AdvanceWars", "Detected UP touch action");
    282                 switch(thread.mGameState) {
     233                switch(Static.gameState) {
    283234                case MAIN_MENU:
    284235                        Log.i("AdvanceWars", "Switching to battle map");
    285                         if(thread.wndMainMenu.getGUIObject("btnNewGame").isClicked(event.getX(), event.getY())) {
    286                                 thread.mGameState = GameState.BATTLE_MAP;
    287                         }else if(thread.wndMainMenu.getGUIObject("btnLoadGame").isClicked(event.getX(), event.getY())) {
     236                        if(Static.wndMainMenu.getGUIObject("btnNewGame").isClicked(event.getX(), event.getY())) {
     237                                Static.gameState = GameState.BATTLE_MAP;
     238                        }else if(Static.wndMainMenu.getGUIObject("btnLoadGame").isClicked(event.getX(), event.getY())) {
    288239                                BufferedReader b;
    289240                        try {
     
    298249                                int offsetY = Integer.parseInt(offset.substring(offset.indexOf("x")+1));
    299250                               
    300                                 Static.map = new Map(thread.grassTile, width, height, new Point(offsetX, offsetY));
     251                                Static.map = new Map(Static.grassTile, width, height, new Point(offsetX, offsetY));
    301252                               
    302253                                Log.i("GameSave", "Created the map");
     
    309260                                                TerrainType type = TerrainType.values()[Integer.parseInt(arr[y])];
    310261                                                if(type.equals(TerrainType.LAND))
    311                                                         Static.map.setTile(x, y, new Tile(thread.grassTile, new Point(10, 25)));
     262                                                        Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(10, 25)));
    312263                                                else
    313                                                         Static.map.setTile(x, y, new Tile(thread.oceanTile, new Point(10, 25)));
     264                                                        Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(10, 25)));
    314265                                        }
    315266                                }
     
    321272                                        int y = Integer.parseInt(unit.substring(unit.indexOf(",")+1));
    322273                                       
    323                                         Player humanPlayer = new Player("Human", mGame.mThread.mUnitPaint1);
     274                                        Player humanPlayer = new Player("Human", Static.thread.mUnitPaint1);
    324275                                       
    325276                                        Static.map.getTile(x, y).addUnit(new Soldier(humanPlayer));
     
    330281                                ioe.printStackTrace();
    331282                        }
    332                                 thread.mGameState = GameState.BATTLE_MAP;
    333                         }else if(thread.wndMainMenu.getGUIObject("btnQuit").isClicked(event.getX(), event.getY())) {
    334                                 mGame.finish();
     283                        Static.gameState = GameState.BATTLE_MAP;
     284                        }else if(Static.wndMainMenu.getGUIObject("btnQuit").isClicked(event.getX(), event.getY())) {
     285                                Static.game.finish();
    335286                        }
    336287                        break;
     
    344295                                Unit target = Static.map.getTile(x, y).currentUnit;
    345296                               
    346                                 if(thread.selectedUnit != null && thread.selectedUnit.getMovementRange().contains(new Point(x, y))) {
    347                                         if(target == null || target == thread.selectedUnit) {
    348                                                         Static.map.getTile(thread.selectedUnit.location.x, thread.selectedUnit.location.y).removeUnit();
    349                                                         Static.map.getTile(x, y).addUnit(thread.selectedUnit);
     297                                if(Static.thread.selectedUnit != null && Static.thread.selectedUnit.getMovementRange().contains(new Point(x, y))) {
     298                                        if(target == null || target == Static.thread.selectedUnit) {
     299                                                        Static.map.getTile(Static.thread.selectedUnit.location.x, Static.thread.selectedUnit.location.y).removeUnit();
     300                                                        Static.map.getTile(x, y).addUnit(Static.thread.selectedUnit);
    350301                                        }else {
    351302                                                // the target contains another unit. If the unit is enemy-controlled, attack it
    352303                                        }
    353                                         thread.selectedUnit = null;
     304                                        Static.thread.selectedUnit = null;
    354305                                }else
    355                                         thread.selectedUnit = target;
     306                                        Static.thread.selectedUnit = target;
    356307                        }
    357308                       
     
    366317        return true;
    367318    }
    368    
    369     /**
    370      * Fetches the animation thread corresponding to this LunarView.
    371      *
    372      * @return the animation thread
    373      */
    374     public DrawingThread getThread() {
    375         return thread;
    376     }
    377 
    378     /**
    379      * Installs a pointer to the text view used for messages.
    380      */
    381     public void setTextView(TextView textView) {
    382         mStatusText = textView;
    383     }
    384319
    385320    /* Callback invoked when the surface dimensions change. */
    386321    public void surfaceChanged(SurfaceHolder holder, int format, int width,
    387322            int height) {
    388         thread.setSurfaceSize(width, height);
     323        Static.thread.setSurfaceSize(width, height);
    389324    }
    390325
     
    394329     */
    395330    public void surfaceCreated(SurfaceHolder holder) {
    396         thread.setRunning(true);
    397         thread.start();
     331        Static.run = true;
     332        Static.thread.start();
    398333    }
    399334
     
    407342        // it might touch the Surface after we return and explode
    408343        boolean retry = true;
    409         thread.setRunning(false);
     344        Static.run = false;
    410345        while (retry) {
    411346            try {
    412                 thread.join();
     347                Static.thread.join();
    413348                retry = false;
    414349            } catch (InterruptedException e) {
  • src/com/medievaltech/advancewars/Static.java

    r331d180 r4666fae  
    11package com.medievaltech.advancewars;
     2
     3import com.medievaltech.advancewars.Enum.*;
     4import com.medievaltech.advancewars.GameView.DrawingThread;
    25
    36public class Static {
    47        public static Map map;
     8       
     9        public static GameState gameState;
     10   
     11    public static Tile grassTile, oceanTile;
     12   
     13    public static Turn turn;
     14   
     15    public static Player human, enemy;
     16   
     17    public static DrawingThread thread;
     18   
     19    public static Game game;
     20   
     21    public static boolean run = false;
     22   
     23    public static com.medievaltech.gui.Window wndMainMenu;
    524}
Note: See TracChangeset for help on using the changeset viewer.