From 808281786090e0385c9b64f43e0c98487e2cdf8e Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Fri, 11 Jun 2021 20:55:42 +1200 Subject: [PATCH] Added player control example base --- .../nz/ac/massey/javaecs/examples/App.java | 14 +- .../examples/Systems/CollisionSystem.java | 41 ++-- demo2/.editorconfig | 17 ++ demo2/.gitattributes | 2 + demo2/.gitignore | 28 +++ demo2/.travis.yml | 4 + demo2/pom.xml | 195 ++++++++++++++++++ .../nz/ac/massey/javaecs/examples/App.java | 17 ++ .../ac/massey/javaecs/examples/AppTest.java | 18 ++ .../nz/ac/massey/javaecs/examples/App.java | 17 +- .../Components/ComflabulationComponent.java | 6 +- .../Components/DirectionComponent.java | 6 +- .../Components/PositionComponent.java | 6 +- .../examples/Systems/ComflabSystem.java | 4 +- .../examples/Systems/MovementSystem.java | 2 +- 15 files changed, 332 insertions(+), 45 deletions(-) create mode 100644 demo2/.editorconfig create mode 100644 demo2/.gitattributes create mode 100644 demo2/.gitignore create mode 100644 demo2/.travis.yml create mode 100644 demo2/pom.xml create mode 100644 demo2/src/main/java/nz/ac/massey/javaecs/examples/App.java create mode 100644 demo2/src/test/java/nz/ac/massey/javaecs/examples/AppTest.java diff --git a/demo1/src/main/java/nz/ac/massey/javaecs/examples/App.java b/demo1/src/main/java/nz/ac/massey/javaecs/examples/App.java index 5a6cdf5..49bbc09 100644 --- a/demo1/src/main/java/nz/ac/massey/javaecs/examples/App.java +++ b/demo1/src/main/java/nz/ac/massey/javaecs/examples/App.java @@ -70,28 +70,28 @@ public final class App { // Bottom boundary Entity newEnt = gameEngine.createEntity(); 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, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(200, 50, 64, 64, 64)); // Left boundary newEnt = gameEngine.createEntity(); 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, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(50, 200, 64, 64, 64)); // Top boundary newEnt = gameEngine.createEntity(); 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, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(200, 50, 64, 64, 64)); // Right boundary newEnt = gameEngine.createEntity(); 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, Render.class, new Render(BoxRender.class)); gameEngine.addComponent(newEnt, BoxRender.class, new BoxRender(50, 200, 64, 64, 64)); @@ -110,8 +110,8 @@ public final class App { gameEngine.addComponent(newEnt, Reporter.class, new Reporter()); gameEngine.addComponent(newEnt, Vec2D.class, new Vec2D(5,5,0)); - /* // Uncomment this block to enable the stress-test (runs 500 square entities, with physics etc.) - Random rand = new Random(); + // Uncomment this block to enable the stress-test (runs 500 square entities, with physics etc.) + /*Random rand = new Random(); for (int i = 0; i < 500; i++) { newEnt = gameEngine.createEntity(); double size = 1 + rand.nextDouble(); @@ -122,7 +122,7 @@ public final class App { 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, TimedExistence.class, new TimedExistence(25 + rand.nextDouble() * 90)); - } */ + }*/ /************************************ ** Run init() on the systems ** diff --git a/demo1/src/main/java/nz/ac/massey/javaecs/examples/Systems/CollisionSystem.java b/demo1/src/main/java/nz/ac/massey/javaecs/examples/Systems/CollisionSystem.java index 46634ab..9530306 100644 --- a/demo1/src/main/java/nz/ac/massey/javaecs/examples/Systems/CollisionSystem.java +++ b/demo1/src/main/java/nz/ac/massey/javaecs/examples/Systems/CollisionSystem.java @@ -42,9 +42,8 @@ public class CollisionSystem extends ECSSystem { Collider collider = (Collider) gameEngine.getComponentData(entity, Collider.class); if (ridgidBody.mass >= 0) { // Only ignore non-static colliders - //processed.add(entity); for (Entity entity2 : entities) { - if (!processed.contains(entity2)) { + if (!processed.contains(entity2)) { // If this entity has been added, we've already calc'ed its forces completely // skip entities that have already been processed Vec2D otherPos = (Vec2D) gameEngine.getComponentData(entity2, Vec2D.class); RidgidBody otherRidgidBody = (RidgidBody) gameEngine.getComponentData(entity2, @@ -74,7 +73,7 @@ public class CollisionSystem extends ECSSystem { double totalYVel = Math.abs(ridgidBody.yVel) + Math.abs(otherRidgidBody.yVel); double xMult = 0; double yMult = 0; - double multTot = (1 + ridgidBody.bounciness); // reflect a force back 100%, (srtabilises) then add any extra in bounce + double multTot = 1; // reflect a force back 100%, (srtabilises) then add any extra in bounce double totalMass = ridgidBody.mass + otherRidgidBody.mass; boolean fullEdge = false; // left-hand collision @@ -90,7 +89,7 @@ public class CollisionSystem extends ECSSystem { xMult = -multTot; } // Bottom collision - if (blCollide && brCollide) { + else if (blCollide && brCollide) { fullEdge = true; //pos.y = otherPos.y + otherCollider.y; pos.y = otherPos.y - collider.y; @@ -127,12 +126,12 @@ public class CollisionSystem extends ECSSystem { } else if (deltaYVel > deltaXVel){ // Top edge - if (deltaYVel > deltaMin) pos.y = otherPos.y + otherCollider.y; + //if (deltaYVel > deltaMin) pos.y = otherPos.y + otherCollider.y; yMult = multTot; } else{ - if (deltaXVel > deltaMin) pos.x = otherPos.x + collider.x; + //if (deltaXVel > deltaMin) pos.x = otherPos.x + collider.x; xMult = -multTot; } } @@ -150,11 +149,11 @@ public class CollisionSystem extends ECSSystem { } else if (deltaYVel < deltaXVel){ //pos.y = otherPos.y + otherCollider.y; - if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y; + //if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y; yMult = -multTot; } else{ - if (deltaXVel > deltaMin) pos.x = otherPos.x - collider.x; + //if (deltaXVel > deltaMin) pos.x = otherPos.x - collider.x; xMult = -multTot; } } @@ -171,11 +170,11 @@ public class CollisionSystem extends ECSSystem { } } else if (deltaYVel > deltaXVel){ - if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y; + //if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y; yMult = -multTot; } else{ - if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x; + //if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x; xMult = multTot; } } @@ -192,11 +191,11 @@ public class CollisionSystem extends ECSSystem { } } else if (deltaYVel > deltaXVel){ - if (deltaYVel > deltaMin) pos.y = otherPos.y +otherCollider.y; + //if (deltaYVel > deltaMin) pos.y = otherPos.y +otherCollider.y; yMult = multTot; } else { - if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x; + //if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x; xMult = multTot; } } @@ -207,17 +206,16 @@ public class CollisionSystem extends ECSSystem { // entity 2 is immovable; full acceleration applied to the current entity if (otherRidgidBody.mass >= 0){ - yMult *= otherRidgidBody.bounciness; - xMult *= otherRidgidBody.bounciness; - ridgidBody.xVel += totalXVel * xMult * ((totalMass - ridgidBody.mass) / totalMass); - ridgidBody.yVel += totalYVel * yMult * ((totalMass - ridgidBody.mass) / totalMass); - - otherRidgidBody.xVel -= totalXVel * xMult * ((totalMass - otherRidgidBody.mass) / totalMass); - otherRidgidBody.yVel -= totalYVel * yMult * ((totalMass - otherRidgidBody.mass) / totalMass); + ridgidBody.xVel += totalXVel * ((totalMass - ridgidBody.mass) / totalMass) * (xMult + (Math.abs(xMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness))); + ridgidBody.yVel += totalYVel * ((totalMass - ridgidBody.mass) / totalMass) * (yMult + (Math.abs(yMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness))); + otherRidgidBody.xVel -= totalXVel * ((totalMass - otherRidgidBody.mass) / totalMass) * (xMult + (Math.abs(xMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness))); + otherRidgidBody.yVel -= totalYVel * ((totalMass - otherRidgidBody.mass) / totalMass) * (yMult + (Math.abs(yMult) + (ridgidBody.bounciness * otherRidgidBody.bounciness))); } else{ - ridgidBody.xVel += totalXVel * xMult; - ridgidBody.yVel += totalYVel * yMult; + double newXVel = totalXVel * (xMult * (Math.abs(xMult) + ridgidBody.bounciness)); + double newYVel = totalYVel * (yMult * (Math.abs(yMult) + ridgidBody.bounciness)); + ridgidBody.xVel += newXVel; + ridgidBody.yVel += newYVel; } Object grav = gameEngine.getComponentData(entity, Gravity.class); if (grav != null && otherRidgidBody.mass < 0) { @@ -231,6 +229,7 @@ public class CollisionSystem extends ECSSystem { } } } + //processed.add(entity); // Add this entity to the already processed ones } } diff --git a/demo2/.editorconfig b/demo2/.editorconfig new file mode 100644 index 0000000..1f494b0 --- /dev/null +++ b/demo2/.editorconfig @@ -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 diff --git a/demo2/.gitattributes b/demo2/.gitattributes new file mode 100644 index 0000000..8dfa1eb --- /dev/null +++ b/demo2/.gitattributes @@ -0,0 +1,2 @@ +# When shell scripts end in CRLF, bash gives a cryptic error message +*.sh text eol=lf diff --git a/demo2/.gitignore b/demo2/.gitignore new file mode 100644 index 0000000..7194620 --- /dev/null +++ b/demo2/.gitignore @@ -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/ diff --git a/demo2/.travis.yml b/demo2/.travis.yml new file mode 100644 index 0000000..8bdbb2f --- /dev/null +++ b/demo2/.travis.yml @@ -0,0 +1,4 @@ +language: java +jdk: openjdk8 +after_success: + - mvn coveralls:report diff --git a/demo2/pom.xml b/demo2/pom.xml new file mode 100644 index 0000000..f1e7f59 --- /dev/null +++ b/demo2/pom.xml @@ -0,0 +1,195 @@ + + 4.0.0 + nz.ac.massey.javaecs.examples + demo2 + 1.0-SNAPSHOT + + 1.8 + 1.8 + UTF-8 + 5.6.0 + 3.0.0-M3 + 3.1.0 + 8.29 + 4.0.1 + 3.0.0-M4 + 0.8.4 + 3.0.0 + 4.3.0 + + 0% + 0% + 20 + 5 + + + + org.junit.jupiter + junit-jupiter-api + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + + enforce + + + + + 3.6.3 + + + true + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${maven-checkstyle-plugin.version} + + + com.puppycrawl.tools + checkstyle + ${checkstyle.version} + + + com.github.ngeor + checkstyle-rules + ${checkstyle-rules.version} + + + + com/github/ngeor/checkstyle.xml + true + ${skipTests} + + + + checkstyle + validate + + check + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + org.jacoco + jacoco-maven-plugin + ${jacoco-maven-plugin.version} + + + pre-unit-test + + prepare-agent + + + + post-unit-test + test + + report + + + + check-unit-test + test + + check + + + ${project.build.directory}/jacoco.exec + + + BUNDLE + + + INSTRUCTION + COVEREDRATIO + ${jacoco.unit-tests.limit.instruction-ratio} + + + BRANCH + COVEREDRATIO + ${jacoco.unit-tests.limit.branch-ratio} + + + + + CLASS + + + COMPLEXITY + TOTALCOUNT + ${jacoco.unit-tests.limit.class-complexity} + + + + + METHOD + + + COMPLEXITY + TOTALCOUNT + ${jacoco.unit-tests.limit.method-complexity} + + + + + + + + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + + + + + travis + + + env.TRAVIS + + + + + + org.eluder.coveralls + coveralls-maven-plugin + ${coveralls-maven-plugin.version} + + + + + + diff --git a/demo2/src/main/java/nz/ac/massey/javaecs/examples/App.java b/demo2/src/main/java/nz/ac/massey/javaecs/examples/App.java new file mode 100644 index 0000000..5874920 --- /dev/null +++ b/demo2/src/main/java/nz/ac/massey/javaecs/examples/App.java @@ -0,0 +1,17 @@ +package nz.ac.massey.javaecs.examples; + +/** + * Hello world! + */ +public final class App { + private App() { + } + + /** + * Says hello to the world. + * @param args The arguments of the program. + */ + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/demo2/src/test/java/nz/ac/massey/javaecs/examples/AppTest.java b/demo2/src/test/java/nz/ac/massey/javaecs/examples/AppTest.java new file mode 100644 index 0000000..1978087 --- /dev/null +++ b/demo2/src/test/java/nz/ac/massey/javaecs/examples/AppTest.java @@ -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); + } +} diff --git a/perftest/src/main/java/nz/ac/massey/javaecs/examples/App.java b/perftest/src/main/java/nz/ac/massey/javaecs/examples/App.java index 9c79f6b..2988d76 100644 --- a/perftest/src/main/java/nz/ac/massey/javaecs/examples/App.java +++ b/perftest/src/main/java/nz/ac/massey/javaecs/examples/App.java @@ -18,7 +18,12 @@ public final class App { */ public static void main(String[] args) { 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); @@ -29,7 +34,9 @@ public final class App { MovementSystem movementSystem = new MovementSystem(engine); engine.registerSystem(MovementSystem.class, movementSystem); - double startTime = System.nanoTime(); + + startTime = System.nanoTime(); + for (int i = 0; i < nEntities; i++) { Entity entity = engine.createEntity(); engine.addComponent(entity, PositionComponent.class, new PositionComponent()); @@ -39,17 +46,17 @@ public final class App { engine.addComponent(entity, ComflabulationComponent.class, new ComflabulationComponent()); } } - double endTime = System.nanoTime(); + endTime = System.nanoTime(); - System.out.println("Entity creation Time taken: " + (endTime - startTime) / 1e6+ " ms " + "("+ (endTime - startTime) / 1e9 + "seconds )"); + System.out.println("Entity creation Time taken: " + (endTime - startTime) / 1e6+ " ms " + "("+ (endTime - startTime) / 1e9 + " seconds )"); - Float dt = 1.0f/60.0f; + double dt = 1.0f/60.0f; startTime = System.nanoTime(); movementSystem.update(dt); comflabSystem.update(dt); endTime = System.nanoTime(); - System.out.println("Time taken: " + (endTime - startTime) / 1e6+ " ms " + "("+ (endTime - startTime) / 1e9 + "seconds )"); + System.out.println("Single Loop Time taken: " + (endTime - startTime) / 1e6+ " ms " + "("+ (endTime - startTime) / 1e9 + " seconds )"); System.out.println("Hello World!"); } diff --git a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/ComflabulationComponent.java b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/ComflabulationComponent.java index 64b1648..71369ae 100644 --- a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/ComflabulationComponent.java +++ b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/ComflabulationComponent.java @@ -1,12 +1,12 @@ package nz.ac.massey.javaecs.examples.Components; public class ComflabulationComponent { - public Float thingy; - public Integer dingy; + public double thingy; + public int dingy; public boolean mingy; public String stringy; - public ComflabulationComponent(Float thingy, Integer dingy, boolean mingy, String stringy){ + public ComflabulationComponent(double thingy, int dingy, boolean mingy, String stringy){ this.thingy = thingy; this.dingy = dingy; this.mingy = mingy; diff --git a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/DirectionComponent.java b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/DirectionComponent.java index 9b6937d..ad9e304 100644 --- a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/DirectionComponent.java +++ b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/DirectionComponent.java @@ -1,10 +1,10 @@ package nz.ac.massey.javaecs.examples.Components; public class DirectionComponent { - public Float x; - public Float y; + public double x; + public double y; - public DirectionComponent(Float x, Float y){ + public DirectionComponent(double x, double y){ this.x = x; this.y = y; } diff --git a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/PositionComponent.java b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/PositionComponent.java index eeaf643..9aa3500 100644 --- a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/PositionComponent.java +++ b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Components/PositionComponent.java @@ -1,10 +1,10 @@ package nz.ac.massey.javaecs.examples.Components; public class PositionComponent { - public Float x; - public Float y; + public double x; + public double y; - public PositionComponent(Float x, Float y){ + public PositionComponent(double x, double y){ this.x = x; this.y = y; } diff --git a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Systems/ComflabSystem.java b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Systems/ComflabSystem.java index 2c78fc5..59e3303 100644 --- a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Systems/ComflabSystem.java +++ b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Systems/ComflabSystem.java @@ -26,10 +26,10 @@ public class ComflabSystem extends ECSSystem { public void update() { } - public void update(Float dt) { + public void update(double dt) { for (Entity entity : entities) { ComflabulationComponent comflab = (ComflabulationComponent)ecs.getComponentData(entity, ComflabulationComponent.class); - comflab.thingy *= 1.000001f; + comflab.thingy *= 1.000001; comflab.mingy = !comflab.mingy; comflab.dingy++; } diff --git a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Systems/MovementSystem.java b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Systems/MovementSystem.java index 865541c..c0626bd 100644 --- a/perftest/src/main/java/nz/ac/massey/javaecs/examples/Systems/MovementSystem.java +++ b/perftest/src/main/java/nz/ac/massey/javaecs/examples/Systems/MovementSystem.java @@ -29,7 +29,7 @@ public class MovementSystem extends ECSSystem { public void update() { } - public void update(Float dt) { + 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);