Skip to content

Commit

Permalink
Added new Systems
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Hahn committed Feb 11, 2014
1 parent 30f52b9 commit dc9ebd8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
35 changes: 30 additions & 5 deletions crane-examples/src/main/scala/Example.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package crane.examples

/** Crane Imports **/
import crane.{Entity, Component, System, EntityProcessingSystem, World}
import crane.{Entity, Component, System, EntityProcessingSystem, TimedSystem, IntervalSystem, World}

/** External Imports **/
import com.github.nscala_time.time.Imports._
Expand Down Expand Up @@ -51,7 +51,7 @@ object Example extends App {
}
}

// Equivalent to the system above, except you don't iterate over each entity
// 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])
Expand All @@ -68,7 +68,7 @@ object Example extends App {
}

// Example of a system that processes every n milliseconds
class TimedSystem(milliseconds: Int) extends System {
class CustomTimedSystem(milliseconds: Int) extends System {
var start = DateTime.now
override def process(delta: Int) {
if((start to DateTime.now).millis >= milliseconds) {
Expand All @@ -83,8 +83,33 @@ object Example extends App {
}
}

// Equivalent to above
class MyTimedSystem extends TimedSystem(3000) {
override def processTime (delta: Int) {
val entities = world.getEntitiesByComponents(classOf[Useless])
println("Killing %d entities".format(entities.length))
entities.foreach { entity: Entity =>
entity.kill
println("Banana bread")
}
}
}

// Another Special System that runs "processInterval" every nth time
class MyIntervalSystem extends IntervalSystem(100) {
override def processInterval(delta: Int) {
val entities = world.getEntitiesByComponents(classOf[Useless])
println("Killing %d entities".format(entities.length))
entities.foreach { entity: Entity =>
entity.kill
println("Banana bread")
}
}
}


// Create World
val world = new World
val world = World()
// Default is already 1 - this does not need to be set unless you need variable deltas
// world.delta = 1

Expand All @@ -104,7 +129,7 @@ object Example extends App {
world.addEntity(player)
world.addEntity(useless)
world.addSystem(new MovementSystem)
world.addSystem(new TimedSystem(3000), 1)
world.addSystem(new CustomTimedSystem(3000), 1)

world.createGroup("THINGS")
world.groups("THINGS") += player
Expand Down
5 changes: 5 additions & 0 deletions crane/src/main/scala/crane/Entity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import scala.collection.mutable.ArrayBuffer
* @constructor create an entity
* @param tag optional String to identify a specific entity (must be unique)
*/

object Entity {
def apply(tag: String = "") = new Entity(tag)
}

class Entity(var tag:String = "") {
// Private Variables
private var _world = None : Option[World]
Expand Down
32 changes: 32 additions & 0 deletions crane/src/main/scala/crane/System.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package crane

/** External Import **/
import com.github.nscala_time.time.Imports._

/** The base System class - must override process **/
abstract class System {
def process(delta: Int)
Expand All @@ -18,3 +21,32 @@ abstract class EntityProcessingSystem[T <: AnyRef](include: List[T], exclude: Li
}
}
}

abstract class TimedSystem(milliseconds: Int) extends System {

var start = DateTime.now

def processTime(delta: Int)

override def process(delta: Int) {
if((start to DateTime.now).millis >= milliseconds) {
processTime(delta)
start = DateTime.now
}
}
}

abstract class IntervalSystem(count: Int) extends System {

var counter = 1

def processInterval(delta: Int)

override def process(delta: Int) {
counter += 1
if(counter >= count) {
processInterval(delta)
counter = 0
}
}
}
5 changes: 5 additions & 0 deletions crane/src/main/scala/crane/World.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import collection.JavaConversions._
* @constructor creates the world
* @param delta the delta in integers
*/

object World {
def apply(delta: Int=1) = new World(delta)
}

class World(var delta: Int=1) {
// Private Variables
private val _entities: ArrayBuffer[Entity] = new ArrayBuffer[Entity]
Expand Down
2 changes: 1 addition & 1 deletion project/build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ object CraneBuild extends Build {
object Dependencies {
import Dependency._
val crane = Seq(
Dependency.akkaActor, Dependency.akkaTransactor, Dependency.scalaSTM,
Dependency.akkaActor, Dependency.akkaTransactor, Dependency.scalaSTM, Dependency.scalatime,
Dependency.scalatest
)
val examples = Seq(
Expand Down

0 comments on commit dc9ebd8

Please sign in to comment.