update
This commit is contained in:
1 parent
04c9b367ae
commit
406af7315b
7 files changed
+400
-484
No files matched your search
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,85 +1,5 @@
|
|||||||
/* AUTO-GENERATED FILE. DO NOT MODIFY.
|
|
||||||
*
|
|
||||||
* This class was automatically generated by the
|
|
||||||
* aapt tool from the resource data it found. It
|
|
||||||
* should not be modified by hand.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.adroid.game.pacman;
|
package com.adroid.game.pacman;
|
||||||
|
|
||||||
|
/* This stub is for using by IDE only. It is NOT the R class actually packed into APK */
|
||||||
public final class R {
|
public final class R {
|
||||||
public static final class attr {
|
|
||||||
}
|
|
||||||
public static final class drawable {
|
|
||||||
public static final int blue_gradien=0x7f020000;
|
|
||||||
public static final int corner=0x7f020001;
|
|
||||||
public static final int food=0x7f020002;
|
|
||||||
public static final int foodup=0x7f020003;
|
|
||||||
public static final int ghost=0x7f020004;
|
|
||||||
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 pacman=0x7f02000b;
|
|
||||||
public static final int pacman_final1=0x7f02000c;
|
|
||||||
public static final int pacmanta=0x7f02000d;
|
|
||||||
public static final int pacmantaa=0x7f02000e;
|
|
||||||
public static final int sfood=0x7f02000f;
|
|
||||||
public static final int streight=0x7f020010;
|
|
||||||
}
|
|
||||||
public static final class id {
|
|
||||||
public static final int menu_settings=0x7f080000;
|
|
||||||
}
|
|
||||||
public static final class layout {
|
|
||||||
public static final int activity_main=0x7f030000;
|
|
||||||
}
|
|
||||||
public static final class menu {
|
|
||||||
public static final int activity_main=0x7f070000;
|
|
||||||
}
|
|
||||||
public static final class raw {
|
|
||||||
public static final int lvl=0x7f040000;
|
|
||||||
public static final int lvl0=0x7f040001;
|
|
||||||
public static final int pacman_cherry=0x7f040002;
|
|
||||||
public static final int pacman_die=0x7f040003;
|
|
||||||
public static final int pacman_ghost_eating=0x7f040004;
|
|
||||||
public static final int pacman_siren=0x7f040005;
|
|
||||||
public static final int pacman_start=0x7f040006;
|
|
||||||
public static final int pacman_walk=0x7f040007;
|
|
||||||
public static final int pacmandie=0x7f040008;
|
|
||||||
}
|
|
||||||
public static final class string {
|
|
||||||
public static final int app_name=0x7f050000;
|
|
||||||
public static final int hello_world=0x7f050001;
|
|
||||||
public static final int menu_settings=0x7f050002;
|
|
||||||
}
|
|
||||||
public static final class style {
|
|
||||||
/**
|
|
||||||
Base application theme, dependent on API level. This theme is replaced
|
|
||||||
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
|
|
||||||
|
|
||||||
|
|
||||||
Theme customizations available in newer API levels can go in
|
|
||||||
res/values-vXX/styles.xml, while customizations related to
|
|
||||||
backward-compatibility can go here.
|
|
||||||
|
|
||||||
|
|
||||||
Base application theme for API 11+. This theme completely replaces
|
|
||||||
AppBaseTheme from res/values/styles.xml on API 11+ devices.
|
|
||||||
|
|
||||||
API 11 theme customizations can go here.
|
|
||||||
|
|
||||||
Base application theme for API 14+. This theme completely replaces
|
|
||||||
AppBaseTheme from BOTH res/values/styles.xml and
|
|
||||||
res/values-v11/styles.xml on API 14+ devices.
|
|
||||||
|
|
||||||
API 14 theme customizations can go here.
|
|
||||||
*/
|
|
||||||
public static final int AppBaseTheme=0x7f060000;
|
|
||||||
/** Application theme.
|
|
||||||
All customizations that are NOT specific to a particular API-level can go here.
|
|
||||||
*/
|
|
||||||
public static final int AppTheme=0x7f060001;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+6
-6
@@ -19,13 +19,13 @@ x.xx..0000000000000000.....x
|
|||||||
x.xxxxxxx0xxxxxxxxx0xxxxxx.x
|
x.xxxxxxx0xxxxxxxxx0xxxxxx.x
|
||||||
x.xxxxxxx.xxxxxxxxx.xxxxxx.x
|
x.xxxxxxx.xxxxxxxxx.xxxxxx.x
|
||||||
x.xxx...............xx.....x
|
x.xxx...............xx.....x
|
||||||
x.xxx.xxxxxxxx.xxxx.xx.xxxxx
|
x.xxx.xxxxxxx.xxxxx.xx.xxxxx
|
||||||
x.xxx.xxxxxxxx.xxxx.xx.xxxxx
|
x.xxx.xxxxxxx.xxxxx.xx.xxxxx
|
||||||
xo........................ox
|
xo........................ox
|
||||||
x.xxxxxxxxxxxx.xxxx.xxxxxx.x
|
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
|
||||||
x.xxxxxxxxxxxx.xxxx.xxxxxx.x
|
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
|
||||||
x.xxxxxxxxxxxx.xxxx.xxxxxx.x
|
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
|
||||||
x.xxxxxxxxxxxx.xxxx.xxxxxx.x
|
x.xxxxxxxxxxx.xxxxx.xxxxxx.x
|
||||||
x....xx.o..................x
|
x....xx.o..................x
|
||||||
x.xx.xx.xxxxxxxxxx.xxxxxxx.x
|
x.xx.xx.xxxxxxxxxx.xxxxxxx.x
|
||||||
x.xx....xxxxxxxxxx.xxxxxxx.x
|
x.xx....xxxxxxxxxx.xxxxxxx.x
|
||||||
|
|||||||
@@ -1,14 +1,8 @@
|
|||||||
package com.android.game.pacman.game;
|
package com.android.game.pacman.game;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Timer;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.BitmapFactory;
|
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Matrix;
|
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.hardware.Sensor;
|
import android.hardware.Sensor;
|
||||||
import android.hardware.SensorEvent;
|
import android.hardware.SensorEvent;
|
||||||
@@ -16,324 +10,318 @@ import android.hardware.SensorEventListener;
|
|||||||
import android.hardware.SensorManager;
|
import android.hardware.SensorManager;
|
||||||
import android.os.CountDownTimer;
|
import android.os.CountDownTimer;
|
||||||
import android.os.Vibrator;
|
import android.os.Vibrator;
|
||||||
import android.util.DisplayMetrics;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
import com.adroid.game.pacman.R;
|
import com.adroid.game.pacman.R;
|
||||||
import com.android.game.pacman.model.Block;
|
import com.android.game.pacman.model.*;
|
||||||
import com.android.game.pacman.model.Board;
|
|
||||||
import com.android.game.pacman.model.Collision;
|
|
||||||
import com.android.game.pacman.model.Ghost;
|
|
||||||
import com.android.game.pacman.model.PacMan;
|
|
||||||
import com.android.game.pacman.model.SolidObject;
|
|
||||||
import com.android.game.pacman.model.SoundStuff;
|
|
||||||
import com.android.game.pacman.model.Vect;
|
|
||||||
import com.android.game.pacman.utils.GameEnum;
|
import com.android.game.pacman.utils.GameEnum;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class GameLogic extends SurfaceView implements SurfaceHolder.Callback,
|
public class GameLogic extends SurfaceView implements SurfaceHolder.Callback,
|
||||||
SensorEventListener {
|
SensorEventListener {
|
||||||
|
|
||||||
private PacMan pacman;
|
private PacMan pacman;
|
||||||
|
|
||||||
private LinkedList<Block> wall;
|
private LinkedList<Block> wall;
|
||||||
private LinkedList<Block> path;
|
private LinkedList<Block> path;
|
||||||
private LinkedList<SolidObject> food;
|
private LinkedList<SolidObject> food;
|
||||||
private LinkedList<Ghost> ghosts;
|
private LinkedList<Ghost> ghosts;
|
||||||
|
|
||||||
public GameLoop loop;
|
public GameLoop loop;
|
||||||
private Board boardGame;
|
private Board boardGame;
|
||||||
private SoundStuff ss;
|
private SoundStuff ss;
|
||||||
private CountDownTimer task;
|
private CountDownTimer task;
|
||||||
private SensorManager sensorManager;
|
private SensorManager sensorManager;
|
||||||
private Vibrator v;
|
private Vibrator v;
|
||||||
|
|
||||||
private int points = 0;
|
private int points = 0;
|
||||||
private int eatInRow = 0; // mnoznik do zjedzonych duszkow
|
private int eatInRow = 0; // mnoznik do zjedzonych duszkow
|
||||||
private boolean accOn;
|
private boolean accOn;
|
||||||
private boolean start = false;
|
private boolean start = false;
|
||||||
|
|
||||||
private final static Paint paint = new Paint();
|
private final static Paint paint = new Paint();
|
||||||
public static int BOARD_TILE_SIZE;
|
public static int BOARD_TILE_SIZE;
|
||||||
public static int BOARD_HEIGHT;
|
public static int BOARD_HEIGHT;
|
||||||
public static int BOARD_WIDTH;
|
public static int BOARD_WIDTH;
|
||||||
public final static int GHOST_EAT_POINT = 200;
|
public final static int GHOST_EAT_POINT = 200;
|
||||||
public final static int FOODUP_POINT = 150;
|
public final static int FOODUP_POINT = 150;
|
||||||
public final static int FOOD_POINT = 50;
|
public final static int FOOD_POINT = 50;
|
||||||
public static int WIDTH;
|
public static int WIDTH;
|
||||||
public static int HEIGHT;
|
public static int HEIGHT;
|
||||||
|
|
||||||
public GameLogic(Context context, int w, int h) {
|
public GameLogic(Context context, int w, int h) {
|
||||||
super(context);
|
super(context);
|
||||||
setFocusable(true);
|
setFocusable(true);
|
||||||
getHolder().addCallback(this);
|
getHolder().addCallback(this);
|
||||||
|
|
||||||
WIDTH = w;
|
WIDTH = w;
|
||||||
HEIGHT = h;
|
HEIGHT = h;
|
||||||
|
|
||||||
loop = new GameLoop(getHolder(), this);
|
loop = new GameLoop(getHolder(), this);
|
||||||
ss = new SoundStuff(context);
|
ss = new SoundStuff(context);
|
||||||
paint.setStyle(Paint.Style.FILL);
|
paint.setStyle(Paint.Style.FILL);
|
||||||
// przez 10 sek po 5 zaczna mrugac duchy
|
// przez 10 sek po 5 zaczna mrugac duchy
|
||||||
task = new CountDownTimer(10000, 1000) {
|
task = new CountDownTimer(10000, 1000) {
|
||||||
private boolean first = false;
|
private boolean first = false;
|
||||||
private int ID;
|
private int ID;
|
||||||
int times = 0;
|
int times = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTick(long millisUntilFinished) {
|
public void onTick(long millisUntilFinished) {
|
||||||
|
|
||||||
if (!first) {
|
if (!first) {
|
||||||
ID = SoundStuff.sp.play(SoundStuff.ghostSiren, 1, 1, 1, -1,
|
ID = SoundStuff.sp.play(SoundStuff.ghostSiren, 1, 1, 1, -1,
|
||||||
1);
|
1);
|
||||||
|
|
||||||
for (Ghost ghost : ghosts) {
|
for (Ghost ghost : ghosts) {
|
||||||
ghost.setEatable(true);
|
ghost.setEatable(true);
|
||||||
}
|
}
|
||||||
first = true;
|
first = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (times == 5)
|
if (times == 5)
|
||||||
for (Ghost ghost : ghosts) {
|
for (Ghost ghost : ghosts) {
|
||||||
ghost.setBlinking(true);
|
ghost.setBlinking(true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
times++;
|
times++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFinish() {
|
public void onFinish() {
|
||||||
for (Ghost ghost : ghosts) {
|
for (Ghost ghost : ghosts) {
|
||||||
ghost.reset();
|
ghost.reset();
|
||||||
}
|
}
|
||||||
SoundStuff.sp.stop(ID);
|
SoundStuff.sp.stop(ID);
|
||||||
eatInRow = 0;
|
eatInRow = 0;
|
||||||
first = false;
|
first = false;
|
||||||
times = 0;
|
times = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(Canvas canvas) {
|
public void render(Canvas canvas) {
|
||||||
// tlo
|
// tlo
|
||||||
paint.setColor(Color.BLUE);
|
paint.setColor(Color.BLUE);
|
||||||
canvas.drawRect(0, 0, WIDTH, HEIGHT, paint);
|
canvas.drawRect(0, 0, WIDTH, HEIGHT, paint);
|
||||||
// napis z punktami
|
// napis z punktami
|
||||||
paint.setColor(Color.WHITE);
|
paint.setColor(Color.WHITE);
|
||||||
paint.setTextSize(30);
|
paint.setTextSize(30);
|
||||||
canvas.drawText("Points: " + points, 10, getHeight() - 40, paint);
|
canvas.drawText("Points: " + points, 10, getHeight() - 40, paint);
|
||||||
|
|
||||||
for (Block obj : path) {
|
for (Block obj : path) {
|
||||||
obj.draw(canvas);
|
obj.draw(canvas);
|
||||||
}
|
}
|
||||||
for (SolidObject obj : food) {
|
for (SolidObject obj : food) {
|
||||||
obj.draw(canvas);
|
obj.draw(canvas);
|
||||||
}
|
}
|
||||||
pacman.draw(canvas);
|
pacman.draw(canvas);
|
||||||
for (Ghost ghost : ghosts) {
|
for (Ghost ghost : ghosts) {
|
||||||
ghost.draw(canvas);
|
ghost.draw(canvas);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadNextLvl() {
|
public void loadNextLvl() {
|
||||||
task.cancel();
|
task.cancel();
|
||||||
pacman.stop();
|
pacman.stop();
|
||||||
boardGame = new Board(getResources(), R.raw.lvl);
|
boardGame = new Board(getResources(), R.raw.lvl);
|
||||||
|
|
||||||
wall = boardGame.getWall();
|
wall = boardGame.getWall();
|
||||||
path = boardGame.getPath();
|
path = boardGame.getPath();
|
||||||
food = boardGame.getbGame();
|
food = boardGame.getbGame();
|
||||||
pacman = new PacMan(getResources(), new Vect((BOARD_TILE_SIZE
|
pacman = new PacMan(getResources(), new Vect((BOARD_TILE_SIZE
|
||||||
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
|
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
|
||||||
boardGame.getBlock(), 100f, 3);
|
boardGame.getBlock(), 100f, 3);
|
||||||
ghosts = new LinkedList<Ghost>();
|
ghosts = new LinkedList<Ghost>();
|
||||||
|
|
||||||
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
||||||
11 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
11 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||||
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
||||||
13 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
13 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||||
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
||||||
15 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
15 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||||
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
||||||
17 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
17 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||||
int id = -1;
|
int id = -1;
|
||||||
do {
|
do {
|
||||||
id = SoundStuff.sp.play(SoundStuff.gameStart, 1, 1, 1, 0, 1);
|
id = SoundStuff.sp.play(SoundStuff.gameStart, 1, 1, 1, 0, 1);
|
||||||
} while (id == 0);
|
} while (id == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(double time) {
|
public void update(double time) {
|
||||||
|
|
||||||
pacman.update(time, getWidth(), getHeight());
|
pacman.update(time, getWidth(), getHeight());
|
||||||
for (Ghost ghost : ghosts) {
|
for (Ghost ghost : ghosts) {
|
||||||
ghost.update(time, getWidth(), getHeight());
|
ghost.update(time, getWidth(), getHeight());
|
||||||
}
|
}
|
||||||
otherCollision();
|
otherCollision();
|
||||||
handleCollsion();
|
handleCollsion();
|
||||||
if (food.isEmpty()) {
|
if (food.isEmpty()) {
|
||||||
loadNextLvl();
|
loadNextLvl();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doAi(int deficultiy) {
|
public void doAi(int deficultiy) {
|
||||||
switch (deficultiy) {
|
switch (deficultiy) {
|
||||||
case GameEnum.EASY:
|
case GameEnum.EASY:
|
||||||
// todo
|
// todo
|
||||||
break;
|
break;
|
||||||
// todo
|
// todo
|
||||||
case GameEnum.NORMAL:
|
case GameEnum.NORMAL:
|
||||||
break;
|
break;
|
||||||
case GameEnum.HARD:
|
case GameEnum.HARD:
|
||||||
// todo
|
// todo
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void otherCollision() {
|
private void otherCollision() {
|
||||||
for (SolidObject obj : food) {
|
for (SolidObject obj : food) {
|
||||||
if (Collision.detectNormalCollision(pacman.boundingRect,
|
if (Collision.detectNormalCollision(pacman.boundingRect,
|
||||||
obj.boundingRect)) {
|
obj.boundingRect)) {
|
||||||
obj.setIsTouched(true);
|
obj.setIsTouched(true);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Ghost ghost : ghosts) {
|
for (Ghost ghost : ghosts) {
|
||||||
if (Collision.detectNormalCollision(pacman.boundingRect,
|
if (Collision.detectNormalCollision(pacman.boundingRect,
|
||||||
ghost.boundingRect)) {
|
ghost.boundingRect)) {
|
||||||
if (ghost.isEatable()) {
|
if (ghost.isEatable() && !ghost.isOnlyEyes()) {
|
||||||
ghost.setIsTouched(true);
|
ghost.setIsTouched(true);
|
||||||
eatInRow++;
|
ghost.reset();
|
||||||
points += GHOST_EAT_POINT * eatInRow;
|
ghost.setEyes(true);
|
||||||
SoundStuff.sp.play(SoundStuff.pacmanEatGhost, 1, 1, 1, 0,
|
eatInRow++;
|
||||||
1f);
|
points += GHOST_EAT_POINT * eatInRow;
|
||||||
v.vibrate(100);
|
SoundStuff.sp.play(SoundStuff.pacmanEatGhost, 1, 1, 1, 0,
|
||||||
|
1f);
|
||||||
|
v.vibrate(100);
|
||||||
|
|
||||||
} else
|
} else if (!ghost.isOnlyEyes())
|
||||||
pacman.die();
|
pacman.die();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleCollsion() {
|
private void handleCollsion() {
|
||||||
for (int i = 0; i < food.size(); i++) {
|
for (int i = 0; i < food.size(); i++) {
|
||||||
if (food.get(i).getIsTouched()) {
|
if (food.get(i).getIsTouched()) {
|
||||||
if (food.get(i).kind == GameEnum.FOODUP) {
|
if (food.get(i).kind == GameEnum.FOODUP) {
|
||||||
points += FOODUP_POINT;
|
points += FOODUP_POINT;
|
||||||
v.vibrate(100);
|
v.vibrate(100);
|
||||||
task.onFinish();
|
task.onFinish();
|
||||||
task.start();
|
task.start();
|
||||||
|
|
||||||
} else
|
} else
|
||||||
points += FOOD_POINT;
|
points += FOOD_POINT;
|
||||||
|
|
||||||
food.remove(i);
|
food.remove(i);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(android.view.MotionEvent event) {
|
public boolean onTouchEvent(android.view.MotionEvent event) {
|
||||||
|
|
||||||
pacman.dir(event);
|
pacman.dir(event);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder holder, int format, int width,
|
public void surfaceChanged(SurfaceHolder holder, int format, int width,
|
||||||
int height) {
|
int height) {
|
||||||
WIDTH = width;
|
WIDTH = width;
|
||||||
HEIGHT = height;
|
HEIGHT = height;
|
||||||
BOARD_TILE_SIZE = (int) (WIDTH / BOARD_WIDTH);
|
BOARD_TILE_SIZE = (int) (WIDTH / BOARD_WIDTH);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
|
||||||
boardGame = new Board(getResources(), R.raw.lvl0);
|
boardGame = new Board(getResources(), R.raw.lvl0);
|
||||||
|
|
||||||
wall = boardGame.getWall();
|
wall = boardGame.getWall();
|
||||||
path = boardGame.getPath();
|
path = boardGame.getPath();
|
||||||
food = boardGame.getbGame();
|
food = boardGame.getbGame();
|
||||||
pacman = new PacMan(getResources(), new Vect((BOARD_TILE_SIZE
|
pacman = new PacMan(getResources(), new Vect((BOARD_TILE_SIZE
|
||||||
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
|
* BOARD_WIDTH / 2), BOARD_TILE_SIZE * 23), getContext(),
|
||||||
boardGame.getBlock(), 100f, 3);
|
boardGame.getBlock(), 100f, 3);
|
||||||
ghosts = new LinkedList<Ghost>();
|
ghosts = new LinkedList<Ghost>();
|
||||||
|
|
||||||
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
||||||
11 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
11 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||||
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
||||||
13 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
13 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||||
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
||||||
15 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
15 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||||
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
ghosts.add(new Ghost(2, boardGame.getBlock(), getResources(), new Vect(
|
||||||
17 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
17 * BOARD_TILE_SIZE, 14 * BOARD_TILE_SIZE)));
|
||||||
int id = -1;
|
int id = -1;
|
||||||
do {
|
do {
|
||||||
id = SoundStuff.sp.play(SoundStuff.gameStart, 1, 1, 1, 0, 1);
|
id = SoundStuff.sp.play(SoundStuff.gameStart, 1, 1, 1, 0, 1);
|
||||||
} while (id == 0);
|
} while (id == 0);
|
||||||
sensorManager = (SensorManager) getContext().getSystemService(
|
sensorManager = (SensorManager) getContext().getSystemService(
|
||||||
getContext().SENSOR_SERVICE);
|
getContext().SENSOR_SERVICE);
|
||||||
// add listener. The listener will be HelloAndroid (this) class
|
// add listener. The listener will be HelloAndroid (this) class
|
||||||
sensorManager.registerListener(this,
|
sensorManager.registerListener(this,
|
||||||
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
|
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
|
||||||
SensorManager.SENSOR_DELAY_GAME);
|
SensorManager.SENSOR_DELAY_GAME);
|
||||||
v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
|
v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
|
||||||
loop.setRunning(true);
|
loop.setRunning(true);
|
||||||
loop.start();
|
loop.start();
|
||||||
start = true;
|
start = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
boolean retry = true;
|
boolean retry = true;
|
||||||
while (retry) {
|
while (retry) {
|
||||||
try {
|
try {
|
||||||
loop.join();
|
loop.join();
|
||||||
retry = false;
|
retry = false;
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAccuracyChanged(Sensor arg0, int arg1) {
|
public void onAccuracyChanged(Sensor arg0, int arg1) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSensorChanged(SensorEvent event) {
|
public void onSensorChanged(SensorEvent event) {
|
||||||
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER && start) {
|
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER && start) {
|
||||||
|
|
||||||
pacman.dirAcc(event);
|
pacman.dirAcc(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAccOn() {
|
public boolean isAccOn() {
|
||||||
return accOn;
|
return accOn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccOn(boolean accOn) {
|
public void setAccOn(boolean accOn) {
|
||||||
this.accOn = accOn;
|
this.accOn = accOn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,179 +1,187 @@
|
|||||||
package com.android.game.pacman.model;
|
package com.android.game.pacman.model;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
|
||||||
|
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
|
|
||||||
import com.adroid.game.pacman.R;
|
import com.adroid.game.pacman.R;
|
||||||
import com.android.game.pacman.game.GameLogic;
|
import com.android.game.pacman.game.GameLogic;
|
||||||
import com.android.game.pacman.utils.GameEnum;
|
import com.android.game.pacman.utils.GameEnum;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class Ghost extends GameObject {
|
public class Ghost extends GameObject {
|
||||||
private LinkedList<GameEnum> movement;
|
private LinkedList<GameEnum> movement;
|
||||||
private Rect sourceRect;
|
private Rect sourceRect;
|
||||||
// uzywane kied duszek jest do zjedzenia
|
// uzywane kied duszek jest do zjedzenia
|
||||||
private int spriteEatHeight;
|
private int spriteEatHeight;
|
||||||
private int spriteEatWidth;
|
private int spriteEatWidth;
|
||||||
private Rect sourceEatRect;
|
private Rect sourceEatRect;
|
||||||
|
|
||||||
private Bitmap eyes;
|
private Bitmap eyes;
|
||||||
private Bitmap eatableBitmap;
|
private Bitmap eatableBitmap;
|
||||||
|
|
||||||
private boolean eatable = false;
|
private boolean eatable = false;
|
||||||
private boolean onlyEyes = false;
|
private boolean onlyEyes = false;
|
||||||
private boolean blinking = false;
|
private boolean blinking = false;
|
||||||
|
|
||||||
private double frameTicker;
|
private double frameTicker;
|
||||||
private double framePeriod = 0.5f;
|
private double framePeriod = 0.5f;
|
||||||
private boolean swap;
|
private boolean swap;
|
||||||
|
|
||||||
private double time; // do mrugania;
|
private double time; // do mrugania;
|
||||||
private int currentFrame;
|
private int currentFrame;
|
||||||
|
|
||||||
public Ghost(double speed, Block[][] board, Resources res, Vect initPosition) {
|
public Ghost(double speed, Block[][] board, Resources res, Vect initPosition) {
|
||||||
super(speed, board);
|
super(speed, board);
|
||||||
|
|
||||||
bitmap = BitmapFactory.decodeResource(res,
|
bitmap = BitmapFactory.decodeResource(res,
|
||||||
com.adroid.game.pacman.R.drawable.ghost);
|
com.adroid.game.pacman.R.drawable.ghost);
|
||||||
|
|
||||||
spriteHeight = bitmap.getHeight();
|
spriteHeight = bitmap.getHeight();
|
||||||
spriteWidth = bitmap.getWidth() / 4; // ilosc dostepnych klatek
|
spriteWidth = bitmap.getWidth() / 4; // ilosc dostepnych klatek
|
||||||
sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
|
sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
|
||||||
|
|
||||||
eatableBitmap = BitmapFactory
|
eatableBitmap = BitmapFactory
|
||||||
.decodeResource(res, R.drawable.ghost_eat2);
|
.decodeResource(res, R.drawable.ghost_eat2);
|
||||||
spriteEatHeight = eatableBitmap.getHeight();
|
spriteEatHeight = eatableBitmap.getHeight();
|
||||||
spriteEatWidth = eatableBitmap.getWidth() / 2; // ilosc dostepnych
|
spriteEatWidth = eatableBitmap.getWidth() / 2; // ilosc dostepnych
|
||||||
// klatek
|
// klatek
|
||||||
sourceEatRect = new Rect(0, 0, spriteWidth, spriteHeight);
|
sourceEatRect = new Rect(0, 0, spriteWidth, spriteHeight);
|
||||||
|
|
||||||
eyes = BitmapFactory.decodeResource(res, R.drawable.ghost_eye);
|
eyes = BitmapFactory.decodeResource(res, R.drawable.ghost_eye);
|
||||||
|
|
||||||
dirToChange = GameEnum.STOP;
|
dirToChange = GameEnum.STOP;
|
||||||
this.res = res;
|
this.res = res;
|
||||||
position = initPosition;
|
position = initPosition;
|
||||||
nextDir = GameEnum.STOP;
|
nextDir = GameEnum.STOP;
|
||||||
direction = new Vect(0, 0);
|
direction = new Vect(0, 0);
|
||||||
stop = false;
|
stop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doAi() {
|
public void doAi() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(double gameTime, int canvasWidth, int canvasHeiht) {
|
public void update(double gameTime, int canvasWidth, int canvasHeiht) {
|
||||||
|
|
||||||
move(gameTime);
|
move(gameTime);
|
||||||
calculateSourceRect(gameTime);
|
calculateSourceRect(gameTime);
|
||||||
canChangeDir();
|
canChangeDir();
|
||||||
if (eatable) {
|
if (eatable) {
|
||||||
if (isTouched) {
|
if (isTouched) {
|
||||||
eatable = false;
|
eatable = true;
|
||||||
onlyEyes = true;
|
onlyEyes = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Canvas canvas) {
|
public void draw(Canvas canvas) {
|
||||||
|
|
||||||
destRect.set((float) position.x - GameLogic.BOARD_TILE_SIZE / 2,
|
destRect.set((float) position.x - GameLogic.BOARD_TILE_SIZE / 2,
|
||||||
(float) position.y - GameLogic.BOARD_TILE_SIZE / 2,
|
(float) position.y - GameLogic.BOARD_TILE_SIZE / 2,
|
||||||
(float) position.x + spriteWidth / 2 + spriteWidth,
|
(float) position.x + spriteWidth / 2 + spriteWidth,
|
||||||
(float) position.y + spriteHeight / 2 + spriteHeight);
|
(float) position.y + spriteHeight / 2 + spriteHeight);
|
||||||
boundingRect.set((float) position.x, (float) position.y,
|
boundingRect.set((float) position.x, (float) position.y,
|
||||||
(float) position.x + spriteWidth, (float) position.y
|
(float) position.x + spriteWidth, (float) position.y
|
||||||
+ spriteHeight);
|
+ spriteHeight);
|
||||||
if (eatable) {
|
if (eatable) {
|
||||||
boundingRect.set((float) position.x, (float) position.y,
|
boundingRect.set((float) position.x, (float) position.y,
|
||||||
(float) position.x + spriteEatWidth, (float) position.y
|
(float) position.x + spriteEatWidth, (float) position.y
|
||||||
+ spriteEatHeight);
|
+ spriteEatHeight);
|
||||||
destRect.set((float) position.x - GameLogic.BOARD_TILE_SIZE / 2,
|
destRect.set((float) position.x - GameLogic.BOARD_TILE_SIZE / 2,
|
||||||
(float) position.y - GameLogic.BOARD_TILE_SIZE / 2,
|
(float) position.y - GameLogic.BOARD_TILE_SIZE / 2,
|
||||||
(float) position.x + spriteWidth / 2 + spriteWidth,
|
(float) position.x + spriteWidth / 2 + spriteWidth,
|
||||||
(float) position.y + spriteHeight / 2 + spriteHeight);
|
(float) position.y + spriteHeight / 2 + spriteHeight);
|
||||||
canvas.drawBitmap(eatableBitmap, sourceEatRect, destRect, null);
|
canvas.drawBitmap(eatableBitmap, sourceEatRect, destRect, null);
|
||||||
} else if (onlyEyes)
|
} else if (onlyEyes)
|
||||||
canvas.drawBitmap(eyes, (float) position.x, (float) position.y,
|
canvas.drawBitmap(eyes, (float) position.x, (float) position.y,
|
||||||
null);
|
null);
|
||||||
else
|
else
|
||||||
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
|
canvas.drawBitmap(bitmap, sourceRect, destRect, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEatable(boolean eat) {
|
public void setEatable(boolean eat) {
|
||||||
eatable = eat;
|
eatable = eat;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public void reset(){
|
|
||||||
time = 0;
|
|
||||||
currentFrame = 0;
|
|
||||||
framePeriod = 0.5f;
|
|
||||||
blinking = false;
|
|
||||||
eatable = false;
|
|
||||||
frameTicker = 0;
|
|
||||||
swap=false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEatable() {
|
public void reset() {
|
||||||
return eatable;
|
time = 0;
|
||||||
}
|
currentFrame = 0;
|
||||||
|
framePeriod = 0.5f;
|
||||||
|
blinking = false;
|
||||||
|
eatable = false;
|
||||||
|
frameTicker = 0;
|
||||||
|
swap = false;
|
||||||
|
}
|
||||||
|
|
||||||
private void calculateSourceRect(double gameTime) {
|
public boolean isEatable() {
|
||||||
|
return eatable;
|
||||||
|
}
|
||||||
|
|
||||||
if (!eatable) {
|
public boolean isOnlyEyes() {
|
||||||
if (angle == 270) {// gora
|
return onlyEyes;
|
||||||
sourceRect.left = 1 * spriteWidth;
|
}
|
||||||
sourceRect.right = sourceRect.left + spriteWidth;
|
|
||||||
|
|
||||||
}
|
private void calculateSourceRect(double gameTime) {
|
||||||
if (angle == 90) {// dol
|
|
||||||
sourceRect.left = 3 * spriteWidth;
|
|
||||||
sourceRect.right = sourceRect.left + spriteWidth;
|
|
||||||
|
|
||||||
}
|
if (!eatable) {
|
||||||
if (angle == 180) {// prawo
|
if (angle == 270) {// gora
|
||||||
sourceRect.left = 0 * spriteWidth;
|
sourceRect.left = 1 * spriteWidth;
|
||||||
sourceRect.right = sourceRect.left + spriteWidth;
|
sourceRect.right = sourceRect.left + spriteWidth;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (angle == 0) {// lewo
|
if (angle == 90) {// dol
|
||||||
sourceRect.left = 2 * spriteWidth;
|
sourceRect.left = 3 * spriteWidth;
|
||||||
sourceRect.right = sourceRect.left + spriteWidth;
|
sourceRect.right = sourceRect.left + spriteWidth;
|
||||||
|
|
||||||
}
|
}
|
||||||
} else if (eatable && blinking) {
|
if (angle == 180) {// prawo
|
||||||
frameTicker += gameTime;
|
sourceRect.left = 0 * spriteWidth;
|
||||||
time += gameTime;
|
sourceRect.right = sourceRect.left + spriteWidth;
|
||||||
if (frameTicker > framePeriod) {
|
|
||||||
frameTicker = 0;
|
|
||||||
// increment the frame
|
|
||||||
if (!swap)
|
|
||||||
currentFrame++;
|
|
||||||
else
|
|
||||||
currentFrame--;
|
|
||||||
if (currentFrame == 0 || currentFrame == 1) {
|
|
||||||
swap = !swap;
|
|
||||||
if (time > 1) {
|
|
||||||
time = 0;
|
|
||||||
framePeriod -= 0.08;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if (angle == 0) {// lewo
|
||||||
|
sourceRect.left = 2 * spriteWidth;
|
||||||
|
sourceRect.right = sourceRect.left + spriteWidth;
|
||||||
|
|
||||||
}
|
}
|
||||||
// define the rectangle to cut out sprite
|
} else if (eatable && blinking && !onlyEyes) {
|
||||||
sourceEatRect.left = currentFrame * spriteEatWidth;
|
frameTicker += gameTime;
|
||||||
sourceEatRect.right = sourceEatRect.left + spriteEatWidth;
|
time += gameTime;
|
||||||
}
|
if (frameTicker > framePeriod) {
|
||||||
|
frameTicker = 0;
|
||||||
|
// increment the frame
|
||||||
|
if (!swap)
|
||||||
|
currentFrame++;
|
||||||
|
else
|
||||||
|
currentFrame--;
|
||||||
|
if (currentFrame == 0 || currentFrame == 1) {
|
||||||
|
swap = !swap;
|
||||||
|
if (time > 1) {
|
||||||
|
time = 0;
|
||||||
|
framePeriod -= 0.08;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setBlinking(boolean blinking) {
|
}
|
||||||
this.blinking = blinking;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// define the rectangle to cut out sprite
|
||||||
|
sourceEatRect.left = currentFrame * spriteEatWidth;
|
||||||
|
sourceEatRect.right = sourceEatRect.left + spriteEatWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEyes(boolean set) {
|
||||||
|
onlyEyes = set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBlinking(boolean blinking) {
|
||||||
|
this.blinking = blinking;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user