-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
rofidakhaled
wants to merge
10
commits into
iluwatar:master
Choose a base branch
from
rofidakhaled:feature/485-ecs-pattern-implementation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0bb2e90
implementing ECS pattern
rofidakhaled 49cfb6b
adding test cases to all classes
rofidakhaled cd41e1a
Update EntityTest.java
rofidakhaled 38ef4a6
even more test cases
rofidakhaled a9edc07
fixing maintainability, and intentionality issues
rofidakhaled 80e42c3
Update HealthComponentTest.java
rofidakhaled b4c6a30
fixing more maintainability and intentionality issues
rofidakhaled cada3be
fixing the last issues
rofidakhaled f92c2a4
even more issues fixed
rofidakhaled b6a3848
Merge branch 'master' into feature/485-ecs-pattern-implementation
iluwatar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
121 changes: 121 additions & 0 deletions
121
entity-component-system/etc/entity-component-system.urm.puml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
80
entity-component-system/src/main/java/com/iluwatar/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
88
entity-component-system/src/main/java/com/iluwatar/Component.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.