GameModel使用单例模式

dev_1027
xiaoshengjie 2 years ago
parent b563018987
commit 10d5726d64

@ -13,13 +13,12 @@ import java.awt.*;
public class Bullet extends GameObject {
public static final int bWidth = ImageManger.bulletL.getWidth();
public static final int bHeight = ImageManger.bulletL.getHeight();
// public int x, y;
// public int x, y;
private Dir dir = Dir.DOWN;
private final int speed = 10;
private boolean living = true;
//TankFrame tf = null;
public Group group = Group.BAD;
public GameModel gm;
public Rectangle rectangle = new Rectangle();
public Group getGroup() {
@ -30,26 +29,25 @@ public class Bullet extends GameObject {
this.group = group;
}
public Bullet(int x, int y, Dir dir, Group group, GameModel gm) {
public Bullet(int x, int y, Dir dir, Group group) {
super();
this.x = x;
this.y = y;
this.dir = dir;
this.gm = gm;
this.group = group;
this.rectangle.x = x;
this.rectangle.y = y;
this.rectangle.width = bWidth;
this.rectangle.height = bHeight;
gm.add(this);
new Thread(() -> {
new Audio("audio/tank_fire.wav");
}).start();
GameModel.INSTANCE.add(this);
}
public void paint(Graphics g) {
if (!living) gm.remove(this);
if (!living) GameModel.INSTANCE.remove(this);
switch (dir) {
case DOWN:
g.drawImage(ImageManger.bulletD, x, y, null);
@ -104,7 +102,7 @@ public class Bullet extends GameObject {
if (rect1.intersects(rect2)) {
tank.die();
this.die();
gm.add(new Explode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, gm));
new Explode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2);
}
}

@ -13,19 +13,18 @@ public class Explode extends GameObject {
public static final int bWidth = ImageManger.explodes[0].getWidth();
public static final int bHeight = ImageManger.explodes[0].getHeight();
private int x, y;
public GameModel gm;
private int step = 0;
public Explode(int x, int y, GameModel gameModel) {
public Explode(int x, int y) {
this.x = x;
this.y = y;
this.gm = gameModel;
new Thread(new Runnable() {
public void run() {
new Audio("audio/explode.wav").play();
}
}).start();
GameModel.INSTANCE.add(this);
}
@ -33,7 +32,7 @@ public class Explode extends GameObject {
public void paint(Graphics g) {
g.drawImage(ImageManger.explodes[step++], x, y, null);
if (step >= ImageManger.explodes.length) {
gm.remove(this);
GameModel.INSTANCE.remove(this);
}
}
}

@ -15,22 +15,28 @@ import java.util.List;
* @date 2022/10/27 14:30
*/
public class GameModel {
public Tank myTank = new Tank(150, 150, Dir.DOWN, Group.GOOD, this);
public static GameModel INSTANCE = new GameModel();
static {
INSTANCE.init();
}
Tank myTank;
List<GameObject> gameModelList = new ArrayList<>();
//Collider collider1 = new BulletAndTankCollider();//子弹和坦克的碰撞
//Collider collider2 = new TankTankCollider();//坦克和坦克的碰撞
ColliderChain chain = new ColliderChain();
public int count = 0;
public GameModel() {
gameModelList.add(myTank);
}
public void init() {
myTank = new Tank(150, 150, Dir.DOWN, Group.GOOD);
//初始化敌人坦克
int initCountTank = Integer.parseInt((String) PropertyMgr.get("initTankCount"));
for (int i = 0; i < initCountTank; i++) {
gameModelList.add(new Tank(80 + i * 100, 50, Dir.DOWN, Group.BAD, this));
new Tank(80 + i * 100, 50, Dir.DOWN, Group.BAD);
}
add(new Wall(200,200,50,100,this));
add(new Wall(500,200,100,200,this));
new Wall(200, 200, 50, 100);
new Wall(500, 200, 100, 200);
}
public void add(GameObject object) {
@ -52,7 +58,7 @@ public class GameModel {
GameObject o2 = gameModelList.get(j);
//collider1.collider(o1, o2, this);//
//collider2.collider(o1, o2, this);
if (chain.collider(o1, o2, this)) return;
if (chain.collider(o1, o2)) return;
}
}
}

@ -25,18 +25,16 @@ public class Tank extends GameObject {
//public TankFrame tankFrame = null;
private Random random = new Random();
public Group group = Group.BAD;
public GameModel gm;
public Rectangle rectangle = new Rectangle();
int oldX, oldY;
public String uuid;
public Tank(int x, int y, Dir dir, Group group, GameModel gm) {
public Tank(int x, int y, Dir dir, Group group) {
super();
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.gm = gm;
this.oldY = y;
this.oldX = x;
this.uuid = UUID.randomUUID().toString();
@ -45,6 +43,7 @@ public class Tank extends GameObject {
rectangle.y = y;
rectangle.width = Tank.tankWidth;
rectangle.height = Tank.tankHeight;
GameModel.INSTANCE.add(this);
}
@ -81,7 +80,7 @@ public class Tank extends GameObject {
System.out.println("tank paint:" + this.toString());
if (!living) {
System.out.println("tank remove:" + this.toString());
gm.remove(this);
GameModel.INSTANCE.remove(this);
return;
}
switch (this.dir) {
@ -161,20 +160,20 @@ public class Tank extends GameObject {
int by = y + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2;
switch (dir) {
case DOWN:
new Bullet(bx, by + 6, this.dir, this.getGroup(), this.gm);
new Bullet(bx, by - 6, this.dir, this.getGroup(), this.gm);
new Bullet(bx, by + 6, this.dir, this.getGroup());
new Bullet(bx, by - 6, this.dir, this.getGroup());
break;
case UP:
new Bullet(bx + 1, by + 6, this.dir, this.getGroup(), this.gm);
new Bullet(bx + 1, by - 6, this.dir, this.getGroup(), this.gm);
new Bullet(bx + 1, by + 6, this.dir, this.getGroup());
new Bullet(bx + 1, by - 6, this.dir, this.getGroup());
break;
case LEFT:
new Bullet(bx + 6, by, this.dir, this.getGroup(), this.gm);
new Bullet(bx - 6, by, this.dir, this.getGroup(), this.gm);
new Bullet(bx + 6, by, this.dir, this.getGroup());
new Bullet(bx - 6, by, this.dir, this.getGroup());
break;
case RIGHT:
new Bullet(bx + 6, by + 1, this.dir, this.getGroup(), this.gm);
new Bullet(bx - 6, by + 1, this.dir, this.getGroup(), this.gm);
new Bullet(bx + 6, by + 1, this.dir, this.getGroup());
new Bullet(bx - 6, by + 1, this.dir, this.getGroup());
break;
}
}

@ -17,7 +17,7 @@ import java.util.ArrayList;
public class TankFrame extends Frame {
public static final int GAME_WIDTH = Integer.parseInt((String) PropertyMgr.get("gameWidth"));
public static final int GAME_HEIGHT = Integer.parseInt((String) PropertyMgr.get("gameHeight"));
GameModel gameModel = new GameModel();
// GameModel gameModel = new GameModel();
// public GameFactory gf = new DefaultFactory();
public TankFrame() {
@ -55,7 +55,7 @@ public class TankFrame extends Frame {
@Override
public void paint(Graphics g) {
gameModel.paint(g);
GameModel.INSTANCE.paint(g);
}
/**
@ -105,7 +105,7 @@ public class TankFrame extends Frame {
bD = false;
break;
case KeyEvent.VK_SPACE:
gameModel.myTank.fire();
GameModel.INSTANCE.myTank.fire();
break;
}
setDirMethod();
@ -113,14 +113,14 @@ public class TankFrame extends Frame {
public void setDirMethod() {
if (!bD && !bU && !bR && !bL) {
gameModel.myTank.setMoving(false);
GameModel.INSTANCE.myTank.setMoving(false);
} else {
gameModel.myTank.setMoving(true);
GameModel.INSTANCE.myTank.setMoving(true);
}
if (bL) gameModel.myTank.setDir(Dir.LEFT);
if (bR) gameModel.myTank.setDir(Dir.RIGHT);
if (bU) gameModel.myTank.setDir(Dir.UP);
if (bD) gameModel.myTank.setDir(Dir.DOWN);
if (bL) GameModel.INSTANCE.myTank.setDir(Dir.LEFT);
if (bR) GameModel.INSTANCE.myTank.setDir(Dir.RIGHT);
if (bU) GameModel.INSTANCE.myTank.setDir(Dir.UP);
if (bD) GameModel.INSTANCE.myTank.setDir(Dir.DOWN);
}
}

@ -8,9 +8,7 @@ package com.study.tank;
public class TestMain {
public static void main(String[] args) throws InterruptedException {
TankFrame tf = new TankFrame();
new Thread(() -> {
new Audio("audio/war1.wav").loop();
}).start();

@ -9,16 +9,15 @@ import java.awt.*;
public class Wall extends GameObject {
int width;
int height;
public GameModel gm;
public Rectangle rectangle;
public Wall(int x, int y, int width, int height, GameModel gameModel) {
public Wall(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.gm = gameModel;
rectangle = new Rectangle(x, y, width, height);
GameModel.INSTANCE.add(this);
}
@Override

@ -11,18 +11,18 @@ import java.awt.*;
public class BulletAndTankCollider implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
public boolean collider(GameObject o1, GameObject o2) {
if (o1 instanceof Bullet && o2 instanceof Tank) {
Bullet bullet = (Bullet) o1;
Tank tank = (Tank) o2;
if (collideWithTank(bullet, tank, gameModel)) return true;
if (collideWithTank(bullet, tank)) return true;
} else if (o1 instanceof Tank && o2 instanceof Bullet) {
return collider(o2, o1, gameModel);
return collider(o2, o1);
}
return false;
}
public boolean collideWithTank(Bullet bullet, Tank tank, GameModel gameModel) {
public boolean collideWithTank(Bullet bullet, Tank tank) {
if (bullet.group == tank.getGroup()) return false;
if (tank.group == Group.GOOD) return false;
Rectangle rect1 = new Rectangle(bullet.x, bullet.y, bullet.bWidth, bullet.bHeight);
@ -31,7 +31,7 @@ public class BulletAndTankCollider implements Collider {
if (rect1.intersects(rect2)) {
tank.die();
bullet.die();
gameModel.add(new Explode(bullet.x + bullet.bWidth / 2 - Explode.bWidth / 2, bullet.y + bullet.bWidth - Explode.bHeight / 2, gameModel));
new Explode(bullet.x + bullet.bWidth / 2 - Explode.bWidth / 2, bullet.y + bullet.bWidth - Explode.bHeight / 2);
return true;
}
return false;

@ -9,7 +9,7 @@ import com.study.tank.*;
public class BulletWallCollider implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
public boolean collider(GameObject o1, GameObject o2) {
if (o1 instanceof Bullet && o2 instanceof Wall) {
Bullet bullet = (Bullet) o1;
Wall w = (Wall) o2;
@ -18,7 +18,7 @@ public class BulletWallCollider implements Collider {
return true;
}
} else if (o2 instanceof Bullet && o1 instanceof Wall) {
collider(o2, o1, gameModel);
collider(o2, o1);
}
return false;
}

@ -8,5 +8,5 @@ import com.study.tank.GameObject;
* @date 2022/10/27 15:39
*/
public interface Collider {
boolean collider(GameObject o1, GameObject o2, GameModel gameModel);
boolean collider(GameObject o1, GameObject o2);
}

@ -22,9 +22,9 @@ public class ColliderChain implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
public boolean collider(GameObject o1, GameObject o2) {
for (Collider c : colliders) {
if (c.collider(o1, o2, gameModel)) return true;
if (c.collider(o1, o2)) return true;
}
return false;
}

@ -11,7 +11,7 @@ import java.awt.*;
public class TankTankCollider implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
public boolean collider(GameObject o1, GameObject o2) {
if (o1 instanceof Tank && o2 instanceof Tank) {
Tank tank1 = (Tank) o1;
Tank tank2 = (Tank) o2;
@ -27,8 +27,8 @@ public class TankTankCollider implements Collider {
if (tank1.rectangle.intersects(tank2.rectangle)) {
tank1.die();
tank2.die();
gameModel.add(new Explode(tank1.x + tank1.tankWidth / 2 - Explode.bWidth / 2, tank1.y + tank1.tankHeight / 2 - Explode.bHeight/2, gameModel));
gameModel.add(new Explode(tank2.x + tank2.tankWidth / 2 - Explode.bWidth / 2, tank2.y + tank2.tankHeight / 2-Explode.bHeight/2, gameModel));
new Explode(tank1.x + tank1.tankWidth / 2 - Explode.bWidth / 2, tank1.y + tank1.tankHeight / 2 - Explode.bHeight/2);
new Explode(tank2.x + tank2.tankWidth / 2 - Explode.bWidth / 2, tank2.y + tank2.tankHeight / 2-Explode.bHeight/2);
return true;
}
}

@ -9,7 +9,7 @@ import com.study.tank.*;
public class WallTankCollider implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
public boolean collider(GameObject o1, GameObject o2) {
if (o1 instanceof Tank && o2 instanceof Wall) {
Tank tank = (Tank) o1;
Wall w = (Wall) o2;
@ -18,7 +18,7 @@ public class WallTankCollider implements Collider {
tank.stop();
}
} else if (o2 instanceof Tank && o1 instanceof Wall) {
collider(o2, o1, gameModel);
collider(o2, o1);
}
return false;
}

Loading…
Cancel
Save