Compare commits
3 Commits
acb756e651
...
76f7f49812
Author | SHA1 | Date |
---|---|---|
|
76f7f49812 | 3 years ago |
|
c3b59f519e | 3 years ago |
|
2860e1a336 | 3 years ago |
@ -1,5 +0,0 @@
|
||||
package com.example.tankbattle;
|
||||
|
||||
public interface FireStrategy {
|
||||
void fire(Tank tank);
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.tankbattle;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
public abstract class GameObject {
|
||||
public int x;
|
||||
public int y;
|
||||
|
||||
public abstract void paint(Graphics g);
|
||||
}
|
@ -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,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;
|
||||
}
|
||||
}
|
@ -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> {
|
||||
|
@ -1,4 +1,9 @@
|
||||
package com.example.tankbattle;
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
import com.example.tankbattle.Audio;
|
||||
import com.example.tankbattle.Bullet;
|
||||
import com.example.tankbattle.Group;
|
||||
import com.example.tankbattle.Tank;
|
||||
|
||||
public class DefaultFireStrategy implements FireStrategy{
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
import com.example.tankbattle.Tank;
|
||||
|
||||
public interface FireStrategy {
|
||||
void fire(Tank tank);
|
||||
}
|
@ -1,4 +1,10 @@
|
||||
package com.example.tankbattle;
|
||||
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{
|
||||
|
Loading…
Reference in new issue