Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Asteroids/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dk.sdu.mmmi.cbse</groupId>
<artifactId>Player</artifactId>
<version>1.0.1-SNAPSHOT</version>
<scope>compile</scope>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class AsteroidPlugin implements IGamePluginService {

@Override
public void start(GameData gameData, World world) {
Entity asteroid = createAsteroid(gameData);
world.addEntity(asteroid);

}

@Override
Expand All @@ -27,15 +26,4 @@ public void stop(GameData gameData, World world) {
}
}

private Entity createAsteroid(GameData gameData) {
Entity asteroid = new Asteroid();
Random rnd = new Random();
int size = rnd.nextInt(10) + 5;
asteroid.setPolygonCoordinates(size, -size, -size, -size, -size, size, size, size);
asteroid.setX(0);
asteroid.setY(0);
asteroid.setRadius(size);
asteroid.setRotation(rnd.nextInt(90));
return asteroid;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package dk.sdu.mmmi.cbse.asteroid;

import dk.sdu.mmmi.cbse.common.asteroids.Asteroid;
import dk.sdu.mmmi.cbse.common.asteroids.AsteroidSPI;
import dk.sdu.mmmi.cbse.common.asteroids.IAsteroidSplitter;
import dk.sdu.mmmi.cbse.common.data.Entity;
import dk.sdu.mmmi.cbse.common.data.GameData;
import dk.sdu.mmmi.cbse.common.data.World;
import dk.sdu.mmmi.cbse.common.services.IEntityProcessingService;
import dk.sdu.mmmi.cbse.playersystem.Player;

public class AsteroidProcessor implements IEntityProcessingService {
import java.util.Random;

public class AsteroidProcessor implements IEntityProcessingService, AsteroidSPI {

private IAsteroidSplitter asteroidSplitter = new AsteroidSplitterImpl();

Expand Down Expand Up @@ -39,6 +43,19 @@ public void process(GameData gameData, World world) {

}

long currentTime = System.currentTimeMillis();
if(currentTime - gameData.getLastSpawnedAstroid() >= gameData.getAstroidCooldown()) {
for (int i = 0; i < 3; i++) {
Entity asteroid = createAsteroid(gameData);
asteroid.setHealth(400);
if(asteroid.getX() - world.getEntities(Player.class).get(0).getX() < 10 || asteroid.getY()- world.getEntities(Player.class).get(0).getY() < 10) {
world.addEntity(asteroid);
}

}
gameData.setLastSpawnedAstroid(currentTime);
}

}

/**
Expand All @@ -52,5 +69,20 @@ public void removeAsteroidSplitter(IAsteroidSplitter asteroidSplitter) {
this.asteroidSplitter = null;
}

public IAsteroidSplitter getAsteroidSplitter() {
return asteroidSplitter;
}

public Entity createAsteroid(GameData gameData) {
Entity asteroid = new Asteroid();
Random rnd = new Random();
asteroid.setPolygonCoordinates(-10, -15, -5, -25, 5, -20, 15, -10, 10, 5, 0, 10, -10, 5, -15, -5);
asteroid.setX(rnd.nextInt(700));
asteroid.setY(rnd.nextInt(700));
asteroid.setRadius(20);
asteroid.setRotation(rnd.nextInt(360));
asteroid.setHealth(400);
return asteroid;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package dk.sdu.mmmi.cbse.asteroid;

import dk.sdu.mmmi.cbse.common.asteroids.Asteroid;
import dk.sdu.mmmi.cbse.common.asteroids.IAsteroidSplitter;
import dk.sdu.mmmi.cbse.common.data.Entity;
import dk.sdu.mmmi.cbse.common.data.World;

import java.util.Random;

/**
*
* @author corfixen
Expand All @@ -12,6 +15,30 @@ public class AsteroidSplitterImpl implements IAsteroidSplitter {

@Override
public void createSplitAsteroid(Entity e, World world) {
if (e.getRadius()<=5) {
System.out.println("Too Small!!");
return;
}
//int splitAmount = new Random().nextInt(2) + 1;
int splitAmount = 2;
for (int i = 0; i < splitAmount; i++) {
//System.out.println(splitAmount);
int rndSpawn = new Random().nextInt(10)+10;
Entity asteroid = new Asteroid();
Random randomRotation = new Random();
double[] polygonCoords = new double[e.getPolygonCoordinates().length];
for (int j = 0; j < polygonCoords.length; j++) {
polygonCoords[j] = e.getPolygonCoordinates()[j]/2;
}
asteroid.setPolygonCoordinates(polygonCoords);
asteroid.setX(e.getX()+rndSpawn);
asteroid.setY(e.getY()+rndSpawn);
asteroid.setRadius(e.getRadius()/2);
asteroid.setRotation(e.getRotation() - 60 + randomRotation.nextInt(120));
asteroid.setHealth(e.getHealth()/2);
world.addEntity(asteroid);
}

}

}
6 changes: 5 additions & 1 deletion Asteroids/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import dk.sdu.mmmi.cbse.asteroid.AsteroidSplitterImpl;
import dk.sdu.mmmi.cbse.common.asteroids.IAsteroidSplitter;
import dk.sdu.mmmi.cbse.common.services.IEntityProcessingService;
import dk.sdu.mmmi.cbse.common.services.IGamePluginService;

module Asteroid {
requires Common;
requires CommonAsteroids;
requires Player;
requires Common;
provides IGamePluginService with dk.sdu.mmmi.cbse.asteroid.AsteroidPlugin;
provides IEntityProcessingService with dk.sdu.mmmi.cbse.asteroid.AsteroidProcessor;
provides IAsteroidSplitter with AsteroidSplitterImpl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public void process(GameData gameData, World world) {
double changeY = Math.sin(Math.toRadians(bullet.getRotation()));
bullet.setX(bullet.getX() + changeX * 3);
bullet.setY(bullet.getY() + changeY * 3);
if(bullet.getX() > gameData.getDisplayWidth() || bullet.getX() < -10) {
world.removeEntity(bullet);
}
if(bullet.getY() > gameData.getWorldHeight() || bullet.getY() < -10) {
world.removeEntity(bullet);
}
}
}

Expand All @@ -30,6 +36,8 @@ public Entity createBullet(Entity shooter, GameData gameData) {
bullet.setY(shooter.getY() + changeY * 10);
bullet.setRotation(shooter.getRotation());
bullet.setRadius(1);
bullet.setHealth(50);
return bullet;
}

}
18 changes: 18 additions & 0 deletions Collision/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,23 @@
<artifactId>Common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dk.sdu.mmmi.cbse</groupId>
<artifactId>CommonAsteroids</artifactId>
<version>1.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dk.sdu.mmmi.cbse</groupId>
<artifactId>Player</artifactId>
<version>1.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dk.sdu.mmmi.cbse</groupId>
<artifactId>Enemy</artifactId>
<version>1.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package dk.sdu.mmmi.cbse.collisionsystem;

import dk.sdu.mmmi.cbse.common.asteroids.Asteroid;
import dk.sdu.mmmi.cbse.common.asteroids.IAsteroidSplitter;
import dk.sdu.mmmi.cbse.common.bullet.Bullet;
import dk.sdu.mmmi.cbse.common.services.IPostEntityProcessingService;
import dk.sdu.mmmi.cbse.common.data.Entity;
import dk.sdu.mmmi.cbse.common.data.GameData;
import dk.sdu.mmmi.cbse.common.data.World;
import dk.sdu.mmmi.cbse.enemysystem.Enemy;
import dk.sdu.mmmi.cbse.playersystem.Player;

import java.util.ServiceLoader;

public class CollisionDetector implements IPostEntityProcessingService {

public CollisionDetector() {
}
private static final IAsteroidSplitter splitter = ServiceLoader.load(IAsteroidSplitter.class).iterator().next();

@Override
public void process(GameData gameData, World world) {
Expand All @@ -23,8 +29,49 @@ public void process(GameData gameData, World world) {

// CollisionDetection
if (this.collides(entity1, entity2)) {
world.removeEntity(entity1);
world.removeEntity(entity2);
int tempHealth = entity1.getHealth();
entity1.setHealth(entity1.getHealth() - entity2.getHealth());
entity2.setHealth(entity2.getHealth() - tempHealth);
if (entity1 instanceof Asteroid) {
if(entity1.getHealth()<=0) {
splitter.createSplitAsteroid(entity1, world);
System.out.println("split e1 as: " + entity1.getID());
world.removeEntity(entity1);
} else {
entity1.setRotation(entity1.getRotation() + entity2.getRotation());
}
} else if (entity1 instanceof Player && entity1.getHealth()<=0) {
System.out.println("You Died!");
System.exit(400);
} else if (entity1 instanceof Enemy) {
if(entity1.getHealth()<=0) {
world.removeEntity(entity1);
}
} else if (entity1 instanceof Bullet) {
if(entity1.getHealth()<=0) {
world.removeEntity(entity1);
}
}
if (entity2 instanceof Asteroid) {
if(entity2.getHealth()<=0) {
splitter.createSplitAsteroid(entity2, world);
System.out.println("split e2 as: " + entity2.getID());
world.removeEntity(entity2);
} else {
entity2.setRotation(entity1.getRotation() + entity1.getRotation());
}
} else if (entity2 instanceof Player && entity2.getHealth()<=0) {
System.out.println("You Died!");
System.exit(400);
} else if (entity2 instanceof Enemy) {
if(entity2.getHealth()<=0) {
world.removeEntity(entity2);
}
} else if (entity2 instanceof Bullet) {
if(entity2.getHealth()<=0) {
world.removeEntity(entity2);
}
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion Collision/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import dk.sdu.mmmi.cbse.common.asteroids.IAsteroidSplitter;
import dk.sdu.mmmi.cbse.common.services.IPostEntityProcessingService;

module Collision {
requires Common;
requires CommonAsteroids;
requires Player;
requires Enemy;
requires CommonBullet;
requires Common;
provides IPostEntityProcessingService with dk.sdu.mmmi.cbse.collisionsystem.CollisionDetector;
uses IAsteroidSplitter;
}
47 changes: 47 additions & 0 deletions Common/src/main/java/dk/sdu/mmmi/cbse/common/data/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ public class Entity implements Serializable {
private double y;
private double rotation;
private float radius;
private int health;
private long lastFiredBullet;
private int EnemyBulletCooldown = 1000;
private long lastEnemyMove;
private int enemyMoveCooldown = 100;

public int getEnemyBulletCooldown() {
return EnemyBulletCooldown;
}

public void setEnemyBulletCooldown(int enemyBulletCooldown) {
EnemyBulletCooldown = enemyBulletCooldown;
}

public long getLastEnemyMove() {
return lastEnemyMove;
}

public void setLastEnemyMove(long lastEnemyMove) {
this.lastEnemyMove = lastEnemyMove;
}

public int getEnemyMoveCooldown() {
return enemyMoveCooldown;
}

public void setEnemyMoveCooldown(int enemyMoveCooldown) {
this.enemyMoveCooldown = enemyMoveCooldown;
}




public String getID() {
Expand Down Expand Up @@ -60,4 +91,20 @@ public void setRadius(float radius) {
public float getRadius() {
return this.radius;
}

public int getHealth() {
return health;
}

public void setHealth(int health) {
this.health = health;
}

public long getLastFiredBullet() {
return lastFiredBullet;
}

public void setLastFiredBullet(long lastFiredBullet) {
this.lastFiredBullet = lastFiredBullet;
}
}
Loading