Compare commits

...
13 Commits
Author SHA1 Message Date
BrychanD 05c57efc52 Updated examples to the latest version
For release, used in the report/presentation
2021-06-15 18:13:49 +12:00
BrychanD 3c45ca9de8 Fixed exceptions due to the renderer not being ready 2021-06-12 15:56:13 +12:00
BrychanD 117589b17b Got example 2 working
Player input; and a different way of setting up the renderer
2021-06-12 15:53:30 +12:00
BrychanD bca92faa88 Upgraded to 0.9.9, minor physics improvements 2021-06-12 12:54:54 +12:00
BrychanD 4febf41721 Added some skeletons 2021-06-11 21:09:41 +12:00
BrychanD 8082817860 Added player control example base 2021-06-11 20:55:42 +12:00
BrychanD 21513f69ed Added a perftest 2021-06-11 14:42:19 +12:00
BrychanD 87389e78eb Merge branch 'master' of https://git.software.kauripeak.co.nz/BrychanD/JavaECS-Examples 2021-06-11 11:37:53 +12:00
BrychanD 73228ed05a Improved the physics system 2021-06-11 11:37:50 +12:00
BrychanD b39b2f1d10 Added perftest project 2021-06-11 11:36:38 +12:00
BrychanD 294c0fc52f Merge branch 'master' of https://git.software.kauripeak.co.nz/BrychanD/JavaECS-Examples 2021-06-10 22:44:18 +12:00
BrychanD 0db1abcbdf Improved the program 2021-06-10 22:44:16 +12:00
BrychanD 4d76428553 Update 'README.md' 2021-06-10 13:33:44 +12:00
43 changed files with 1283 additions and 270 deletions
+1 -1
View File
@@ -3,4 +3,4 @@
Demo 1 implements a simple physics system Demo 1 implements a simple physics system
### See the example run: ### See the example run:
![example 1](./content/example1.webp) ![example 1](./content/example1/example1.webp)
+2
View File
@@ -26,3 +26,5 @@ buildNumber.properties
.factorypath .factorypath
.project .project
.vscode/ .vscode/
*.jar
+9 -9
View File
@@ -19,8 +19,8 @@
<!-- JaCoCo thresholds. Increase gradually as you add tests. --> <!-- JaCoCo thresholds. Increase gradually as you add tests. -->
<jacoco.unit-tests.limit.instruction-ratio>0%</jacoco.unit-tests.limit.instruction-ratio> <jacoco.unit-tests.limit.instruction-ratio>0%</jacoco.unit-tests.limit.instruction-ratio>
<jacoco.unit-tests.limit.branch-ratio>0%</jacoco.unit-tests.limit.branch-ratio> <jacoco.unit-tests.limit.branch-ratio>0%</jacoco.unit-tests.limit.branch-ratio>
<jacoco.unit-tests.limit.class-complexity>20</jacoco.unit-tests.limit.class-complexity> <jacoco.unit-tests.limit.class-complexity>2100</jacoco.unit-tests.limit.class-complexity>
<jacoco.unit-tests.limit.method-complexity>5</jacoco.unit-tests.limit.method-complexity> <jacoco.unit-tests.limit.method-complexity>1000</jacoco.unit-tests.limit.method-complexity>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
@@ -38,12 +38,12 @@
<dependency> <dependency>
<groupId>nz.ac.massey.javaecs</groupId> <groupId>nz.ac.massey.javaecs</groupId>
<artifactId>javaecs</artifactId> <artifactId>javaecs</artifactId>
<version>0.9.2-PRERELEASE</version> <version>1.0.1</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <!-- <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId> <artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin.version}</version> <version>${maven-enforcer-plugin.version}</version>
@@ -93,8 +93,8 @@
</goals> </goals>
</execution> </execution>
</executions> </executions>
</plugin> </plugin> -->
<plugin> <!--<plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version> <version>${maven-surefire-plugin.version}</version>
@@ -165,7 +165,7 @@
</configuration> </configuration>
</execution> </execution>
</executions> </executions>
</plugin> </plugin> -->
</plugins> </plugins>
</build> </build>
<reporting> <reporting>
@@ -179,7 +179,7 @@
</reporting> </reporting>
<profiles> <profiles>
<!-- Publish coverage report to Coveralls, only when running in Travis. --> <!-- Publish coverage report to Coveralls, only when running in Travis. -->
<profile> <!-- <profile>
<id>travis</id> <id>travis</id>
<activation> <activation>
<property> <property>
@@ -195,6 +195,6 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</profile> </profile> -->
</profiles> </profiles>
</project> </project>
@@ -1,10 +1,9 @@
package nz.ac.massey.javaecs.examples; package nz.ac.massey.javaecs.examples;
import java.sql.Time;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Semaphore;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import nz.ac.massey.javaecs.*; import nz.ac.massey.javaecs.*;
import nz.ac.massey.javaecs.examples.Components.*; import nz.ac.massey.javaecs.examples.Components.*;
import nz.ac.massey.javaecs.examples.Systems.*; import nz.ac.massey.javaecs.examples.Systems.*;
@@ -16,14 +15,22 @@ public final class App {
private App() { private App() {
} }
public static Semaphore rendererReadySemaphore = new Semaphore(0);
/** /**
* Simple physics simulation. * Simple physics simulation.
* @param args The arguments of the program. * @param args The arguments of the program.
* @throws InterruptedException * @throws InterruptedException
*/ */
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
/************************************
** Initialise engine **
*************************************/
Engine gameEngine = new Engine(16384); Engine gameEngine = new Engine(16384);
/************************************
** Register game components **
*************************************/
gameEngine.registerComponent(Gravity.class); gameEngine.registerComponent(Gravity.class);
gameEngine.registerComponent(RidgidBody.class); gameEngine.registerComponent(RidgidBody.class);
gameEngine.registerComponent(Vec2D.class); gameEngine.registerComponent(Vec2D.class);
@@ -35,6 +42,9 @@ public final class App {
gameEngine.registerComponent(Reporter.class); gameEngine.registerComponent(Reporter.class);
gameEngine.registerComponent(TextRender.class); gameEngine.registerComponent(TextRender.class);
/************************************
** Register and initialise systems **
*************************************/
PhysicsSystem physicsSystem = new PhysicsSystem(gameEngine); PhysicsSystem physicsSystem = new PhysicsSystem(gameEngine);
gameEngine.registerSystem(PhysicsSystem.class, physicsSystem); gameEngine.registerSystem(PhysicsSystem.class, physicsSystem);
@@ -45,7 +55,7 @@ public final class App {
LogUpdateSystem logUpdateSystem = new LogUpdateSystem(gameEngine); LogUpdateSystem logUpdateSystem = new LogUpdateSystem(gameEngine);
gameEngine.registerSystem(LogUpdateSystem.class, logUpdateSystem); gameEngine.registerSystem(LogUpdateSystem.class, logUpdateSystem);
RenderSystem renderSystem = new RenderSystem(gameEngine, 10.); RenderSystem renderSystem = new RenderSystem(gameEngine, 5.);
gameEngine.registerSystem(RenderSystem.class, renderSystem); gameEngine.registerSystem(RenderSystem.class, renderSystem);
ExistenceSystem existenceSystem = new ExistenceSystem(gameEngine); ExistenceSystem existenceSystem = new ExistenceSystem(gameEngine);
@@ -54,100 +64,108 @@ public final class App {
ReportSystem reportSystem = new ReportSystem(gameEngine); ReportSystem reportSystem = new ReportSystem(gameEngine);
gameEngine.registerSystem(ReportSystem.class, reportSystem); gameEngine.registerSystem(ReportSystem.class, reportSystem);
/************************************
** Create known entities **
*************************************/
// Define a play area 100 * 100, make sure to use worldspace not screenspace (top left is 0, 100, not 0,0) // Define a play area 100 * 100, make sure to use worldspace not screenspace (top left is 0, 100, not 0,0)
// Bottom boundary // Bottom boundary
Entity newEnt = gameEngine.createEntity(); Entity newEnt = gameEngine.createEntity();
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., -50.,0)); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., -50.,0));
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0., -1.)); gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0.25, -1.));
gameEngine.addComponent(newEnt, Collider.class, new Collider(200., 50.)); // 200 along, 50 up gameEngine.addComponent(newEnt, Collider.class, new Collider(200., 50.)); // 200 along, 50 up
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(200, 50, 64, 64, 64)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(200, 50, 64, 64, 64));
// Left boundary // Left boundary
newEnt = gameEngine.createEntity(); newEnt = gameEngine.createEntity();
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., -50.,0)); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., -50.,0));
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0., -1.)); gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0.25, -1.));
gameEngine.addComponent(newEnt, Collider.class, new Collider(50., 200.)); gameEngine.addComponent(newEnt, Collider.class, new Collider(50., 200.));
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(50, 200, 64, 64, 64)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(50, 200, 64, 64, 64));
// Top boundary // Top boundary
newEnt = gameEngine.createEntity(); newEnt = gameEngine.createEntity();
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., 100.,0)); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(-50., 100.,0));
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0., -1.)); gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0.25, -1.));
gameEngine.addComponent(newEnt, Collider.class, new Collider(200., 50.)); gameEngine.addComponent(newEnt, Collider.class, new Collider(200., 50.));
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(200, 50, 64, 64, 64)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(200, 50, 64, 64, 64));
// Right boundary // Right boundary
newEnt = gameEngine.createEntity(); newEnt = gameEngine.createEntity();
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(100., -50.,0)); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(100., -50.,0));
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0., -1.)); gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(0., 0., 0., 0., 0.f, 0.25, -1.));
gameEngine.addComponent(newEnt, Collider.class, new Collider(50., 200.)); gameEngine.addComponent(newEnt, Collider.class, new Collider(50., 200.));
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(50, 200, 64, 64, 64)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(50, 200, 64, 64, 64));
// One object // One object
newEnt = gameEngine.createEntity(); newEnt = gameEngine.createEntity();
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(62., 90.,0)); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(62., 90.,0));
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(1., 0., 0., 0., 0.f, 0., 1.)); gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(1., 0., 0., 0., 0.f, 0.66, 120.));
gameEngine.addComponent(newEnt, Collider.class, new Collider(10., 10.)); gameEngine.addComponent(newEnt, Collider.class, new Collider(10., 10.));
gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(10, 10, 255, 64, 64)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(10, 10, 255, 64, 64));
// The textobject that prints the current number of entities
newEnt = gameEngine.createEntity(); newEnt = gameEngine.createEntity();
gameEngine.addComponent(newEnt, TextRender.class, new TextRender("", 24, Color.WHITE)); gameEngine.addComponent(newEnt, TextRender.class, new TextRender("", 24, Color.WHITE));
gameEngine.addComponent(newEnt, Render.class, new Render(TextRender.class)); gameEngine.addComponent(newEnt, Render.class, new Render(TextRender.class));
gameEngine.addComponent(newEnt, Reporter.class, new Reporter()); gameEngine.addComponent(newEnt, Reporter.class, new Reporter());
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(5,5,0)); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(5,5,0));
// Other objects
// Uncomment this block to enable the stress-test (runs 500 square entities, with physics etc.)
Random rand = new Random(); Random rand = new Random();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 500; i++) {
newEnt = gameEngine.createEntity(); newEnt = gameEngine.createEntity();
double size = 1 + rand.nextDouble();
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(10 + rand.nextDouble()* 80, 10+ rand.nextDouble()* 80,0)); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(10 + rand.nextDouble()* 80, 10+ rand.nextDouble()* 80,0));
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(-2.5 + rand.nextDouble()* 5., 0., 0., 0., 0.f, 0., rand.nextDouble() * 20.)); gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(-2.5 + rand.nextDouble()* 5., 0., 0., 0., 0.f, 0.3, 2. * size));
double size = 3 + rand.nextDouble() * 7;
gameEngine.addComponent(newEnt, Collider.class, new Collider(size, size)); gameEngine.addComponent(newEnt, Collider.class, new Collider(size, size));
gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s
gameEngine.addComponent(newEnt, LogUpdate.class, null); // Add to object to print pos of
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(size, size, 30 + rand.nextInt(225), 30 + rand.nextInt(225), 30 + rand.nextInt(225))); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(size, size, 30 + rand.nextInt(225), 30 + rand.nextInt(225), 30 + rand.nextInt(225)));
gameEngine.addComponent(newEnt, TimedExistence.class, new TimedExistence(5 + rand.nextDouble() * 90)); gameEngine.addComponent(newEnt, TimedExistence.class, new TimedExistence(25 + rand.nextDouble() * 90));
} }
//gameEngine.addComponent(newEnt, LogUpdate.class, null); /************************************
** Run init() on the systems **
*************************************/
renderSystem.init();
rendererReadySemaphore.acquire();
physicsSystem.init(); physicsSystem.init();
collisionSystem.init(); collisionSystem.init();
logUpdateSystem.init(); logUpdateSystem.init();
renderSystem.init();
existenceSystem.init(); existenceSystem.init();
reportSystem.init(); reportSystem.init();
/************************************
** Prepare and enter the game loop **
*************************************/
double dt = 0.001; double dt = 0.001;
double frameRate = 1.0 / 144; // 1 second divided by target number of frames double frameRate = 1.0 / 144; // 1 second divided by target number of frames
double idleTime = 0.0; // the amount of time spent sleeping
boolean exit = false; boolean exit = false;
long loopStart = System.currentTimeMillis(); Thread.sleep(5000);
long startTime = System.nanoTime(); long startTime = System.nanoTime();
while (!exit){ while (!exit){
// Run system updates
collisionSystem.update(); collisionSystem.update(dt);
physicsSystem.update(dt); physicsSystem.update(dt);
existenceSystem.update(dt); existenceSystem.update(dt);
reportSystem.update(); reportSystem.update(dt);
//logUpdateSystem.update(); renderSystem.update(dt);
// The render system should be uncoupled from the physics and collision
renderSystem.update();
dt = (System.nanoTime() - startTime) / 1e9; // convert nanoseconds to seconds dt = (System.nanoTime() - startTime) / 1e9; // convert nanoseconds to seconds
startTime = System.nanoTime(); startTime = System.nanoTime();
if (dt < frameRate){ if (dt < frameRate){
idleTime = frameRate-dt;
Thread.sleep((int)((frameRate-dt)*1000)); Thread.sleep((int)((frameRate-dt)*1000));
dt = (System.nanoTime() - startTime) / 1e9; dt = (System.nanoTime() - startTime) / 1e9;
startTime = System.nanoTime(); startTime = System.nanoTime();
} }
dt = 0.001; // Setting dt here to a fixed time-step will lock the rate of the physics. The above code ensures that it is
// executed no more than once 1/framerate seconds.
// dt = 0.001;
} }
} }
} }
@@ -9,7 +9,4 @@ public class Collider {
// bottom-right, top-right is specified by position // bottom-right, top-right is specified by position
public double x = 0.0; public double x = 0.0;
public double y = 0.0; public double y = 0.0;
public boolean collided = false;
public double travelDirection = 0.0;
} }
@@ -1,17 +1,11 @@
package nz.ac.massey.javaecs.examples.Systems; package nz.ac.massey.javaecs.examples.Systems;
import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import nz.ac.massey.javaecs.ECSSystem; import nz.ac.massey.javaecs.ECSSystem;
import nz.ac.massey.javaecs.Engine; import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity; import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.Collider; import nz.ac.massey.javaecs.examples.Components.Collider;
import nz.ac.massey.javaecs.examples.Components.Gravity;
import nz.ac.massey.javaecs.examples.Components.RidgidBody; import nz.ac.massey.javaecs.examples.Components.RidgidBody;
import nz.ac.massey.javaecs.examples.Components.Vec2D; import nz.ac.massey.javaecs.examples.Components.Vec2D;
@@ -29,97 +23,28 @@ public class CollisionSystem extends ECSSystem {
@Override @Override
public void init() { public void init() {
// TODO Auto-generated method stub
}
public void updateNew() {
Set<Entity> processed = new HashSet<>();
// Gets all items we have collided with
for (Entity entity : entities) {
RidgidBody ridgidBody = (RidgidBody) gameEngine.getComponentData(entity, RidgidBody.class);
// Only considering elements with mass just now
if (ridgidBody.mass > 0){
Vec2D pos = (Vec2D) gameEngine.getComponentData(entity, Vec2D.class);
Collider collider = (Collider) gameEngine.getComponentData(entity, Collider.class);
// The pos of each bounding line of the entity
// i.e.
//
// -------------
// |###########|
// |###########|
// |###########|
// |###########|
// -------------
//
double eleft = pos.x;
double ebottom = pos.y;
double eright = pos.x + collider.x;
double etop = pos.y + collider.y;
double evectorAngle = calcAngle(ridgidBody.xVel, ridgidBody.yVel);
if (ridgidBody.mass > 0){
// Dont't recalc for entities that aren't static
processed.add(entity);
}
for (Entity entity2 : entities) {
if (!processed.contains(entity2)) {
// skip entities that have already been processed
Vec2D oPos = (Vec2D) gameEngine.getComponentData(entity2, Vec2D.class);
RidgidBody oRidgidBody = (RidgidBody) gameEngine.getComponentData(entity2, RidgidBody.class);
Collider oCollider = (Collider) gameEngine.getComponentData(entity2, Collider.class);
double oleft = oPos.x;
double obottom = oPos.y;
double oright = oPos.x + oCollider.x;
double otop = oPos.y + oCollider.y;
// Check if a collision occured
// Few conditions required, that is,
// - oS2 must be greater than eS0, else they aren't close
// - eS2 must be greater than oS0, for the same reason
// - either
if (!(eleft > oright || oleft > eright || etop < obottom || otop < ebottom)){
// Collided
// Grab the angle the other is moving, to figure the quadrant to move to
double ovectorAngle = calcAngle(oRidgidBody.xVel, oRidgidBody.yVel);
// Grab the vector sums
double xVelSum = ridgidBody.xVel + oRidgidBody.xVel;
double yVelSum = ridgidBody.yVel + oRidgidBody.yVel;
// Collision occurred
}
}
}
}
}
} }
// This is a quick & naive physics implementation.
// I expect it is highly inefficient, though it is able to run it with 500
// subscribed entities
// with no major performance issues
// Replace this with a proper system as required - I.e. Box2D or etc.
@Override @Override
public void update() { public void update(double dt) {
// Parallelising this loop ()
Set<Entity> processed = new HashSet<>();
// Gets all items we have collided with
for (Entity entity : entities) { for (Entity entity : entities) {
Vec2D pos = (Vec2D) gameEngine.getComponentData(entity, Vec2D.class); Vec2D pos = (Vec2D) gameEngine.getComponentData(entity, Vec2D.class);
RidgidBody ridgidBody = (RidgidBody) gameEngine.getComponentData(entity, RidgidBody.class); RidgidBody ridgidBody = (RidgidBody) gameEngine.getComponentData(entity, RidgidBody.class);
Collider collider = (Collider) gameEngine.getComponentData(entity, Collider.class); Collider collider = (Collider) gameEngine.getComponentData(entity, Collider.class);
if (ridgidBody.mass > 0){ if (ridgidBody.mass >= 0) {
// Only ignore non-static colliders // Only ignore non-static colliders
processed.add(entity);
}
for (Entity entity2 : entities) { for (Entity entity2 : entities) {
if (!processed.contains(entity2)) {
// skip entities that have already been processed // skip entities that have already been processed
Vec2D otherPos = (Vec2D) gameEngine.getComponentData(entity2, Vec2D.class); Vec2D otherPos = (Vec2D) gameEngine.getComponentData(entity2, Vec2D.class);
RidgidBody otherRidgidBody = (RidgidBody) gameEngine.getComponentData(entity2, RidgidBody.class); RidgidBody otherRidgidBody = (RidgidBody) gameEngine.getComponentData(entity2, RidgidBody.class);
Collider otherCollider = (Collider) gameEngine.getComponentData(entity2, Collider.class); Collider otherCollider = (Collider) gameEngine.getComponentData(entity2, Collider.class);
if (otherRidgidBody.mass < 0) {
if (ridgidBody.mass >= 0 || otherRidgidBody.mass >= 0) {
// Evaluate a collision in the top-left // Evaluate a collision in the top-left
boolean tlCollide = pos.x > otherPos.x && pos.x < otherPos.x + otherCollider.x boolean tlCollide = pos.x > otherPos.x && pos.x < otherPos.x + otherCollider.x
&& pos.y > otherPos.y && pos.y < otherPos.y + otherCollider.y; && pos.y > otherPos.y && pos.y < otherPos.y + otherCollider.y;
@@ -135,84 +60,46 @@ public class CollisionSystem extends ECSSystem {
&& pos.x + collider.x < otherPos.x + otherCollider.x && pos.y > otherPos.y && pos.x + collider.x < otherPos.x + otherCollider.x && pos.y > otherPos.y
&& pos.y < otherPos.y + otherCollider.y; && pos.y < otherPos.y + otherCollider.y;
if (tlCollide || blCollide || brCollide || trCollide) { if (tlCollide || blCollide || brCollide || trCollide) {
// Determine collision nature
if (tlCollide) {
int k = 0;
}
if (trCollide) {
int k = 0;
}
if (blCollide) {
int k = 0;
}
if (brCollide) {
int k = 0;
}
// Get the vels // Get the vels
double totalXVel = Math.abs(ridgidBody.xVel) + Math.abs(otherRidgidBody.xVel); double totalXVel = Math.abs(ridgidBody.xVel) + Math.abs(otherRidgidBody.xVel);
double totalYVel = Math.abs(ridgidBody.yVel) + Math.abs(otherRidgidBody.yVel); double totalYVel = Math.abs(ridgidBody.yVel) + Math.abs(otherRidgidBody.yVel);
double totalMass = ridgidBody.mass + otherRidgidBody.mass; double xMult = 0;
int xMult = 0; double yMult = 0;
int yMult = 0; double multTot = 1;
boolean fullEdge = false; boolean fullEdge = false;
// left-hand collision // left-hand collision
if (tlCollide && blCollide) { if (tlCollide && blCollide) {
fullEdge = true; fullEdge = true;
xMult = 2; pos.x = otherPos.x + otherCollider.x;
} xMult = multTot;
// Bottom collision
if (blCollide && brCollide) {
fullEdge = true;
yMult = -2;
} }
// right-hand collision // right-hand collision
if (brCollide && trCollide) { else if (brCollide && trCollide) {
fullEdge = true; fullEdge = true;
xMult = -2; pos.x = otherPos.x - collider.x;
xMult = -multTot;
}
// Bottom collision
else if (blCollide && brCollide) {
fullEdge = true;
// pos.y = otherPos.y + otherCollider.y;
pos.y = otherPos.y - collider.y;
yMult = -multTot;
} }
// top collision // top collision
if (trCollide && tlCollide) { else if (trCollide && tlCollide) {
fullEdge = true; fullEdge = true;
yMult = 2; // pos.y = otherPos.y - collider.y;
pos.y = otherPos.y + otherCollider.y;
yMult = multTot;
} }
if (fullEdge) { if (fullEdge) {
// Finally, actuate the calculation double newXVel = totalXVel * (xMult
if (ridgidBody.mass < 0) { * (Math.abs(xMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness)));
// entity 1 is immovable double newYVel = totalYVel * (yMult
otherRidgidBody.xVel += totalXVel * (-1 * xMult); * (Math.abs(yMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness)));
otherRidgidBody.yVel += totalYVel * (-1 * yMult); ridgidBody.xVel += newXVel;
ridgidBody.yVel += newYVel;
Object grav = gameEngine.getComponentData(entity, Gravity.class);
if (grav != null) {
Gravity gravity = (Gravity) grav;
otherRidgidBody.xAcc += gravity.x * -1.;
otherRidgidBody.yAcc += gravity.y * -1.;
}
} else if (otherRidgidBody.mass < 0) {
// entity 2 is immovable; full acceleration applied to the current entity
ridgidBody.xVel += totalXVel * xMult;
ridgidBody.yVel += totalYVel * yMult;
Object grav = gameEngine.getComponentData(entity, Gravity.class);
if (grav != null) {
// negate gravity while collided
Gravity gravity = (Gravity) grav;
ridgidBody.xAcc += gravity.x * -1.;
ridgidBody.yAcc += gravity.y * -1.;
}
} else {
// collision is elastic; add the forces x & y and the masses, and share between
// objects
/*ridgidBody.xVel += (ridgidBody.mass / totalMass) * totalXVel * xMult;
otherRidgidBody.xVel += (otherRidgidBody.mass / totalMass) * totalXVel
* (-1 * xMult);
ridgidBody.yVel += (ridgidBody.mass / totalMass) * totalYVel * yMult;
otherRidgidBody.yVel += (otherRidgidBody.mass / totalMass) * totalYVel
* (-1 * yMult);*/
}
}
else{
} }
} }
} }
@@ -220,33 +107,4 @@ public class CollisionSystem extends ECSSystem {
} }
} }
} }
/**
* Zero degrees is straight up,
* 90 east
* -90 (270) = west
* 180 south
* @param xVel
* @param yVel
* @return
*/
public static double calcAngle(double xVel, double yVel){
if (xVel == 0){ // Div zero; = +- 90
return yVel < 0 ? 270 : 90;
}
else if (xVel < 0 && yVel < 0){ // bottom-left quad (180 - 270)
return 180. + Math.tanh(yVel/xVel);
}
else if (xVel < 0){ // Top left quad (270 - 360)
return 360 - Math.tanh(yVel/xVel);
}
else if (yVel < 0){
return 180 - Math.tanh(yVel/xVel);
}
else{
return Math.tanh(yVel/xVel);
}
}
} }
@@ -11,7 +11,6 @@ import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.BoxRender; import nz.ac.massey.javaecs.examples.Components.BoxRender;
import nz.ac.massey.javaecs.examples.Components.Collider; import nz.ac.massey.javaecs.examples.Components.Collider;
import nz.ac.massey.javaecs.examples.Components.Gravity; import nz.ac.massey.javaecs.examples.Components.Gravity;
import nz.ac.massey.javaecs.examples.Components.LogUpdate;
import nz.ac.massey.javaecs.examples.Components.Render; import nz.ac.massey.javaecs.examples.Components.Render;
import nz.ac.massey.javaecs.examples.Components.RidgidBody; import nz.ac.massey.javaecs.examples.Components.RidgidBody;
import nz.ac.massey.javaecs.examples.Components.TimedExistence; import nz.ac.massey.javaecs.examples.Components.TimedExistence;
@@ -32,25 +31,19 @@ public class ExistenceSystem extends ECSSystem{
@Override @Override
public void init() { public void init() {
rand = new Random(); rand = new Random();
} }
@Override @Override
public void update() {
// TODO Auto-generated method stub
}
public void update(double dt){ public void update(double dt){
Queue<Entity> deletions = new LinkedList<>(); Queue<Entity> deletions = new LinkedList<>();
for (Entity entity : entities) { for (Entity entity : entities) {
BoxRender shape = (BoxRender)gameEngine.getComponentData(entity, BoxRender.class); BoxRender shape = (BoxRender)gameEngine.getComponentData(entity, BoxRender.class);
TimedExistence timedExistence = (TimedExistence)gameEngine.getComponentData(entity, TimedExistence.class); TimedExistence timedExistence = (TimedExistence)gameEngine.getComponentData(entity, TimedExistence.class);
// Get the rate the fade should occur at our current frame rate. // Get the rate the fade should occur at our current frame rate.
double fadeRate = timedExistence.lifeTime / dt; // I.e. might say 200 frames left double fadeRate = timedExistence.lifeTime / dt;
// Fade the opacity // Fade the opacity
shape.a -= shape.a / fadeRate; shape.a -= shape.a / fadeRate;
// Clauses to keep things safe // Keep the alpha between 0-1
if (shape.a < 0) shape.a = 0.; if (shape.a < 0) shape.a = 0.;
else if (shape.a > 1) shape.a = 1.; else if (shape.a > 1) shape.a = 1.;
// Reduce the time the shape may continue living for // Reduce the time the shape may continue living for
@@ -60,15 +53,18 @@ public class ExistenceSystem extends ECSSystem{
deletions.add(entity); deletions.add(entity);
} }
} }
// As we were using the iterator to traverse entities, we could not delete them as we read them.
while (deletions.size() > 0){ while (deletions.size() > 0){
gameEngine.destroyEntity(deletions.remove()); gameEngine.destroyEntity(deletions.remove());
} }
// Finally, randomly add an entity as required // Finally, randomly add an entity as required
if (rand.nextInt(16384) < 8){ int newRate = gameEngine.getNumEntities();
newRate = newRate < 13 ? 7 - newRate / 2 : 1;
if (rand.nextInt(2048) <= 1*newRate){
Entity newEnt = gameEngine.createEntity(); Entity newEnt = gameEngine.createEntity();
double size = 1 + rand.nextDouble() * 10;
gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(10 + rand.nextDouble()* 80, 10+ rand.nextDouble()* 80,0)); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(10 + rand.nextDouble()* 80, 10+ rand.nextDouble()* 80,0));
gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(-2.5 + rand.nextDouble()* 5., 0., 0., 0., 0.f, 0., rand.nextDouble() * 20.)); gameEngine.addComponent(newEnt, RidgidBody.class, new RidgidBody(-2.5 + rand.nextDouble()* 5., 0., 0., 0., 0.f, 0.75, 2. * size));
double size = 3 + rand.nextDouble() * 7;
gameEngine.addComponent(newEnt, Collider.class, new Collider(size, size)); gameEngine.addComponent(newEnt, Collider.class, new Collider(size, size));
gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s gameEngine.addComponent(newEnt, Gravity.class, new Gravity(0, -9.80665, 45., 45.)); // Terminal velocity is 45 m/s
gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, Render.class, new Render(BoxRender.class));
@@ -24,7 +24,7 @@ public class LogUpdateSystem extends ECSSystem{
} }
@Override @Override
public void update() { public void update(double dt) {
for (Entity entity : entities) { for (Entity entity : entities) {
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class); Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
System.out.println(String.format("X: %.6g, Y: %.6g", pos.x, pos.y)); System.out.println(String.format("X: %.6g, Y: %.6g", pos.x, pos.y));
@@ -30,17 +30,11 @@ public class PhysicsSystem extends ECSSystem {
} }
@Override @Override
public void update() {
// TODO Auto-generated method stub
}
public void update(double dt){ public void update(double dt){
for (Entity entity : entities) { for (Entity entity : entities) {
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class); Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
RidgidBody ridgidBody = (RidgidBody)gameEngine.getComponentData(entity, RidgidBody.class); RidgidBody ridgidBody = (RidgidBody)gameEngine.getComponentData(entity, RidgidBody.class);
Gravity gravity = (Gravity)gameEngine.getComponentData(entity, Gravity.class); Gravity gravity = (Gravity)gameEngine.getComponentData(entity, Gravity.class);
Collider collider = (Collider)gameEngine.getComponentData(entity, Collider.class);
// Firstly, add the result of the accelerative forces to the ridgidbody // Firstly, add the result of the accelerative forces to the ridgidbody
ridgidBody.xVel += ridgidBody.xAcc * dt; ridgidBody.xVel += ridgidBody.xAcc * dt;
ridgidBody.yVel += ridgidBody.yAcc * dt; ridgidBody.yVel += ridgidBody.yAcc * dt;
@@ -49,7 +43,6 @@ public class PhysicsSystem extends ECSSystem {
ridgidBody.xAcc = 0.; ridgidBody.xAcc = 0.;
ridgidBody.yAcc = 0.; ridgidBody.yAcc = 0.;
if (!collider.collided){
// Special case of gravity // Special case of gravity
if (gravity.terminalX < 0 || Math.abs(ridgidBody.xVel) < gravity.terminalX){ if (gravity.terminalX < 0 || Math.abs(ridgidBody.xVel) < gravity.terminalX){
ridgidBody.xAcc += gravity.x; ridgidBody.xAcc += gravity.x;
@@ -57,7 +50,6 @@ public class PhysicsSystem extends ECSSystem {
if (gravity.terminalY < 0 || Math.abs(ridgidBody.yVel) < gravity.terminalY){ if (gravity.terminalY < 0 || Math.abs(ridgidBody.yVel) < gravity.terminalY){
ridgidBody.yAcc += gravity.y; ridgidBody.yAcc += gravity.y;
} }
}
// Finally, move the vec2d by the new velocity // Finally, move the vec2d by the new velocity
@@ -4,11 +4,9 @@ import java.util.BitSet;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Group; import javafx.scene.Group;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.scene.text.Text; import javafx.scene.text.Text;
@@ -16,6 +14,7 @@ import javafx.stage.Stage;
import nz.ac.massey.javaecs.ECSSystem; import nz.ac.massey.javaecs.ECSSystem;
import nz.ac.massey.javaecs.Engine; import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity; import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.App;
import nz.ac.massey.javaecs.examples.Components.BoxRender; import nz.ac.massey.javaecs.examples.Components.BoxRender;
import nz.ac.massey.javaecs.examples.Components.Render; import nz.ac.massey.javaecs.examples.Components.Render;
import nz.ac.massey.javaecs.examples.Components.TextRender; import nz.ac.massey.javaecs.examples.Components.TextRender;
@@ -26,8 +25,8 @@ public class RenderSystem extends ECSSystem{
Engine gameEngine; Engine gameEngine;
Thread renderThread; Thread renderThread;
double renderScale; double renderScale;
int screenX = 1024; int screenX = 512;
int screenY = 1024; int screenY = 512;
public RenderSystem(Engine gameEngine, double renderScale){ public RenderSystem(Engine gameEngine, double renderScale){
this.gameEngine = gameEngine; this.gameEngine = gameEngine;
@@ -40,31 +39,24 @@ public class RenderSystem extends ECSSystem{
@Override @Override
public void init() { public void init() {
// Spawn a new thread // Spawn a new asynchronous thread (won't rejoin the main program flow)
renderThread = new Thread(new RenderStarter()); renderThread = new Thread(new RenderStarter());
renderThread.start(); // this thread is asynchronous, it isn't expected to rejoin renderThread.start();
} }
@Override @Override
public void update() { public void update(double dt) {
/* while (JFXView.root == null){ // This approach needs reconsidering, as it creates an excessive amount of objects that
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} */
Group renderScene = new Group(); Group renderScene = new Group();
for (Entity entity : entities) { for (Entity entity : entities) {
//Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
if (((Render)gameEngine.getComponentData(entity, Render.class)).renderType == BoxRender.class){ if (((Render)gameEngine.getComponentData(entity, Render.class)).renderType == BoxRender.class){
BoxRender box = (BoxRender)gameEngine.getComponentData(entity, BoxRender.class); BoxRender box = (BoxRender)gameEngine.getComponentData(entity, BoxRender.class);
Vec2D vec2d = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class); Vec2D vec2d = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
Rectangle rect = new Rectangle(box.xSize* renderScale, box.ySize * renderScale); Rectangle rect = new Rectangle(box.xSize* renderScale, box.ySize * renderScale);
rect.setFill(new Color(box.r, box.g, box.b, box.a)); rect.setFill(new Color(box.r, box.g, box.b, box.a));
rect.setX(12 + (vec2d.x * renderScale)); rect.setX(6 + (vec2d.x * renderScale));
rect.setY(1012 - (vec2d.y * renderScale) - rect.getHeight()); rect.setY(506 - (vec2d.y * renderScale) - rect.getHeight());
renderScene.getChildren().add(rect); renderScene.getChildren().add(rect);
} }
else if (((Render)gameEngine.getComponentData(entity, Render.class)).renderType == TextRender.class){ else if (((Render)gameEngine.getComponentData(entity, Render.class)).renderType == TextRender.class){
@@ -77,6 +69,9 @@ public class RenderSystem extends ECSSystem{
renderScene.getChildren().add(t); renderScene.getChildren().add(t);
} }
} }
// Must be run by the UI thread, not the main thread.
// This dispatches the operation run() to the UI.
Platform.runLater(new Runnable(){ Platform.runLater(new Runnable(){
@Override @Override
public void run() { public void run() {
@@ -87,7 +82,6 @@ public class RenderSystem extends ECSSystem{
} }
// Add a renderer // Add a renderer
public class RenderStarter implements Runnable{ public class RenderStarter implements Runnable{
@Override @Override
@@ -107,10 +101,11 @@ public class RenderSystem extends ECSSystem{
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
// based on the colorful circles sample at https://docs.oracle.com/javase/8/javafx/get-started-tutorial/animation.htm // based on the colorful circles sample at https://docs.oracle.com/javase/8/javafx/get-started-tutorial/animation.htm
root = new Group(); root = new Group();
Scene scene = new Scene(root, 1024, 1024, Color.BLACK); Scene scene = new Scene(root, 512, 512, Color.BLACK);
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.show(); primaryStage.show();
// Signal the renderer is ready
App.rendererReadySemaphore.release();
} }
public static void run(){ public static void run(){
@@ -20,12 +20,10 @@ public class ReportSystem extends ECSSystem{
} }
@Override @Override
public void init() { public void init() {
// TODO Auto-generated method stub
} }
@Override @Override
public void update() { public void update(double dt) {
for (Entity entity : entities) { for (Entity entity : entities) {
TextRender text = (TextRender)gameEngine.getComponentData(entity, TextRender.class); TextRender text = (TextRender)gameEngine.getComponentData(entity, TextRender.class);
text.content = "Num Entities: " + gameEngine.getNumEntities(); text.content = "Num Entities: " + gameEngine.getNumEntities();
+17
View File
@@ -0,0 +1,17 @@
# Editor configuration, see http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 80
[*.sh]
end_of_line = lf
[*.java]
indent_size = 4
max_line_length = 120
+2
View File
@@ -0,0 +1,2 @@
# When shell scripts end in CRLF, bash gives a cryptic error message
*.sh text eol=lf
+28
View File
@@ -0,0 +1,28 @@
#
# Standard Maven .gitignore
#
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
#
# IntelliJ
#
*.iml
.idea/*
!.idea/runConfigurations/
#
# Visual Studio Code
#
.settings/
.classpath
.factorypath
.project
.vscode/
+4
View File
@@ -0,0 +1,4 @@
language: java
jdk: openjdk8
after_success:
- mvn coveralls:report
+200
View File
@@ -0,0 +1,200 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nz.ac.massey.javaecs.examples</groupId>
<artifactId>demo2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.6.0</junit.version>
<maven-enforcer-plugin.version>3.0.0-M3</maven-enforcer-plugin.version>
<maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version>
<checkstyle.version>8.29</checkstyle.version>
<checkstyle-rules.version>4.0.1</checkstyle-rules.version>
<maven-surefire-plugin.version>3.0.0-M4</maven-surefire-plugin.version>
<jacoco-maven-plugin.version>0.8.4</jacoco-maven-plugin.version>
<maven-javadoc-plugin.version>3.0.0</maven-javadoc-plugin.version>
<coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
<!-- JaCoCo thresholds. Increase gradually as you add tests. -->
<jacoco.unit-tests.limit.instruction-ratio>0%</jacoco.unit-tests.limit.instruction-ratio>
<jacoco.unit-tests.limit.branch-ratio>0%</jacoco.unit-tests.limit.branch-ratio>
<jacoco.unit-tests.limit.class-complexity>20</jacoco.unit-tests.limit.class-complexity>
<jacoco.unit-tests.limit.method-complexity>5</jacoco.unit-tests.limit.method-complexity>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nz.ac.massey.javaecs</groupId>
<artifactId>javaecs</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>3.6.3</version>
</requireMavenVersion>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
<dependency>
<groupId>com.github.ngeor</groupId>
<artifactId>checkstyle-rules</artifactId>
<version>${checkstyle-rules.version}</version>
</dependency>
</dependencies>
<configuration>
<configLocation>com/github/ngeor/checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<skip>${skipTests}</skip>
</configuration>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check-unit-test</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>${jacoco.unit-tests.limit.instruction-ratio}</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>${jacoco.unit-tests.limit.branch-ratio}</minimum>
</limit>
</limits>
</rule>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>TOTALCOUNT</value>
<maximum>${jacoco.unit-tests.limit.class-complexity}</maximum>
</limit>
</limits>
</rule>
<rule>
<element>METHOD</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>TOTALCOUNT</value>
<maximum>${jacoco.unit-tests.limit.method-complexity}</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
</plugin>
</plugins>
</reporting>
<profiles>
<!-- Publish coverage report to Coveralls, only when running in Travis. -->
<profile>
<id>travis</id>
<activation>
<property>
<name>env.TRAVIS</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>${coveralls-maven-plugin.version}</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -0,0 +1,120 @@
package nz.ac.massey.javaecs.examples;
import java.io.FileInputStream;
import java.util.concurrent.Semaphore;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.Control;
import nz.ac.massey.javaecs.examples.Components.Input;
import nz.ac.massey.javaecs.examples.Components.Render;
import nz.ac.massey.javaecs.examples.Components.Sprite;
import nz.ac.massey.javaecs.examples.Components.Vec2D;
import nz.ac.massey.javaecs.examples.Systems.ControlSystem;
import nz.ac.massey.javaecs.examples.Systems.PlayerSystem;
import nz.ac.massey.javaecs.examples.Systems.RenderSystem;
/**
* Hello world!
*/
public class App {
static App thisApp;
public App() {
thisApp = this;
}
static Engine gameEngine;
public static Stage worldStage;
public static Group worldGroup;
static Integer syncManager = 0;
static Semaphore rendererFinished = new Semaphore(0);
/**
* Says hello to the world.
* @param args The arguments of the program.
*/
public static void main(String[] args) throws Exception{
/************************************
** Initialise engine **
*************************************/
gameEngine = new Engine(16384);
/************************************
** Register game components **
*************************************/
gameEngine.registerComponent(Control.class);
gameEngine.registerComponent(Render.class);
gameEngine.registerComponent(Sprite.class);
gameEngine.registerComponent(Vec2D.class);
gameEngine.registerComponent(Input.class);
/************************************
** Register and initialise systems **
*************************************/
ControlSystem controlSystem = new ControlSystem(gameEngine);
gameEngine.registerSystem(ControlSystem.class, controlSystem);
RenderSystem renderSystem = new RenderSystem(gameEngine, 5.);
gameEngine.registerSystem(RenderSystem.class, renderSystem);
PlayerSystem playerSystem = new PlayerSystem(gameEngine);
gameEngine.registerSystem(PlayerSystem.class, playerSystem);
/************************************
** Create known entities **
*************************************/
// Input controller - gets player input
Entity inputEntity = gameEngine.createEntity();
gameEngine.addComponent(inputEntity, Input.class, new Input());
Entity playerEntity = gameEngine.createEntity();
gameEngine.addComponent(playerEntity, Vec2D.class, new Vec2D(25,25));
gameEngine.addComponent(playerEntity, Render.class, new Render(Sprite.class));
gameEngine.addComponent(playerEntity, Control.class, new Render(Control.class));
// Load the sprite
System.err.println(System.getProperty("user.dir"));
Image image = new Image(new FileInputStream("sprite.png"));
gameEngine.addComponent(playerEntity, Sprite.class, new Sprite(image, 50, 50));
// Push rendering into its own thread (to meet the design principle of a JavaFX program)
Thread renderThread = new Thread(new RenderStarter());
renderThread.start();
// Use a semaphore to block until the renderer is set-up
rendererFinished.acquire();
/************************************
** Run init() on the systems **
*************************************/
controlSystem.init(worldStage);
playerSystem.init(inputEntity);
renderSystem.init(worldGroup);
double dt = 0.001;
double frameRate = 1.0 / 144; // 1 second divided by target number of frames
boolean exit = false;
long startTime = System.nanoTime();
while (!exit){
// Run system updates
controlSystem.update(dt);
playerSystem.update(dt);
renderSystem.update(dt);
dt = (System.nanoTime() - startTime) / 1e9; // convert nanoseconds to seconds
startTime = System.nanoTime();
if (dt < frameRate){
Thread.sleep((int)((frameRate-dt)*1000));
dt = (System.nanoTime() - startTime) / 1e9;
startTime = System.nanoTime();
}
}
renderThread.join(); // Wait for any render task to exit
}
}
@@ -0,0 +1,5 @@
package nz.ac.massey.javaecs.examples.Components;
public class Control {
public int input = -1;
}
@@ -0,0 +1,9 @@
package nz.ac.massey.javaecs.examples.Components;
import java.util.concurrent.atomic.AtomicInteger;
public class Input {
// As this is a user-input- (i.e. timeframe in 10's or 100's of milliseconds) -derived value, the performance impact of
// AtomicInteger is irrelevant compared to its synchronisation ability.
public AtomicInteger input = new AtomicInteger(-1);
}
@@ -0,0 +1,11 @@
package nz.ac.massey.javaecs.examples.Components;
import java.lang.reflect.Type;
public class Render {
public Type renderType;
public Render(Type t){
renderType = t;
}
}
@@ -0,0 +1,15 @@
package nz.ac.massey.javaecs.examples.Components;
import javafx.scene.image.Image;
public class Sprite {
public Image sprite;
public Integer height;
public Integer width;
public Sprite(Image sprite, int height, int width){
this.sprite = sprite;
this.width = width;
this.height = height;
}
}
@@ -0,0 +1,11 @@
package nz.ac.massey.javaecs.examples.Components;
public class Vec2D {
public double x;
public double y;
public Vec2D(double x, double y){
this.x = x;
this.y = y;
}
}
@@ -0,0 +1,28 @@
package nz.ac.massey.javaecs.examples;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JFXView extends Application {
public static Group root;
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("JavaECS Demo 2");
App.worldGroup = new Group();
App.worldStage = primaryStage;
Scene scene = new Scene(App.worldGroup, 512, 512, Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
// Notifiy that the renderer is ready
App.rendererFinished.release();
}
public static void run(){
launch(new String[0]);
}
}
@@ -0,0 +1,16 @@
package nz.ac.massey.javaecs.examples;
import javafx.scene.Group;
// Add a renderer
public class RenderStarter implements Runnable{
@Override
public void run() {
JFXView.run();
}
void updateScene(Group newGroup){
JFXView.root.getChildren().clear();
JFXView.root.getChildren().add(newGroup);
}
}
@@ -0,0 +1,81 @@
package nz.ac.massey.javaecs.examples.Systems;
import java.util.BitSet;
import javafx.application.Platform;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import nz.ac.massey.javaecs.ECSSystem;
import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.Input;
/**
* The control system creates a handler for input in the world stage.
* It then passes input events as they occur to the Input component
* The ControlSystem reads this input every frame update and enacts
* on the player controller
*/
public class ControlSystem extends ECSSystem {
Engine gameEngine;
Entity inputEntity;
public ControlSystem(Engine gamEngine){
this.gameEngine = gamEngine;
registrationSet = new BitSet();
registrationSet.set(gameEngine.getComponentIndex(Input.class));
}
public void init(Stage world) {
// use a single entity to control inputs, it is present in the world stage
// and is responsible for collecting & passing data
inputEntity = entities.iterator().next();
// Add an event handler to the stage. This
Platform.runLater(new Runnable(){
@Override
public void run() {
world.addEventHandler(KeyEvent.KEY_PRESSED, (key) -> {
if(key.getCode()==KeyCode.W) {
updateKeyPress(0);
System.err.println("W pressed");
}
else if(key.getCode()==KeyCode.S) {
updateKeyPress(1);
System.err.println("S pressed");
}
else if(key.getCode()==KeyCode.A) {
updateKeyPress(2);
System.err.println("A pressed");
}
else if(key.getCode()==KeyCode.D) {
updateKeyPress(3);
System.err.println("D pressed");
}
});
}
});
}
@Override
public void update(double dt) {
//Input inputUpdate = (Input)gameEngine.getComponentData(inputEntity, Input.class);
// TODO Auto-generated method stub
}
// Updates the input's keypressed state
protected void updateKeyPress(int dir){
Input inputUpdate = (Input)gameEngine.getComponentData(inputEntity, Input.class);
inputUpdate.input.set(dir);
}
@Override
public void init() throws Exception {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,59 @@
package nz.ac.massey.javaecs.examples.Systems;
import java.util.BitSet;
import nz.ac.massey.javaecs.ECSSystem;
import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.Control;
import nz.ac.massey.javaecs.examples.Components.Input;
import nz.ac.massey.javaecs.examples.Components.Sprite;
import nz.ac.massey.javaecs.examples.Components.Vec2D;
public class PlayerSystem extends ECSSystem{
Engine gameEngine;
Entity inputEntity; // Entity which recieves event data
public PlayerSystem(Engine gameEngine){
this.gameEngine = gameEngine;
registrationSet = new BitSet();
registrationSet.set(gameEngine.getComponentIndex(Control.class));
}
public void init(Entity inputEntity){
this.inputEntity = inputEntity;
}
@Override
public void init() throws Exception {
// TODO Auto-generated method stub
}
@Override
public void update(double dt) {
int state = ((Input)gameEngine.getComponentData(inputEntity, Input.class)).input.get();
// The value of the input may change before control leaves here; sychronisation isn't
// hugely important through this section. So we can just ignore the change
for (Entity entity : entities) {
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
Sprite sprite = (Sprite)gameEngine.getComponentData(entity, Sprite.class);
if (state == 0){ // W
pos.y -= sprite.height/2;
}
else if (state == 1){ // S
pos.y += sprite.height/2;
}
else if (state == 2){ // A
pos.x -= sprite.width/2;
}
else if (state == 3){ // D
pos.x += sprite.width/2;
}
}
// Finally, unset the state
((Input)gameEngine.getComponentData(inputEntity, Input.class)).input.set(-1); // Unset the state, it has been handled
}
}
@@ -0,0 +1,69 @@
package nz.ac.massey.javaecs.examples.Systems;
import java.util.BitSet;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.image.ImageView;
import nz.ac.massey.javaecs.ECSSystem;
import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.Render;
import nz.ac.massey.javaecs.examples.Components.Sprite;
import nz.ac.massey.javaecs.examples.Components.Vec2D;
/*
* This render system is different from demo 1, as its main functionality is provided directly by the program, not as an entity.
*/
public class RenderSystem extends ECSSystem {
Engine gameEngine;
Group renderGroup;
Thread renderThread;
double renderScale;
int screenX = 512;
int screenY = 512;
public RenderSystem(Engine gameEngine, double renderScale){
this.gameEngine = gameEngine;
// Set registrations
registrationSet = new BitSet();
registrationSet.set(gameEngine.getComponentIndex(Render.class));
this.renderScale = renderScale;
}
public void init(Group renderGroup) throws Exception {
this.renderGroup = renderGroup;
}
@Override
public void init() throws Exception {
// TODO Auto-generated method stub
}
@Override
public void update(double dt) {
Group frame = new Group();
for (Entity entity : entities) {
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
if (((Render)gameEngine.getComponentData(entity, Render.class)).renderType == Sprite.class){
// Render the sprite
Sprite sprite = (Sprite)gameEngine.getComponentData(entity, Sprite.class);
ImageView spriteRender = new ImageView(sprite.sprite);
spriteRender.setX(pos.x);
spriteRender.setY(pos.y);
spriteRender.setFitWidth(sprite.width);
spriteRender.setFitHeight(sprite.height);
frame.getChildren().add(spriteRender);
}
}
// Must be run by the UI thread, not the main thread.
// This dispatches the operation run() to the UI.
Platform.runLater(new Runnable(){
@Override
public void run() {
renderGroup.getChildren().clear();
renderGroup.getChildren().add(frame);
}
});
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -0,0 +1,18 @@
package nz.ac.massey.javaecs.examples;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Unit test for simple App.
*/
class AppTest {
/**
* Rigorous Test.
*/
@Test
void testApp() {
assertEquals(1, 1);
}
}
+17
View File
@@ -0,0 +1,17 @@
# Editor configuration, see http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 80
[*.sh]
end_of_line = lf
[*.java]
indent_size = 4
max_line_length = 120
+2
View File
@@ -0,0 +1,2 @@
# When shell scripts end in CRLF, bash gives a cryptic error message
*.sh text eol=lf
+31
View File
@@ -0,0 +1,31 @@
#
# Standard Maven .gitignore
#
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
#
# IntelliJ
#
*.iml
.idea/*
!.idea/runConfigurations/
#
# Visual Studio Code
#
.settings/
.classpath
.factorypath
.project
.vscode/
# Ignore logs, these came from an error in the VM
*.log
+4
View File
@@ -0,0 +1,4 @@
language: java
jdk: openjdk8
after_success:
- mvn coveralls:report
+200
View File
@@ -0,0 +1,200 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nz.ac.massey.javaecs.examples</groupId>
<artifactId>perftest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.6.0</junit.version>
<maven-enforcer-plugin.version>3.0.0-M3</maven-enforcer-plugin.version>
<maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version>
<checkstyle.version>8.29</checkstyle.version>
<checkstyle-rules.version>4.0.1</checkstyle-rules.version>
<maven-surefire-plugin.version>3.0.0-M4</maven-surefire-plugin.version>
<jacoco-maven-plugin.version>0.8.4</jacoco-maven-plugin.version>
<maven-javadoc-plugin.version>3.0.0</maven-javadoc-plugin.version>
<coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
<!-- JaCoCo thresholds. Increase gradually as you add tests. -->
<jacoco.unit-tests.limit.instruction-ratio>0%</jacoco.unit-tests.limit.instruction-ratio>
<jacoco.unit-tests.limit.branch-ratio>0%</jacoco.unit-tests.limit.branch-ratio>
<jacoco.unit-tests.limit.class-complexity>20</jacoco.unit-tests.limit.class-complexity>
<jacoco.unit-tests.limit.method-complexity>5</jacoco.unit-tests.limit.method-complexity>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nz.ac.massey.javaecs</groupId>
<artifactId>javaecs</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>3.6.3</version>
</requireMavenVersion>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
<dependency>
<groupId>com.github.ngeor</groupId>
<artifactId>checkstyle-rules</artifactId>
<version>${checkstyle-rules.version}</version>
</dependency>
</dependencies>
<configuration>
<configLocation>com/github/ngeor/checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<skip>${skipTests}</skip>
</configuration>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check-unit-test</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>${jacoco.unit-tests.limit.instruction-ratio}</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>${jacoco.unit-tests.limit.branch-ratio}</minimum>
</limit>
</limits>
</rule>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>TOTALCOUNT</value>
<maximum>${jacoco.unit-tests.limit.class-complexity}</maximum>
</limit>
</limits>
</rule>
<rule>
<element>METHOD</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>TOTALCOUNT</value>
<maximum>${jacoco.unit-tests.limit.method-complexity}</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
</plugin>
</plugins>
</reporting>
<profiles>
<!-- Publish coverage report to Coveralls, only when running in Travis. -->
<profile>
<id>travis</id>
<activation>
<property>
<name>env.TRAVIS</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>${coveralls-maven-plugin.version}</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
@@ -0,0 +1,65 @@
package nz.ac.massey.javaecs.examples;
import nz.ac.massey.javaecs.*;
import nz.ac.massey.javaecs.examples.Components.*;
import nz.ac.massey.javaecs.examples.Systems.*;
/**
* Benchmark,
* A port of https://github.com/abeimler/ecs_benchmark
*/
public final class App {
private App() {
}
/**
* Says hello to the world.
* @param args The arguments of the program.
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
int nEntities = 1_000_000;
double startTime = System.nanoTime();
Engine engine = new Engine(nEntities);
double endTime = System.nanoTime();
System.out.println("Constructor Time taken: " + (endTime - startTime) / 1e6+ " ms " + "("+ (endTime - startTime) / 1e9 + " seconds )");
engine.registerComponent(ComflabulationComponent.class);
engine.registerComponent(DirectionComponent.class);
engine.registerComponent(PositionComponent.class);
ComflabSystem comflabSystem = new ComflabSystem(engine);
engine.registerSystem(ComflabSystem.class, comflabSystem);
MovementSystem movementSystem = new MovementSystem(engine);
engine.registerSystem(MovementSystem.class, movementSystem);
startTime = System.nanoTime();
for (int i = 0; i < nEntities; i++) {
Entity entity = engine.createEntity();
engine.addComponent(entity, PositionComponent.class, new PositionComponent());
engine.addComponent(entity, DirectionComponent.class, new DirectionComponent());
if (i % 2 != 0){
engine.addComponent(entity, ComflabulationComponent.class, new ComflabulationComponent());
}
}
endTime = System.nanoTime();
System.out.println("Entity creation Time taken: " + (endTime - startTime) / 1e6+ " ms " + "("+ (endTime - startTime) / 1e9 + " seconds )");
// Delay as object creation is multithreaded in at least OpenJDK 1.8 - lets use see the single core performance
Thread.sleep(5000);
double dt = 1.0f/60.0f;
startTime = System.nanoTime();
movementSystem.update(dt);
comflabSystem.update(dt);
endTime = System.nanoTime();
System.out.println("Single Loop Time taken: " + (endTime - startTime) / 1e6+ " ms " + "("+ (endTime - startTime) / 1e9 + " seconds )");
System.out.println("Hello World!");
}
}
@@ -0,0 +1,22 @@
package nz.ac.massey.javaecs.examples.Components;
public class ComflabulationComponent {
public double thingy;
public int dingy;
public boolean mingy;
public String stringy;
public ComflabulationComponent(double thingy, int dingy, boolean mingy, String stringy){
this.thingy = thingy;
this.dingy = dingy;
this.mingy = mingy;
this.stringy = stringy;
}
public ComflabulationComponent(){
thingy = 0.0f;
dingy = 0;
mingy = false;
stringy = "";
}
}
@@ -0,0 +1,16 @@
package nz.ac.massey.javaecs.examples.Components;
public class DirectionComponent {
public double x;
public double y;
public DirectionComponent(double x, double y){
this.x = x;
this.y = y;
}
public DirectionComponent(){
x = 0.0f;
y = 0.0f;
}
}
@@ -0,0 +1,16 @@
package nz.ac.massey.javaecs.examples.Components;
public class PositionComponent {
public double x;
public double y;
public PositionComponent(double x, double y){
this.x = x;
this.y = y;
}
public PositionComponent(){
x = 0.0f;
y = 0.0f;
}
}
@@ -0,0 +1,34 @@
package nz.ac.massey.javaecs.examples.Systems;
import java.util.BitSet;
import nz.ac.massey.javaecs.ECSSystem;
import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.ComflabulationComponent;
public class ComflabSystem extends ECSSystem {
Engine ecs;
public ComflabSystem(Engine ecs){
this.ecs = ecs;
registrationSet = new BitSet();
registrationSet.set(ecs.getComponentIndex(ComflabulationComponent.class));
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public void update(double dt) {
for (Entity entity : entities) {
ComflabulationComponent comflab = (ComflabulationComponent)ecs.getComponentData(entity, ComflabulationComponent.class);
comflab.thingy *= 1.000001;
comflab.mingy = !comflab.mingy;
comflab.dingy++;
}
}
}
@@ -0,0 +1,39 @@
package nz.ac.massey.javaecs.examples.Systems;
import java.util.BitSet;
import nz.ac.massey.javaecs.ECSSystem;
import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.DirectionComponent;
import nz.ac.massey.javaecs.examples.Components.PositionComponent;
public class MovementSystem extends ECSSystem {
Engine ecs;
public MovementSystem(Engine ecs){
this.ecs = ecs;
registrationSet = new BitSet();
registrationSet.set(ecs.getComponentIndex(PositionComponent.class));
registrationSet.set(ecs.getComponentIndex(DirectionComponent.class));
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public void update(double dt) {
for (Entity entity : entities) {
PositionComponent position = (PositionComponent)ecs.getComponentData(entity, PositionComponent.class);
DirectionComponent direction = (DirectionComponent)ecs.getComponentData(entity, DirectionComponent.class);
position.x += direction.x * dt;
position.y += direction.y * dt;
}
}
}
@@ -0,0 +1,18 @@
package nz.ac.massey.javaecs.examples;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Unit test for simple App.
*/
class AppTest {
/**
* Rigorous Test.
*/
@Test
void testApp() {
assertEquals(1, 1);
}
}