添加墙和碰撞策略

dp_gamemodel
kn5886348135 3 years ago
parent c3b59f519e
commit 76f7f49812

@ -39,6 +39,14 @@ public class Bullet extends GameObject{
this.group = group; this.group = group;
} }
public Rectangle getRect() {
return rect;
}
public void setRect(Rectangle rect) {
this.rect = rect;
}
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
if (!living) { if (!living) {
@ -102,7 +110,7 @@ public class Bullet extends GameObject{
return false; return false;
} }
private void die() { public void die() {
this.living = false; this.living = false;
} }
} }

@ -27,6 +27,11 @@ public class GameModel {
for (int i = 0; i < initTankCount; i++) { for (int i = 0; i < initTankCount; i++) {
add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, this)); add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, this));
} }
add(new Wall(150, 150, 200, 50));
add(new Wall(550, 150, 200, 50));
add(new Wall(500, 300, 200, 50));
add(new Wall(300, 500, 50, 200));
} }
public void add(GameObject go) { public void add(GameObject go) {

@ -24,6 +24,8 @@ public class Tank extends GameObject{
public Group group = Group.BAD; public Group group = Group.BAD;
int oldX, oldY;
FireStrategy fs; FireStrategy fs;
public GameModel gm; public GameModel gm;
@ -87,6 +89,9 @@ public class Tank extends GameObject{
} }
private void move() { private void move() {
oldX=x;
oldY=y;
if (!moving) return; if (!moving) return;
switch (dir) { switch (dir) {
case LEFT: case LEFT:
@ -175,4 +180,9 @@ public class Tank extends GameObject{
this.moving = false; this.moving = false;
} }
public void back() {
x = oldX;
y = oldY;
}
} }

@ -0,0 +1,52 @@
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);
}
}

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

@ -12,6 +12,8 @@ public class ColliderChain implements Collider {
public ColliderChain() { public ColliderChain() {
add(new BulletTankCollider()); add(new BulletTankCollider());
add(new TankTankCollider()); add(new TankTankCollider());
add(new BulletWallCollider());
add(new TankWallCollider());
} }
public void add(Collider c) { public void add(Collider c) {

@ -12,8 +12,10 @@ public class TankTankCollider implements Collider {
Tank t1 = (Tank) o1; Tank t1 = (Tank) o1;
Tank t2 = (Tank) o2; Tank t2 = (Tank) o2;
if ( t1.getRect().intersects(t2.getRect())){ if ( t1.getRect().intersects(t2.getRect())){
t1.stop(); // t1.stop();
// t2.stop(); // t2.stop();
t1.back();
t2.back();
} }
} }
return true; 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;
}
}
Loading…
Cancel
Save