责任链+调停者

dev_1027
xiaoshengjie 2 years ago
parent db28df2c9b
commit 0de31db767

@ -10,15 +10,16 @@ import java.awt.*;
* @Auther: xiaoshengjie * @Auther: xiaoshengjie
* @Date: 2022/10/22/11:16 * @Date: 2022/10/22/11:16
*/ */
public class Bullet extends BaseBullet { public class Bullet extends GameObject {
public static final int bWidth = ImageManger.bulletL.getWidth(); public static final int bWidth = ImageManger.bulletL.getWidth();
public static final int bHeight = ImageManger.bulletL.getHeight(); public static final int bHeight = ImageManger.bulletL.getHeight();
private int x, y; public int x, y;
private Dir dir = Dir.DOWN; private Dir dir = Dir.DOWN;
private final int speed = 10; private final int speed = 10;
private boolean living = true; private boolean living = true;
TankFrame tf = null; //TankFrame tf = null;
private Group group = Group.BAD; public Group group = Group.BAD;
public GameModel gm;
public Group getGroup() { public Group getGroup() {
return group; return group;
@ -28,13 +29,13 @@ public class Bullet extends BaseBullet {
this.group = group; this.group = group;
} }
public Bullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { public Bullet(int x, int y, Dir dir, Group group, GameModel gm) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.tf = tankFrame; this.gm = gm;
this.group = group; this.group = group;
tankFrame.bullets.add(this); gm.add(this);
new Thread(() -> { new Thread(() -> {
new Audio("audio/tank_fire.wav"); new Audio("audio/tank_fire.wav");
}).start(); }).start();
@ -42,7 +43,7 @@ public class Bullet extends BaseBullet {
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) tf.bullets.remove(this); if (!living) gm.remove(this);
switch (dir) { switch (dir) {
case DOWN: case DOWN:
g.drawImage(ImageManger.bulletD, x, y, null); g.drawImage(ImageManger.bulletD, x, y, null);
@ -86,7 +87,7 @@ public class Bullet extends BaseBullet {
/** /**
* *
*/ */
public void collideWithTank(BaseTank tank) { public void collideWithTank(Tank tank) {
if (this.group == tank.getGroup()) return; if (this.group == tank.getGroup()) return;
Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight); Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight);
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight); Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight);
@ -94,7 +95,7 @@ public class Bullet extends BaseBullet {
if (rect1.intersects(rect2)) { if (rect1.intersects(rect2)) {
tank.die(); tank.die();
this.die(); this.die();
tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf)); gm.add(new Explode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, gm));
} }
} }

@ -9,39 +9,31 @@ import java.awt.*;
* @Auther: xiaoshengjie * @Auther: xiaoshengjie
* @Date: 2022/10/22/11:16 * @Date: 2022/10/22/11:16
*/ */
public class Explode extends BaseExplode { 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;
TankFrame tf = null; 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, TankFrame tankFrame) { this.x = x;
this.x = x; this.y = y;
this.y = y; this.gm = gameModel;
this.tf = tankFrame;
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(); }
}
// public void paint(Graphics g) { @Override
// g.drawImage(ImageManger.explodes[step++], x, y, null); public void paint(Graphics g) {
// if (step >= ImageManger.explodes.length) { g.drawImage(ImageManger.explodes[step++], x, y, null);
// tf.explodes.remove(this); if (step >= ImageManger.explodes.length) {
// } gm.remove(this);
// } }
}
@Override
public void paint(Graphics g) {
g.drawImage(ImageManger.explodes[step++], x, y, null);
if (step >= ImageManger.explodes.length) {
tf.explodes.remove(this);
}
}
} }

@ -0,0 +1,57 @@
package com.study.tank;
import com.study.tank.cor.BulletAndTankCollider;
import com.study.tank.cor.Collider;
import com.study.tank.cor.ColliderChain;
import com.study.tank.cor.TankTankCollider;
import com.study.tank.factory.BaseTank;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author xsj
* @date 2022/10/27 14:30
*/
public class GameModel {
public Tank myTank = new Tank(150, 150, Dir.DOWN, Group.GOOD, this);
List<GameObject> gameModelList = new ArrayList<>();
//Collider collider1 = new BulletAndTankCollider();//子弹和坦克的碰撞
//Collider collider2 = new TankTankCollider();//坦克和坦克的碰撞
ColliderChain chain = new ColliderChain();
public GameModel() {
//初始化敌人坦克
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));
}
}
public void add(GameObject object) {
this.gameModelList.add(object);
}
public void remove(GameObject object) {
this.gameModelList.remove(object);
}
public void paint(Graphics g) {
myTank.paint(g);
for (int i = 0; i < gameModelList.size(); i++) {
gameModelList.get(i).paint(g);
}
for (int i = 0; i < gameModelList.size(); i++) {
for (int j = 0; j < gameModelList.size(); j++) {
GameObject o1 = gameModelList.get(i);
GameObject o2 = gameModelList.get(j);
//collider1.collider(o1, o2, this);//
//collider2.collider(o1, o2, this);
chain.collider(o1, o2, this);
}
}
}
}

@ -0,0 +1,13 @@
package com.study.tank;
import java.awt.*;
/**
* @author xsj
* @date 2022/10/27 14:20
*/
public abstract class GameObject {
public int x, y;
public abstract void paint(Graphics g);
}

@ -2,8 +2,8 @@ package com.study.tank;
import com.study.tank.factory.BaseTank; import com.study.tank.factory.BaseTank;
import com.study.tank.strategy.FireStrategy; import com.study.tank.strategy.FireStrategy;
import com.study.tank.strategy.Imp.DefaultFireStrategy; //import com.study.tank.strategy.Imp.DefaultFireStrategy;
import com.study.tank.strategy.Imp.GoodTankFireStrategy; //import com.study.tank.strategy.Imp.GoodTankFireStrategy;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
@ -13,7 +13,7 @@ import java.util.Random;
* @Auther: xiaoshengjie * @Auther: xiaoshengjie
* @Date: 2022/10/22/10:08 * @Date: 2022/10/22/10:08
*/ */
public class Tank extends BaseTank { public class Tank extends GameObject {
public static final int tankWidth = ImageManger.goodTankU.getWidth(); public static final int tankWidth = ImageManger.goodTankU.getWidth();
public static final int tankHeight = ImageManger.goodTankU.getHeight(); public static final int tankHeight = ImageManger.goodTankU.getHeight();
private boolean moving = true; private boolean moving = true;
@ -23,54 +23,26 @@ public class Tank extends BaseTank {
private final int speed = 4; private final int speed = 4;
public TankFrame tankFrame = null; public TankFrame tankFrame = null;
private Random random = new Random(); private Random random = new Random();
//Group group = Group.BAD; public Group group = Group.BAD;
public GameModel gm;
// FireStrategy fs; public Rectangle rectangle = new Rectangle();
int oldX, oldY;
// public Group getGroup() {
// return group; public Tank(int x, int y, Dir dir, Group group, GameModel gm) {
// } this.x = x;
// this.y = y;
// public void setGroup(Group group) {
// this.group = group;
// }
public Tank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
// this.x = x;
// this.y = y;
super(x, y, group);
this.dir = dir; this.dir = dir;
//this.group = group; this.group = group;
this.tankFrame = tankFrame; this.gm = gm;
// try { }
// if (this.group == Group.GOOD) {
// String goodName = (String) PropertyMgr.get("goodFs"); public int getX() {
// //fs = (GoodTankFireStrategy) Class.forName(goodName).newInstance(); return x;
// fs = (FireStrategy) Class.forName(goodName).getDeclaredConstructor().newInstance(); }
// } else {
// fs = new DefaultFireStrategy(); public int getY() {
// } return y;
// } catch (Exception e) { }
//
// }
}
// public Tank(int x, int y, Dir dir, Group group, boolean moving, TankFrame tankFrame) {
// this.x = x;
// this.y = y;
// this.dir = dir;
// this.group = group;
// this.tankFrame = tankFrame;
// this.moving = false;
// }
// public int getX() {
// return x;
// }
//
// public int getY() {
// return y;
// }
public Dir getDir() { public Dir getDir() {
return dir; return dir;
@ -80,13 +52,13 @@ public class Tank extends BaseTank {
return speed; return speed;
} }
// public void setX(int x) { public void setX(int x) {
// this.x = x; this.x = x;
// } }
//
// public void setY(int y) { public void setY(int y) {
// this.y = y; this.y = y;
// } }
public boolean isMoving() { public boolean isMoving() {
return moving; return moving;
@ -100,36 +72,19 @@ public class Tank extends BaseTank {
this.dir = dir; this.dir = dir;
} }
// public void paint(Graphics g) { public Group getGroup() {
// System.out.println("tank paint"); return group;
// if (!living) { }
// tankFrame.tanks.remove(this);
// moving = false;
// return;
// }
// switch (this.dir) {
// case DOWN:
// g.drawImage(this.group == Group.GOOD ? ImageManger.goodTankD : ImageManger.badTankD, this.x, this.y, null);
// break;
// case UP:
// g.drawImage(this.group == Group.GOOD ? ImageManger.goodTankU : ImageManger.badTankU, this.x, this.y, null);
// break;
// case RIGHT:
// g.drawImage(this.group == Group.GOOD ? ImageManger.goodTankR : ImageManger.badTankR, this.x, this.y, null);
// break;
// case LEFT:
// g.drawImage(this.group == Group.GOOD ? ImageManger.goodTankL : ImageManger.badTankL, this.x, this.y, null);
// break;
// }
// move();
// }
public void setGroup(Group group) {
this.group = group;
}
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
System.out.println("tank paint"); System.out.println("tank paint");
if (!living) { if (!living) {
tankFrame.tanks.remove(this); gm.remove(this);
moving = false; moving = false;
return; return;
} }
@ -152,8 +107,13 @@ public class Tank extends BaseTank {
public void move() { public void move() {
if (!living) return; if (!living) return;
if (!this.moving) return; if (!this.moving) {
switch (dir) { goBack();
return;
}
oldX = x;
oldY = y;
switch (this.dir) {
case LEFT: case LEFT:
x -= speed; x -= speed;
break; break;
@ -203,26 +163,34 @@ public class Tank extends BaseTank {
int by = this.getY() + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2; int by = this.getY() + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2;
switch (this.dir) { switch (this.dir) {
case DOWN: case DOWN:
new Bullet(bx, by + 6, this.dir, this.getGroup(), this.tankFrame); new Bullet(bx, by + 6, this.dir, this.getGroup(), this.gm);
new Bullet(bx, by - 6, this.dir, this.getGroup(), this.tankFrame); new Bullet(bx, by - 6, this.dir, this.getGroup(), this.gm);
break; break;
case UP: case UP:
new Bullet(bx + 1, by + 6, this.dir, this.getGroup(), this.tankFrame); new Bullet(bx + 1, by + 6, this.dir, this.getGroup(), this.gm);
new Bullet(bx + 1, by - 6, this.dir, this.getGroup(), this.tankFrame); new Bullet(bx + 1, by - 6, this.dir, this.getGroup(), this.gm);
break; break;
case LEFT: case LEFT:
new Bullet(bx + 6, by, this.dir, this.getGroup(), this.tankFrame); new Bullet(bx + 6, by, this.dir, this.getGroup(), this.gm);
new Bullet(bx - 6, by, this.dir, this.getGroup(), this.tankFrame); new Bullet(bx - 6, by, this.dir, this.getGroup(), this.gm);
break; break;
case RIGHT: case RIGHT:
new Bullet(bx + 6, by + 1, this.dir, this.getGroup(), this.tankFrame); new Bullet(bx + 6, by + 1, this.dir, this.getGroup(), this.gm);
new Bullet(bx - 6, by + 1, this.dir, this.getGroup(), this.tankFrame); new Bullet(bx - 6, by + 1, this.dir, this.getGroup(), this.gm);
break; break;
} }
} }
@Override public void stop() {
this.moving = false;
}
public void goBack() {
x = oldX;
y = oldY;
}
public void die() { public void die() {
this.living = false; this.living = false;
} }

@ -17,11 +17,8 @@ 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"));
public ArrayList<BaseTank> tanks = new ArrayList<BaseTank>(); GameModel gameModel = new GameModel();
public Tank myTank = new Tank(150, 150, Dir.DOWN, Group.GOOD, this); // public GameFactory gf = new DefaultFactory();
public ArrayList<BaseBullet> bullets = new ArrayList<BaseBullet>();
public ArrayList<BaseExplode> explodes = new ArrayList<BaseExplode>();
public GameFactory gf = new DefaultFactory();
public TankFrame() { public TankFrame() {
//设置窗口大小 //设置窗口大小
@ -58,26 +55,7 @@ public class TankFrame extends Frame {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
Color c = g.getColor(); gameModel.paint(g);
g.setColor(Color.BLUE);
g.drawString("子弹的数量:" + bullets.size(), 5, 40);
g.drawString("敌人的数量:" + tanks.size(), 5, 60);
g.setColor(c);
myTank.paint(g);
for (int i = 0; i < tanks.size(); i++) {
tanks.get(i).paint(g);
}
for (int i = 0; i < bullets.size(); i++) {
bullets.get(i).paint(g);
}
for (int i = 0; i < bullets.size(); i++) {
for (int j = 0; j < tanks.size(); j++) {
bullets.get(i).collideWithTank(tanks.get(j));
}
}
for (int i = 0; i < explodes.size(); i++) {
explodes.get(i).paint(g);
}
} }
/** /**
@ -128,7 +106,7 @@ public class TankFrame extends Frame {
bD = false; bD = false;
break; break;
case KeyEvent.VK_SPACE: case KeyEvent.VK_SPACE:
myTank.fire(); gameModel.myTank.fire();
break; break;
} }
setDirMethod(); setDirMethod();
@ -136,14 +114,14 @@ public class TankFrame extends Frame {
public void setDirMethod() { public void setDirMethod() {
if (!bD && !bU && !bR && !bL) { if (!bD && !bU && !bR && !bL) {
myTank.setMoving(false); gameModel.myTank.setMoving(false);
} else { } else {
myTank.setMoving(true); gameModel.myTank.setMoving(true);
} }
if (bL) myTank.setDir(Dir.LEFT); if (bL) gameModel.myTank.setDir(Dir.LEFT);
if (bR) myTank.setDir(Dir.RIGHT); if (bR) gameModel.myTank.setDir(Dir.RIGHT);
if (bU) myTank.setDir(Dir.UP); if (bU) gameModel.myTank.setDir(Dir.UP);
if (bD) myTank.setDir(Dir.DOWN); if (bD) gameModel.myTank.setDir(Dir.DOWN);
} }
} }

@ -10,11 +10,6 @@ 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();
//初始化敌人坦克
int initCountTank = Integer.parseInt((String) PropertyMgr.get("initTankCount"));
for (int i = 0; i < initCountTank; i++) {
tf.tanks.add(tf.gf.createTank(80 + i * 100, 50, Dir.DOWN, Group.BAD, tf));
}
new Thread(() -> { new Thread(() -> {
new Audio("audio/war1.wav").loop(); new Audio("audio/war1.wav").loop();

@ -0,0 +1,40 @@
package com.study.tank.cor;
import com.study.tank.*;
import java.awt.*;
/**
* @author xsj
* @date 2022/10/27 17:13
*/
public class BulletAndTankCollider implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
if (o1 instanceof Bullet && o2 instanceof Tank) {
Bullet bullet = (Bullet) o1;
Tank tank = (Tank) o2;
if (collideWithTank(bullet, tank, gameModel)) return true;
} else if (o1 instanceof Tank && o2 instanceof Bullet) {
return collider(o2, o1, gameModel);
}
return false;
}
public boolean collideWithTank(Bullet bullet, Tank tank, GameModel gameModel) {
if (bullet.group == tank.getGroup()) return false;
Rectangle rect1 = new Rectangle(bullet.x, bullet.y, bullet.bWidth, bullet.bHeight);
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight);
//碰撞矩形
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));
return true;
}
return false;
}
}

@ -0,0 +1,12 @@
package com.study.tank.cor;
import com.study.tank.GameModel;
import com.study.tank.GameObject;
/**
* @author xsj
* @date 2022/10/27 15:39
*/
public interface Collider {
boolean collider(GameObject o1, GameObject o2, GameModel gameModel);
}

@ -0,0 +1,29 @@
package com.study.tank.cor;
import com.study.tank.GameModel;
import com.study.tank.GameObject;
import java.util.ArrayList;
import java.util.List;
/**
* @author xsj
* @date 2022/10/28 9:32
*/
public class ColliderChain implements Collider {
List<Collider> colliders = new ArrayList<>();
public ColliderChain() {
colliders.add(new BulletAndTankCollider());
colliders.add(new TankTankCollider());
}
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
for (Collider c : colliders) {
if (c.collider(o1, o2, gameModel)) return true;
}
return false;
}
}

@ -0,0 +1,37 @@
package com.study.tank.cor;
import com.study.tank.*;
import java.awt.*;
/**
* @author xsj
* @date 2022/10/27 17:35
*/
public class TankTankCollider implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
if (o1 instanceof Tank && o2 instanceof Tank) {
Tank tank1 = (Tank) o1;
Tank tank2 = (Tank) o2;
if (tank1.group == tank2.group) {
if (tank1.rectangle.intersects(tank2.rectangle)) {
tank1.stop();
tank2.stop();
return true;
}
} else {
//碰撞矩形
if (tank1.rectangle.intersects(tank2.rectangle)) {
tank1.die();
tank2.die();
gameModel.add(new Explode(tank1.getX() + tank1.tankWidth/2, tank1.y + tank1.tankHeight/2, gameModel));
gameModel.add(new Explode(tank2.getX() + tank2.tankWidth/2, tank2.y + tank2.tankHeight/2, gameModel));
return true;
}
}
}
return false;
}
}

@ -1,25 +1,25 @@
package com.study.tank.factory; //package com.study.tank.factory;
//
import com.study.tank.*; //import com.study.tank.*;
//
/** ///**
* @author xsj // * @author xsj
* @date 2022/10/26 13:13 // * @date 2022/10/26 13:13
*/ // */
public class DefaultFactory extends GameFactory { //public class DefaultFactory extends GameFactory {
//
@Override // @Override
public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { // public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
return new Tank(x, y, dir, group, tankFrame); // return new Tank(x, y, dir, group, tankFrame);
} // }
//
@Override // @Override
public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { // public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
return new Bullet(x,y,dir,group,tankFrame); // return new Bullet(x,y,dir,group,tankFrame);
} // }
//
@Override // @Override
public BaseExplode createExplode(int x, int y, TankFrame tankFrame) { // public BaseExplode createExplode(int x, int y, TankFrame tankFrame) {
return new Explode(x, y, tankFrame); // return new Explode(x, y, tankFrame);
} // }
} //}

@ -1,46 +1,46 @@
package com.study.tank.factory; //package com.study.tank.factory;
//
import com.study.tank.Audio; //import com.study.tank.Audio;
import com.study.tank.ImageManger; //import com.study.tank.ImageManger;
import com.study.tank.TankFrame; //import com.study.tank.TankFrame;
//
import java.awt.*; //import java.awt.*;
//
/** ///**
* @author xsj // * @author xsj
* @date 2022/10/26 13:37 // * @date 2022/10/26 13:37
*/ // */
public class RectExplode extends BaseExplode { //public class RectExplode extends BaseExplode {
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;
TankFrame tf = null; // TankFrame tf = null;
private int step = 0; // private int step = 0;
//
//
public RectExplode(int x, int y, TankFrame tankFrame) { // public RectExplode(int x, int y, TankFrame tankFrame) {
this.x = x; // this.x = x;
this.y = y; // this.y = y;
this.tf = tankFrame; // this.tf = tankFrame;
//
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();
} // }
//
@Override // @Override
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);
Color c = g.getColor(); // Color c = g.getColor();
g.setColor(Color.red); // g.setColor(Color.red);
g.fillRect(x, y, 10 * step, 10 * step); // g.fillRect(x, y, 10 * step, 10 * step);
// if (step >= ImageManger.explodes.length) { //// if (step >= ImageManger.explodes.length) {
// tf.explodes.remove(this); //// tf.explodes.remove(this);
// } //// }
step++; // step++;
if (step > 5) tf.explodes.remove(this); // if (step > 5) tf.explodes.remove(this);
g.setColor(c); // g.setColor(c);
} // }
} //}

@ -1,25 +1,25 @@
package com.study.tank.factory; //package com.study.tank.factory;
//
import com.study.tank.*; //import com.study.tank.*;
//
/** ///**
* @author xsj // * @author xsj
* @date 2022/10/26 13:36 // * @date 2022/10/26 13:36
*/ // */
public class RectFactory extends GameFactory { //public class RectFactory extends GameFactory {
//
@Override // @Override
public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { // public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
return new WarTank(x, y, dir, group, tankFrame); // return new WarTank(x, y, dir, group, tankFrame);
} // }
//
@Override // @Override
public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { // public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
return new RoundBullet(x, y, dir, group, tankFrame); // return new RoundBullet(x, y, dir, group, tankFrame);
} // }
//
@Override // @Override
public BaseExplode createExplode(int x, int y, TankFrame tankFrame) { // public BaseExplode createExplode(int x, int y, TankFrame tankFrame) {
return new RectExplode(x, y, tankFrame); // return new RectExplode(x, y, tankFrame);
} // }
} //}

@ -1,100 +1,112 @@
package com.study.tank.factory; //package com.study.tank.factory;
//
import com.study.tank.*; //import com.study.tank.*;
//
import java.awt.*; //import java.awt.*;
//
/** ///**
* @Description: // * @Description:
* @Auther: xiaoshengjie // * @Auther: xiaoshengjie
* @Date: 2022/10/22/11:16 // * @Date: 2022/10/22/上午11:16
*/ // */
public class RoundBullet extends BaseBullet { //public class RoundBullet extends BaseBullet {
public static final int bWidth = ImageManger.bulletL.getWidth(); // public static final int bWidth = ImageManger.bulletL.getWidth();
public static final int bHeight = ImageManger.bulletL.getHeight(); // public static final int bHeight = ImageManger.bulletL.getHeight();
private int x, y; // private int x, y;
private Dir dir = Dir.DOWN; // private Dir dir = Dir.DOWN;
private final int speed = 10; // private final int speed = 10;
private boolean living = true; // private boolean living = true;
TankFrame tf = null; // TankFrame tf = null;
private Group group = Group.BAD; // private Group group = Group.BAD;
//
public Group getGroup() { // public Group getGroup() {
return group; // return group;
} // }
//
public void setGroup(Group group) { // public void setGroup(Group group) {
this.group = group; // this.group = group;
} // }
//
public RoundBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { // public RoundBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
this.x = x; // this.x = x;
this.y = y; // this.y = y;
this.dir = dir; // this.dir = dir;
this.tf = tankFrame; // this.tf = tankFrame;
this.group = group; // this.group = group;
tankFrame.bullets.add(this); // tankFrame.bullets.add(this);
new Thread(() -> { // new Thread(() -> {
new Audio("audio/tank_fire.wav"); // new Audio("audio/tank_fire.wav");
}).start(); // }).start();
} // }
//
@Override // @Override
public void paint(Graphics g) {
if (!living) tf.bullets.remove(this);
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 20, 20);
g.setColor(c);
move();
}
// public void paint(Graphics g) { // public void paint(Graphics g) {
// if (!living) tf.bullets.remove(this); // if (!living) tf.bullets.remove(this);
// Color c = g.getColor();
// g.setColor(Color.RED);
// g.fillOval(x, y, 20, 20);
// g.setColor(c);
// move();
// }
//
//// public void paint(Graphics g) {
//// if (!living) tf.bullets.remove(this);
//// switch (dir) {
//// case DOWN:
//// g.drawImage(ImageManger.bulletD, x, y, null);
//// break;
//// case UP:
//// g.drawImage(ImageManger.bulletU, x, y, null);
//// break;
//// case RIGHT:
//// g.drawImage(ImageManger.bulletR, x, y, null);
//// break;
//// case LEFT:
//// g.drawImage(ImageManger.bulletL, x, y, null);
//// break;
//// }
//// move();
//// }
//
// /**
// * 移动
// */
// public void move() {
// switch (dir) { // switch (dir) {
// case DOWN: // case LEFT:
// g.drawImage(ImageManger.bulletD, x, y, null); // x -= speed;
// break;
// case UP:
// g.drawImage(ImageManger.bulletU, x, y, null);
// break; // break;
// case RIGHT: // case RIGHT:
// g.drawImage(ImageManger.bulletR, x, y, null); // x += speed;
// break; // break;
// case LEFT: // case UP:
// g.drawImage(ImageManger.bulletL, x, y, null); // y -= speed;
// break;
// case DOWN:
// y += speed;
// break; // break;
// } // }
// move(); // if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
// living = false;
// }
// } // }
//
/** // /**
* // * 碰撞
*/ // */
public void move() { //// public void collideWithTank(Tank tank) {
switch (dir) { //// if (this.group == tank.getGroup()) return;
case LEFT: //// Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight);
x -= speed; //// Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight);
break; //// //碰撞矩形
case RIGHT: //// if (rect1.intersects(rect2)) {
x += speed; //// tank.die();
break; //// this.die();
case UP: //// tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf));
y -= speed; //// }
break; //// }
case DOWN: // @Override
y += speed; // public void collideWithTank(BaseTank tank) {
break;
}
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
living = false;
}
}
/**
*
*/
// public void collideWithTank(Tank tank) {
// if (this.group == tank.getGroup()) return; // if (this.group == tank.getGroup()) return;
// Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight); // Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight);
// Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight); // Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight);
@ -105,21 +117,9 @@ public class RoundBullet extends BaseBullet {
// tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf)); // tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf));
// } // }
// } // }
@Override //
public void collideWithTank(BaseTank tank) { // public void die() {
if (this.group == tank.getGroup()) return; // this.living = false;
Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight); // }
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight); //
//碰撞矩形 //}
if (rect1.intersects(rect2)) {
tank.die();
this.die();
tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf));
}
}
public void die() {
this.living = false;
}
}

@ -1,170 +1,170 @@
package com.study.tank.factory; //package com.study.tank.factory;
//
import com.study.tank.*; //import com.study.tank.*;
import com.study.tank.strategy.FireStrategy; //import com.study.tank.strategy.FireStrategy;
import com.study.tank.strategy.Imp.DefaultFireStrategy; //import com.study.tank.strategy.Imp.DefaultFireStrategy;
//
import java.awt.*; //import java.awt.*;
import java.util.Random; //import java.util.Random;
//
/** ///**
* @Description: // * @Description:
* @Auther: xiaoshengjie // * @Auther: xiaoshengjie
* @Date: 2022/10/22/10:08 // * @Date: 2022/10/22/上午10:08
*/ // */
public class WarTank extends BaseTank { //public class WarTank extends BaseTank {
public static final int tankWidth = ImageManger.goodTankU.getWidth(); // public static final int tankWidth = ImageManger.goodTankU.getWidth();
public static final int tankHeight = ImageManger.goodTankU.getHeight(); // public static final int tankHeight = ImageManger.goodTankU.getHeight();
private boolean moving = true; // private boolean moving = true;
private int x, y; // private int x, y;
private boolean living = true; // private boolean living = true;
public Dir dir = Dir.DOWN; // public Dir dir = Dir.DOWN;
private final int speed = 4; // private final int speed = 4;
public TankFrame tankFrame = null; // public TankFrame tankFrame = null;
private Random random = new Random(); // private Random random = new Random();
Group group = Group.BAD; // Group group = Group.BAD;
//
FireStrategy fs; // FireStrategy fs;
//
public Group getGroup() { // public Group getGroup() {
return group; // return group;
} // }
//
public void setGroup(Group group) { // public void setGroup(Group group) {
this.group = group;
}
public WarTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
// this.x = x;
// this.y = y;
super(x, y, group);
this.dir = dir;
// this.group = group; // this.group = group;
this.tankFrame = tankFrame; // }
try { //
if (this.group == Group.GOOD) { // public WarTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
String goodName = (String) PropertyMgr.get("goodFs"); //// this.x = x;
//fs = (GoodTankFireStrategy) Class.forName(goodName).newInstance(); //// this.y = y;
fs = (FireStrategy) Class.forName(goodName).getDeclaredConstructor().newInstance(); // super(x, y, group);
} else { // this.dir = dir;
fs = new DefaultFireStrategy(); //// this.group = group;
} // this.tankFrame = tankFrame;
} catch (Exception e) { // try {
// if (this.group == Group.GOOD) {
} // String goodName = (String) PropertyMgr.get("goodFs");
} // //fs = (GoodTankFireStrategy) Class.forName(goodName).newInstance();
// fs = (FireStrategy) Class.forName(goodName).getDeclaredConstructor().newInstance();
// public Tank(int x, int y, Dir dir, Group group, boolean moving, TankFrame tankFrame) { // } else {
// fs = new DefaultFireStrategy();
// }
// } catch (Exception e) {
//
// }
// }
//
//// public Tank(int x, int y, Dir dir, Group group, boolean moving, TankFrame tankFrame) {
//// this.x = x;
//// this.y = y;
//// this.dir = dir;
//// this.group = group;
//// this.tankFrame = tankFrame;
//// this.moving = false;
//// }
//
// public int getX() {
// return x;
// }
//
// public int getY() {
// return y;
// }
//
// public Dir getDir() {
// return dir;
// }
//
// public int getSpeed() {
// return speed;
// }
//
// public void setX(int x) {
// this.x = x; // this.x = x;
// }
//
// public void setY(int y) {
// this.y = y; // this.y = y;
// }
//
// public boolean isMoving() {
// return moving;
// }
//
// public void setMoving(boolean moving) {
// this.moving = moving;
// }
//
// public void setDir(Dir dir) {
// this.dir = dir; // this.dir = dir;
// this.group = group; // }
// this.tankFrame = tankFrame; //
// this.moving = false; // @Override
// } // public void paint(Graphics g) {
// System.out.println("war paint");
public int getX() { // if (!living) {
return x; // tankFrame.tanks.remove(this);
} // moving = false;
// return;
public int getY() { // }
return y; // Color c = g.getColor();
} // g.setColor(group == Group.GOOD ? Color.BLUE : Color.YELLOW);
// g.fillRect(x, y, 40, 40);
public Dir getDir() { // g.setColor(c);
return dir; // move();
} // }
//
public int getSpeed() { // public void move() {
return speed; // if (!living) return;
} // if (!this.moving) return;
// switch (dir) {
public void setX(int x) { // case LEFT:
this.x = x; // x -= speed;
} // break;
// case RIGHT:
public void setY(int y) { // x += speed;
this.y = y; // break;
} // case UP:
// y -= speed;
public boolean isMoving() { // break;
return moving; // case DOWN:
} // y += speed;
// break;
public void setMoving(boolean moving) { // }
this.moving = moving; // if (group == Group.BAD && random.nextInt(100) > 95)
} // this.fire();
//
public void setDir(Dir dir) { // if (this.group == Group.BAD && random.nextInt(100) > 95)
this.dir = dir; // this.randomDir();
} //
// boundsCheck();
@Override // }
public void paint(Graphics g) { //
System.out.println("war paint"); // private void boundsCheck() {
if (!living) { // if (this.x < 0) x = TankFrame.GAME_WIDTH - WarTank.tankWidth;
tankFrame.tanks.remove(this); // if (this.y < 28) y = TankFrame.GAME_HEIGHT - WarTank.tankHeight;
moving = false; // if (this.x > TankFrame.GAME_WIDTH - tankWidth) x = 0;
return; // if (this.y > TankFrame.GAME_HEIGHT - tankHeight) y = 28;
} // }
Color c = g.getColor(); //
g.setColor(group == Group.GOOD ? Color.BLUE : Color.YELLOW); // //0.4几率的随机方向
g.fillRect(x, y, 40, 40); // private void randomDir() {
g.setColor(c); // if (random.nextInt(100) > 60)
move(); // this.dir = Dir.values()[random.nextInt(4)];
} // }
//
public void move() { // /**
if (!living) return; // * 开火
if (!this.moving) return; // */
switch (dir) { // public void fire() {
case LEFT: // //fs.fire(this);
x -= speed; // int bx = this.getX() + this.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2 + 2;
break; // int by = this.getY() + this.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2 + 2;
case RIGHT: // new Bullet(bx, by, this.dir, this.getGroup(), this.tankFrame);
x += speed; // }
break; //
case UP: // @Override
y -= speed; // public void die() {
break; // this.living = false;
case DOWN: // }
y += speed; //}
break;
}
if (group == Group.BAD && random.nextInt(100) > 95)
this.fire();
if (this.group == Group.BAD && random.nextInt(100) > 95)
this.randomDir();
boundsCheck();
}
private void boundsCheck() {
if (this.x < 0) x = TankFrame.GAME_WIDTH - WarTank.tankWidth;
if (this.y < 28) y = TankFrame.GAME_HEIGHT - WarTank.tankHeight;
if (this.x > TankFrame.GAME_WIDTH - tankWidth) x = 0;
if (this.y > TankFrame.GAME_HEIGHT - tankHeight) y = 28;
}
//0.4几率的随机方向
private void randomDir() {
if (random.nextInt(100) > 60)
this.dir = Dir.values()[random.nextInt(4)];
}
/**
*
*/
public void fire() {
//fs.fire(this);
int bx = this.getX() + this.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2 + 2;
int by = this.getY() + this.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2 + 2;
new Bullet(bx, by, this.dir, this.getGroup(), this.tankFrame);
}
@Override
public void die() {
this.living = false;
}
}

@ -1,20 +1,20 @@
package com.study.tank.strategy.Imp; //package com.study.tank.strategy.Imp;
//
import com.study.tank.Bullet; //import com.study.tank.Bullet;
import com.study.tank.ImageManger; //import com.study.tank.ImageManger;
import com.study.tank.Tank; //import com.study.tank.Tank;
import com.study.tank.strategy.FireStrategy; //import com.study.tank.strategy.FireStrategy;
//
/** ///**
* @author xsj // * @author xsj
* @date 2022/10/25 10:30 // * @date 2022/10/25 10:30
*/ // */
public class DefaultFireStrategy implements FireStrategy { //public class DefaultFireStrategy implements FireStrategy {
//
@Override // @Override
public void fire(Tank tank) { // public void fire(Tank tank) {
int bx = tank.getX() + tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2 + 2; // int bx = tank.getX() + tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2 + 2;
int by = tank.getY() + tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2 + 2; // int by = tank.getY() + tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2 + 2;
new Bullet(bx, by, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx, by, tank.dir, tank.getGroup(), tank.tankFrame);
} // }
} //}

@ -1,39 +1,39 @@
package com.study.tank.strategy.Imp; //package com.study.tank.strategy.Imp;
//
import com.study.tank.Bullet; //import com.study.tank.Bullet;
import com.study.tank.ImageManger; //import com.study.tank.ImageManger;
import com.study.tank.Tank; //import com.study.tank.Tank;
import com.study.tank.strategy.FireStrategy; //import com.study.tank.strategy.FireStrategy;
//
/** ///**
* @author xsj // * @author xsj
* @date 2022/10/25 13:28 // * @date 2022/10/25 13:28
*/ // */
public class GoodTankFireStrategy implements FireStrategy { //public class GoodTankFireStrategy implements FireStrategy {
//
@Override // @Override
public void fire(Tank tank) { // public void fire(Tank tank) {
int bx = tank.getX() + Tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2; // int bx = tank.getX() + Tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2;
int by = tank.getY() + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2; // int by = tank.getY() + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2;
switch (tank.dir) { // switch (tank.dir) {
case DOWN: // case DOWN:
new Bullet(bx, by + 6, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx, by + 6, tank.dir, tank.getGroup(), tank.tankFrame);
new Bullet(bx, by - 6, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx, by - 6, tank.dir, tank.getGroup(), tank.tankFrame);
break; // break;
case UP: // case UP:
new Bullet(bx + 1, by + 6, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx + 1, by + 6, tank.dir, tank.getGroup(), tank.tankFrame);
new Bullet(bx + 1, by - 6, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx + 1, by - 6, tank.dir, tank.getGroup(), tank.tankFrame);
break; // break;
case LEFT: // case LEFT:
new Bullet(bx + 6, by, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx + 6, by, tank.dir, tank.getGroup(), tank.tankFrame);
new Bullet(bx - 6, by, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx - 6, by, tank.dir, tank.getGroup(), tank.tankFrame);
break; // break;
case RIGHT: // case RIGHT:
new Bullet(bx + 6, by + 1, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx + 6, by + 1, tank.dir, tank.getGroup(), tank.tankFrame);
new Bullet(bx - 6, by + 1, tank.dir, tank.getGroup(), tank.tankFrame); // new Bullet(bx - 6, by + 1, tank.dir, tank.getGroup(), tank.tankFrame);
break; // break;
//
} // }
//
} // }
} //}

Loading…
Cancel
Save