Changeset 113d7cf in advance-wars for src/com


Ignore:
Timestamp:
May 9, 2011, 6:23:05 PM (13 years ago)
Author:
dportnoy <devnull@…>
Branches:
master
Children:
bdd63ba
Parents:
ae564dc
Message:

Implemented saving/loading of the map and the units on it. Also did some package refactoring (renamed com.medievaltech.game to com.medievaltech.unit and moved some classes into com.medievaltech.advancewars)

Location:
src/com/medievaltech
Files:
1 deleted
2 edited
9 moved

Legend:

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

    rae564dc r113d7cf  
    11package com.medievaltech.advancewars;
    22
    3 import com.medievaltech.advancewars.R;
    4 import com.medievaltech.advancewars.GameView;
     3import java.io.*;
     4
    55import com.medievaltech.advancewars.GameView.DrawingThread;
    66
     
    1414
    1515public class Game extends Activity {
    16     private static final int MENU_EXIT = 1;
    17     private static final int MENU_NEW = 2;
     16        private static final int MENU_SAVE = 1;
     17        private static final int MENU_MAIN = 2;
     18    private static final int MENU_EXIT = 3;
    1819   
    1920    public enum State{BUST, ACTIVE, DOUBLEDOWN};
     
    3536        super.onCreateOptionsMenu(menu);
    3637
     38        menu.add(0, MENU_SAVE, 0, R.string.menu_save);
     39        menu.add(0, MENU_MAIN, 0, R.string.menu_main);
    3740        menu.add(0, MENU_EXIT, 0, R.string.menu_exit);
    3841        return true;
     
    4952    public boolean onOptionsItemSelected(MenuItem item) {
    5053        switch (item.getItemId()) {
     54                case MENU_SAVE:
     55                        try {
     56                                PrintWriter p = new PrintWriter(new FileWriter(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
     57                                mThread.mMap.save(p);
     58                                p.close();
     59                        }catch(IOException ioe) {
     60                                ioe.printStackTrace();
     61                        }
     62                        break;
     63                case MENU_MAIN:
     64                        mThread.mGameState = GameState.MAIN_MENU;
     65                        break;
    5166            case MENU_EXIT:
    52                 this.finish();
     67                finish();
    5368                break;
    5469        }
     
    93108           
    94109            mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");
    95            
    96                     mGameView.getThread().mPlayerWins = savedInstanceState.getInt("playerWins");
    97                     mGameView.getThread().mPlayerLosses = savedInstanceState.getInt("playerLosses");
    98110        }
    99111    }
     
    117129    protected void onSaveInstanceState(Bundle outState) {
    118130        outState.putSerializable("gameState", mGameView.getThread().mGameState);
    119             outState.putSerializable("playerWins", mGameView.getThread().mPlayerWins);
    120             outState.putSerializable("playerLosses", mGameView.getThread().mPlayerLosses);
    121            
     131       
    122132        super.onSaveInstanceState(outState);
    123         Log.w("Blackjack", "onSaveInstanceState called");
     133        Log.w("AdvanceWars", "onSaveInstanceState called");
    124134    }
    125135   
  • src/com/medievaltech/advancewars/GameView.java

    rae564dc r113d7cf  
    11package com.medievaltech.advancewars;
    22
    3 import com.medievaltech.game.*;
     3import java.io.*;
     4
     5import com.medievaltech.advancewars.Tile.TerrainType;
     6import com.medievaltech.unit.*;
    47import com.medievaltech.gui.*;
    58
     
    1215import android.widget.TextView;
    1316
    14 /**
    15  * View that draws, takes keystrokes, etc. for a simple LunarLander game.
    16  *
    17  * Has a mode which RUNNING, PAUSED, etc. Has a x, y, dx, dy, ... capturing the
    18  * current ship physics. All x/y etc. are measured with (0,0) at the lower left.
    19  * updatePhysics() advances the physics based on realtime. draw() renders the
    20  * ship, and does an invalidate() to prompt another draw() as soon as possible
    21  * by the system.
    22  */
    2317class GameView extends SurfaceView implements SurfaceHolder.Callback {
    2418       
     
    3933        private int mCanvasHeight = 1;
    4034        private int mCanvasWidth = 1;
    41         public int mPlayerWins = 0, mPlayerLosses = 0;
    4235
    4336        /** Message handler used by thread to interact with TextView */
     
    5144                mUnitPaint;
    5245       
    53         private Map mMap;
     46        //maybe make this private and make an accessor for it
     47        public Map mMap;
     48       
     49        public Tile grassTile, oceanTile;
    5450
    5551        /** Indicate whether the surface has been created & is ready to draw */
     
    5955        private SurfaceHolder mSurfaceHolder;
    6056       
    61         private com.medievaltech.gui.Window wndMainMenu, wndBattleMap;
     57        private com.medievaltech.gui.Window wndMainMenu;
    6258        private Unit selectedUnit;
    6359       
     
    6662            mSurfaceHolder = surfaceHolder;
    6763            mHandler = handler;
    68             mContext = context;
    6964           
    7065            mLinePaint = new Paint();
     
    107102            wndMainMenu.addGUIObject("btnQuit", new Button("Quit", 100, 195, 120, 20, mLinePaint, mButtonPaint));
    108103           
    109             Tile grassTile = new Tile(mTilePaint1);
    110             Tile oceanTile = new Tile(mTilePaint2);
     104            grassTile = new Tile(mTilePaint1, TerrainType.LAND);
     105            oceanTile = new Tile(mTilePaint2, TerrainType.SEA);
    111106           
    112107            mMap = new Map(grassTile, 6, 8, new Point(10, 25));
     
    336331        private void doDraw(Canvas canvas) {
    337332                canvas.drawColor(Color.BLACK);
    338                
    339                 String text;
    340                 Paint.FontMetrics metrics = mTextPaint.getFontMetrics();
    341333               
    342334                switch(mGameState) {
     
    380372    }
    381373
    382     /** Handle to the application context, used to e.g. fetch Drawables. */
    383     private Context mContext;
    384 
    385374    /** Pointer to the text view to display "Paused.." etc. */
    386375    private TextView mStatusText;
     
    421410                                thread.mGameState = GameState.BATTLE_MAP;
    422411                        }else if(thread.wndMainMenu.getGUIObject("btnLoadGame").isClicked(event.getX(), event.getY())) {
     412                                BufferedReader b;
     413                        try {
     414                                b = new BufferedReader(new FileReader(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
     415                               
     416                                int width = Integer.parseInt(b.readLine());
     417                                int height = Integer.parseInt(b.readLine());
     418                               
     419                                String offset = b.readLine();
     420                                Log.i("GameSave", offset);
     421                                int offsetX = Integer.parseInt(offset.substring(0, offset.indexOf("x")));
     422                                int offsetY = Integer.parseInt(offset.substring(offset.indexOf("x")+1));
     423                               
     424                                thread.mMap = new Map(thread.grassTile, width, height, new Point(offsetX, offsetY));
     425                               
     426                                Log.i("GameSave", "Created the map");
     427                               
     428                                for(int x=0; x<width; x++) {
     429                                        String line = b.readLine();
     430                                        Log.i("GameSave", line);
     431                                        String[] arr = line.split(",");
     432                                        for(int y=0; y<arr.length; y++) {
     433                                                TerrainType type = TerrainType.values()[Integer.parseInt(arr[y])];
     434                                                if(type.equals(TerrainType.LAND))
     435                                                        thread.mMap.setTile(x, y, new Tile(thread.grassTile, new Point(10, 25)));
     436                                                else
     437                                                        thread.mMap.setTile(x, y, new Tile(thread.oceanTile, new Point(10, 25)));
     438                                        }
     439                                }
     440                               
     441                                while(b.ready()) {
     442                                        String unit = b.readLine();
     443                                        Log.i("GameSave", unit);
     444                                        int x = Integer.parseInt(unit.substring(0, unit.indexOf(",")));
     445                                        int y = Integer.parseInt(unit.substring(unit.indexOf(",")+1));
     446                                       
     447                                        mGame.mThread.mMap.getTile(x, y).addUnit(new Soldier(mGame.mThread.mUnitPaint));
     448                                }
     449                               
     450                                b.close();
     451                        }catch(IOException ioe) {
     452                                ioe.printStackTrace();
     453                        }
    423454                                thread.mGameState = GameState.BATTLE_MAP;
    424455                        }else if(thread.wndMainMenu.getGUIObject("btnQuit").isClicked(event.getX(), event.getY())) {
  • src/com/medievaltech/advancewars/Map.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.advancewars;
    22
    3 import android.graphics.Canvas;
    4 import android.graphics.Point;
     3import java.io.*;
     4
     5import com.medievaltech.unit.Unit;
     6
     7
     8import android.graphics.*;
    59
    610public class Map {
     
    4246        }
    4347       
     48        public void save(PrintWriter p) {
     49                p.println(getWidth());
     50                p.println(getHeight());
     51                p.println(offset.x+"x"+offset.y);
     52               
     53                for(int x=0; x<getWidth(); x++) {
     54                        p.print(grid[x][0].type.ordinal());
     55                        for(int y=1; y<getHeight(); y++)
     56                                p.print(","+grid[x][y].type.ordinal());
     57                        p.println();
     58                }
     59               
     60                for(int x=0; x<getWidth(); x++) {
     61                        for(int y=1; y<getHeight(); y++) {
     62                                if(grid[x][y].currentUnit != null) {
     63                                        Unit u = grid[x][y].currentUnit;
     64                                        //p.println(u.type);
     65                                        p.println(u.location.x+","+u.location.y);
     66                                }
     67                        }
     68                }
     69        }
     70       
    4471        public void draw(Canvas c) {
    4572                for(int x=0; x<getWidth(); x++)
  • src/com/medievaltech/advancewars/Tile.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.advancewars;
     2
     3import com.medievaltech.unit.Unit;
    24
    35import android.graphics.Canvas;
     
    1719        private Paint p;
    1820       
    19         public Tile(Paint p) {
     21        public Tile(Paint p, TerrainType type) {
    2022                this.p = p;
     23                this.type = type;
    2124                this.currentUnit = null;
    2225        }
  • src/com/medievaltech/unit/Artillery.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.unit;
    22
    33import java.util.List;
  • src/com/medievaltech/unit/Mech.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.unit;
    22
    33import java.util.List;
  • src/com/medievaltech/unit/Recon.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.unit;
    22
    33import java.util.List;
  • src/com/medievaltech/unit/SmTank.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.unit;
    22
    33import java.util.List;
  • src/com/medievaltech/unit/Soldier.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.unit;
    22
    33import java.util.List;
  • src/com/medievaltech/unit/Transport.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.unit;
    22
    33import android.graphics.Paint;
  • src/com/medievaltech/unit/Unit.java

    rae564dc r113d7cf  
    1 package com.medievaltech.game;
     1package com.medievaltech.unit;
    22
    3 import java.util.LinkedList;
    4 import java.util.List;
    5 
    6 import android.graphics.Canvas;
    7 import android.graphics.Paint;
    8 import android.graphics.Point;
     3import java.util.*;
     4import android.graphics.*;
    95
    106public abstract class Unit
     
    1814       
    1915        public Type type;
    20         public Player owner;
     16        //public Player owner;
    2117       
    2218        public int maxHealth;
Note: See TracChangeset for help on using the changeset viewer.