physics: slime block bouncing

This commit is contained in:
Bixilon 2021-06-09 00:22:24 +02:00
parent 4be7ede5de
commit 7acca43662
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 74 additions and 7 deletions

View File

@ -14,6 +14,7 @@ package de.bixilon.minosoft.data.mappings.blocks.types
import com.google.gson.JsonObject
import de.bixilon.minosoft.data.entities.block.BlockEntity
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.data.inventory.ItemStack
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.mappings.blocks.BlockState
@ -105,6 +106,8 @@ open class Block(
open fun randomTick(connection: PlayConnection, blockState: BlockState, blockPosition: Vec3i, random: Random) {}
open fun onEntityLand(connection: PlayConnection, entity: Entity, blockPosition: Vec3i, blockState: BlockState) {}
companion object : ResourceLocationDeserializer<Block> {
override fun deserialize(mappings: Registries?, resourceLocation: ResourceLocation, data: JsonObject): Block {
check(mappings != null) { "Registries is null!" }
@ -118,6 +121,7 @@ open class Block(
"ComparatorBlock" -> ComparatorBlock(resourceLocation, mappings, data)
"CampfireBlock" -> CampfireBlock(resourceLocation, mappings, data)
"TorchBlock" -> TorchBlock(resourceLocation, mappings, data)
"SlimeBlock" -> SlimeBlock(resourceLocation, mappings, data)
else -> Block(resourceLocation, mappings, data)
}

View File

@ -0,0 +1,42 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.mappings.blocks.types
import com.google.gson.JsonObject
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.mappings.blocks.BlockState
import de.bixilon.minosoft.data.mappings.versions.Registries
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import glm_.vec3.Vec3i
open class SlimeBlock(resourceLocation: ResourceLocation, mappings: Registries, data: JsonObject) : Block(resourceLocation, mappings, data) {
override fun onEntityLand(connection: PlayConnection, entity: Entity, blockPosition: Vec3i, blockState: BlockState) {
super.onEntityLand(connection, entity, blockPosition, blockState)
if (entity.isSneaking) {
return
}
bounce(entity)
}
private fun bounce(entity: Entity) {
if (entity.velocity.y < 0.0) {
entity.velocity.y = -entity.velocity.y
}
}
}

View File

@ -303,6 +303,10 @@ class LocalPlayerEntity(
val collisionMovement = connection.collisionDetector.collide(null, movement, aabb, true)
forceMove(collisionMovement)
horizontalCollision = collisionMovement.x != movement.x || collisionMovement.z != movement.z
verticalCollision = collisionMovement.y != movement.y
this.onGround = verticalCollision && movement.y < 0.0f
@ -310,18 +314,34 @@ class LocalPlayerEntity(
fall(collisionMovement.y)
var velocityChanged = false
if (movement.y != collisionMovement.y) {
if (movement.y < 0.0 && collisionMovement.y != 0.0) {
val landingPosition = belowBlockPosition
val landingBlockState = connection.world[belowBlockPosition]
val previousVelocity = Vec3d(velocity)
landingBlockState?.block?.onEntityLand(connection, this, landingPosition, landingBlockState)
velocityChanged = velocity != previousVelocity
}
if (!velocityChanged) {
velocity.y = 0.0
}
}
if (!velocityChanged) {
if (movement.x != collisionMovement.x) {
velocity.x = 0.0
}
if (movement.y != collisionMovement.y) {
velocity.y = 0.0
}
if (movement.z != collisionMovement.z) {
velocity.z = 0.0
}
}
forceMove(collisionMovement)
if (onGround && canStep) {
// ToDo: Play step sound

View File

@ -52,6 +52,7 @@ object BlockEntityFixer {
"Airportal" to "minecraft:end_portal",
"EndGateway" to "minecraft:end_gateway",
"Banner" to "minecraft:shield",
"minecraft:noteblock" to "minecraft:note_block"
).asResourceLocationMap()