抽象工厂,动态添加产品族

dp
kn5886348135 3 years ago
parent 33989e28ee
commit 6ad8fb6d0a

@ -61,8 +61,8 @@ public class Audio {
int len = 0; int len = 0;
sourceDataLine.open(audioFormat, 1024 * 5); sourceDataLine.open(audioFormat, 1024 * 5);
sourceDataLine.start(); sourceDataLine.start();
System.out.println(audioInputStream.markSupported()); // System.out.println(audioInputStream.markSupported());
// audioInputStream.mark(12358946); audioInputStream.mark(12358946);
while ((len = audioInputStream.read(b)) > 0) { while ((len = audioInputStream.read(b)) > 0) {
sourceDataLine.write(b, 0, len); sourceDataLine.write(b, 0, len);
} }

@ -1,9 +1,12 @@
package com.example.tankbattle; package com.example.tankbattle;
import com.example.tankbattle.abstractfactory.BaseBullet;
import com.example.tankbattle.abstractfactory.BaseTank;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Rectangle; import java.awt.Rectangle;
public class Bullet { public class Bullet extends BaseBullet {
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();
@ -40,6 +43,7 @@ public class Bullet {
this.group = group; this.group = group;
} }
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) { if (!living) {
tf.bullets.remove(this); tf.bullets.remove(this);
@ -89,7 +93,8 @@ 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){ @Override
public void collideWith(BaseTank tank) {
if (this.group == tank.getGroup()) return; if (this.group == tank.getGroup()) return;
if (this.rect.intersects(tank.rect)) { if (this.rect.intersects(tank.rect)) {
tank.die(); tank.die();

@ -7,7 +7,7 @@ public class DefaultFireStrategy implements FireStrategy {
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.tf); t.tf.bullets.add(new Bullet(bX, bY, t.dir, t.group, t.tf));
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();
} }

@ -1,8 +1,10 @@
package com.example.tankbattle; package com.example.tankbattle;
import com.example.tankbattle.abstractfactory.BaseExplode;
import java.awt.*; import java.awt.*;
public class Explode { public class Explode extends BaseExplode {
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();
@ -18,6 +20,7 @@ public class Explode {
this.tf = tf; this.tf = tf;
} }
@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);

@ -1,9 +1,11 @@
package com.example.tankbattle; package com.example.tankbattle;
import com.example.tankbattle.abstractfactory.BaseTank;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
public class Tank { public class Tank extends BaseTank {
private static final int SPEED = 2; private static final int SPEED = 2;
public static int WIDTH = ResourceMgr.goodTankU.getWidth(); public static int WIDTH = ResourceMgr.goodTankU.getWidth();
@ -132,6 +134,7 @@ public class Tank {
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) tf.tanks.remove(this);
switch (dir) { switch (dir) {
@ -169,6 +172,7 @@ public class Tank {
this.y = y; this.y = y;
} }
@Override
public void die() { public void die() {
this.living = false; this.living = false;
} }

@ -1,5 +1,11 @@
package com.example.tankbattle; package com.example.tankbattle;
import com.example.tankbattle.abstractfactory.BaseBullet;
import com.example.tankbattle.abstractfactory.BaseExplode;
import com.example.tankbattle.abstractfactory.BaseTank;
import com.example.tankbattle.abstractfactory.DefaultFactory;
import com.example.tankbattle.abstractfactory.GameFactory;
import java.awt.Color; import java.awt.Color;
import java.awt.Frame; import java.awt.Frame;
import java.awt.Graphics; import java.awt.Graphics;
@ -15,15 +21,17 @@ public class TankFrame extends Frame {
Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this); Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this);
List<Bullet> bullets = new ArrayList<>(); public List<BaseBullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>(); public List<BaseTank> tanks = new ArrayList<>();
List<Explode> explodes = new ArrayList<>(); public List<BaseExplode> explodes = new ArrayList<>();
public GameFactory gf = new DefaultFactory();
public static final int GAME_WIDTH = Integer.valueOf(PropertyMgr.get("gameWidth"));
static final int GAME_WIDTH = Integer.valueOf(PropertyMgr.get("gameWidth")); public static final int GAME_HEIGHT = Integer.valueOf(PropertyMgr.get("gameHeight"));
static final int GAME_HEIGHT = Integer.valueOf(PropertyMgr.get("gameHeight"));
public TankFrame() { public TankFrame() {
setSize(GAME_WIDTH, GAME_HEIGHT); setSize(GAME_WIDTH, GAME_HEIGHT);
setResizable(false); setResizable(false);
setTitle("tank battle"); setTitle("tank battle");
@ -38,6 +46,7 @@ public class TankFrame extends Frame {
} }
Image offScreenImage = null; Image offScreenImage = null;
@Override @Override
public void update(Graphics g) { public void update(Graphics g) {
if (offScreenImage == null) { if (offScreenImage == null) {
@ -114,6 +123,7 @@ public class TankFrame extends Frame {
} }
setMainTankDir(); setMainTankDir();
new Thread(()->new Audio("audio/tank_move.wav").play()).start();
} }
@Override @Override
@ -148,10 +158,14 @@ public class TankFrame extends Frame {
myTank.setMoving(false); myTank.setMoving(false);
} else { } else {
myTank.setMoving(true); myTank.setMoving(true);
if (bL) myTank.setDir(Dir.LEFT); if (bL)
if (bU) myTank.setDir(Dir.UP); myTank.setDir(Dir.LEFT);
if (bR) myTank.setDir(Dir.RIGHT); if (bU)
if (bD) myTank.setDir(Dir.DOWN); myTank.setDir(Dir.UP);
if (bR)
myTank.setDir(Dir.RIGHT);
if (bD)
myTank.setDir(Dir.DOWN);
} }
} }
} }

@ -0,0 +1,9 @@
package com.example.tankbattle.abstractfactory;
import java.awt.Graphics;
public abstract class BaseBullet {
public abstract void paint(Graphics g);
public abstract void collideWith(BaseTank tank);
}

@ -0,0 +1,7 @@
package com.example.tankbattle.abstractfactory;
import java.awt.Graphics;
public abstract class BaseExplode {
public abstract void paint(Graphics g);
}

@ -0,0 +1,23 @@
package com.example.tankbattle.abstractfactory;
import com.example.tankbattle.Group;
import java.awt.Graphics;
import java.awt.Rectangle;
public abstract class BaseTank {
public Group group = Group.BAD;
public Rectangle rect = new Rectangle();
public abstract void paint(Graphics g);
public Group getGroup() {
return this.group;
}
public abstract void die();
public abstract int getX();
public abstract int getY();
}

@ -0,0 +1,25 @@
package com.example.tankbattle.abstractfactory;
import com.example.tankbattle.Bullet;
import com.example.tankbattle.Dir;
import com.example.tankbattle.Explode;
import com.example.tankbattle.Group;
import com.example.tankbattle.Tank;
import com.example.tankbattle.TankFrame;
public class DefaultFactory extends GameFactory {
@Override
public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tf) {
return new Tank(x, y, dir, group, tf);
}
@Override
public BaseExplode createExplode(int x, int y, TankFrame tf) {
return new Explode(x, y, tf);
}
@Override
public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tf) {
return new Bullet(x, y, dir, group, tf);
}
}

@ -0,0 +1,14 @@
package com.example.tankbattle.abstractfactory;
import com.example.tankbattle.Dir;
import com.example.tankbattle.Group;
import com.example.tankbattle.TankFrame;
public abstract class GameFactory {
public abstract BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tf);
public abstract BaseExplode createExplode(int x, int y, TankFrame tf);
public abstract BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tf);
}

@ -0,0 +1,105 @@
package com.example.tankbattle.abstractfactory;
import com.example.tankbattle.Dir;
import com.example.tankbattle.Explode;
import com.example.tankbattle.Group;
import com.example.tankbattle.ResourceMgr;
import com.example.tankbattle.Tank;
import com.example.tankbattle.TankFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class RectBullet extends BaseBullet {
private static final int SPEED = 6;
public static int WIDTH = ResourceMgr.bulletD.getWidth();
public static int HEIGHT = ResourceMgr.bulletD.getHeight();
Rectangle rect = new Rectangle();
private int x, y;
private Dir dir;
private boolean living = true;
TankFrame tf = null;
private Group group = Group.BAD;
public RectBullet(int x, int y, Dir dir, Group group, TankFrame tf) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tf = tf;
rect.x = this.x;
rect.y = this.y;
rect.width = WIDTH;
rect.height = HEIGHT;
tf.bullets.add(this);
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public void paint(Graphics g) {
if (!living) {
tf.bullets.remove(this);
}
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.fillRect(x, y, 20, 20);
g.setColor(c);
move();
}
private void move() {
switch (dir) {
case LEFT:
x -= SPEED;
break;
case UP:
y -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case DOWN:
y += SPEED;
break;
default:
break;
}
rect.x = this.x;
rect.y = this.y;
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false;
}
@Override
public void collideWith(BaseTank tank) {
if (this.group == tank.getGroup()) return;
if (this.rect.intersects(tank.rect)) {
tank.die();
this.die();
int eX = tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2;
int eY = tank.getY() + Tank.HEIGHT / 2 - Explode.HEIGHT / 2;
tf.explodes.add(tf.gf.createExplode(eX, eY, tf));
}
}
private void die() {
this.living = false;
}
}

@ -0,0 +1,45 @@
package com.example.tankbattle.abstractfactory;
import com.example.tankbattle.Audio;
import com.example.tankbattle.ResourceMgr;
import com.example.tankbattle.TankFrame;
import java.awt.Color;
import java.awt.Graphics;
public class RectExplode extends BaseExplode {
public static int WIDTH = ResourceMgr.explodes[0].getWidth();
public static int HEIGHT = ResourceMgr.explodes[0].getHeight();
private int x, y;
TankFrame tf = null;
private int step = 0;
public RectExplode(int x, int y, TankFrame tf) {
this.x = x;
this.y = y;
this.tf = tf;
new Thread(() -> new Audio("audio/explode.wav").play()).start();
}
@Override
public void paint(Graphics g) {
// g.drawImage(ResourceMgr.explodes[step++], x, y, null);
Color c = g.getColor();
g.setColor(Color.RED);
g.fillRect(x, y, 10*step, 10*step);
step++;
if (step >= 15) {
tf.explodes.remove(this);
}
g.setColor(c);
}
}

@ -0,0 +1,23 @@
package com.example.tankbattle.abstractfactory;
import com.example.tankbattle.Dir;
import com.example.tankbattle.Group;
import com.example.tankbattle.TankFrame;
public class RectFactory extends GameFactory {
@Override
public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tf) {
return new RectTank(x, y, dir, group, tf);
}
@Override
public BaseExplode createExplode(int x, int y, TankFrame tf) {
return new RectExplode(x, y, tf);
}
@Override
public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tf) {
return new RectBullet(x, y, dir, group, tf);
}
}

@ -0,0 +1,191 @@
package com.example.tankbattle.abstractfactory;
import com.example.tankbattle.Audio;
import com.example.tankbattle.Bullet;
import com.example.tankbattle.DefaultFireStrategy;
import com.example.tankbattle.Dir;
import com.example.tankbattle.FireStrategy;
import com.example.tankbattle.Group;
import com.example.tankbattle.PropertyMgr;
import com.example.tankbattle.ResourceMgr;
import com.example.tankbattle.Tank;
import com.example.tankbattle.TankFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class RectTank extends BaseTank{
private static final int SPEED = 2;
public static int WIDTH = ResourceMgr.goodTankU.getWidth();
public static int HEIGHT = ResourceMgr.goodTankU.getHeight();
public Rectangle rect = new Rectangle();
private Random random = new Random();
int x, y;
Dir dir = Dir.DOWN;
private boolean moving = true;
TankFrame tf = null;
private boolean living = true;
Group group = Group.BAD;
FireStrategy fs;
public RectTank(int x, int y, Dir dir, Group group, TankFrame tf) {
super();
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tf = tf;
rect.x = this.x;
rect.y = this.y;
rect.width = WIDTH;
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();
}
}
public void fire(){
// fs.fire(this);
int bX = this.x + RectTank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = this.y + RectTank.HEIGHT / 2 - Bullet.HEIGHT / 2;
Dir[] dirs = Dir.values();
for (Dir dir : dirs) {
tf.gf.createBullet(bX, bY, dir, group, tf);
}
if (group == Group.GOOD)
new Thread(() -> new Audio("audio/tank_fire.wav").play()).start();
}
public Dir getDir() {
return dir;
}
@Override
public int getX() {
return x;
}
@Override
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Override
public int getY() {
return y;
}
public boolean isMoving() {
return moving;
}
private void move() {
if (!moving) return;
switch (dir) {
case LEFT:
x -= SPEED;
break;
case UP:
y -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case DOWN:
y += SPEED;
break;
default:
break;
}
if (this.group == Group.BAD && random.nextInt(100) > 95)
this.fire();
if (this.group == Group.BAD && random.nextInt(100) > 95)
randomDir();
boundsCheck();
}
private void boundsCheck() {
if (this.x < 2)
x = 2;
if (this.y < 28)
y = 28;
if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH)
x = TankFrame.GAME_WIDTH - Tank.WIDTH;
if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT)
y = TankFrame.GAME_HEIGHT - Tank.HEIGHT;
}
private void randomDir() {
this.dir = Dir.values()[random.nextInt(4)];
}
@Override
public void paint(Graphics g) {
if (!living) tf.tanks.remove(this);
Color c = g.getColor();
g.setColor(group == Group.GOOD ? Color.RED : Color.BLUE);
g.fillRect(x, y, 40, 40);
g.setColor(c);
move();
}
public void setDir(Dir dir) {
this.dir = dir;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
@Override
public void die() {
this.living = false;
}
}

@ -5,5 +5,5 @@ bulletSpeed=10
gameWidth=1080 gameWidth=1080
gameHeight=720 gameHeight=720
#fireStrategy #fireStrategy
goodFS=com.mashibing.tank.FourDirFireStrategy goodFS=com.example.tankbattle.FourDirFireStrategy
badFS=com.mashibing.tank.DefaultFireStrategy badFS=com.example.tankbattle.DefaultFireStrategy
Loading…
Cancel
Save