Compare commits
5 Commits
master
...
dp_gamemod
Author | SHA1 | Date |
---|---|---|
|
788791c52f | 3 years ago |
|
76f7f49812 | 3 years ago |
|
c3b59f519e | 3 years ago |
|
2860e1a336 | 3 years ago |
|
acb756e651 | 3 years ago |
@ -1,29 +1,37 @@
|
||||
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 HEIGHT = ResourceMgr.explodes[0].getHeight();
|
||||
|
||||
private int x, y;
|
||||
|
||||
TankFrame tf = null;
|
||||
|
||||
private int step = 0;
|
||||
|
||||
public Explode(int x, int y, TankFrame tf) {
|
||||
public Explode(int x, int y) {
|
||||
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);
|
||||
|
||||
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();
|
||||
}
|
@ -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> {
|
@ -1,4 +1,4 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
package com.example.tankbattle.demo;
|
||||
|
||||
public interface CustomComparable<T> {
|
||||
int compareTo(T o);
|
@ -1,4 +1,4 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
package com.example.tankbattle.demo;
|
||||
|
||||
public interface CustomComparator<T> {
|
||||
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> {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
package com.example.tankbattle.demo;
|
||||
|
||||
public class DogComparator implements CustomComparator<Dog> {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
package com.example.tankbattle.demo;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
package com.example.tankbattle.demo;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue