parent
c3b59f519e
commit
76f7f49812
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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…
Reference in new issue