troche optymalizacji, dodanie zmiany spriata duszka,...
This commit is contained in:
1 parent
bee72776e7
commit
de06fab9ba
11 files changed
+286
-193
No files matched your search
@@ -16,13 +16,16 @@ public final class R {
|
||||
public static final int food=0x7f020002;
|
||||
public static final int foodup=0x7f020003;
|
||||
public static final int ghost=0x7f020004;
|
||||
public static final int ic_launcher=0x7f020005;
|
||||
public static final int inky=0x7f020006;
|
||||
public static final int level1=0x7f020007;
|
||||
public static final int pacmanta=0x7f020008;
|
||||
public static final int pacmantaa=0x7f020009;
|
||||
public static final int sfood=0x7f02000a;
|
||||
public static final int streight=0x7f02000b;
|
||||
public static final int ghost_eat=0x7f020005;
|
||||
public static final int ghost_eat2=0x7f020006;
|
||||
public static final int ghost_eye=0x7f020007;
|
||||
public static final int ic_launcher=0x7f020008;
|
||||
public static final int inky=0x7f020009;
|
||||
public static final int level1=0x7f02000a;
|
||||
public static final int pacmanta=0x7f02000b;
|
||||
public static final int pacmantaa=0x7f02000c;
|
||||
public static final int sfood=0x7f02000d;
|
||||
public static final int streight=0x7f02000e;
|
||||
}
|
||||
public static final class id {
|
||||
public static final int menu_settings=0x7f080000;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.android.game.pacman.game;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
@@ -8,9 +8,9 @@ import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Paint;
|
||||
import android.os.Vibrator;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
@@ -28,47 +28,59 @@ import com.android.game.pacman.utils.GameEnum;
|
||||
public class GameLogic extends SurfaceView implements SurfaceHolder.Callback {
|
||||
|
||||
private PacMan pacman;
|
||||
|
||||
private LinkedList<Block> wall;
|
||||
private LinkedList<Block> path;
|
||||
private LinkedList<SolidObject> food;
|
||||
private LinkedList<Ghost> ghosts;
|
||||
|
||||
private GameLoop loop;
|
||||
private ArrayList<Block> wall;
|
||||
private ArrayList<Block> path;
|
||||
private ArrayList<SolidObject> food;
|
||||
private Board boardGame;
|
||||
private SoundStuff ss;
|
||||
private Bitmap background;
|
||||
private Ghost ghost1;
|
||||
private Ghost ghost2;
|
||||
private Ghost ghost3;
|
||||
private Ghost ghost4;
|
||||
private int points = 0;
|
||||
private int eatInRow = 0; // mnoznik do zjedzonych duszkow
|
||||
|
||||
private Paint paint;
|
||||
|
||||
private Vibrator v;
|
||||
|
||||
static int BOARD_TILE_SIZE = 17;
|
||||
static int BOARD_HEIGHT = 31;
|
||||
static int BOARD_WIDTH = 28;
|
||||
static int GHOST_EAT_POINT = 200;
|
||||
static int FOODUP_POINT = 150;
|
||||
static int FOOD_POINT = 50;
|
||||
|
||||
public GameLogic(Context context) {
|
||||
super(context);
|
||||
setFocusable(true);
|
||||
getHolder().addCallback(this);
|
||||
Bitmap tmpbackground = BitmapFactory.decodeResource(getResources(),
|
||||
R.drawable.blue_gradien);
|
||||
R.drawable.blue_gradien);
|
||||
|
||||
float scaleWidth = ((float) 480) / 256;
|
||||
float scaleHeight = ((float) 800) / 256;
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.postScale(scaleWidth, scaleHeight);
|
||||
background = Bitmap.createBitmap(tmpbackground, 0, 0,
|
||||
256, 256, matrix,
|
||||
background = Bitmap.createBitmap(tmpbackground, 0, 0, 256, 256, matrix,
|
||||
false);
|
||||
loop = new GameLoop(getHolder(), this);
|
||||
ss = new SoundStuff(context);
|
||||
|
||||
paint = new Paint();
|
||||
paint.setStyle(Paint.Style.FILL);
|
||||
v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
|
||||
}
|
||||
|
||||
public void render(Canvas canvas) {
|
||||
canvas.drawBitmap(background, 0f, 0f, null);
|
||||
// tlo
|
||||
paint.setColor(Color.BLUE);
|
||||
canvas.drawRect(0, 0, 480, 1000, paint);
|
||||
// napis z punktami
|
||||
paint.setColor(Color.WHITE);
|
||||
paint.setTextSize(30);
|
||||
canvas.drawText("Points: " + points, 10, getHeight() - 40, paint);
|
||||
|
||||
for (Block obj : wall) {
|
||||
obj.draw(canvas);
|
||||
}
|
||||
for (Block obj : path) {
|
||||
obj.draw(canvas);
|
||||
}
|
||||
@@ -76,10 +88,9 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback {
|
||||
obj.draw(canvas);
|
||||
}
|
||||
pacman.draw(canvas);
|
||||
ghost1.draw(canvas);
|
||||
ghost2.draw(canvas);
|
||||
ghost3.draw(canvas);
|
||||
ghost4.draw(canvas);
|
||||
for (Ghost ghost : ghosts) {
|
||||
ghost.draw(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
public void prepareLevel() {
|
||||
@@ -88,13 +99,15 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback {
|
||||
|
||||
public void update() {
|
||||
|
||||
pacman.update(System.currentTimeMillis(), getWidth(), getHeight());
|
||||
for (Ghost ghost : ghosts) {
|
||||
ghost.update(System.currentTimeMillis(), getWidth(), getHeight());
|
||||
}
|
||||
otherCollision();
|
||||
handleCollsion();
|
||||
pacman.update(System.currentTimeMillis(), getWidth(), getHeight());
|
||||
ghost1.update(System.currentTimeMillis(), getWidth(), getHeight());
|
||||
ghost2.update(System.currentTimeMillis(), getWidth(), getHeight());
|
||||
ghost3.update(System.currentTimeMillis(), getWidth(), getHeight());
|
||||
ghost4.update(System.currentTimeMillis(), getWidth(), getHeight());
|
||||
if (food.isEmpty()) {
|
||||
// do end game stuff
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -115,21 +128,43 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback {
|
||||
|
||||
private void otherCollision() {
|
||||
for (SolidObject obj : food) {
|
||||
if (Collision.detectNormalCollision(pacman.getBoundingRect(),
|
||||
obj.getBoundingRect())) {
|
||||
if (Collision.detectNormalCollision(pacman.boundingRect,
|
||||
obj.boundingRect)) {
|
||||
obj.setIsTouched(true);
|
||||
|
||||
}
|
||||
}
|
||||
for (Ghost ghost : ghosts) {
|
||||
if (Collision.detectNormalCollision(pacman.boundingRect,
|
||||
ghost.boundingRect)) {
|
||||
if (ghost.isEatable()) {
|
||||
ghost.setIsTouched(true);
|
||||
eatInRow++;
|
||||
points += GHOST_EAT_POINT * eatInRow;
|
||||
v.vibrate(100);
|
||||
|
||||
} else
|
||||
pacman.die();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void handleCollsion() {
|
||||
for (int i = 0; i < food.size(); i++) {
|
||||
if (food.get(i).getIsTouched()) {
|
||||
if (food.get(i).getKind() == GameEnum.FOODUP) {
|
||||
Vibrator v = (Vibrator) getContext().getSystemService(
|
||||
Context.VIBRATOR_SERVICE);
|
||||
if (food.get(i).kind == GameEnum.FOODUP) {
|
||||
points += FOODUP_POINT;
|
||||
v.vibrate(100);
|
||||
}
|
||||
|
||||
// TODO start timer!!
|
||||
for (Ghost ghost : ghosts) {
|
||||
ghost.setEatable(true);
|
||||
|
||||
}
|
||||
} else
|
||||
points += FOOD_POINT;
|
||||
|
||||
food.remove(i);
|
||||
|
||||
}
|
||||
@@ -160,11 +195,17 @@ public class GameLogic extends SurfaceView implements SurfaceHolder.Callback {
|
||||
food = boardGame.getbGame();
|
||||
pacman = new PacMan(getResources(), new Vect((int) (BOARD_TILE_SIZE
|
||||
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
|
||||
boardGame.getBlock(), 17, 5);
|
||||
ghost1=new Ghost(17, 2, boardGame.getBlock(),getResources(),new Vect(12*BOARD_TILE_SIZE, 14*BOARD_TILE_SIZE));
|
||||
ghost2=new Ghost(17, 2, boardGame.getBlock(),getResources(),new Vect(13*BOARD_TILE_SIZE, 14*BOARD_TILE_SIZE));
|
||||
ghost3=new Ghost(17, 2, boardGame.getBlock(),getResources(),new Vect(14*BOARD_TILE_SIZE, 14*BOARD_TILE_SIZE));
|
||||
ghost4=new Ghost(17, 2, boardGame.getBlock(),getResources(),new Vect(15*BOARD_TILE_SIZE, 14*BOARD_TILE_SIZE));
|
||||
boardGame.getBlock(), BOARD_TILE_SIZE, 3);
|
||||
ghosts = new LinkedList<Ghost>();
|
||||
|
||||
ghosts.add(new Ghost(17, 2, boardGame.getBlock(), getResources(),
|
||||
new Vect(12 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||
ghosts.add(new Ghost(17, 2, boardGame.getBlock(), getResources(),
|
||||
new Vect(13 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||
ghosts.add(new Ghost(17, 2, boardGame.getBlock(), getResources(),
|
||||
new Vect(14 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||
ghosts.add(new Ghost(17, 2, boardGame.getBlock(), getResources(),
|
||||
new Vect(15 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||
loop.setRunning(true);
|
||||
loop.start();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.android.game.pacman.game;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
public class GameLoop extends Thread {
|
||||
@@ -19,21 +20,38 @@ public class GameLoop extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long lastFpsTime = 0;
|
||||
//How many frames were in last FPS update
|
||||
int frameCounter = 0;
|
||||
|
||||
Canvas canvas;
|
||||
while (running) {
|
||||
canvas = null;
|
||||
try {
|
||||
|
||||
frameCounter++;
|
||||
|
||||
int delay = (int)(System.currentTimeMillis() - lastFpsTime);
|
||||
//If last FPS was calculated more than 1 second ago
|
||||
if (delay > 1000) {
|
||||
//Calculate FPS
|
||||
double FPS = (((double)frameCounter)/delay)*1000; //delay is in milliseconds, that's why *1000
|
||||
frameCounter = 0; //Reset frame counter
|
||||
lastFpsTime = System.currentTimeMillis(); //Reset fps timer
|
||||
|
||||
Log.v("FPS: ",String.valueOf(FPS));
|
||||
}
|
||||
gamePanel.update();
|
||||
|
||||
canvas = this.surfaceHolder.lockCanvas();
|
||||
synchronized (surfaceHolder) {
|
||||
gamePanel.update();
|
||||
|
||||
|
||||
gamePanel.render(canvas);
|
||||
}
|
||||
} finally {
|
||||
if (canvas != null) {
|
||||
|
||||
|
||||
surfaceHolder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setRunning(Boolean looping) {
|
||||
|
||||
@@ -12,31 +12,26 @@ public class Block extends SolidObject {
|
||||
private boolean selected = false;
|
||||
private int size;
|
||||
private Rect sourceRect;
|
||||
|
||||
private Paint paint;
|
||||
|
||||
|
||||
public Block(int x, int y, int size) {
|
||||
super(x * size, y * size);
|
||||
this.size = size;
|
||||
|
||||
paint =new Paint();
|
||||
paint.setColor(Color.BLACK);
|
||||
paint.setStyle(Paint.Style.FILL);
|
||||
}
|
||||
|
||||
public void draw(Canvas canvas) {
|
||||
Paint paint = new Paint();
|
||||
|
||||
boundingRect.set(x, y, x + size, y + size);
|
||||
if (kind == GameEnum.PATH) {
|
||||
paint.setColor(Color.BLACK);
|
||||
if (!isTouched)
|
||||
paint.setStyle(Paint.Style.FILL);
|
||||
else {
|
||||
paint.setStyle(Paint.Style.STROKE);
|
||||
isTouched = false;
|
||||
}
|
||||
canvas.drawRect(boundingRect, paint);
|
||||
if(kind==GameEnum.PATH)
|
||||
canvas.drawRect(boundingRect,paint);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -5,28 +5,22 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
import android.util.Log;
|
||||
|
||||
import com.adroid.game.pacman.R;
|
||||
import com.android.game.pacman.utils.GameEnum;
|
||||
|
||||
public class Board {
|
||||
|
||||
private ArrayList<SolidObject> bGame;
|
||||
private LinkedList<SolidObject> bGame;
|
||||
private int x;
|
||||
private int y;
|
||||
private int size;
|
||||
private char[][] lvl;
|
||||
private ArrayList<Block> path;
|
||||
private ArrayList<Block> wall;
|
||||
private LinkedList<Block> path;
|
||||
private LinkedList<Block> wall;
|
||||
private Block[][] blockTab;
|
||||
Resources res;
|
||||
|
||||
@@ -36,10 +30,10 @@ public class Board {
|
||||
this.size = size;
|
||||
this.res = res;
|
||||
blockTab = new Block[x][y];
|
||||
bGame = new ArrayList<SolidObject>(x * y);
|
||||
bGame = new LinkedList<SolidObject>();
|
||||
lvl = readLvl(x, y, res);
|
||||
path = new ArrayList<Block>();
|
||||
wall = new ArrayList<Block>();
|
||||
path = new LinkedList<Block>();
|
||||
wall = new LinkedList<Block>();
|
||||
// Bitmap bitmap=
|
||||
for (int i = 0; i < y; i++) {
|
||||
for (int j = 0; j < x; j++) {
|
||||
@@ -84,13 +78,13 @@ public class Board {
|
||||
}
|
||||
|
||||
private void setFood(int x, int y, Resources res) {
|
||||
Food newF = new Food(x, y, res,GameEnum.FOOD);
|
||||
Food newF = new Food(x, y, res);
|
||||
bGame.add(newF);
|
||||
setPath(x, y);
|
||||
}
|
||||
|
||||
private void setFoodUp(int x, int y, Resources res) {
|
||||
Food newF = new Food(x, y, res,GameEnum.FOODUP);
|
||||
FoodUp newF = new FoodUp(x, y, res);
|
||||
|
||||
bGame.add(newF);
|
||||
setPath(x, y);
|
||||
@@ -101,15 +95,15 @@ public class Board {
|
||||
return blockTab;
|
||||
}
|
||||
|
||||
public ArrayList<SolidObject> getbGame() {
|
||||
public LinkedList<SolidObject> getbGame() {
|
||||
return bGame;
|
||||
}
|
||||
|
||||
public ArrayList<Block> getPath() {
|
||||
public LinkedList<Block> getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public ArrayList<Block> getWall() {
|
||||
public LinkedList<Block> getWall() {
|
||||
return wall;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,16 +10,17 @@ import com.android.game.pacman.utils.GameEnum;
|
||||
public class Food extends SolidObject {
|
||||
|
||||
|
||||
|
||||
public Food(int x,int y,Resources res,GameEnum kind){
|
||||
|
||||
public Food(int x,int y,Resources res){
|
||||
super(x*17, y*17);
|
||||
if(kind ==GameEnum.FOOD)
|
||||
bitmap = BitmapFactory.decodeResource(res,
|
||||
com.adroid.game.pacman.R.drawable.food);
|
||||
else
|
||||
kind =GameEnum.FOOD;
|
||||
if(bitmap==null){
|
||||
bitmap = BitmapFactory.decodeResource(res,
|
||||
com.adroid.game.pacman.R.drawable.foodup);
|
||||
this.kind=kind;
|
||||
com.adroid.game.pacman.R.drawable.food);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import com.android.game.pacman.utils.GameEnum;
|
||||
public class GameObject {
|
||||
protected Vect position;
|
||||
protected Bitmap bitmap;
|
||||
Rect boundingRect = new Rect();
|
||||
public Rect boundingRect = new Rect();
|
||||
protected boolean isTouched = false;
|
||||
protected GameEnum dirToChange;
|
||||
protected int speed;
|
||||
@@ -24,54 +24,54 @@ public class GameObject {
|
||||
private Block[][] board;
|
||||
protected GameEnum nextDir;
|
||||
protected Rect sourceRect; // the rectangle to be drawn from the animation
|
||||
protected int spriteWidth; // the width of the sprite to calculate the cut out
|
||||
protected int spriteHeight;
|
||||
protected int spriteWidth; // the width of the sprite to calculate the cut
|
||||
// out
|
||||
protected int spriteHeight;
|
||||
protected Resources res;
|
||||
|
||||
public void update() {
|
||||
|
||||
}
|
||||
public GameObject(int size,int speed,Block[][] board){
|
||||
this.speed=speed;
|
||||
this.size=size;
|
||||
this.board=board;
|
||||
|
||||
public GameObject(int size, int speed, Block[][] board) {
|
||||
this.speed = speed;
|
||||
this.size = size;
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
public void rotate(double angle) {
|
||||
rotate = true;
|
||||
this.angle = angle;
|
||||
}
|
||||
|
||||
public void draw(Canvas canvas) {
|
||||
canvas.drawBitmap(bitmap, position.x, position.y, null);
|
||||
boundingRect = new Rect(position.x, position.y, position.x
|
||||
+ bitmap.getWidth(), position.y + bitmap.getHeight());
|
||||
|
||||
boundingRect.set(position.x, position.y, position.x + spriteWidth,
|
||||
position.y + spriteHeight);
|
||||
canvas.drawBitmap(bitmap, sourceRect, boundingRect, null);
|
||||
}
|
||||
|
||||
public Rect getBoundingRect() {
|
||||
return boundingRect;
|
||||
}
|
||||
synchronized public void move() {
|
||||
|
||||
if (dirToChange == GameEnum.UP) {
|
||||
direction = new Vect(0, -1);
|
||||
rotate(270);
|
||||
}
|
||||
if (dirToChange == GameEnum.DOWN) {
|
||||
direction = new Vect(0, 1);
|
||||
rotate(90);
|
||||
}
|
||||
if (dirToChange == GameEnum.LEFT) {
|
||||
direction = new Vect(-1, 0);
|
||||
rotate(180);
|
||||
}
|
||||
if (dirToChange == GameEnum.RIGHT) {
|
||||
direction = new Vect(1, 0);
|
||||
rotate(0);
|
||||
}
|
||||
if (target == null) {
|
||||
if (dirToChange == GameEnum.UP) {
|
||||
direction = new Vect(0, -1);
|
||||
rotate(270);
|
||||
}
|
||||
if (dirToChange == GameEnum.DOWN) {
|
||||
direction = new Vect(0, 1);
|
||||
rotate(90);
|
||||
}
|
||||
if (dirToChange == GameEnum.LEFT) {
|
||||
direction = new Vect(-1, 0);
|
||||
rotate(180);
|
||||
}
|
||||
if (dirToChange == GameEnum.RIGHT) {
|
||||
direction = new Vect(1, 0);
|
||||
rotate(0);
|
||||
}
|
||||
if (canMove(position, direction)) {
|
||||
stop = false;
|
||||
|
||||
|
||||
target = position.add(direction.mulitply(size));
|
||||
} else
|
||||
stop = true;
|
||||
@@ -82,28 +82,32 @@ public class GameObject {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean moveTowards(Vect goal) {
|
||||
if (position.equals(goal))
|
||||
return true;
|
||||
Vect dir = Vect.normalize(goal.subtract(position)); //kierunek ruchu
|
||||
position = position.add(dir.mulitply(speed)); //nowa pozycja
|
||||
|
||||
if (Math.abs(Vect.dot(dir, Vect.normalize(goal.subtract(position))) + 1) < speed) //jezeli minął cel
|
||||
Vect dir = Vect.normalize(goal.subtract(position)); // kierunek ruchu
|
||||
position = position.add(dir.mulitply(speed)); // nowa pozycja
|
||||
|
||||
if (Math.abs(Vect.dot(dir, Vect.normalize(goal.subtract(position))) + 1) < speed) // jezeli
|
||||
// minął
|
||||
// cel
|
||||
position = goal;
|
||||
|
||||
return position.equals(goal);
|
||||
return position.equals(goal);
|
||||
}
|
||||
|
||||
private boolean canMove(Vect position, Vect direction) {
|
||||
if (direction == null)
|
||||
return false;
|
||||
int nx = (int) (position.x / size) + (int) direction.x;
|
||||
int ny = (int) (position.y / size) + (int) direction.y;
|
||||
if (nx < 0 || ny < 0 || nx >= board.length
|
||||
|| ny >= board[0].length)
|
||||
if (nx < 0 || ny < 0 || nx >= board.length || ny >= board[0].length)
|
||||
return false;
|
||||
return board[nx][ny].kind == GameEnum.PATH;
|
||||
|
||||
}
|
||||
|
||||
private boolean check(GameEnum dir) {
|
||||
Vect newdir = null;
|
||||
if (dir == GameEnum.UP)
|
||||
@@ -118,22 +122,26 @@ public class GameObject {
|
||||
return canMove(position, newdir);
|
||||
}
|
||||
|
||||
|
||||
public void setDirection(GameEnum dir) {
|
||||
|
||||
if (check(dir)) {
|
||||
dirToChange=dir;
|
||||
if (position.x % 17 == 0 && position.y % 17 == 0) {
|
||||
if (check(dir))
|
||||
dirToChange = dir;
|
||||
} else
|
||||
nextDir = dir;
|
||||
|
||||
}
|
||||
|
||||
public void canChangeDir() {
|
||||
if (check(nextDir)) {
|
||||
|
||||
dirToChange=nextDir;
|
||||
nextDir = GameEnum.STOP;
|
||||
if (position.x % 17 == 0 && position.y % 17 == 0) {
|
||||
if (check(nextDir)) {
|
||||
dirToChange = nextDir;
|
||||
nextDir = GameEnum.STOP;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public void setIsTouched(boolean state){
|
||||
isTouched=state;
|
||||
}
|
||||
}
|
||||
@@ -3,21 +3,40 @@ package com.android.game.pacman.model;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.util.Log;
|
||||
|
||||
import com.adroid.game.pacman.R;
|
||||
import com.android.game.pacman.utils.GameEnum;
|
||||
|
||||
public class Ghost extends GameObject {
|
||||
private LinkedList<GameEnum> movement;
|
||||
private int lvl;// table of actual lvl importanto to do ai
|
||||
private Rect sourceRect;
|
||||
private boolean eatable=false;
|
||||
public Ghost(int size, int speed, Block[][] board, Resources res,Vect initPosition) {
|
||||
|
||||
private Bitmap eyes;
|
||||
private Bitmap eatableBitmap;
|
||||
|
||||
private boolean eatable = false;
|
||||
private boolean onlyEyes = false;
|
||||
private boolean blinking = false;
|
||||
|
||||
private int farame = 0;
|
||||
private int framecount = 0;
|
||||
private long frameTicker;
|
||||
private int framePeriod;
|
||||
private boolean swap;
|
||||
|
||||
public Ghost(int size, int speed, Block[][] board, Resources res,
|
||||
Vect initPosition) {
|
||||
super(size, speed, board);
|
||||
bitmap = BitmapFactory.decodeResource(res,
|
||||
com.adroid.game.pacman.R.drawable.ghost);
|
||||
eatableBitmap = BitmapFactory.decodeResource(res, R.drawable.ghost_eat);
|
||||
eyes = BitmapFactory.decodeResource(res, R.drawable.ghost_eye);
|
||||
spriteHeight = bitmap.getHeight();
|
||||
spriteWidth = bitmap.getWidth() / 4; // ilosc dostepnych klatek
|
||||
sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
|
||||
@@ -27,7 +46,7 @@ public class Ghost extends GameObject {
|
||||
nextDir = GameEnum.STOP;
|
||||
direction = new Vect(0, 0);
|
||||
this.size = size;
|
||||
stop=false;
|
||||
stop = false;
|
||||
}
|
||||
|
||||
public void doAi() {
|
||||
@@ -37,40 +56,63 @@ public class Ghost extends GameObject {
|
||||
public void update(long gameTime, int canvasWidth, int canvasHeiht) {
|
||||
|
||||
move();
|
||||
calculateSourceRect();
|
||||
calculateSourceRect(gameTime);
|
||||
canChangeDir();
|
||||
if (eatable) {
|
||||
if (isTouched) {
|
||||
eatable = false;
|
||||
onlyEyes = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
Rect destRect = new Rect(position.x, position.y, position.x
|
||||
+ spriteWidth, position.y + spriteHeight);
|
||||
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
|
||||
boundingRect = destRect;
|
||||
|
||||
boundingRect.set(position.x, position.y, position.x + spriteWidth,
|
||||
position.y + spriteHeight);
|
||||
if (eatable) {
|
||||
canvas.drawBitmap(eatableBitmap, position.x, position.y, null);
|
||||
} else if (onlyEyes)
|
||||
canvas.drawBitmap(eyes, position.x, position.y, null);
|
||||
else
|
||||
canvas.drawBitmap(bitmap, sourceRect, boundingRect, null);
|
||||
}
|
||||
|
||||
private void calculateSourceRect() {
|
||||
public void setEatable(boolean eat) {
|
||||
eatable = eat;
|
||||
|
||||
if (angle == 270) {// gora
|
||||
sourceRect.left = 1 * spriteWidth;
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
}
|
||||
|
||||
}
|
||||
if (angle == 90) {// dol
|
||||
sourceRect.left = 3 * spriteWidth;
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
public boolean isEatable() {
|
||||
return eatable;
|
||||
}
|
||||
|
||||
}
|
||||
if (angle == 180) {// prawo
|
||||
sourceRect.left = 0 * spriteWidth;
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
private void calculateSourceRect(long gameTime) {
|
||||
|
||||
if (!eatable || !onlyEyes) {
|
||||
if (angle == 270) {// gora
|
||||
sourceRect.left = 1 * spriteWidth;
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
|
||||
}
|
||||
if (angle == 0) {// lewo
|
||||
sourceRect.left = 2 * spriteWidth;
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
}
|
||||
if (angle == 90) {// dol
|
||||
sourceRect.left = 3 * spriteWidth;
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
|
||||
}
|
||||
if (angle == 180) {// prawo
|
||||
sourceRect.left = 0 * spriteWidth;
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
|
||||
}
|
||||
if (angle == 0) {// lewo
|
||||
sourceRect.left = 2 * spriteWidth;
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
|
||||
}
|
||||
} else if (eatable && blinking) {
|
||||
//TODO change spite
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.android.game.pacman.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.BitmapFactory;
|
||||
@@ -13,19 +11,17 @@ import com.android.game.pacman.utils.GameEnum;
|
||||
|
||||
public class PacMan extends GameObject {
|
||||
|
||||
|
||||
// rectangle
|
||||
|
||||
|
||||
private int frameNr; // number of frames in animation
|
||||
private int currentFrame = 0; // the current frame
|
||||
private double frameTicker; // the time of the last frame update
|
||||
private int framePeriod; // milliseconds between each frame (1000/fps)
|
||||
// the height of the sprite
|
||||
// the height of the sprite
|
||||
private Resources res;
|
||||
private float x1, x2, y1, y2, dx, dy;
|
||||
private boolean isPlaying = false;
|
||||
boolean swap = false;
|
||||
|
||||
public PacMan(Resources res, Vect initPosition, Context context,
|
||||
Block[][] board, int size, int speed) {
|
||||
super(size, speed, board);
|
||||
@@ -46,29 +42,26 @@ public class PacMan extends GameObject {
|
||||
direction = new Vect(0, 1);
|
||||
|
||||
this.size = size;
|
||||
stop=true;
|
||||
stop = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
Rect destRect = new Rect(position.x, position.y, position.x
|
||||
+ spriteWidth, position.y + spriteHeight);
|
||||
if (rotate) {
|
||||
boundingRect.set(position.x, position.y, position.x + spriteWidth,
|
||||
position.y + spriteHeight);
|
||||
|
||||
|
||||
canvas.save();
|
||||
canvas.rotate((float) angle,
|
||||
(float) position.x + (spriteWidth / 2), (float) position.y
|
||||
+ (spriteHeight / 2));
|
||||
|
||||
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
|
||||
canvas.drawBitmap(bitmap, sourceRect, boundingRect, null);
|
||||
canvas.restore();
|
||||
|
||||
rotate = false;
|
||||
} else {
|
||||
|
||||
|
||||
|
||||
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
|
||||
|
||||
}
|
||||
boundingRect = destRect;
|
||||
}
|
||||
|
||||
// the update method for ship
|
||||
@@ -76,7 +69,7 @@ public class PacMan extends GameObject {
|
||||
|
||||
move();
|
||||
calculateSourceRect(gameTime);
|
||||
borders();
|
||||
//borders();
|
||||
handleSound();
|
||||
canChangeDir();
|
||||
|
||||
@@ -163,4 +156,10 @@ public class PacMan extends GameObject {
|
||||
sourceRect.right = sourceRect.left + spriteWidth;
|
||||
}
|
||||
|
||||
public void die() {
|
||||
stop=true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,10 +9,10 @@ import com.android.game.pacman.utils.GameEnum;
|
||||
public class SolidObject {
|
||||
protected int x;
|
||||
protected int y;
|
||||
protected Bitmap bitmap;
|
||||
Rect boundingRect = new Rect();
|
||||
static Bitmap bitmap;
|
||||
public Rect boundingRect = new Rect();
|
||||
protected boolean isTouched = false;
|
||||
protected GameEnum kind;
|
||||
public GameEnum kind;
|
||||
protected boolean rotate = false;
|
||||
protected double angle = 0;
|
||||
public SolidObject(int x, int y){
|
||||
@@ -25,9 +25,7 @@ public class SolidObject {
|
||||
canvas.drawBitmap(bitmap, x, y, null);
|
||||
boundingRect = new Rect(x, y, x + bitmap.getWidth(),y + bitmap.getHeight());
|
||||
}
|
||||
public Rect getBoundingRect() {
|
||||
return boundingRect;
|
||||
}
|
||||
|
||||
|
||||
public void setIsTouched(boolean b) {
|
||||
isTouched = b;
|
||||
@@ -37,9 +35,8 @@ public class SolidObject {
|
||||
return isTouched;
|
||||
}
|
||||
|
||||
public GameEnum getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
|
||||
public void rotate(double angle) {
|
||||
rotate = true;
|
||||
this.angle = angle;
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.android.game.pacman.model;
|
||||
|
||||
public class Stage {
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user