Subato

GeoGame

package name.panitz.oose;
import java.util.List;
import java.util.ArrayList;

public class SimpleGeoGame implements GeoGame {
  List<GeometricObject> geos = new ArrayList<>();
  int width;
  int height;
  
  public SimpleGeoGame(int width,int height, GeometricObject...  geos) {
    for (var geo:geos) this.geos.add(geo);
    this.width = width;
    this.height = height;
  }
  
  public SimpleGeoGame(GeometricObject... geometricObjects) {
    this(800,600,geometricObjects);
  }
  
  @Override
  public void move() {
    for (GeometricObject geo : geos) {
      geo.move();
    }
  }

  @Override
  public void collisionCheck() {
    for (int i = 0; i < geos.size(); i++) {
      GeometricObject geo = geos.get(i);
      if (geo.corner.x <= 0 || geo.corner.x+geo.width>=getWidth()){
        geo.velocity.x *= -1;
      }
      if (geo.corner.y <= 0 || geo.corner.y+geo.height>=getHeight()){
        geo.velocity.y *= -1;
      }
        
      for (int j = i+1; j < geos.size(); j++) {
        GeometricObject geo2 = geos.get(j);
        if (geo.touches(geo2)){
          Vertex v = geo.velocity;
          geo.velocity = geo2.velocity;
          geo2.velocity = v;
        }
      }
    }
  }
  @Override
  public List<GeometricObject> getGeos() {
    return geos;
  }
  @Override
  public int getWidth() {
    return width;
  }
  @Override
  public int getHeight() {
    return height;
  }
}