From 8a88437585cfd9a1a16fec489404ac2d475d26ec Mon Sep 17 00:00:00 2001 From: ndido98 Date: Sun, 3 Oct 2021 11:07:21 +0200 Subject: [PATCH] Change addComponent to setComponent Closes #116 --- .../main/scala/ecscala/ViewBenchmark.scala | 2 +- .../scala/ecscala/utils/JmhSettings.scala | 4 ++-- .../scala/dev/atedeg/ecscala/Entity.scala | 4 ++-- .../scala/dev/atedeg/ecscala/System.scala | 2 +- .../atedeg/ecscala/dsl/ExtensionMethods.scala | 6 +++--- .../scala/dev/atedeg/ecscala/EntityTest.scala | 6 +++--- .../scala/dev/atedeg/ecscala/SystemTest.scala | 6 +++--- .../scala/dev/atedeg/ecscala/ViewTest.scala | 2 +- .../scala/dev/atedeg/ecscala/WorldTest.scala | 16 +++++++-------- .../fixtures/SystemBuilderFixture.scala | 4 ++-- .../atedeg/ecscala/fixtures/ViewFixture.scala | 20 +++++++++---------- .../ecscalademo/systems/CollisionSystem.scala | 8 ++++---- .../ecscalademo/systems/DragBallSystem.scala | 2 +- .../systems/VelocityEditingSystem.scala | 2 +- 14 files changed, 42 insertions(+), 42 deletions(-) diff --git a/benchmarks/src/main/scala/ecscala/ViewBenchmark.scala b/benchmarks/src/main/scala/ecscala/ViewBenchmark.scala index 5aa3b872..13c0545c 100644 --- a/benchmarks/src/main/scala/ecscala/ViewBenchmark.scala +++ b/benchmarks/src/main/scala/ecscala/ViewBenchmark.scala @@ -12,6 +12,6 @@ class ViewBenchmark extends JmhSettings { @Benchmark def viewIterationBenchmark: Unit = { val view = world.getView[Position &: Velocity &: CNil] - view foreach (_.head.addComponent(Position(2, 3))) + view foreach (_.head.setComponent(Position(2, 3))) } } diff --git a/benchmarks/src/main/scala/ecscala/utils/JmhSettings.scala b/benchmarks/src/main/scala/ecscala/utils/JmhSettings.scala index 916ea8a5..0816536f 100644 --- a/benchmarks/src/main/scala/ecscala/utils/JmhSettings.scala +++ b/benchmarks/src/main/scala/ecscala/utils/JmhSettings.scala @@ -37,7 +37,7 @@ class JmhSettings { @Setup def setup: Unit = { val entities = (0 until nEntities) map (_ => world.createEntity()) - entities foreach (_.addComponent(Position(1, 2))) - entities foreach (_.addComponent(Velocity(3, 4))) + entities foreach (_.setComponent(Position(1, 2))) + entities foreach (_.setComponent(Velocity(3, 4))) } } diff --git a/core/src/main/scala/dev/atedeg/ecscala/Entity.scala b/core/src/main/scala/dev/atedeg/ecscala/Entity.scala index 9aa7455d..c6220378 100644 --- a/core/src/main/scala/dev/atedeg/ecscala/Entity.scala +++ b/core/src/main/scala/dev/atedeg/ecscala/Entity.scala @@ -16,7 +16,7 @@ sealed trait Entity { * @return * itself. */ - def addComponent[C <: Component: ComponentTag](component: C): Entity + def setComponent[C <: Component: ComponentTag](component: C): Entity /** * @tparam C @@ -55,7 +55,7 @@ object Entity { private case class EntityImpl(private val id: Id, private val world: World) extends Entity { - override def addComponent[C <: Component](component: C)(using ct: ComponentTag[C]): Entity = { + override def setComponent[C <: Component](component: C)(using ct: ComponentTag[C]): Entity = { component.setEntity(Some(this)) world addComponent (this -> component) this diff --git a/core/src/main/scala/dev/atedeg/ecscala/System.scala b/core/src/main/scala/dev/atedeg/ecscala/System.scala index bace6cc9..4d983deb 100644 --- a/core/src/main/scala/dev/atedeg/ecscala/System.scala +++ b/core/src/main/scala/dev/atedeg/ecscala/System.scala @@ -82,7 +82,7 @@ trait System[L <: CList](using private val clt: CListTag[L]) { val (component, ct) = taggedComponent component match { case Deleted => entity.removeComponent(using ct) - case _ => entity.addComponent(component)(using ct) + case _ => entity.setComponent(component)(using ct) } } } diff --git a/core/src/main/scala/dev/atedeg/ecscala/dsl/ExtensionMethods.scala b/core/src/main/scala/dev/atedeg/ecscala/dsl/ExtensionMethods.scala index 61e77911..38afa4f2 100644 --- a/core/src/main/scala/dev/atedeg/ecscala/dsl/ExtensionMethods.scala +++ b/core/src/main/scala/dev/atedeg/ecscala/dsl/ExtensionMethods.scala @@ -18,7 +18,7 @@ trait ExtensionMethods { * }}} */ def withComponents[L <: CList](componentList: L)(using clt: CListTag[L]): Entity = { - componentList.taggedWith(clt) foreach { entity.addComponent(_)(using _) } + componentList.taggedWith(clt) foreach { entity.setComponent(_)(using _) } entity } @@ -29,7 +29,7 @@ trait ExtensionMethods { * entity withComponent Component() * }}} */ - def withComponent[C <: Component: ComponentTag](component: C): Entity = entity.addComponent(component) + def withComponent[C <: Component: ComponentTag](component: C): Entity = entity.setComponent(component) /** * This method enables the following syntax: @@ -38,7 +38,7 @@ trait ExtensionMethods { * entity += Component() * }}} */ - def +=[C <: Component: ComponentTag](component: C): Entity = entity.addComponent(component) + def +=[C <: Component: ComponentTag](component: C): Entity = entity.setComponent(component) /** * This method enables the following syntax: diff --git a/core/src/test/scala/dev/atedeg/ecscala/EntityTest.scala b/core/src/test/scala/dev/atedeg/ecscala/EntityTest.scala index a4610ff4..2614f8bf 100644 --- a/core/src/test/scala/dev/atedeg/ecscala/EntityTest.scala +++ b/core/src/test/scala/dev/atedeg/ecscala/EntityTest.scala @@ -19,8 +19,8 @@ class EntityTest extends AnyWordSpec with Matchers { val entity2 = world.createEntity() val c1 = Position(1, 1) val c2 = Position(2, 2) - entity1 addComponent c1 - entity2 addComponent c2 + entity1 setComponent c1 + entity2 setComponent c2 entity1.getComponent[Position] shouldBe Some(c1) entity1.getComponent[Position] flatMap (_.entity) shouldBe Some(entity1) entity2.getComponent[Position] shouldBe Some(c2) @@ -32,7 +32,7 @@ class EntityTest extends AnyWordSpec with Matchers { val entity = world.createEntity() val component = Position(1, 1) component.entity shouldBe empty - entity addComponent component + entity setComponent component component.entity should contain(entity) removal(entity, component) component.entity shouldBe empty diff --git a/core/src/test/scala/dev/atedeg/ecscala/SystemTest.scala b/core/src/test/scala/dev/atedeg/ecscala/SystemTest.scala index 91b5f277..897a1e72 100644 --- a/core/src/test/scala/dev/atedeg/ecscala/SystemTest.scala +++ b/core/src/test/scala/dev/atedeg/ecscala/SystemTest.scala @@ -47,7 +47,7 @@ class SystemTest extends AnyWordSpec with Matchers { } "add components" in new ViewFixture { world.addSystem[Position &: Velocity &: CNil](System((entity, comps, _) => { - entity addComponent Mass(10) + entity setComponent Mass(10) comps })) world.update(10) @@ -90,12 +90,12 @@ class SystemTest extends AnyWordSpec with Matchers { val testSystem = System[Comps].withBefore { (deltaTime, world, view) => view foreach (entityComponentsPair => { val (entity, Position(px, py) &: _) = entityComponentsPair - entity.addComponent(Position(px * 2, py * 2)) + entity.setComponent(Position(px * 2, py * 2)) }) }.withAfter { (deltaTime, world, view) => view foreach (entityComponentsPair => { val (entity, Position(px, py) &: _) = entityComponentsPair - entity.addComponent(Position(px + 1, py + 1)) + entity.setComponent(Position(px + 1, py + 1)) }) }.withUpdate { (_, components, _) => components } diff --git a/core/src/test/scala/dev/atedeg/ecscala/ViewTest.scala b/core/src/test/scala/dev/atedeg/ecscala/ViewTest.scala index e36b9547..5659f966 100644 --- a/core/src/test/scala/dev/atedeg/ecscala/ViewTest.scala +++ b/core/src/test/scala/dev/atedeg/ecscala/ViewTest.scala @@ -53,7 +53,7 @@ class ViewTest extends AnyWordSpec with Matchers { } "allow to change the entities and reflect the changes on successive iteration" in new ViewFixture { val view = world.getView[Velocity &: Mass &: CNil] - view foreach (_.head.addComponent(Mass(11))) + view foreach (_.head.setComponent(Mass(11))) view should contain theSameElementsAs List((entity3, Velocity(1, 1) &: Mass(11))) } } diff --git a/core/src/test/scala/dev/atedeg/ecscala/WorldTest.scala b/core/src/test/scala/dev/atedeg/ecscala/WorldTest.scala index 71e8be0c..91dccb63 100644 --- a/core/src/test/scala/dev/atedeg/ecscala/WorldTest.scala +++ b/core/src/test/scala/dev/atedeg/ecscala/WorldTest.scala @@ -31,8 +31,8 @@ class WorldTest extends AnyWordSpec with Matchers { "not have a component of a removed entity" in new WorldFixture { val entity = world.createEntity() val entity1 = world.createEntity() - entity.addComponent(Position(1, 2)) - entity1.addComponent(Position(1, 2)) + entity.setComponent(Position(1, 2)) + entity1.setComponent(Position(1, 2)) world.removeEntity(entity) world.getComponents[Position] should contain(Map(entity1 -> Position(1, 2))) @@ -53,8 +53,8 @@ class WorldTest extends AnyWordSpec with Matchers { "not have the components from the removed entities" in new WorldFixture { val entity = world.createEntity() val entity1 = world.createEntity() - entity.addComponent(Position(1, 2)) - entity1.addComponent(Position(3, 4)) + entity.setComponent(Position(1, 2)) + entity1.setComponent(Position(3, 4)) world.clearEntities() @@ -64,7 +64,7 @@ class WorldTest extends AnyWordSpec with Matchers { "has entities with components" should { "return the components" in new WorldFixture with ComponentsFixture { val entity = world.createEntity() - entity addComponent Position(1, 1) + entity setComponent Position(1, 1) world.getComponents[Position] should contain(Map(entity -> Position(1, 1))) } } @@ -72,9 +72,9 @@ class WorldTest extends AnyWordSpec with Matchers { "not return the components" in new WorldFixture with ComponentsFixture { val entity = world.createEntity() val component = Position(1, 1) - entity addComponent component + entity setComponent component entity removeComponent component - entity addComponent Position(1, 1) + entity setComponent Position(1, 1) entity.removeComponent[Position] world.getComponents[Position] shouldBe empty @@ -105,7 +105,7 @@ class WorldTest extends AnyWordSpec with Matchers { "a System is removed" should { "not execute its update" in new WorldFixture { val entity = world.createEntity() - entity.addComponent(Position(1, 1)) + entity.setComponent(Position(1, 1)) val system = SystemBuilder[Position &: CNil].withBefore { (_, _, _) => () }.withAfter { (_, _, _) => () }.withUpdate { (_, c, _) => diff --git a/core/src/test/scala/dev/atedeg/ecscala/fixtures/SystemBuilderFixture.scala b/core/src/test/scala/dev/atedeg/ecscala/fixtures/SystemBuilderFixture.scala index fb69bd71..8f9a6393 100644 --- a/core/src/test/scala/dev/atedeg/ecscala/fixtures/SystemBuilderFixture.scala +++ b/core/src/test/scala/dev/atedeg/ecscala/fixtures/SystemBuilderFixture.scala @@ -6,6 +6,6 @@ import dev.atedeg.ecscala.util.types.given trait SystemBuilderFixture { val world = World() val entity = world.createEntity() - entity.addComponent(Position(1, 1)) - entity.addComponent(Velocity(1, 1)) + entity.setComponent(Position(1, 1)) + entity.setComponent(Velocity(1, 1)) } diff --git a/core/src/test/scala/dev/atedeg/ecscala/fixtures/ViewFixture.scala b/core/src/test/scala/dev/atedeg/ecscala/fixtures/ViewFixture.scala index d4f3bf51..193e15fe 100644 --- a/core/src/test/scala/dev/atedeg/ecscala/fixtures/ViewFixture.scala +++ b/core/src/test/scala/dev/atedeg/ecscala/fixtures/ViewFixture.scala @@ -10,18 +10,18 @@ trait ViewFixture extends ComponentsFixture with WorldFixture { val entity4 = world.createEntity() val entity5 = world.createEntity() - entity1.addComponent(Position(1, 1)) - entity1.addComponent(Velocity(1, 1)) + entity1.setComponent(Position(1, 1)) + entity1.setComponent(Velocity(1, 1)) - entity2.addComponent(Mass(1)) + entity2.setComponent(Mass(1)) - entity3.addComponent(Position(1, 1)) - entity3.addComponent(Velocity(1, 1)) - entity3.addComponent(Mass(1)) + entity3.setComponent(Position(1, 1)) + entity3.setComponent(Velocity(1, 1)) + entity3.setComponent(Mass(1)) - entity4.addComponent(Position(1, 1)) - entity4.addComponent(Velocity(1, 1)) + entity4.setComponent(Position(1, 1)) + entity4.setComponent(Velocity(1, 1)) - entity5.addComponent(Position(1, 1)) - entity5.addComponent(Mass(1)) + entity5.setComponent(Position(1, 1)) + entity5.setComponent(Mass(1)) } diff --git a/demo/src/main/scala/dev/atedeg/ecscalademo/systems/CollisionSystem.scala b/demo/src/main/scala/dev/atedeg/ecscalademo/systems/CollisionSystem.scala index 71217eba..1e851592 100644 --- a/demo/src/main/scala/dev/atedeg/ecscalademo/systems/CollisionSystem.scala +++ b/demo/src/main/scala/dev/atedeg/ecscalademo/systems/CollisionSystem.scala @@ -30,16 +30,16 @@ class CollisionSystem(private val playState: PlayState, private val regions: Wri if (isColliding((positionA, positionB), (circleA.radius, circleB.radius))) { if (isStuck((positionA, positionB), (circleA.radius, circleB.radius))) { val (newPositionA, newPositionB) = unstuck((positionA, positionB), (circleA.radius, circleB.radius)) - candidateAEntity addComponent newPositionA - candidateBEntity addComponent newPositionB + candidateAEntity setComponent newPositionA + candidateBEntity setComponent newPositionB // Update the positions for correctly calculating the collision velocities positionA = newPositionA positionB = newPositionB } val (newVelocityA, newVelocityB) = newVelocities((positionA, positionB), (velocityA, velocityB), (circleA.radius, circleB.radius)) - candidateAEntity addComponent newVelocityA - candidateBEntity addComponent newVelocityB + candidateAEntity setComponent newVelocityA + candidateBEntity setComponent newVelocityB } } } diff --git a/demo/src/main/scala/dev/atedeg/ecscalademo/systems/DragBallSystem.scala b/demo/src/main/scala/dev/atedeg/ecscalademo/systems/DragBallSystem.scala index 044cd29e..0ce14365 100644 --- a/demo/src/main/scala/dev/atedeg/ecscalademo/systems/DragBallSystem.scala +++ b/demo/src/main/scala/dev/atedeg/ecscalademo/systems/DragBallSystem.scala @@ -22,7 +22,7 @@ class DragBallSystem(private val playState: PlayState, private val mouseState: M override def update(deltaTime: DeltaTime, world: World): Unit = { playState.selectedBall match { - case Some(entity) => entity.addComponent(Position(mouseState.coordinates)) + case Some(entity) => entity.setComponent(Position(mouseState.coordinates)) case _ => () } } diff --git a/demo/src/main/scala/dev/atedeg/ecscalademo/systems/VelocityEditingSystem.scala b/demo/src/main/scala/dev/atedeg/ecscalademo/systems/VelocityEditingSystem.scala index 9a18e142..595fd8ca 100644 --- a/demo/src/main/scala/dev/atedeg/ecscalademo/systems/VelocityEditingSystem.scala +++ b/demo/src/main/scala/dev/atedeg/ecscalademo/systems/VelocityEditingSystem.scala @@ -19,7 +19,7 @@ class VelocityEditingSystem(private val playState: PlayState, private val mouseS val newVelocity = mouseState.coordinates - selectedBallPosition val newDirection = newVelocity.normalized val newIntensity = newVelocity.norm clamped (minVelocityIntensity, maxVelocityIntensity) - selectedBall.addComponent(Velocity(newDirection * newIntensity * intensityMultiplier)) + selectedBall.setComponent(Velocity(newDirection * newIntensity * intensityMultiplier)) playState.gameState = State.Pause } }