Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Entity Component System (ECS) architecture #3149

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Empty file.
121 changes: 121 additions & 0 deletions entity-component-system/etc/entity-component-system.urm.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
@startuml
package com.iluwatar {
class App {
+ App()
+ main(args : String[]) {static}
}
abstract class Component {
- isEnabled : boolean
- name : String
- parent : Entity
+ Component()
+ getEnabled() : boolean
+ getName() : String
+ getParent() : Entity
+ setEnabled(isEnabled : boolean)
+ setName(name : String)
+ setParent(parent : Entity)
+ update(float) {abstract}
}
class Entity {
- children : List<Entity>
- components : List<Component>
- entityId : UUID
- gameSystem : GameSystem
- isEnabled : boolean
- name : String
- parent : Entity
- transform : TransformComponent
+ Entity(entityName : String)
+ addChild(child : Entity)
+ addComponent(component : Component)
+ getChildren() : List<Entity>
+ getComponent(componentName : String) : Component
+ getEntityId() : UUID
+ getGameSystem() : GameSystem
+ getMeshRenderComponents() : List<Component>
+ getName() : String
+ getParent() : Entity
+ getTransformComponent() : TransformComponent
+ isEnabled() : boolean
+ removeChild(child : Entity)
+ removeComponent(component : Component)
+ renderEntity()
+ setEnabled(enabled : boolean)
+ setGameSystem(gameSystem : GameSystem)
+ setIsEnabled(isEnabled : boolean)
+ setName(name : String)
+ setParent(newParent : Entity)
+ update(deltaTime : float)
}
class GameSystem {
- entities : List<Entity>
+ GameSystem()
+ addEntity(entity : Entity)
- calculateDistance(point1 : float[], point2 : float[]) : float
+ getSystemMatrix(entity : Entity) : float[][]
+ getSystemPosition(entity : Entity) : float[]
- multiplyMatrices(matrix1 : float[][], matrix2 : float[][]) : float[][]
+ removeEntity(entity : Entity)
+ renderSystem()
+ sortEntitiesByDistance(referencePoint : float[])
+ update(deltaTime : float)
}
class HealthComponent {
- currentHealth : float
- isAlive : boolean
- logger : Logger {static}
- maxHealth : float
+ HealthComponent(maxHealth : float)
+ applyDamage(damage : float)
+ getCurrentHealth() : float
+ getMaxHealth() : float
+ heal(amount : float)
+ isAlive() : boolean
+ setAlive(isAlive : boolean)
+ setCurrentHealth(currentHealth : float)
+ setMaxHealth(maxHealth : float)
+ update(deltaTime : float)
}
class TransformComponent {
- position : float[]
- rotation : float[]
- scale : float[]
+ TransformComponent()
+ TransformComponent(initPosition : float[], initRotation : float[], initScale : float[])
- applyRotation(matrix : float[][])
+ getPosition() : float[]
+ getRotation() : float[]
+ getScale() : float[]
+ getTransformMatrix() : float[][]
+ setPosition(position : float[])
+ setRotation(eulerAngles : float[])
+ setScale(scale : float[])
+ update(deltaTime : float)
}
class VelocityComponent {
- logger : Logger {static}
- velocityX : float
- velocityY : float
- velocityZ : float
+ VelocityComponent(velocityX : float, velocityY : float, velocityZ : float)
+ applyForce(forceX : float, forceY : float, forceZ : float)
+ applyFriction(frictionCoefficient : float)
+ getVelocityX() : float
+ getVelocityY() : float
+ getVelocityZ() : float
+ setVelocityX(velocityX : float)
+ setVelocityY(velocityY : float)
+ setVelocityZ(velocityZ : float)
+ update(deltaTime : float)
}
}
Entity --> "-transform" TransformComponent
Entity --> "-parent" Entity
Entity --> "-children" Entity
Entity --> "-gameSystem" GameSystem
Entity --> "-components" Component
HealthComponent --|> Component
TransformComponent --|> Component
VelocityComponent --|> Component
@enduml
74 changes: 74 additions & 0 deletions entity-component-system/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).

The MIT License
Copyright © 2014-2022 Ilkka Seppälä

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.26.0-SNAPSHOT</version>
</parent>
<groupId>com.iluwatar</groupId>
<version>1.26.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<artifactId>entity-component-system</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<mainClass>com.iluwatar.entity-component-system.Main</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
80 changes: 80 additions & 0 deletions entity-component-system/src/main/java/com/iluwatar/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar;

/**
* The main entry point for the application.
* This class simulates a game loop where entities are created, updated, and their states are modified.
*/
Comment on lines +27 to +30
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, above the App class, please explain the pattern briefly and describe how the code example implements it.

public class App {

/**
* The main method that runs the application.
* It creates entities, adds components, updates them over several frames,
* and demonstrates applying damage and forces to entities.
*
* @param args Command-line arguments (not used in this application)
*/
public static void main(String[] args) {


Entity entity1 = new Entity("Entity1");
Entity entity2 = new Entity("Entity2");
Comment on lines +43 to +44
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments where needed. Remember, this is learning material.


TransformComponent transform1 = new TransformComponent(new float[]{0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f});
TransformComponent transform2 = new TransformComponent(new float[]{5.0f, 0.0f, 0.0f},
new float[]{0.0f, 45.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f});

entity1.addComponent(transform1);
entity2.addComponent(transform2);

HealthComponent health1 = new HealthComponent(100); // Ensure HealthComponent is implemented
entity1.addComponent(health1);

VelocityComponent velocity1 = new VelocityComponent(1.0f, 0.0f, 0.0f);
entity1.addComponent(velocity1);

GameSystem gameSystem = new GameSystem();
gameSystem.addEntity(entity1);
gameSystem.addEntity(entity2);

float deltaTime = 1.0f / 60.0f;

for (int i = 0; i < 10; i++) {

gameSystem.update(deltaTime);

if (i == 5) {
health1.applyDamage(30);
}

if (i == 3) {
velocity1.applyForce(0.5f, 0.0f, 0.0f);
}

}
}
}
88 changes: 88 additions & 0 deletions entity-component-system/src/main/java/com/iluwatar/Component.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar;

/**
* Abstract class representing a component in the ECS system.
* Each component can be enabled or disabled and associated with an entity.
* Subclasses of this class must implement the update method.
*/
public abstract class Component {

private String name;
private boolean isEnabled;
Comment on lines +34 to +35
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lombok to get rid of boilerplate code such as getters and setters


/**
* Default constructor for the component.
* Initializes a new component with default values.
*/
protected Component() {
// Constructor left empty intentionally, no specific initialization required
}

/**
* Gets the name of the component.
*
* @return the name of the component
*/
public String getName() {
return name;
}

/**
* Sets the name of the component.
*
* @param name the name of the component
*/
public void setName(String name) {
this.name = name;
}

/**
* Gets the enabled state of the component.
*
* @return true if the component is enabled, false otherwise
*/
public boolean getEnabled() {
return isEnabled;
}

/**
* Sets the enabled state of the component.
*
* @param isEnabled true to enable the component, false to disable it
*/
public void setEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}

/**
* Abstract method to update the component.
* Subclasses must implement this method to define their update behavior.
*
* @param deltaTime the time elapsed since the last update
*/
public abstract void update(float deltaTime);
}
Loading
Loading