GameMap.java :  » Game » andforge-game-development-tutorial-client » com » andlabs » gd » map » Android Open Source

Android Open Source » Game » andforge game development tutorial client 
andforge game development tutorial client » com » andlabs » gd » map » GameMap.java
package com.andlabs.gd.map;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;

import com.andlabs.gd.base.Point;
import com.andlabs.gd.rendering.RenderingInitializer;

/**
 * @author johannes
 */
public class GameMap implements RenderingInitializer{
  public static final int GRAS = 0;
  public static final int TREE = GRAS + 1;
  public static final int ROCK = TREE + 1;
  public static final int GOLD = ROCK + 1;
  private GameMap mEast;
  private GameMap mWest;
  private GameMap mSouth;
  private GameMap mNorth;
  private GameMap mSouthEast;
  private GameMap mSouthWest;
  private GameMap mNorthEast;
  private GameMap mNorthWest;
  private Bitmap mGras, mRock, mTree, mGold;
  private int mCellSize;
  private int mHeight, mWidth;
  private int type;
  public int mId;
  private int mX, mY;
  
  //======================
  // Fields
  //======================
  public List<List<Integer>> map = new ArrayList<List<Integer>>();
  private List<Integer> row;
  //======================
  // Constructors
  //======================

  public GameMap(){
    for(int i = 0; i < 5; i++){
      row = new ArrayList<Integer>();
      for(int c = 0; c < 5; c++){
        double prob = Math.random();
        
        if(prob < 0.6){
          row.add(GRAS);
        } else if(prob >= 0.6 && prob < 0.825){
          row.add(TREE);
        } else if(prob >= 0.825 && prob < 0.95){
          row.add(ROCK);
        } else {
          row.add(GOLD);
        }
          
      }
      map.add(row);
    }
  }
  //======================
  // Overridden Methods
  //======================

  //======================
  // Public Methods
  //======================
  
  public void render(Canvas canvas, Point heroPosition){
    
    int offsetX = (int) (heroPosition.x - canvas.getWidth());
    int offsetY = (int) (heroPosition.y - canvas.getHeight());
    
//    int numberOfCells = mWidth / mCellSize + 2;
//    int numberOfRows = mHeight / mCellSize + 2;
//    int startCell = (int) (heroPosition.x / mCellSize) - numberOfCells / 2;
//    int endCell = (int) (heroPosition.x / mCellSize) + numberOfCells / 2;
//    int startRow = (int) (heroPosition.y / mCellSize) - numberOfRows / 2;
//    int endRow = (int) (heroPosition.y / mCellSize) + numberOfRows / 2;
//    int startRowLeft = (int) (heroPosition.y % mCellSize);
//    int startCellLeft = (int) (heroPosition.x % mCellSize);
    
    for(int i = 0; i < map.size(); i++){
      
      mY = i * mCellSize - offsetY;
      row = map.get(i);
      for(int e = 0; e < row.size(); e++){
        mX = e * mCellSize - offsetX;
        
        type = row.get(e);
        switch(type){
        case GRAS:
          canvas.drawBitmap(mGras, mX, mY , null);
          break;
        case GOLD:
          canvas.drawBitmap(mGold, mX, mY, null);
          break;
        case ROCK:
          canvas.drawBitmap(mRock, mX, mY , null);
          break;
        case TREE:
          canvas.drawBitmap(mTree, mX, mY, null);
          break;
        }
      }
    }
  }

  @Override
  public void initializeRendering(Context context) {
    AssetManager manager = context.getAssets();    

    try {
      mGold = BitmapFactory.decodeStream(manager.open("map/gold.png"));
      mGras = BitmapFactory.decodeStream(manager.open("map/gras.png"));
      mTree = BitmapFactory.decodeStream(manager.open("map/tree.png"));
      mRock = BitmapFactory.decodeStream(manager.open("map/rock.png"));
    
    } catch (IOException e) {
      Log.e("GameMap", "error while loading map images", e);
      throw new RuntimeException(e);
    }
    
    mCellSize = mGras.getHeight();    
  }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.