package com.appspot.hexagon;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.appspot.hexagon.constants.MapConstants;
import com.appspot.hexagon.dbo.ImageCrop;
import com.appspot.hexagon.dbo.LocationAnchor;
import com.appspot.hexagon.dbo.MapCell;
import com.appspot.hexagon.dbo.PlayerObject;
import com.appspot.hexagon.dbo.WorldMap;
import com.appspot.hexagon.util.CSVUtil;
import com.appspot.hexagon.util.FrameRateCounter;
import com.appspot.hexagon.util.Graphic2DUtil;
import com.appspot.hexagon.util.HexagonDBHelper;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
public class HexagonGameThread extends Thread {
public static final int STATE_PAUSE = 2;
public static final int STATE_READY = 3;
public static final int STATE_RUNNING = 4;
private PlayerObject avatar = null;
private PlayerObject avatarHexagon = null;
private LocationAnchor nextAnchor = null;
private Resources res = null;
private static int background_color = Color.BLACK;
private FrameRateCounter fpsCounter = null;
private Handler handler = null;
private SurfaceHolder holder = null;
private Context context = null;
private int gameMode;
private boolean isRunning = false;
private RectF gameBackground = null;
private Paint cPaint = null;
private DecimalFormat df = null;
private boolean drawBackground;
private boolean drawAnchor;
private Canvas canvas = null;
private Bitmap rawTileMapImg = null;
private Bitmap[][] tileImg = null;
// private HexagonDBHelper dbHelper = null;
// private SQLiteDatabase db = null;
public static String TAG = HexagonGameThread.class.getName();
public List<String[]> lsMapData = null;
public static final List<ImageCrop> imageMapss = MapConstants.getImageGalleries();
public HexagonGameThread(SurfaceHolder holder, Context context,
Handler handler) {
this.holder = holder;
this.handler = handler;
this.context = context;
// this.
df = new DecimalFormat("#########0.00");
res = context.getResources();
drawBackground = true;
drawAnchor = true;
init();
}
public void init() {
// dbHelper = new HexagonDBHelper(context);
lsMapData = populateWorld(R.raw.map_vector);
cPaint = new Paint();
// canvas = holder.lockCanvas(null);
// drawBackground(canvas);
Drawable plant_circle = res.getDrawable(R.drawable.plant_circle);
Drawable imgHexagon = res.getDrawable(R.drawable.hexagon);
Drawable ylw_pushpin = res.getDrawable(R.drawable.ylw_pushpin);
// TODO: replace with SQL query to get background image gallery
int current_background_img = R.drawable.map_chip;
rawTileMapImg = BitmapFactory.decodeStream(res
.openRawResource(current_background_img));
int screen_row = context.getWallpaperDesiredMinimumHeight() / 32;
int screen_row_remainder = context.getWallpaperDesiredMinimumHeight() % 32;
if (screen_row_remainder > 0) {
++screen_row;
}
int screen_col = context.getWallpaperDesiredMinimumWidth() / 32;
int screen_col_remainder = context.getWallpaperDesiredMinimumWidth() % 32;
if (screen_col_remainder > 0) {
++screen_col;
}
tileImg = new Bitmap[screen_row][screen_col];
/***
* for(int row=0;row<screen_row;row++){ for(int
* col=0;col<screen_col;col++){ int left,top,right,bottom,height,width;
* // TODO: query from mapcell left = top = right = bottom = 0; height =
* width = 32; right = left + width; bottom = top + height;
*
* //tileImg[row][col] = Bitmap.createBitmap(rawTileMapImg, left, top,
* right, bottom); } }
****/
Log.i(TAG, "screen(row=" + screen_row + ",col=" + screen_col + ")");
tileImg = findMapTilesByCurrentLocation(screen_row, screen_col);
nextAnchor = new LocationAnchor(0, 0, ylw_pushpin);
nextAnchor.setStayAliveTime(1000);
avatarHexagon = new PlayerObject(100, 100, imgHexagon);
gameBackground = new RectF(0, 0, context
.getWallpaperDesiredMinimumWidth(), context
.getWallpaperDesiredMinimumHeight());
fpsCounter = new FrameRateCounter();
}
public void doStart() {
synchronized (holder) {
setState(STATE_RUNNING);
}
}
public void pause() {
synchronized (holder) {
if (gameMode == STATE_RUNNING)
setState(STATE_PAUSE);
}
}
@Override
public void run() {
while (isRunning) {
try {
canvas = holder.lockCanvas(null);
synchronized (holder) {
// --- update game state ---
if (gameMode == STATE_RUNNING) {
updatePhysics();
}
// -- render game --
doDraw(canvas);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
fpsCounter.update();
}
}
private void updatePhysics() {
// TODO Auto-generated method stub
}
public synchronized void restoreState(Bundle savedState) {
synchronized (holder) {
setState(STATE_PAUSE);
}
}
private void setState(int gameMode) {
this.gameMode = gameMode;
}
private void doDraw(Canvas canvas) {
// draw background
// TODO: do a query on map tile, draw background using map tiles
if (drawBackground) {
drawBackground(canvas);
}
// do a query on world objects, draw each of them on screen
// TODO: draw Player(s) and other character(s)
avatarHexagon.drawOnScreen(canvas);
if (drawAnchor) {
if (nextAnchor.isAlive()) {
nextAnchor.drawOnScreen(canvas);
}
}
String msg = "FPS = " + df.format(fpsCounter.getFps());
cPaint.setColor(Color.WHITE);
canvas.drawRoundRect(new RectF(0, 10, gameBackground.width() / 2, 30),
2.0f, 2.0f, cPaint);
cPaint.setColor(Color.BLUE);
canvas.drawRoundRect(new RectF(2, 12, gameBackground.width() / 2 - 2,
28), 2.0f, 2.0f, cPaint);
cPaint.setColor(Color.WHITE);
canvas.drawText(msg, 22F, 22F, cPaint);
}
private void drawBackground(Canvas canvas) {
// TODO Auto-generated method stub
cPaint.setColor(Color.BLACK);
canvas.drawRect(gameBackground, cPaint);
for (int row = 0; row < tileImg.length; row++) {
for (int col = 0; col < tileImg[row].length; col++) {
canvas.drawBitmap(tileImg[row][col], row
* tileImg[row][col].getHeight(), col
* tileImg[row][col].getWidth(), null);
}
}
}
// remove following 2 methods or move them to Graphic2DUtil
public static Bitmap loadBitmapFromDrawable(Drawable sprite) {
Bitmap.Config DEFAULT_BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
int width = sprite.getIntrinsicWidth();
int height = sprite.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
DEFAULT_BITMAP_CONFIG);
Canvas canvas = new Canvas(bitmap);
sprite.setBounds(0, 0, width, height);
sprite.draw(canvas);
return bitmap;
}
public static Drawable cropBitmap(Bitmap sprite, int left, int top,
int right, int bottom) {
Bitmap croppedSprite = Bitmap.createBitmap(sprite, left, top, right,
bottom);
return null;
}
private ArrayList<String[]> populateWorld(int fileIndex) {
// ContentValues worldMap
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
国外的一款Android游戏源码 (317个子文件)
all-wcprops 1KB
all-wcprops 1016B
all-wcprops 980B
all-wcprops 950B
all-wcprops 918B
all-wcprops 722B
all-wcprops 689B
all-wcprops 605B
all-wcprops 599B
all-wcprops 585B
all-wcprops 582B
all-wcprops 508B
all-wcprops 482B
all-wcprops 466B
all-wcprops 340B
all-wcprops 286B
all-wcprops 257B
all-wcprops 232B
all-wcprops 228B
all-wcprops 228B
all-wcprops 224B
all-wcprops 220B
all-wcprops 198B
all-wcprops 186B
all-wcprops 176B
all-wcprops 170B
all-wcprops 89B
all-wcprops 86B
all-wcprops 85B
all-wcprops 78B
all-wcprops 78B
all-wcprops 78B
all-wcprops 74B
all-wcprops 71B
all-wcprops 70B
all-wcprops 70B
all-wcprops 70B
all-wcprops 69B
all-wcprops 66B
all-wcprops 66B
all-wcprops 66B
all-wcprops 66B
resources.ap_ 62KB
Hexagon.apk 86KB
HexagonGameThread.class 13KB
AStarPathFinder.class 7KB
SQLiteTestActivity.class 5KB
PlayerObject.class 5KB
HexagonGameView.class 4KB
LocationAnchor.class 3KB
AStarPathFinder$Node.class 3KB
MapCellImpl.class 3KB
ImageCrop.class 2KB
AStarPathFinder$PriorityList.class 2KB
HexagonDBHelper.class 2KB
MapCell.class 2KB
CSVUtil.class 2KB
Path.class 2KB
ImageGallery.class 2KB
HexagonGameActivity.class 2KB
Graphic2DUtil.class 2KB
MapConstants.class 2KB
WorldMap.class 1KB
PlayerObject$HorizontalDirection.class 1KB
PlayerObject$VerticalDirection.class 1KB
Path$Step.class 1KB
HexagonGameView$1.class 1KB
FrameRateCounter.class 958B
ClosestHeuristic.class 927B
ManhattanHeuristic.class 916B
ClosestSquaredHeuristic.class 872B
ImageCrop$Constants.class 636B
MapCell$Constants.class 612B
R.class 583B
R$drawable.class 571B
ImageGallery$Constants.class 564B
R$string.class 539B
R$raw.class 515B
WorldMap$Constants.class 511B
R$id.class 458B
R$layout.class 426B
TileBasedMap.class 397B
R$attr.class 343B
PathFindingContext.class 294B
AStarHeuristic.class 263B
PathFinder.class 247B
Mover.class 126B
.classpath 280B
stylesheet.css 1KB
map_vector.csv 648B
classes.dex 45KB
entries 2KB
entries 1KB
entries 1KB
entries 1KB
entries 1KB
entries 1KB
entries 1005B
entries 837B
entries 833B
共 317 条
- 1
- 2
- 3
- 4
资源评论
- yejum1232015-12-18对安卓的用户是很有帮助的
- wxdong_a2012-10-04不好,骗子。我的积分很少。。。
- nlwangxin2013-08-13一般控件的功能
- netseatdq2012-07-19一般控件的功能,还不错
- 超级射手李铁2012-09-27这个 简直就是骗积分。。。。
xp74993
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功