Compare commits
No commits in common. '76f7f498126fe2a59a1d203d598d0c4ef8f5a019' and 'acb756e6517d0c7aec08c9af1db26f9095492669' have entirely different histories.
76f7f49812
...
acb756e651
@ -1,9 +1,4 @@
|
|||||||
package com.example.tankbattle.strategy;
|
package com.example.tankbattle;
|
||||||
|
|
||||||
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{
|
public class DefaultFireStrategy implements FireStrategy{
|
||||||
|
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.example.tankbattle;
|
||||||
|
|
||||||
|
public interface FireStrategy {
|
||||||
|
void fire(Tank tank);
|
||||||
|
}
|
@ -1,10 +1,4 @@
|
|||||||
package com.example.tankbattle.strategy;
|
package com.example.tankbattle;
|
||||||
|
|
||||||
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{
|
public class FourDirFireStrategy implements FireStrategy{
|
||||||
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.tankbattle;
|
|
||||||
|
|
||||||
import java.awt.Graphics;
|
|
||||||
|
|
||||||
public abstract class GameObject {
|
|
||||||
public int x;
|
|
||||||
public int y;
|
|
||||||
|
|
||||||
public abstract void paint(Graphics g);
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.tankbattle.cor;
|
|
||||||
|
|
||||||
import com.example.tankbattle.GameObject;
|
|
||||||
|
|
||||||
public interface Collider {
|
|
||||||
boolean collide(GameObject o1, GameObject o2);
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
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.demo;
|
package com.example.tankbattle.strategy;
|
||||||
|
|
||||||
|
|
||||||
public class Cat implements CustomComparable<Cat> {
|
public class Cat implements CustomComparable<Cat> {
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.tankbattle.demo;
|
package com.example.tankbattle.strategy;
|
||||||
|
|
||||||
public interface CustomComparable<T> {
|
public interface CustomComparable<T> {
|
||||||
int compareTo(T o);
|
int compareTo(T o);
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.tankbattle.demo;
|
package com.example.tankbattle.strategy;
|
||||||
|
|
||||||
public interface CustomComparator<T> {
|
public interface CustomComparator<T> {
|
||||||
int compare(T o1, T o2);
|
int compare(T o1, T o2);
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.tankbattle.demo;
|
package com.example.tankbattle.strategy;
|
||||||
|
|
||||||
public class Dog implements CustomComparable<Dog> {
|
public class Dog implements CustomComparable<Dog> {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.tankbattle.demo;
|
package com.example.tankbattle.strategy;
|
||||||
|
|
||||||
public class DogComparator implements CustomComparator<Dog> {
|
public class DogComparator implements CustomComparator<Dog> {
|
||||||
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.tankbattle.strategy;
|
|
||||||
|
|
||||||
import com.example.tankbattle.Tank;
|
|
||||||
|
|
||||||
public interface FireStrategy {
|
|
||||||
void fire(Tank tank);
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.tankbattle.demo;
|
package com.example.tankbattle.strategy;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.tankbattle.demo;
|
package com.example.tankbattle.strategy;
|
||||||
|
|
||||||
public class Sorter<T> {
|
public class Sorter<T> {
|
||||||
|
|
Loading…
Reference in new issue