装饰器模式

master
terry 3 years ago
parent eb89138e23
commit 91f4e9d149

Binary file not shown.

@ -7,7 +7,6 @@ import com.demo.tank.util.ResourceManager;
import java.awt.*;
public class Bullet extends GameObject {
private int x, y;
private Direction direction;
private static final int SPEED = 10;
public static final int WIDTH = ResourceManager.bulletD.getWidth();
@ -52,13 +51,17 @@ public class Bullet extends GameObject {
private void move() {
switch (direction) {
case UP: y -= SPEED;
case UP:
y -= SPEED;
break;
case DOWN: y += SPEED;
case DOWN:
y += SPEED;
break;
case LEFT: x -= SPEED;
case LEFT:
x -= SPEED;
break;
case RIGHT: x += SPEED;
case RIGHT:
x += SPEED;
break;
default:
break;
@ -114,4 +117,13 @@ public class Bullet extends GameObject {
this.group = group;
}
@Override
public int getWidth() {
return WIDTH;
}
@Override
public int getHeight() {
return HEIGHT;
}
}

@ -8,7 +8,13 @@ public class DefaultFireStrategy implements FireStrategy {
public void fire(Tank tank) {
int bx = tank.getX() + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int by = tank.getY() + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
new Bullet(bx, by, tank.getDir(), tank.getGroup());
// new Bullet(bx, by, tank.getDir(), tank.getGroup());
// GameModel.getInstance().add(new RectDecorator(new Bullet(bx, by, tank.getDir(), tank.getGroup())));
GameModel.getInstance().add(new RectDecorator(
new TailDecorator(
new Bullet(bx, by, tank.getDir(), tank.getGroup())
)
));
if (tank.getGroup() == Group.GOOD) new Thread(() -> new Audio("audio/tank_fire.wav").play()).start();
}
}

@ -6,7 +6,6 @@ import com.demo.tank.util.ResourceManager;
import java.awt.*;
public class Explode extends GameObject {
private int x, y;
public static final int WIDTH = ResourceManager.explodes[0].getWidth();
public static final int HEIGHT = ResourceManager.explodes[0].getHeight();
@ -42,4 +41,11 @@ public class Explode extends GameObject {
this.y = y;
}
public int getWidth() {
return WIDTH;
}
public int getHeight() {
return HEIGHT;
}
}

@ -0,0 +1,13 @@
package com.demo.tank.course9;
import java.awt.*;
public abstract class GODecorator extends GameObject {
GameObject go;
public GODecorator(GameObject go) {
this.go = go;
}
public abstract void paint(Graphics g);
}

@ -10,6 +10,7 @@ import java.util.List;
public class GameModel {
private static final GameModel GM = new GameModel();
static {
GM.init();
}
@ -24,6 +25,7 @@ public class GameModel {
private GameModel() {
}
private void init() {
tank = new Tank(380, 660, Direction.UP, Group.GOOD);
int enemyTankNum = PropertyManager.getInt("enemy.tank.number");

@ -4,5 +4,10 @@ import java.awt.*;
public abstract class GameObject {
int x, y;
public abstract void paint(Graphics g);
public abstract int getWidth();
public abstract int getHeight();
}

@ -0,0 +1,32 @@
package com.demo.tank.course9;
import java.awt.*;
public class RectDecorator extends GODecorator {
public RectDecorator(GameObject go) {
super(go);
}
@Override
public void paint(Graphics g) {
//decorator x,y 也要跟随游戏物体 变化
this.x = go.x;
this.y = go.y;
go.paint(g);
Color c = g.getColor();
g.setColor(Color.MAGENTA);
g.drawRect(go.x, go.y, getWidth(), getHeight());
g.setColor(c);
}
@Override
public int getWidth() {
return super.go.getWidth();
}
@Override
public int getHeight() {
return super.go.getHeight();
}
}

@ -0,0 +1,31 @@
package com.demo.tank.course9;
import java.awt.*;
public class TailDecorator extends GODecorator {
public TailDecorator(GameObject go) {
super(go);
}
@Override
public void paint(Graphics g) {
go.paint(g);
this.x = go.x;
this.y = go.y;
Color c = g.getColor();
g.setColor(Color.WHITE);
g.drawLine(go.x, go.y, go.x + getWidth(), go.y + getHeight());
g.setColor(c);
}
@Override
public int getWidth() {
return super.go.getWidth();
}
@Override
public int getHeight() {
return super.go.getHeight();
}
}

@ -8,7 +8,6 @@ import java.awt.*;
import java.util.Random;
public class Tank extends GameObject {
private int x,y;
int oldX, oldY;
private Direction dir;
private static final int SPEED = 8;
@ -74,13 +73,17 @@ public class Tank extends GameObject {
//如果没有移动 return
if (!moving) return;
switch (dir) {
case UP: y -= SPEED;
case UP:
y -= SPEED;
break;
case DOWN: y += SPEED;
case DOWN:
y += SPEED;
break;
case LEFT: x -= SPEED;
case LEFT:
x -= SPEED;
break;
case RIGHT: x += SPEED;
case RIGHT:
x += SPEED;
break;
default:
break;
@ -181,4 +184,12 @@ public class Tank extends GameObject {
this.rect = rect;
}
public int getWidth() {
return WIDTH;
}
public int getHeight() {
return HEIGHT;
}
}

@ -4,6 +4,7 @@ import java.util.Random;
public class TankTankCollider implements Collider {
Random random = new Random();
@Override
public void collide(GameObject g1, GameObject g2) {
if (g1 instanceof Tank && g2 instanceof Tank) {

@ -21,4 +21,12 @@ public class Wall extends GameObject{
g.fillRect(x, y, width, height);
g.setColor(c);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}

Loading…
Cancel
Save