SHARE
TWEET

Untitled

a guest Jul 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.granseal.koLambda
  2.  
  3. import java.awt.Graphics2D
  4. import java.awt.geom.AffineTransform
  5. import java.awt.geom.Point2D
  6.  
  7. open class Entity(var pos: Point2D.Float = Point2D.Float()) {
  8.     var parent: Entity? = null
  9.     val props = mutableMapOf<String,Any>()
  10.     val children = mutableListOf<Entity>()
  11.     val updaters = mutableListOf<Entity.(Float) -> Unit>()
  12.     val drawers = mutableListOf<Entity.(Graphics2D) -> Unit>()
  13.     var scale = 1.0
  14.     var rotation = 0.0
  15.     var transform = AffineTransform()
  16.  
  17.     fun getLocalTransform(): AffineTransform {
  18.         transform.setToIdentity()
  19.         transform.translate(pos.x.toDouble(), pos.y.toDouble())
  20.         transform.rotate(rotation)
  21.         transform.scale(scale, scale)
  22.         return transform
  23.     }
  24.  
  25.     fun getWorldTransform(): AffineTransform {
  26.         return if (parent == null) {
  27.             getLocalTransform()
  28.         } else {
  29.             val trans = parent!!.getWorldTransform()
  30.             trans.concatenate(getLocalTransform())
  31.             trans
  32.         }
  33.     }
  34.  
  35.     fun update(delta: Float) {
  36.         updaters.forEach { it(delta) }
  37.         children.forEach { it.update(delta) }
  38.     }
  39.  
  40.     fun draw(g: Graphics2D) {
  41.         val saved = g.transform
  42.         g.transform = getWorldTransform()
  43.         drawers.forEach { it(g) }
  44.         children.forEach { it.draw(g) }
  45.         g.transform = saved
  46.     }
  47.  
  48.     fun add(e: Entity): Entity {
  49.         e.parent = this
  50.         children.add(e)
  51.         return e
  52.     }
  53.  
  54.     fun add(e: Entity.() -> Unit) = add(Entity().apply(e))
  55.  
  56.     fun getAll(): List<Entity> {
  57.         val entities = mutableListOf<Entity>()
  58.         entities.add(this)
  59.         children.forEach {
  60.             entities.addAll(it.getAll())
  61.         }
  62.         return entities
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top