Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

4 years ago
  1. package wow.doge.mygame.implicits
  2. import com.simsilica.es.ComponentFilter
  3. import com.simsilica.es.EntityComponent
  4. import com.simsilica.es.EntityData
  5. import com.simsilica.es.EntitySet
  6. import com.simsilica.es.Filters
  7. import scala.reflect.ClassTag
  8. import scala.util.Try
  9. class EntityQuery(ed: EntityData) {
  10. private var cfilter: Option[ComponentFilter[_ <: EntityComponent]] = None
  11. private val buf = collection.mutable.ListBuffer.empty[Class[_]]
  12. def filter[T <: EntityComponent](field: String, value: Object)(implicit
  13. ev: ClassTag[T]
  14. ): EntityQuery = {
  15. val c = ev.runtimeClass.asInstanceOf[Class[T]]
  16. cfilter = Try(Filters.fieldEquals(c, field, value)).toOption
  17. this
  18. }
  19. def filterOr[T <: EntityComponent](operands: ComponentFilter[_ <: T]*)(
  20. implicit ev: ClassTag[T]
  21. ): EntityQuery = {
  22. val c = ev.runtimeClass.asInstanceOf[Class[T]]
  23. cfilter = Try(Filters.or(c, operands: _*)).toOption
  24. this
  25. }
  26. def filterAnd[T <: EntityComponent](operands: ComponentFilter[_ <: T]*)(
  27. implicit ev: ClassTag[T]
  28. ): EntityQuery = {
  29. val c = ev.runtimeClass.asInstanceOf[Class[T]]
  30. cfilter = Try(Filters.and(c, operands: _*)).toOption
  31. this
  32. }
  33. def component[T <: EntityComponent]()(implicit
  34. ev: ClassTag[T]
  35. ): EntityQuery = {
  36. val c = ev.runtimeClass
  37. buf += c
  38. this
  39. }
  40. def components[T <: EntityComponent](lst: Class[T]*): EntitySet = {
  41. ed.getEntities(lst: _*)
  42. }
  43. def result: EntitySet =
  44. cfilter.fold(ed.getEntities(buf.toList: _*)) { filters =>
  45. ed.getEntities(filters, buf.toList: _*)
  46. }
  47. }