Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EntityGround : Entity {
- override val canBePickedUp: Boolean = false
- override val shouldSendWorldTransformUpdates: Boolean = false
- constructor(world: World) : super(0f, 0f, 0f, world)
- constructor(uuid: UUID) : super(uuid)
- override fun getShape(): CollisionShape = BoxShape(Vector3f(50f, 2.5f, 50f))
- override fun getMass(): Float = 0f
- }
- class EntityCard : Entity {
- constructor(posX: Float, posY: Float, posZ: Float, world: World) : super(posX, posY, posZ, world)
- constructor(uuid: UUID) : super(uuid)
- override val canBePickedUp: Boolean = true
- override val shouldSendWorldTransformUpdates: Boolean = true
- override fun getShape(): CollisionShape = BoxShape(Vector3f(9f, 0.01f, 6f))
- override fun getMass(): Float = 0.1f
- }
- abstract class Entity : Disposable {
- val uuid: UUID
- val body: RigidBody
- val localInertia: Vector3f = Vector3f()
- val motionState: MotionState = MotionState()
- val lastGravity: Vector3f = Vector3f()
- lateinit var world: World
- abstract val canBePickedUp: Boolean
- abstract val shouldSendWorldTransformUpdates: Boolean
- val pickedUp: Boolean
- get() = playerPickedUp != null
- var playerPickedUp: Player? = null
- constructor(posX: Float, posY: Float, posZ: Float, world: World) {
- this.world = world
- uuid = UUID.randomUUID()
- motionState.transform.origin.set(Vector3f(posX, posY, posZ))
- body.proceedToTransform(motionState.transform)
- }
- constructor(uuid: UUID) {
- this.uuid = uuid
- }
- init {
- val mass = getMass()
- val shape = getShape()
- if (mass > 0f) shape.calculateLocalInertia(mass, localInertia)
- else localInertia.set(0f, 0f, 0f)
- val cI = RigidBodyConstructionInfo(mass, motionState, shape, localInertia)
- body = RigidBody(cI)
- body.motionState = motionState
- body.proceedToTransform(motionState.transform)
- body.collisionFlags = body.collisionFlags or CollisionFlags.CUSTOM_MATERIAL_CALLBACK
- body.userPointer = this
- }
- fun update() {
- if (pickedUp) {
- val player = playerPickedUp!!
- val dragTo = Vector3f(player.pointerPosition).add(0f, 10f, 0f)
- val current = body.getWorldTransform(Transform()).origin
- val distance = dragTo.distance(current)
- if (distance > 1) {
- val scale: Float
- val maxV = 10f
- if (distance > 10) {
- scale = maxV
- } else {
- scale = maxV * (distance / 10f)
- }
- dragTo.sub(current.x, current.y, current.z).scale(scale)
- body.setLinearVelocity(dragTo)
- }
- }
- }
- abstract fun getShape(): CollisionShape
- abstract fun getMass(): Float
- override fun dispose() {
- }
- fun pickup() {
- body.activate(true)
- lastGravity.set(body.getGravity(Vector3f()))
- body.setGravity(Vector3f(0f, 0f, 0f))
- }
- fun drop() {
- body.setGravity(lastGravity)
- }
- fun writeSpawnData(output: DataOutputStream) {}
- fun readSpawnData(input: DataInputStream) {}
- }
- abstract class World(val isRemote: Boolean) : Disposable {
- val loadedEntityList: MutableList<Entity> = mutableListOf()
- val entityMap: HashMap<UUID, Entity> = HashMap()
- val playerMap: HashMap<UUID, Player> = HashMap()
- val broadphase: DbvtBroadphase = DbvtBroadphase()
- val collisionConfiguration: CollisionConfiguration = DefaultCollisionConfiguration()
- val dispatcher: CollisionDispatcher = CollisionDispatcher(collisionConfiguration)
- val solver: SequentialImpulseConstraintSolver = SequentialImpulseConstraintSolver()
- val world: DynamicsWorld = DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration)
- var currentTickCount: Int = 0
- init {
- world.setGravity(Vector3f(0f, -98.1f, 0f))
- }
- open fun updateWorld() {
- for (entity in loadedEntityList) {
- entity.update()
- }
- world.stepSimulation(1 / 32f)
- currentTickCount++
- }
- open fun spawnEntityInWorld(entity: Entity) {
- loadedEntityList.add(entity)
- entityMap.put(entity.uuid, entity)
- world.addRigidBody(entity.body)
- }
- override fun dispose() {
- for (entity in loadedEntityList) {
- entity.dispose()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement