Compare commits

...

5 Commits

Author SHA1 Message Date
kn5886348135 788791c52f 装饰器模式
3 years ago
kn5886348135 76f7f49812 添加墙和碰撞策略
3 years ago
kn5886348135 c3b59f519e 门面模式操作游戏对象,责任链模式检测碰撞
3 years ago
kn5886348135 2860e1a336 移动包
3 years ago
kn5886348135 acb756e651 添加gamemodel
3 years ago

@ -3,31 +3,30 @@ package com.example.tankbattle;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Rectangle; import java.awt.Rectangle;
public class Bullet { public class Bullet extends GameObject{
private static final int SPEED = 6; private static final int SPEED = 6;
public static int WIDTH = ResourceMgr.bulletD.getWidth(); public static int WIDTH = ResourceMgr.bulletD.getWidth();
public static int HEIGHT = ResourceMgr.bulletD.getHeight(); public static int HEIGHT = ResourceMgr.bulletD.getHeight();
private Rectangle rect = new Rectangle(); Rectangle rect = new Rectangle();
private int x, y;
private Dir dir; private Dir dir;
private boolean living = true; private boolean living = true;
TankFrame tf = null;
private Group group = Group.BAD; private Group group = Group.BAD;
public Bullet(int x, int y, Dir dir, Group group, TankFrame tf) { 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.tf = tf;
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;
GameModel.getInstance().add(this);
} }
public Group getGroup() { public Group getGroup() {
@ -38,9 +37,18 @@ public class Bullet {
this.group = group; this.group = group;
} }
public Rectangle getRect() {
return rect;
}
public void setRect(Rectangle rect) {
this.rect = rect;
}
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) { if (!living) {
tf.bullets.remove(this); GameModel.getInstance().remove(this);
} }
switch (dir) { switch (dir) {
case LEFT: case LEFT:
@ -62,6 +70,16 @@ public class Bullet {
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:
@ -87,18 +105,20 @@ public class Bullet {
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false; if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false;
} }
public void collideWith(Tank tank){ public boolean collideWith(Tank tank){
if (this.group == tank.getGroup()) return; if (this.group == tank.getGroup()) return false;
if (this.rect.intersects(tank.rect)) { if (rect.intersects(tank.rect)) {
tank.die(); tank.die();
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;
tf.explodes.add(new Explode(eX, eY, tf)); GameModel.getInstance().add(new Explode(eX, eY));
return true;
} }
return false;
} }
private void die() { public void die() {
this.living = false; this.living = false;
} }
} }

@ -1,29 +1,37 @@
package com.example.tankbattle; package com.example.tankbattle;
import java.awt.*; import java.awt.Graphics;
public class Explode { 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();
private int x, y;
TankFrame tf = null;
private int step = 0; private int step = 0;
public Explode(int x, int y, TankFrame tf) { public Explode(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.tf = tf;
new Thread(() -> new Audio("audio/explode.wav").play()).start();
} }
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
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)
tf.explodes.remove(this); GameModel.getInstance().remove(this);
}
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
} }
} }

@ -0,0 +1,98 @@
package com.example.tankbattle;
import com.example.tankbattle.cor.ColliderChain;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
public class GameModel {
Tank myTank;
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() {
}
private void init(){
myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD);
int initTankCount = Integer.valueOf(PropertyMgr.get("initTankCount"));
// 初始化敌方坦克
for (int i = 0; i < initTankCount; i++) {
add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD));
}
add(myTank);
add(new Wall(150, 150, 200, 50));
add(new Wall(550, 150, 200, 50));
add(new Wall(300, 500, 200, 50));
add(new Wall(500, 300, 50, 200));
}
static {
getInstance().init();
}
public void add(GameObject go) {
objects.add(go);
}
public void remove(GameObject go) {
objects.remove(go);
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克的数量:" + tanks.size(), 10, 80);
g.drawString("爆炸的数量:" + explodes.size(), 10, 100);
g.setColor(c);
myTank.paint(g);
for (int i = 0; i < objects.size(); i++) {
objects.get(i).paint(g);
}
// 互相碰撞逻辑
for (int i = 0; i < objects.size(); i++) {
for (int j = 0; j < objects.size(); j++) {
GameObject o1 = objects.get(i);
GameObject o2 = objects.get(j);
// collider.collide(o1, o2);
// collider2.collide(o1, o2);
chain.collide(o1, o2);
}
}
// for (int i = 0; i < bullets.size(); i++) {
// for (int j = 0; j < tanks.size(); j++)
// bullets.get(i).collideWith(tanks.get(j));
// }
// for (Iterator<Bullet> it = bullets.iterator(); it.hasNext()) {
// Bullet b = it.next();
// if (!b.live) it.remove();
// }
}
public Tank getMainTank() {
return myTank;
}
}

@ -0,0 +1,12 @@
package com.example.tankbattle;
import java.awt.Graphics;
public abstract class GameObject {
public int x;
public int y;
public abstract void paint(Graphics g);
public abstract int getWidth();
public abstract int getHeight();
}

@ -4,15 +4,10 @@ public class Main {
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 initTankCount = Integer.valueOf(PropertyMgr.get("initTankCount")); new Thread(()->new Audio("audio/war1.wav").loop()).start();
// 初始化敌方坦克
for (int i = 0; i < initTankCount; i++) {
tf.tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, tf));
}
while (true) { while (true) {
Thread.sleep(50); Thread.sleep(25);
tf.repaint(); tf.repaint();
} }
} }

@ -5,22 +5,22 @@ import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
public class ResourceMgr { public class ResourceMgr {
public static BufferedImage GoodTankL, GoodTankU, GoodTankR, GoodTankD; public static BufferedImage goodTankL, goodTankU, goodTankR, goodTankD;
public static BufferedImage BadTankL, BadTankU, BadTankR, BadTankD; public static BufferedImage badTankL, badTankU, badTankR, badTankD;
public static BufferedImage bulletL, bulletU,bulletR, bulletD; public static BufferedImage bulletL, bulletU,bulletR, bulletD;
public static BufferedImage[] explodes = new BufferedImage[16]; public static BufferedImage[] explodes = new BufferedImage[16];
static { static {
try { try {
GoodTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/GoodTank1.png")); goodTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/GoodTank1.png"));
GoodTankL = ImageUtil.rotateImage(GoodTankU, -90); goodTankL = ImageUtil.rotateImage(goodTankU, -90);
GoodTankR = ImageUtil.rotateImage(GoodTankU, 90); goodTankR = ImageUtil.rotateImage(goodTankU, 90);
GoodTankD = ImageUtil.rotateImage(GoodTankU, 180); goodTankD = ImageUtil.rotateImage(goodTankU, 180);
BadTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png")); badTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png"));
BadTankL = ImageUtil.rotateImage(BadTankU, -90); badTankL = ImageUtil.rotateImage(badTankU, -90);
BadTankR = ImageUtil.rotateImage(BadTankU, 90); badTankR = ImageUtil.rotateImage(badTankU, 90);
BadTankD = ImageUtil.rotateImage(BadTankU, 180); badTankD = ImageUtil.rotateImage(badTankU, 180);
bulletU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletU.png")); bulletU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletU.png"));
bulletL = ImageUtil.rotateImage(bulletU, -90); bulletL = ImageUtil.rotateImage(bulletU, -90);

@ -1,48 +1,66 @@
package com.example.tankbattle; package com.example.tankbattle;
import com.example.tankbattle.strategy.DefaultFireStrategy;
import com.example.tankbattle.strategy.FireStrategy;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
public class Tank { public class Tank extends GameObject{
private static final int SPEED = 1; private static final int SPEED = 2;
public static int WIDTH = ResourceMgr.GoodTankD.getWidth(); public static int WIDTH = ResourceMgr.goodTankU.getWidth();
public static int HEIGHT = ResourceMgr.GoodTankD.getHeight(); public static int HEIGHT = ResourceMgr.goodTankU.getHeight();
Rectangle rect = new Rectangle(); Rectangle rect = new Rectangle();
private Random random = new Random(); private Random random = new Random();
private int x,y; public Dir dir = Dir.DOWN;
private Dir dir = Dir.DOWN;
private boolean moving = true; private boolean moving = true;
private TankFrame tf = null;
private boolean living = true; private boolean living = true;
private Group group = Group.BAD; public Group group = Group.BAD;
int oldX, oldY;
public Tank(int x, int y, Dir dir, Group group, TankFrame tf) { FireStrategy fs;
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.tf = tf;
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;
if(group == Group.GOOD) {
String goodFSName = PropertyMgr.get("goodFS");
try {
fs = (FireStrategy)Class.forName(goodFSName).getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
} else {
fs = new DefaultFireStrategy();
}
GameModel.getInstance().add(this);
}
public Rectangle getRect() {
return rect;
} }
public void fire(){ public void fire(){
int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2; fs.fire(this);
int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
tf.bullets.add(new Bullet(bX, bY, this.dir, this.group, this.tf));
} }
public Dir getDir() { public Dir getDir() {
@ -70,6 +88,9 @@ public class Tank {
} }
private void move() { private void move() {
oldX = x;
oldY = y;
if (!moving) return; if (!moving) return;
switch (dir) { switch (dir) {
case LEFT: case LEFT:
@ -88,51 +109,45 @@ public class Tank {
break; break;
} }
rect.x = this.x; if (this.group == Group.BAD && random.nextInt(100) > 95)
rect.y = this.y; this.fire();
if (this.group == Group.BAD && random.nextInt(100) > 95) this.fire();
if (this.group == Group.BAD && random.nextInt(100) > 95) { if (this.group == Group.BAD && random.nextInt(100) > 95)
randomDir(); randomDir();
}
boundsCheck(); boundsCheck();
rect.x = this.x;
rect.y = this.y;
} }
private void boundsCheck() { private void boundsCheck() {
if (this.x < 0) { if (this.x < 2) x = 2;
this.x = 2; if (this.y < 28) y = 28;
}
if (this.y < 28) {
this.y = 28;
}
if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) { if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH - 2) x = TankFrame.GAME_WIDTH - Tank.WIDTH - 2;
this.x = TankFrame.GAME_WIDTH - Tank.WIDTH; if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2) y = TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2;
}
if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) {
this.y = TankFrame.GAME_HEIGHT - Tank.HEIGHT;
}
} }
private void randomDir() { private void randomDir() {
this.dir = Dir.values()[random.nextInt(4)]; this.dir = Dir.values()[random.nextInt(4)];
} }
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) tf.tanks.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);
break; break;
case UP: case UP:
g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankU : ResourceMgr.BadTankU, x, y, null); g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null);
break; break;
case RIGHT: case RIGHT:
g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankR : ResourceMgr.BadTankR, x, y, null); g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null);
break; break;
case DOWN: case DOWN:
g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankD : ResourceMgr.BadTankD, x, y, null); g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null);
break; break;
default: default:
break; break;
@ -140,6 +155,16 @@ public class Tank {
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;
} }
@ -159,4 +184,14 @@ public class Tank {
public void die() { public void die() {
this.living = false; this.living = false;
} }
public void stop() {
this.moving = false;
}
public void back() {
x = oldX;
y = oldY;
}
} }

@ -8,17 +8,10 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class TankFrame extends Frame { public class TankFrame extends Frame {
Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this); GameModel gm = GameModel.getInstance();
List<Bullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>();
List<Explode> explodes = new ArrayList<>();
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"));
@ -54,35 +47,7 @@ public class TankFrame extends Frame {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
Color c = g.getColor(); gm.paint(g);
g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克的数量:" + tanks.size(), 10, 80);
g.drawString("爆炸的数量:" + explodes.size(), 10, 100);
g.setColor(c);
myTank.paint(g);
for (int i = 0; i < bullets.size(); i++) {
bullets.get(i).paint(g);
}
for (int i = 0; i < tanks.size(); i++) {
tanks.get(i).paint(g);
}
for (int i = 0; i < explodes.size(); i++) {
explodes.get(i).paint(g);
}
for (int i = 0; i < bullets.size(); i++) {
for (int j = 0; j < tanks.size(); j++)
bullets.get(i).collideWith(tanks.get(j));
}
// for (Iterator<Bullet> it = bullets.iterator(); it.hasNext()) {
// Bullet b = it.next();
// if (!b.live) it.remove();
// }
} }
@ -133,7 +98,7 @@ public class TankFrame extends Frame {
bD = false; bD = false;
break; break;
case KeyEvent.VK_CONTROL: case KeyEvent.VK_CONTROL:
myTank.fire(); gm.getMainTank().fire();
break; break;
default: default:
break; break;
@ -143,7 +108,7 @@ public class TankFrame extends Frame {
} }
private void setMainTankDir() { private void setMainTankDir() {
Tank myTank = gm.getMainTank();
if (!bL && !bU && !bR && !bD) { if (!bL && !bU && !bR && !bD) {
myTank.setMoving(false); myTank.setMoving(false);
} else { } else {

@ -0,0 +1,62 @@
package com.example.tankbattle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Wall extends GameObject{
int w, h;
public Rectangle rect;
public int getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
public Rectangle getRect() {
return rect;
}
public void setRect(Rectangle rect) {
this.rect = rect;
}
public Wall(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.rect = new Rectangle(x, y, w, h);
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
g.fillRect(x, y, w, h);
g.setColor(c);
}
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
}
}

@ -0,0 +1,26 @@
package com.example.tankbattle.cor;
import com.example.tankbattle.Bullet;
import com.example.tankbattle.GameObject;
import com.example.tankbattle.Tank;
public class BulletTankCollider implements Collider {
@Override
public boolean collide(GameObject o1, GameObject o2) {
if (o1 instanceof Bullet && o2 instanceof Tank) {
Bullet bullet = (Bullet) o1;
Tank t = (Tank) o2;
if (bullet.collideWith(t)){
return false;
} else {
return true;
}
} else if (o1 instanceof Tank && o2 instanceof Bullet) {
return collide(o2, o1);
} else {
return true;
}
}
}

@ -0,0 +1,25 @@
package com.example.tankbattle.cor;
import com.example.tankbattle.Bullet;
import com.example.tankbattle.GameObject;
import com.example.tankbattle.Wall;
public class BulletWallCollider implements Collider {
@Override
public boolean collide(GameObject o1, GameObject o2) {
if (o1 instanceof Bullet && o2 instanceof Wall) {
Bullet bullet = (Bullet) o1;
Wall wall = (Wall) o2;
if (bullet.getRect().intersects(wall.getRect())) {
System.out.println("bullet die");
bullet.die();
} else if (o1 instanceof Wall && o2 instanceof Bullet) {
return collide(o2, o1);
}
} else {
System.out.println(o1.getClass().getName() + " " + o2.getClass().getName());
}
return true;
}
}

@ -0,0 +1,7 @@
package com.example.tankbattle.cor;
import com.example.tankbattle.GameObject;
public interface Collider {
boolean collide(GameObject o1, GameObject o2);
}

@ -0,0 +1,32 @@
package com.example.tankbattle.cor;
import com.example.tankbattle.GameObject;
import java.util.LinkedList;
import java.util.List;
public class ColliderChain implements Collider {
private List<Collider> colliders = new LinkedList<>();
public ColliderChain() {
add(new BulletTankCollider());
add(new TankTankCollider());
add(new BulletWallCollider());
add(new TankWallCollider());
}
public void add(Collider c) {
colliders.add(c);
}
@Override
public boolean collide(GameObject o1, GameObject o2) {
for (int i = 0; i < colliders.size(); i++) {
if (!colliders.get(i).collide(o1, o2)) {
return false;
}
}
return true;
}
}

@ -0,0 +1,23 @@
package com.example.tankbattle.cor;
import com.example.tankbattle.GameObject;
import com.example.tankbattle.Tank;
public class TankTankCollider implements Collider {
@Override
public boolean collide(GameObject o1, GameObject o2) {
if (o1 instanceof Tank && o2 instanceof Tank) {
Tank t1 = (Tank) o1;
Tank t2 = (Tank) o2;
if ( t1.getRect().intersects(t2.getRect())){
// t1.stop();
// t2.stop();
t1.back();
t2.back();
}
}
return true;
}
}

@ -0,0 +1,23 @@
package com.example.tankbattle.cor;
import com.example.tankbattle.GameObject;
import com.example.tankbattle.Tank;
import com.example.tankbattle.Wall;
public class TankWallCollider implements Collider {
@Override
public boolean collide(GameObject o1, GameObject o2) {
if (o1 instanceof Tank && o2 instanceof Wall) {
Tank tank = (Tank) o1;
Wall wall = (Wall) o2;
if (tank.getRect().intersects(wall.getRect())) {
tank.back();
} else if (o1 instanceof Wall && o2 instanceof Tank) {
return collide(o2, o1);
}
}
return true;
}
}

@ -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;
}
}

@ -1,4 +1,4 @@
package com.example.tankbattle.strategy; package com.example.tankbattle.demo;
public class Cat implements CustomComparable<Cat> { public class Cat implements CustomComparable<Cat> {

@ -1,4 +1,4 @@
package com.example.tankbattle.strategy; package com.example.tankbattle.demo;
public interface CustomComparable<T> { public interface CustomComparable<T> {
int compareTo(T o); int compareTo(T o);

@ -1,4 +1,4 @@
package com.example.tankbattle.strategy; package com.example.tankbattle.demo;
public interface CustomComparator<T> { public interface CustomComparator<T> {
int compare(T o1, T o2); int compare(T o1, T o2);

@ -1,4 +1,4 @@
package com.example.tankbattle.strategy; package com.example.tankbattle.demo;
public class Dog implements CustomComparable<Dog> { public class Dog implements CustomComparable<Dog> {

@ -1,4 +1,4 @@
package com.example.tankbattle.strategy; package com.example.tankbattle.demo;
public class DogComparator implements CustomComparator<Dog> { public class DogComparator implements CustomComparator<Dog> {

@ -1,4 +1,4 @@
package com.example.tankbattle.strategy; package com.example.tankbattle.demo;
import java.util.Arrays; import java.util.Arrays;

@ -1,4 +1,4 @@
package com.example.tankbattle.strategy; package com.example.tankbattle.demo;
public class Sorter<T> { public class Sorter<T> {

@ -0,0 +1,21 @@
package com.example.tankbattle.strategy;
import com.example.tankbattle.Audio;
import com.example.tankbattle.Bullet;
import com.example.tankbattle.GameModel;
import com.example.tankbattle.GameObject;
import com.example.tankbattle.Group;
import com.example.tankbattle.Tank;
import com.example.tankbattle.decorator.RectDecorator;
public class DefaultFireStrategy implements FireStrategy{
@Override
public void fire(Tank t) {
int bX = t.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
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();
}
}

@ -0,0 +1,7 @@
package com.example.tankbattle.strategy;
import com.example.tankbattle.Tank;
public interface FireStrategy {
void fire(Tank tank);
}

@ -0,0 +1,21 @@
package com.example.tankbattle.strategy;
import com.example.tankbattle.Audio;
import com.example.tankbattle.Bullet;
import com.example.tankbattle.Dir;
import com.example.tankbattle.Group;
import com.example.tankbattle.Tank;
public class FourDirFireStrategy implements FireStrategy{
@Override
public void fire(Tank t) {
int bX = t.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
Dir[] dirs = Dir.values();
for(Dir dir : dirs) {
new Bullet(bX, bY, dir, t.group);
}
if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
}
}

@ -4,3 +4,8 @@ tankSpeed=5
bulletSpeed=10 bulletSpeed=10
gameWidth=1080 gameWidth=1080
gameHeight=720 gameHeight=720
#fireStrategy
goodFS=com.example.tankbattle.strategy.FourDirFireStrategy
badFS=com.example.tankbattle.strategy.DefaultFireStrategy
#colliders
colliders=com.example.tankbattle.cor.BulletTankCollider,com.example.tankbattle.cor.TankTankCollider
Loading…
Cancel
Save