装饰器模式

dp_gamemodel
kn5886348135 3 years ago
parent 76f7f49812
commit 788791c52f

@ -13,22 +13,20 @@ public class Bullet extends GameObject{
private Dir dir; private Dir dir;
private boolean living = true; private boolean living = true;
GameModel gm = null;
private Group group = Group.BAD; private Group group = Group.BAD;
public Bullet(int x, int y, Dir dir, Group group, GameModel gm) { public Bullet(int x, int y, Dir dir, Group group) {
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;
rect.x = this.x; rect.x = this.x;
rect.y = this.y; rect.y = this.y;
rect.width = WIDTH; rect.width = WIDTH;
rect.height = HEIGHT; rect.height = HEIGHT;
gm.add(this); GameModel.getInstance().add(this);
} }
public Group getGroup() { public Group getGroup() {
@ -50,7 +48,7 @@ public class Bullet extends GameObject{
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) { if (!living) {
gm.remove(this); GameModel.getInstance().remove(this);
} }
switch (dir) { switch (dir) {
case LEFT: case LEFT:
@ -72,6 +70,16 @@ public class Bullet extends GameObject{
move(); move();
} }
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
}
private void move() { private void move() {
switch (dir) { switch (dir) {
case LEFT: case LEFT:
@ -104,7 +112,7 @@ public class Bullet extends GameObject{
this.die(); this.die();
int eX = tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2; int eX = tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2;
int eY = tank.getY() + Tank.HEIGHT / 2 - Explode.HEIGHT / 2; int eY = tank.getY() + Tank.HEIGHT / 2 - Explode.HEIGHT / 2;
gm.add(new Explode(eX, eY, gm)); GameModel.getInstance().add(new Explode(eX, eY));
return true; return true;
} }
return false; return false;

@ -6,14 +6,11 @@ public class Explode extends GameObject {
public static int WIDTH = ResourceMgr.explodes[0].getWidth(); public static int WIDTH = ResourceMgr.explodes[0].getWidth();
public static int HEIGHT = ResourceMgr.explodes[0].getHeight(); public static int HEIGHT = ResourceMgr.explodes[0].getHeight();
GameModel gm = null;
private int step = 0; private int step = 0;
public Explode(int x, int y, GameModel gm) { public Explode(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.gm = gm;
new Thread(() -> new Audio("audio/explode.wav").play()).start(); new Thread(() -> new Audio("audio/explode.wav").play()).start();
} }
@ -24,7 +21,17 @@ public class Explode extends GameObject {
g.drawImage(ResourceMgr.explodes[step++], x, y, null); g.drawImage(ResourceMgr.explodes[step++], x, y, null);
if (step >= ResourceMgr.explodes.length) if (step >= ResourceMgr.explodes.length)
gm.remove(this); GameModel.getInstance().remove(this);
}
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
} }
} }

@ -1,9 +1,6 @@
package com.example.tankbattle; package com.example.tankbattle;
import com.example.tankbattle.cor.BulletTankCollider;
import com.example.tankbattle.cor.Collider;
import com.example.tankbattle.cor.ColliderChain; import com.example.tankbattle.cor.ColliderChain;
import com.example.tankbattle.cor.TankTankCollider;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
@ -11,35 +8,53 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GameModel { public class GameModel {
Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this);
List<Bullet> bullets = new ArrayList<>(); Tank myTank;
List<Tank> tanks = new ArrayList<>();
List<Explode> explodes = new ArrayList<>();
ColliderChain chain = new ColliderChain();
List<GameObject> objects = new ArrayList<>(); static List<Bullet> bullets = new ArrayList<>();
static List<Tank> tanks = new ArrayList<>();
static List<Explode> explodes = new ArrayList<>();
static ColliderChain chain = new ColliderChain();
static List<GameObject> objects = new ArrayList<>();
private static final GameModel INSTANCE = new GameModel();
public static GameModel getInstance(){
return INSTANCE;
}
public GameModel() { public GameModel() {
}
private void init(){
myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD);
int initTankCount = Integer.valueOf(PropertyMgr.get("initTankCount")); int initTankCount = Integer.valueOf(PropertyMgr.get("initTankCount"));
// 初始化敌方坦克 // 初始化敌方坦克
for (int i = 0; i < initTankCount; i++) { for (int i = 0; i < initTankCount; i++) {
add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, this)); add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD));
} }
add(myTank);
add(new Wall(150, 150, 200, 50)); add(new Wall(150, 150, 200, 50));
add(new Wall(550, 150, 200, 50)); add(new Wall(550, 150, 200, 50));
add(new Wall(500, 300, 200, 50)); add(new Wall(300, 500, 200, 50));
add(new Wall(300, 500, 50, 200)); add(new Wall(500, 300, 50, 200));
}
static {
getInstance().init();
} }
public void add(GameObject go) { public void add(GameObject go) {
this.objects.add(go); objects.add(go);
} }
public void remove(GameObject go) { public void remove(GameObject go) {
this.objects.remove(go); objects.remove(go);
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -57,7 +72,7 @@ public class GameModel {
// 互相碰撞逻辑 // 互相碰撞逻辑
for (int i = 0; i < objects.size(); i++) { for (int i = 0; i < objects.size(); i++) {
for (int j = i + 1; j < objects.size(); j++) { for (int j = 0; j < objects.size(); j++) {
GameObject o1 = objects.get(i); GameObject o1 = objects.get(i);
GameObject o2 = objects.get(j); GameObject o2 = objects.get(j);
// collider.collide(o1, o2); // collider.collide(o1, o2);

@ -7,4 +7,6 @@ public abstract class GameObject {
public int y; public int y;
public abstract void paint(Graphics g); public abstract void paint(Graphics g);
public abstract int getWidth();
public abstract int getHeight();
} }

@ -27,15 +27,13 @@ public class Tank extends GameObject{
int oldX, oldY; int oldX, oldY;
FireStrategy fs; FireStrategy fs;
public GameModel gm;
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;
rect.x = this.x; rect.x = this.x;
rect.y = this.y; rect.y = this.y;
@ -54,6 +52,7 @@ public class Tank extends GameObject{
} else { } else {
fs = new DefaultFireStrategy(); fs = new DefaultFireStrategy();
} }
GameModel.getInstance().add(this);
} }
public Rectangle getRect() { public Rectangle getRect() {
@ -89,8 +88,8 @@ public class Tank extends GameObject{
} }
private void move() { private void move() {
oldX=x; oldX = x;
oldY=y; oldY = y;
if (!moving) return; if (!moving) return;
switch (dir) { switch (dir) {
@ -136,7 +135,7 @@ public class Tank extends GameObject{
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) gm.remove(this); if (!living) GameModel.getInstance().remove(this);
switch (dir) { switch (dir) {
case LEFT: case LEFT:
g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null); g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null);
@ -156,6 +155,16 @@ public class Tank extends GameObject{
move(); move();
} }
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
}
public void setDir(Dir dir) { public void setDir(Dir dir) {
this.dir = dir; this.dir = dir;
} }

@ -11,7 +11,7 @@ import java.awt.event.WindowEvent;
public class TankFrame extends Frame { public class TankFrame extends Frame {
GameModel gm = new GameModel(); GameModel gm = GameModel.getInstance();
static final int GAME_WIDTH = Integer.valueOf(PropertyMgr.get("gameWidth")); static final int GAME_WIDTH = Integer.valueOf(PropertyMgr.get("gameWidth"));
static final int GAME_HEIGHT = Integer.valueOf(PropertyMgr.get("gameHeight")); static final int GAME_HEIGHT = Integer.valueOf(PropertyMgr.get("gameHeight"));

@ -49,4 +49,14 @@ public class Wall extends GameObject{
g.fillRect(x, y, w, h); g.fillRect(x, y, w, h);
g.setColor(c); g.setColor(c);
} }
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
}
} }

@ -0,0 +1,31 @@
package com.example.tankbattle.decorator;
import com.example.tankbattle.GameObject;
import java.awt.Graphics;
public class GODecorator extends GameObject {
GameObject gameObject;
public GODecorator(GameObject gameObject) {
this.gameObject = gameObject;
}
@Override
public void paint(Graphics g) {
this.x = gameObject.x;
this.y = gameObject.y;
paint(g);
gameObject.paint(g);
}
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
}
}

@ -0,0 +1,37 @@
package com.example.tankbattle.decorator;
import com.example.tankbattle.GameObject;
import java.awt.Color;
import java.awt.Graphics;
public class RectDecorator extends GameObject {
GameObject gameObject;
public RectDecorator(GameObject gameObject) {
this.gameObject = gameObject;
}
@Override
public void paint(Graphics g) {
this.x = gameObject.x;
this.y = gameObject.y;
gameObject.paint(g);
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.drawRect(x, y, gameObject.getWidth(), gameObject.getHeight());
g.setColor(c);
}
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
}
}

@ -2,8 +2,11 @@ package com.example.tankbattle.strategy;
import com.example.tankbattle.Audio; import com.example.tankbattle.Audio;
import com.example.tankbattle.Bullet; import com.example.tankbattle.Bullet;
import com.example.tankbattle.GameModel;
import com.example.tankbattle.GameObject;
import com.example.tankbattle.Group; import com.example.tankbattle.Group;
import com.example.tankbattle.Tank; import com.example.tankbattle.Tank;
import com.example.tankbattle.decorator.RectDecorator;
public class DefaultFireStrategy implements FireStrategy{ public class DefaultFireStrategy implements FireStrategy{
@ -11,7 +14,7 @@ public class DefaultFireStrategy implements FireStrategy{
public void fire(Tank t) { public void fire(Tank t) {
int bX = t.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2; int bX = t.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2; int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
new Bullet(bX, bY, t.dir, t.group, t.gm); GameModel.getInstance().add(new RectDecorator(new Bullet(bX, bY, t.dir, t.group)));
if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
} }

@ -14,7 +14,7 @@ public class FourDirFireStrategy implements FireStrategy{
int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2; int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
Dir[] dirs = Dir.values(); Dir[] dirs = Dir.values();
for(Dir dir : dirs) { for(Dir dir : dirs) {
new Bullet(bX, bY, dir, t.group, t.gm); new Bullet(bX, bY, dir, t.group);
} }
if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
} }

Loading…
Cancel
Save