Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.51 KB | None | 0 0
  1. class EntityGround : Entity {
  2.  
  3.     override val canBePickedUp: Boolean = false
  4.     override val shouldSendWorldTransformUpdates: Boolean = false
  5.  
  6.     constructor(world: World) : super(0f, 0f, 0f, world)
  7.  
  8.     constructor(uuid: UUID) : super(uuid)
  9.  
  10.     override fun getShape(): CollisionShape = BoxShape(Vector3f(50f, 2.5f, 50f))
  11.  
  12.     override fun getMass(): Float = 0f
  13. }
  14.  
  15. class EntityCard : Entity {
  16.  
  17.     constructor(posX: Float, posY: Float, posZ: Float, world: World) : super(posX, posY, posZ, world)
  18.  
  19.     constructor(uuid: UUID) : super(uuid)
  20.  
  21.     override val canBePickedUp: Boolean = true
  22.  
  23.     override val shouldSendWorldTransformUpdates: Boolean = true
  24.  
  25.     override fun getShape(): CollisionShape = BoxShape(Vector3f(9f, 0.01f, 6f))
  26.  
  27.     override fun getMass(): Float = 0.1f
  28.  
  29. }
  30.  
  31. abstract class Entity : Disposable {
  32.  
  33.     val uuid: UUID
  34.     val body: RigidBody
  35.     val localInertia: Vector3f = Vector3f()
  36.     val motionState: MotionState = MotionState()
  37.  
  38.     val lastGravity: Vector3f = Vector3f()
  39.  
  40.     lateinit var world: World
  41.  
  42.     abstract val canBePickedUp: Boolean
  43.  
  44.     abstract val shouldSendWorldTransformUpdates: Boolean
  45.  
  46.     val pickedUp: Boolean
  47.         get() = playerPickedUp != null
  48.  
  49.     var playerPickedUp: Player? = null
  50.  
  51.     constructor(posX: Float, posY: Float, posZ: Float, world: World) {
  52.         this.world = world
  53.         uuid = UUID.randomUUID()
  54.         motionState.transform.origin.set(Vector3f(posX, posY, posZ))
  55.         body.proceedToTransform(motionState.transform)
  56.     }
  57.  
  58.     constructor(uuid: UUID) {
  59.         this.uuid = uuid
  60.     }
  61.  
  62.     init {
  63.         val mass = getMass()
  64.         val shape = getShape()
  65.         if (mass > 0f) shape.calculateLocalInertia(mass, localInertia)
  66.         else localInertia.set(0f, 0f, 0f)
  67.         val cI = RigidBodyConstructionInfo(mass, motionState, shape, localInertia)
  68.         body = RigidBody(cI)
  69.         body.motionState = motionState
  70.         body.proceedToTransform(motionState.transform)
  71.         body.collisionFlags = body.collisionFlags or CollisionFlags.CUSTOM_MATERIAL_CALLBACK
  72.         body.userPointer = this
  73.     }
  74.  
  75.     fun update() {
  76.         if (pickedUp) {
  77.             val player = playerPickedUp!!
  78.             val dragTo = Vector3f(player.pointerPosition).add(0f, 10f, 0f)
  79.             val current = body.getWorldTransform(Transform()).origin
  80.             val distance = dragTo.distance(current)
  81.             if (distance > 1) {
  82.                 val scale: Float
  83.                 val maxV = 10f
  84.                 if (distance > 10) {
  85.                     scale = maxV
  86.                 } else {
  87.                     scale = maxV * (distance / 10f)
  88.                 }
  89.                 dragTo.sub(current.x, current.y, current.z).scale(scale)
  90.                 body.setLinearVelocity(dragTo)
  91.             }
  92.         }
  93.     }
  94.  
  95.     abstract fun getShape(): CollisionShape
  96.  
  97.     abstract fun getMass(): Float
  98.  
  99.     override fun dispose() {
  100.  
  101.     }
  102.  
  103.     fun pickup() {
  104.         body.activate(true)
  105.         lastGravity.set(body.getGravity(Vector3f()))
  106.         body.setGravity(Vector3f(0f, 0f, 0f))
  107.     }
  108.  
  109.     fun drop() {
  110.         body.setGravity(lastGravity)
  111.     }
  112.  
  113.     fun writeSpawnData(output: DataOutputStream) {}
  114.  
  115.     fun readSpawnData(input: DataInputStream) {}
  116. }
  117.  
  118. abstract class World(val isRemote: Boolean) : Disposable {
  119.  
  120.     val loadedEntityList: MutableList<Entity> = mutableListOf()
  121.     val entityMap: HashMap<UUID, Entity> = HashMap()
  122.     val playerMap: HashMap<UUID, Player> = HashMap()
  123.  
  124.     val broadphase: DbvtBroadphase = DbvtBroadphase()
  125.     val collisionConfiguration: CollisionConfiguration = DefaultCollisionConfiguration()
  126.     val dispatcher: CollisionDispatcher = CollisionDispatcher(collisionConfiguration)
  127.     val solver: SequentialImpulseConstraintSolver = SequentialImpulseConstraintSolver()
  128.  
  129.     val world: DynamicsWorld = DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration)
  130.  
  131.     var currentTickCount: Int = 0
  132.  
  133.     init {
  134.         world.setGravity(Vector3f(0f, -98.1f, 0f))
  135.     }
  136.  
  137.     open fun updateWorld() {
  138.         for (entity in loadedEntityList) {
  139.             entity.update()
  140.         }
  141.         world.stepSimulation(1 / 32f)
  142.         currentTickCount++
  143.     }
  144.  
  145.     open fun spawnEntityInWorld(entity: Entity) {
  146.         loadedEntityList.add(entity)
  147.         entityMap.put(entity.uuid, entity)
  148.         world.addRigidBody(entity.body)
  149.     }
  150.  
  151.     override fun dispose() {
  152.         for (entity in loadedEntityList) {
  153.             entity.dispose()
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement