Skip to content

Commit

Permalink
Change addComponent to setComponent
Browse files Browse the repository at this point in the history
Closes #116
  • Loading branch information
ndido98 committed Oct 3, 2021
1 parent 741924c commit 8a88437
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion benchmarks/src/main/scala/ecscala/ViewBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
4 changes: 2 additions & 2 deletions benchmarks/src/main/scala/ecscala/utils/JmhSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
4 changes: 2 additions & 2 deletions core/src/main/scala/dev/atedeg/ecscala/Entity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/dev/atedeg/ecscala/System.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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:
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/scala/dev/atedeg/ecscala/EntityTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/scala/dev/atedeg/ecscala/SystemTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 }

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/dev/atedeg/ecscala/ViewTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
Expand Down
16 changes: 8 additions & 8 deletions core/src/test/scala/dev/atedeg/ecscala/WorldTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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()

Expand All @@ -64,17 +64,17 @@ 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)))
}
}
"components are removed from its enities" should {
"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
Expand Down Expand Up @@ -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, _) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
20 changes: 10 additions & 10 deletions core/src/test/scala/dev/atedeg/ecscala/fixtures/ViewFixture.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ => ()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 8a88437

Please sign in to comment.