Skip to content

Commit

Permalink
Merge pull request #6 from chrisbenincasa/downcast-component
Browse files Browse the repository at this point in the history
Return downcasted Component type from Entity
  • Loading branch information
Timothy Hahn committed Oct 29, 2014
2 parents f015a7a + f980d8d commit 561e6a5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target/
*.swp

.idea/
8 changes: 4 additions & 4 deletions crane-examples/src/main/scala/Example.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ object Example extends App {
// val entities = world.getEntitiesWithExclusions(include=List(classOf[Position], classOf[Velocity]), exclude=List(classOf[Useless]))

entities.foreach{ entity: Entity =>
val position = entity.getComponent(classOf[Position])
val velocity = entity.getComponent(classOf[Velocity])
val position = entity.getComponent[Position]
val velocity = entity.getComponent[Velocity]

(position, velocity) match {
case(Some(p: Position), Some(v: Velocity)) =>
Expand All @@ -54,8 +54,8 @@ object Example extends App {
// Equivalent to the system above, except you specify each individual entity
class MovementEntityProcessingSystem extends EntityProcessingSystem(include=List(classOf[Position], classOf[Velocity])) {
override def processEntity(e: Entity, delta: Int) {
val position = e.getComponent(classOf[Position])
val velocity = e.getComponent(classOf[Velocity])
val position = e.getComponent[Position]
val velocity = e.getComponent[Velocity]

(position, velocity) match {
case(Some(p: Position), Some(v: Velocity)) =>
Expand Down
19 changes: 11 additions & 8 deletions crane/src/main/scala/crane/Entity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package crane
/** Crane Exception Imports **/
import crane.exceptions.{DeadEntityException, DuplicateTagException, MissingComponentException}

import scala.reflect.ClassTag

/** External Imports **/
import scala.collection.mutable.ArrayBuffer

Expand Down Expand Up @@ -41,13 +43,15 @@ class Entity(var tag: String = "", val components: ArrayBuffer[Component] = Arra
// Accessors
def world: Option[World] = _world

/** Returns component of a classOf[Component]
*
* @param componentType the component to get (the class, not the instance)
/**
* Returns component of a classOf[Component]
*/
def getComponent[T <: AnyRef](componentType: T): Option[Component] = {
def getComponent[T <: Component : ClassTag]: Option[T] = {
if (alive) {
components find { case(component) => component.getClass == componentType }
components find {
case component: T => true
case _ => false
} map (_.asInstanceOf[T])
} else {
throw new DeadEntityException
}
Expand All @@ -71,11 +75,10 @@ class Entity(var tag: String = "", val components: ArrayBuffer[Component] = Arra

/** Removes component of a classOf[Component]
*
* @param componentType the component to remove (the class, not the instance)
*/
def removeComponent[T <: AnyRef](componentType: T) {
def removeComponent[T <: Component : ClassTag]() {
if (alive) {
val removeEntity: Option[Component] = getComponent(componentType)
val removeEntity: Option[Component] = getComponent[T]
removeEntity match {
case Some(e: Component) =>
components -= e
Expand Down
20 changes: 15 additions & 5 deletions crane/src/test/scala/crane/EntitySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ class EntitySpec extends FlatSpec with MockFactory {
entity.components += component
assert(entity.components.length == 1)

assert(entity.getComponent(component.getClass) match {
assert(entity.getComponent[Component] match {
case Some(s) => true
case _ => false
})
}

it should "downcast Component when getting it from entity" in {
val entity = Entity()
class CustomComponent extends Component
val customComponent = mock[CustomComponent]

entity.components.append(customComponent)

assert(entity.getComponent(classOf[Component]) match {
case Some(s) => false
case _ => true
assert(entity.components.length == 1)

assert(entity.getComponent[CustomComponent] match {
case Some(cc: CustomComponent) => true
case _ => false
})
}

Expand All @@ -51,7 +61,7 @@ class EntitySpec extends FlatSpec with MockFactory {
}

intercept[DeadEntityException] {
entity.removeComponent(classOf[Component])
entity.removeComponent[Component]
}
}
}
Expand Down

0 comments on commit 561e6a5

Please sign in to comment.