责任链+调停者+新增墙体

dev_1027
xiaoshengjie 2 years ago
parent 0de31db767
commit b563018987

@ -13,13 +13,14 @@ 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() {
return group;
@ -30,11 +31,16 @@ public class Bullet extends GameObject {
}
public Bullet(int x, int y, Dir dir, Group group, GameModel gm) {
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");
@ -65,6 +71,7 @@ public class Bullet extends GameObject {
*
*/
public void move() {
System.out.println("bullet move");
switch (dir) {
case LEFT:
x -= speed;
@ -79,6 +86,8 @@ public class Bullet extends GameObject {
y += speed;
break;
}
rectangle.x = x;
rectangle.y = y;
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
living = false;
}
@ -90,7 +99,7 @@ public class Bullet extends GameObject {
public void collideWithTank(Tank tank) {
if (this.group == tank.getGroup()) return;
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.x, tank.y, Tank.tankWidth, Tank.tankHeight);
//碰撞矩形
if (rect1.intersects(rect2)) {
tank.die();

@ -20,13 +20,17 @@ public class GameModel {
//Collider collider1 = new BulletAndTankCollider();//子弹和坦克的碰撞
//Collider collider2 = new TankTankCollider();//坦克和坦克的碰撞
ColliderChain chain = new ColliderChain();
public int count = 0;
public GameModel() {
gameModelList.add(myTank);
//初始化敌人坦克
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));
}
add(new Wall(200,200,50,100,this));
add(new Wall(500,200,100,200,this));
}
public void add(GameObject object) {
@ -38,18 +42,17 @@ public class GameModel {
}
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++) {
for (int j = i + 1; 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);
if (chain.collider(o1, o2, this)) return;
}
}
}

@ -7,6 +7,7 @@ import com.study.tank.strategy.FireStrategy;
import java.awt.*;
import java.util.Random;
import java.util.UUID;
/**
* @Description:
@ -20,29 +21,32 @@ public class Tank extends GameObject {
// private int x, y;
private boolean living = true;
public Dir dir = Dir.DOWN;
private final int speed = 4;
public TankFrame tankFrame = null;
private final int speed = 5;
//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) {
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();
public int getX() {
return x;
rectangle.x = x;
rectangle.y = y;
rectangle.width = Tank.tankWidth;
rectangle.height = Tank.tankHeight;
}
public int getY() {
return y;
}
public Dir getDir() {
return dir;
@ -52,14 +56,6 @@ public class Tank extends GameObject {
return speed;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public boolean isMoving() {
return moving;
}
@ -82,10 +78,10 @@ public class Tank extends GameObject {
@Override
public void paint(Graphics g) {
System.out.println("tank paint");
System.out.println("tank paint:" + this.toString());
if (!living) {
System.out.println("tank remove:" + this.toString());
gm.remove(this);
moving = false;
return;
}
switch (this.dir) {
@ -106,13 +102,12 @@ public class Tank extends GameObject {
}
public void move() {
if (!living) return;
System.out.println("tank move start:" + this.toString());
if (!this.moving) {
goBack();
return;
}
oldX = x;
oldY = y;
oldX = this.x;
oldY = this.y;
switch (this.dir) {
case LEFT:
x -= speed;
@ -139,13 +134,16 @@ public class Tank extends GameObject {
new Audio("audio/tank_move.wav").play();
}).start();
}
System.out.println("tank move end:" + this.toString());
rectangle.x = x;
rectangle.y = y;
}
private void boundsCheck() {
if (this.x < 0) x = TankFrame.GAME_WIDTH - Tank.tankWidth;
if (this.y < 28) y = TankFrame.GAME_HEIGHT - Tank.tankHeight;
if (this.x > TankFrame.GAME_WIDTH - tankWidth) x = 0;
if (this.y > TankFrame.GAME_HEIGHT - tankHeight) y = 28;
if (x < 0) x = TankFrame.GAME_WIDTH - Tank.tankWidth;
if (y < 28) y = TankFrame.GAME_HEIGHT - Tank.tankHeight;
if (x > TankFrame.GAME_WIDTH - tankWidth) x = 0;
if (y > TankFrame.GAME_HEIGHT - tankHeight) y = 28;
}
//0.4几率的随机方向
@ -159,9 +157,9 @@ public class Tank extends GameObject {
*/
public void fire() {
//fs.fire(this);
int bx = this.getX() + Tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2;
int by = this.getY() + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2;
switch (this.dir) {
int bx = x + Tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2;
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);
@ -178,7 +176,6 @@ public class Tank extends GameObject {
new Bullet(bx + 6, by + 1, this.dir, this.getGroup(), this.gm);
new Bullet(bx - 6, by + 1, this.dir, this.getGroup(), this.gm);
break;
}
}

@ -91,7 +91,6 @@ public class TankFrame extends Frame {
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
int key = e.getKeyCode();
System.out.println("弹起:" + key);
switch (key) {
case KeyEvent.VK_A:
bL = false;

@ -0,0 +1,31 @@
package com.study.tank;
import java.awt.*;
/**
* @author xsj
* @date 2022/10/28 15:37
*/
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) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.gm = gameModel;
rectangle = new Rectangle(x, y, width, height);
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
g.fillRect(this.x, this.y, this.width, this.height);
g.setColor(c);
}
}

@ -22,11 +22,11 @@ public class BulletAndTankCollider implements Collider {
return false;
}
public boolean collideWithTank(Bullet bullet, Tank tank, GameModel gameModel) {
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);
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight);
Rectangle rect2 = new Rectangle(tank.x, tank.y, Tank.tankWidth, Tank.tankHeight);
//碰撞矩形
if (rect1.intersects(rect2)) {
tank.die();

@ -0,0 +1,25 @@
package com.study.tank.cor;
import com.study.tank.*;
/**
* @author xsj
* @date 2022/10/28 15:56
*/
public class BulletWallCollider implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
if (o1 instanceof Bullet && o2 instanceof Wall) {
Bullet bullet = (Bullet) o1;
Wall w = (Wall) o2;
if (bullet.rectangle.intersects(w.rectangle)) {
bullet.die();
return true;
}
} else if (o2 instanceof Bullet && o1 instanceof Wall) {
collider(o2, o1, gameModel);
}
return false;
}
}

@ -16,6 +16,8 @@ public class ColliderChain implements Collider {
public ColliderChain() {
colliders.add(new BulletAndTankCollider());
colliders.add(new TankTankCollider());
colliders.add(new WallTankCollider());
colliders.add(new BulletWallCollider());
}

@ -15,19 +15,20 @@ public class TankTankCollider implements Collider {
if (o1 instanceof Tank && o2 instanceof Tank) {
Tank tank1 = (Tank) o1;
Tank tank2 = (Tank) o2;
// Rectangle rect1 = new Rectangle(tank1.x, tank1.y, Tank.tankWidth, Tank.tankHeight);
// Rectangle rect2 = new Rectangle(tank2.x, tank2.y, Tank.tankWidth, Tank.tankHeight);
if (tank1.group == tank2.group) {
if (tank1.rectangle.intersects(tank2.rectangle)) {
tank1.stop();
tank2.stop();
return true;
tank1.goBack();
tank2.goBack();
}
} 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));
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));
return true;
}
}

@ -0,0 +1,25 @@
package com.study.tank.cor;
import com.study.tank.*;
/**
* @author xsj
* @date 2022/10/28 15:43
*/
public class WallTankCollider implements Collider {
@Override
public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) {
if (o1 instanceof Tank && o2 instanceof Wall) {
Tank tank = (Tank) o1;
Wall w = (Wall) o2;
if (tank.group == Group.GOOD) return false;
if (tank.rectangle.intersects(w.rectangle)) {
tank.stop();
}
} else if (o2 instanceof Tank && o1 instanceof Wall) {
collider(o2, o1, gameModel);
}
return false;
}
}
Loading…
Cancel
Save