Skip to content

Commit

Permalink
fixing maintainability, and intentionality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rofidakhaled committed Jan 11, 2025
1 parent 38ef4a6 commit a9edc07
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class App {
* @param args Command-line arguments (not used in this application)
*/
public static void main(String[] args) {
System.out.print("Hello, World!");


Entity entity1 = new Entity("Entity1");
Entity entity2 = new Entity("Entity2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class Component {
* Default constructor for the component.
* Initializes a new component with default values.
*/
public Component() {
protected Component() {
// Constructor left empty intentionally, no specific initialization required
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ public void setEnabled(boolean enabled) {
}
}

public boolean getEnabled() {
return isEnabled;
}

/**
* Updates the entity and its components.
*
Expand Down Expand Up @@ -214,9 +210,9 @@ public List<Component> getComponents() {
* Renders the entity and its components.
*/
public void renderEntity() {
for (Component component : components) {
//for (Component component : components) {
// Render each component
}
//}
}

// Getters and Setters for fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public void update(float deltaTime) {
* Renders the system (rendering logic can be implemented as needed).
*/
public void renderSystem() {
for (Entity entity : entities) {
//for (Entity entity : entities) {
// Implement rendering logic
}
//}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
*/
public class HealthComponent extends Component {

private static final Logger logger = Logger.getLogger(HealthComponent.class.getName());

private float currentHealth;
private float maxHealth;
private boolean isAlive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
*/
public class VelocityComponent extends Component {

private static final Logger logger = Logger.getLogger(VelocityComponent.class.getName());

private float velocityX; // The velocity in the X direction
private float velocityY; // The velocity in the Y direction
Expand Down
42 changes: 18 additions & 24 deletions entity-component-system/src/test/java/com/iluwatar/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class AppTest {
class AppTest {

private Entity entity1;
private Entity entity2;
Expand All @@ -42,7 +42,7 @@ public class AppTest {
new float[]{0.0f, 45.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f});

@BeforeEach
public void setUp() {
void setUp() {
entity1 = new Entity("Entity1");
entity2 = new Entity("Entity2");

Expand All @@ -56,25 +56,20 @@ public void setUp() {
entity1.addComponent(velocity1);
}
@Test
public void testMain_shouldPrintHelloWorld() {

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));

App.main(new String[]{});

assertEquals("Hello, World!", outputStream.toString(), "The output should be 'Hello, World!'");
void testMain_DoesNotThrowException() {
assertDoesNotThrow(() -> App.main(new String[]{}), "Child entity should not throw an exception if the parent is disabled.");
}

@Test
public void testHealthComponentApplyDamage() {
void testHealthComponentApplyDamage() {

health1.applyDamage(30);

assertEquals(70, health1.getCurrentHealth(), "Health should be reduced by 30");
}

@Test
public void testVelocityComponentApplyForce() {
void testVelocityComponentApplyForce() {

velocity1.applyForce(0.5f, 0.0f, 0.0f);

Expand All @@ -84,7 +79,7 @@ public void testVelocityComponentApplyForce() {
}

@Test
public void testEntityUpdate() {
void testEntityUpdate() {

float deltaTime = 1.0f / 60.0f;
velocity1.setVelocityY(12.0f);
Expand All @@ -95,7 +90,7 @@ public void testEntityUpdate() {
}

@Test
public void testEntityHealthAfterDamageAndForce() {
void testEntityHealthAfterDamageAndForce() {

health1.applyDamage(40);

Expand All @@ -108,7 +103,7 @@ public void testEntityHealthAfterDamageAndForce() {
}

@Test
public void testFinalEntityStateAfterSimulation() {
void testFinalEntityStateAfterSimulation() {

GameSystem gameSystem = new GameSystem();
gameSystem.addEntity(entity1);
Expand All @@ -134,13 +129,13 @@ public void testFinalEntityStateAfterSimulation() {
}

@Test
public void testAddTransformComponent() {
void testAddTransformComponent() {
entity1.addComponent(transform1);
assertTrue(entity1.getComponents().contains(transform1), "Entity1 should contain the added TransformComponent.");
}

@Test
public void testGameLoopUpdatesEntityState() {
void testGameLoopUpdatesEntityState() {
GameSystem gameSystem = new GameSystem();
gameSystem.addEntity(entity1);

Expand All @@ -154,15 +149,15 @@ public void testGameLoopUpdatesEntityState() {
}

@Test
public void testHealthReductionOverMultipleDamages() {
void testHealthReductionOverMultipleDamages() {
health1.applyDamage(20);
health1.applyDamage(30);

assertEquals(50, health1.getCurrentHealth(), "Health should be reduced by 50 after two damage applications.");
}

@Test
public void testEntityRemovalFromGameSystem() {
void testEntityRemovalFromGameSystem() {
GameSystem gameSystem = new GameSystem();
gameSystem.addEntity(entity1);

Expand All @@ -173,15 +168,15 @@ public void testEntityRemovalFromGameSystem() {
}

@Test
public void testEntityWithoutComponents() {
void testEntityWithoutComponents() {
Entity entity = new Entity("EmptyEntity");
entity.removeComponent(entity.getTransformComponent());
assertTrue(entity.getComponents().isEmpty(), "Entity should have no components.");
assertEquals("EmptyEntity", entity.getName(), "Entity should have the correct name.");
}

@Test
public void testSetParentAndChildren() {
void testSetParentAndChildren() {
Entity parentEntity = new Entity("ParentEntity");
Entity childEntity = new Entity("ChildEntity");

Expand All @@ -192,9 +187,8 @@ public void testSetParentAndChildren() {
}

@Test
public void testVelocityComponentReset() {
void testVelocityComponentReset() {
velocity1.applyForce(1.0f, 0.0f, 0.0f);
float newVelocityX = velocity1.getVelocityX();

velocity1.resetVelocity();

Expand All @@ -204,7 +198,7 @@ public void testVelocityComponentReset() {
}

@Test
public void testHealthAndForceAppliedTogether() {
void testHealthAndForceAppliedTogether() {
health1.applyDamage(20);
velocity1.applyForce(0.5f, 0.0f, 0.0f);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@

import static org.junit.jupiter.api.Assertions.*;

public class ComponentTest {
class ComponentTest {
@Test
public void testGetName() {
Component component = new HealthComponent(100);
void testGetName() {
Component component = new HealthComponent(10);
component.setName("Health");

assertEquals("Health", component.getName(), "getName should return 'TestComponent'");
assertEquals("Health", component.getName(), "getName should return 'Health'");
}

@Test
public void testSetName() {
void testSetName() {
Component component = new HealthComponent(100);
component.setName("Health");

assertEquals("Health", component.getName(), "getName should return 'TestComponent'");
component.setName("Velocity");
component.setName("Transform");
assertEquals("Transform", component.getName(), "getName should return 'Transform'");
}

@Test
public void testComponentEnabled() {
void testComponentEnabled() {
Component component = new HealthComponent(100);
component.setEnabled(true);

Expand All @@ -57,7 +57,7 @@ public void testComponentEnabled() {
}

@Test
public void testComponentParent() {
void testComponentParent() {
Component component = new HealthComponent(100);
Entity parentEntity = new Entity("ParentEntity");

Expand Down
Loading

0 comments on commit a9edc07

Please sign in to comment.