GameModel使用单例模式

dev_1027
xiaoshengjie 3 years ago
parent b563018987
commit 10d5726d64

@ -19,7 +19,6 @@ public class Bullet extends GameObject {
private boolean living = true; private boolean living = true;
//TankFrame tf = null; //TankFrame tf = null;
public Group group = Group.BAD; public Group group = Group.BAD;
public GameModel gm;
public Rectangle rectangle = new Rectangle(); public Rectangle rectangle = new Rectangle();
public Group getGroup() { public Group getGroup() {
@ -30,26 +29,25 @@ public class Bullet extends GameObject {
this.group = group; 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(); super();
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.gm = gm;
this.group = group; this.group = group;
this.rectangle.x = x; this.rectangle.x = x;
this.rectangle.y = y; this.rectangle.y = y;
this.rectangle.width = bWidth; this.rectangle.width = bWidth;
this.rectangle.height = bHeight; this.rectangle.height = bHeight;
gm.add(this);
new Thread(() -> { new Thread(() -> {
new Audio("audio/tank_fire.wav"); new Audio("audio/tank_fire.wav");
}).start(); }).start();
GameModel.INSTANCE.add(this);
} }
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) gm.remove(this); if (!living) GameModel.INSTANCE.remove(this);
switch (dir) { switch (dir) {
case DOWN: case DOWN:
g.drawImage(ImageManger.bulletD, x, y, null); g.drawImage(ImageManger.bulletD, x, y, null);
@ -104,7 +102,7 @@ public class Bullet extends GameObject {
if (rect1.intersects(rect2)) { if (rect1.intersects(rect2)) {
tank.die(); tank.die();
this.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 bWidth = ImageManger.explodes[0].getWidth();
public static final int bHeight = ImageManger.explodes[0].getHeight(); public static final int bHeight = ImageManger.explodes[0].getHeight();
private int x, y; private int x, y;
public GameModel gm;
private int step = 0; private int step = 0;
public Explode(int x, int y, GameModel gameModel) { public Explode(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.gm = gameModel;
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
new Audio("audio/explode.wav").play(); new Audio("audio/explode.wav").play();
} }
}).start(); }).start();
GameModel.INSTANCE.add(this);
} }
@ -33,7 +32,7 @@ public class Explode extends GameObject {
public void paint(Graphics g) { public void paint(Graphics g) {
g.drawImage(ImageManger.explodes[step++], x, y, null); g.drawImage(ImageManger.explodes[step++], x, y, null);
if (step >= ImageManger.explodes.length) { 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 * @date 2022/10/27 14:30
*/ */
public class GameModel { 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<>(); List<GameObject> gameModelList = new ArrayList<>();
//Collider collider1 = new BulletAndTankCollider();//子弹和坦克的碰撞
//Collider collider2 = new TankTankCollider();//坦克和坦克的碰撞
ColliderChain chain = new ColliderChain(); ColliderChain chain = new ColliderChain();
public int count = 0;
public GameModel() { 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")); int initCountTank = Integer.parseInt((String) PropertyMgr.get("initTankCount"));
for (int i = 0; i < initCountTank; i++) { 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)); new Wall(200, 200, 50, 100);
add(new Wall(500,200,100,200,this)); new Wall(500, 200, 100, 200);
} }
public void add(GameObject object) { public void add(GameObject object) {
@ -52,7 +58,7 @@ public class GameModel {
GameObject o2 = gameModelList.get(j); GameObject o2 = gameModelList.get(j);
//collider1.collider(o1, o2, this);// //collider1.collider(o1, o2, this);//
//collider2.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; //public TankFrame tankFrame = null;
private Random random = new Random(); private Random random = new Random();
public Group group = Group.BAD; public Group group = Group.BAD;
public GameModel gm;
public Rectangle rectangle = new Rectangle(); public Rectangle rectangle = new Rectangle();
int oldX, oldY; int oldX, oldY;
public String uuid; 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(); super();
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.group = group; this.group = group;
this.gm = gm;
this.oldY = y; this.oldY = y;
this.oldX = x; this.oldX = x;
this.uuid = UUID.randomUUID().toString(); this.uuid = UUID.randomUUID().toString();
@ -45,6 +43,7 @@ public class Tank extends GameObject {
rectangle.y = y; rectangle.y = y;
rectangle.width = Tank.tankWidth; rectangle.width = Tank.tankWidth;
rectangle.height = Tank.tankHeight; rectangle.height = Tank.tankHeight;
GameModel.INSTANCE.add(this);
} }
@ -81,7 +80,7 @@ public class Tank extends GameObject {
System.out.println("tank paint:" + this.toString()); System.out.println("tank paint:" + this.toString());
if (!living) { if (!living) {
System.out.println("tank remove:" + this.toString()); System.out.println("tank remove:" + this.toString());
gm.remove(this); GameModel.INSTANCE.remove(this);
return; return;
} }
switch (this.dir) { switch (this.dir) {
@ -161,20 +160,20 @@ public class Tank extends GameObject {
int by = y + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2; int by = y + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2;
switch (dir) { switch (dir) {
case DOWN: case DOWN:
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(), this.gm); new Bullet(bx, by - 6, this.dir, this.getGroup());
break; break;
case UP: case UP:
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(), this.gm); new Bullet(bx + 1, by - 6, this.dir, this.getGroup());
break; break;
case LEFT: case LEFT:
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(), this.gm); new Bullet(bx - 6, by, this.dir, this.getGroup());
break; break;
case RIGHT: case RIGHT:
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(), this.gm); new Bullet(bx - 6, by + 1, this.dir, this.getGroup());
break; break;
} }
} }

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

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

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

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

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

@ -8,5 +8,5 @@ import com.study.tank.GameObject;
* @date 2022/10/27 15:39 * @date 2022/10/27 15:39
*/ */
public interface Collider { 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 @Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) { public boolean collider(GameObject o1, GameObject o2) {
for (Collider c : colliders) { for (Collider c : colliders) {
if (c.collider(o1, o2, gameModel)) return true; if (c.collider(o1, o2)) return true;
} }
return false; return false;
} }

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

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

Loading…
Cancel
Save