Update black hole code structure
This commit is contained in:
parent
4cd0c4c555
commit
2298dd17c7
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.mc.otm.block.entity.blackhole
|
package ru.dbotthepony.mc.otm.block.entity.blackhole
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.objects.ObjectArraySet
|
||||||
import net.minecraft.client.Minecraft
|
import net.minecraft.client.Minecraft
|
||||||
import net.minecraft.core.BlockPos
|
import net.minecraft.core.BlockPos
|
||||||
import net.minecraft.nbt.CompoundTag
|
import net.minecraft.nbt.CompoundTag
|
||||||
@ -28,7 +29,6 @@ import ru.dbotthepony.mc.otm.core.math.Decimal
|
|||||||
import ru.dbotthepony.mc.otm.core.math.plus
|
import ru.dbotthepony.mc.otm.core.math.plus
|
||||||
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
||||||
import ru.dbotthepony.mc.otm.registry.MItems
|
import ru.dbotthepony.mc.otm.registry.MItems
|
||||||
import ru.dbotthepony.mc.otm.registry.MRegistry
|
|
||||||
import ru.dbotthepony.mc.otm.core.math.getSphericalBlockPositions
|
import ru.dbotthepony.mc.otm.core.math.getSphericalBlockPositions
|
||||||
import ru.dbotthepony.mc.otm.core.nbt.map
|
import ru.dbotthepony.mc.otm.core.nbt.map
|
||||||
import ru.dbotthepony.mc.otm.core.nbt.set
|
import ru.dbotthepony.mc.otm.core.nbt.set
|
||||||
@ -36,13 +36,17 @@ import ru.dbotthepony.mc.otm.matter.MatterManager
|
|||||||
import ru.dbotthepony.mc.otm.registry.MDamageTypes
|
import ru.dbotthepony.mc.otm.registry.MDamageTypes
|
||||||
import ru.dbotthepony.mc.otm.registry.MatteryDamageSource
|
import ru.dbotthepony.mc.otm.registry.MatteryDamageSource
|
||||||
import ru.dbotthepony.mc.otm.triggers.BlackHoleTrigger
|
import ru.dbotthepony.mc.otm.triggers.BlackHoleTrigger
|
||||||
import java.util.LinkedList
|
|
||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
import kotlin.math.sqrt
|
import kotlin.math.sqrt
|
||||||
|
|
||||||
class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : MatteryBlockEntity(MBlockEntities.BLACK_HOLE, p_155229_, p_155230_) {
|
class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : MatteryBlockEntity(MBlockEntities.BLACK_HOLE, p_155229_, p_155230_) {
|
||||||
var mass by synchronizer.fraction(BASELINE_MASS, setter = setter@{ mass, field, setByRemote ->
|
var mass by synchronizer.fraction(BASELINE_MASS, setter = setter@{ mass, field, setByRemote ->
|
||||||
|
if (setByRemote) {
|
||||||
|
field.write(mass)
|
||||||
|
return@setter
|
||||||
|
}
|
||||||
|
|
||||||
if (mass <= Decimal.ZERO) {
|
if (mass <= Decimal.ZERO) {
|
||||||
collapse()
|
collapse()
|
||||||
return@setter
|
return@setter
|
||||||
@ -50,77 +54,41 @@ class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mattery
|
|||||||
|
|
||||||
field.write(mass)
|
field.write(mass)
|
||||||
setChanged()
|
setChanged()
|
||||||
var massForDiv = mass
|
updateGravStrength()
|
||||||
|
|
||||||
when (if (level?.isClientSide == false) stabilizers.size else stabilizerClientCount) {
|
|
||||||
1 -> {massForDiv /= 4}
|
|
||||||
2 -> {massForDiv /= 16}
|
|
||||||
3 -> {massForDiv /= 64}
|
|
||||||
4 -> {massForDiv /= 512}
|
|
||||||
5 -> {massForDiv /= 2048}
|
|
||||||
6 -> {massForDiv /= 16384}
|
|
||||||
}
|
|
||||||
|
|
||||||
gravitationStrength = sqrt(massForDiv.div(BASELINE_MASS).toDouble()).coerceIn(0.2, 40.0)
|
|
||||||
|
|
||||||
affectedBounds = BoundingBox(
|
|
||||||
(-30 * gravitationStrength).toInt(),
|
|
||||||
(-30 * gravitationStrength).toInt(),
|
|
||||||
(-30 * gravitationStrength).toInt(),
|
|
||||||
(30 * gravitationStrength).toInt(),
|
|
||||||
(30 * gravitationStrength).toInt(),
|
|
||||||
(30 * gravitationStrength).toInt()
|
|
||||||
).moved(
|
|
||||||
blockPos.x, blockPos.y, blockPos.z
|
|
||||||
)
|
|
||||||
|
|
||||||
affectedBoundsAABB = AABB.of(affectedBounds)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
override fun getRenderBoundingBox(): AABB {
|
var gravitationStrength by synchronizer.double(1.0).property
|
||||||
return AABB(blockPos.offset(-GravitationStabilizerBlockEntity.RANGE, -GravitationStabilizerBlockEntity.RANGE, -GravitationStabilizerBlockEntity.RANGE), blockPos.offset(
|
private set
|
||||||
GravitationStabilizerBlockEntity.RANGE, GravitationStabilizerBlockEntity.RANGE, GravitationStabilizerBlockEntity.RANGE))
|
var affectedBounds = BoundingBox(0, 0, 0, 1, 1, 1)
|
||||||
}
|
private set
|
||||||
|
var affectedBoundsAABB: AABB = AABB.of(affectedBounds)
|
||||||
var gravitationStrength = 1.0
|
|
||||||
private set
|
private set
|
||||||
|
|
||||||
private var suppressUpdates = true
|
var spinDirection by synchronizer.bool().property
|
||||||
var spinDirection = false
|
|
||||||
|
|
||||||
|
private var lastSphereSizeOuter = 0
|
||||||
|
private var sphereIndexOuter = 0
|
||||||
private var sleepTicks = 4
|
private var sleepTicks = 4
|
||||||
|
private val stabilizers = ObjectArraySet<GravitationStabilizerBlockEntity>(6)
|
||||||
|
|
||||||
|
init {
|
||||||
|
updateGravStrength()
|
||||||
|
}
|
||||||
|
|
||||||
override fun setLevel(level: Level) {
|
override fun setLevel(level: Level) {
|
||||||
super.setLevel(level)
|
super.setLevel(level)
|
||||||
sleepTicks = 4
|
sleepTicks = 4
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateMass() {
|
|
||||||
mass = mass
|
|
||||||
}
|
|
||||||
|
|
||||||
private val stabilizers = LinkedList<GravitationStabilizerBlockEntity>()
|
|
||||||
private var stabilizerClientCount by synchronizer.int(setter = setter@{ value, field, setByRemote ->
|
|
||||||
field.write(value)
|
|
||||||
|
|
||||||
if (setByRemote) {
|
|
||||||
updateMass()
|
|
||||||
}
|
|
||||||
}).property
|
|
||||||
|
|
||||||
fun stabilizerAttached(stabilizer: GravitationStabilizerBlockEntity) {
|
fun stabilizerAttached(stabilizer: GravitationStabilizerBlockEntity) {
|
||||||
if (stabilizer in stabilizers)
|
if (stabilizers.add(stabilizer)) {
|
||||||
return
|
updateGravStrength()
|
||||||
|
}
|
||||||
stabilizers.add(stabilizer)
|
|
||||||
mass = mass
|
|
||||||
stabilizerClientCount = stabilizers.size
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stabilizerDetached(stabilizer: GravitationStabilizerBlockEntity) {
|
fun stabilizerDetached(stabilizer: GravitationStabilizerBlockEntity) {
|
||||||
if (stabilizers.remove(stabilizer)) {
|
if (stabilizers.remove(stabilizer)) {
|
||||||
mass = mass
|
updateGravStrength()
|
||||||
stabilizerClientCount = stabilizers.size
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,6 +129,39 @@ class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mattery
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateGravStrength() {
|
||||||
|
var massForDiv = mass
|
||||||
|
|
||||||
|
when (stabilizers.size) {
|
||||||
|
0 -> {}
|
||||||
|
1 -> massForDiv /= 4
|
||||||
|
2 -> massForDiv /= 16
|
||||||
|
3 -> massForDiv /= 64
|
||||||
|
4 -> massForDiv /= 512
|
||||||
|
5 -> massForDiv /= 2048
|
||||||
|
else -> massForDiv /= 16384
|
||||||
|
}
|
||||||
|
|
||||||
|
gravitationStrength = sqrt(massForDiv.div(BASELINE_MASS).toDouble()).coerceIn(0.2, 40.0)
|
||||||
|
affectedBounds = BoundingBox(
|
||||||
|
(-30 * gravitationStrength).toInt(),
|
||||||
|
(-30 * gravitationStrength).toInt(),
|
||||||
|
(-30 * gravitationStrength).toInt(),
|
||||||
|
(30 * gravitationStrength).toInt(),
|
||||||
|
(30 * gravitationStrength).toInt(),
|
||||||
|
(30 * gravitationStrength).toInt()
|
||||||
|
).moved(
|
||||||
|
blockPos.x, blockPos.y, blockPos.z
|
||||||
|
)
|
||||||
|
|
||||||
|
affectedBoundsAABB = AABB.of(affectedBounds)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getRenderBoundingBox(): AABB {
|
||||||
|
return AABB(blockPos.offset(-GravitationStabilizerBlockEntity.RANGE, -GravitationStabilizerBlockEntity.RANGE, -GravitationStabilizerBlockEntity.RANGE), blockPos.offset(
|
||||||
|
GravitationStabilizerBlockEntity.RANGE, GravitationStabilizerBlockEntity.RANGE, GravitationStabilizerBlockEntity.RANGE))
|
||||||
|
}
|
||||||
|
|
||||||
override fun saveLevel(nbt: CompoundTag) {
|
override fun saveLevel(nbt: CompoundTag) {
|
||||||
super.saveLevel(nbt)
|
super.saveLevel(nbt)
|
||||||
nbt["mass"] = mass.serializeNBT()
|
nbt["mass"] = mass.serializeNBT()
|
||||||
@ -173,11 +174,6 @@ class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mattery
|
|||||||
spinDirection = nbt.getBoolean("spin_direction")
|
spinDirection = nbt.getBoolean("spin_direction")
|
||||||
}
|
}
|
||||||
|
|
||||||
var affectedBounds = BoundingBox(0, 0, 0, 1, 1, 1)
|
|
||||||
private set
|
|
||||||
var affectedBoundsAABB: AABB = AABB.of(affectedBounds)
|
|
||||||
private set
|
|
||||||
|
|
||||||
private fun setDeltaMovement(living: Entity, center: Vec3, distance: Double, weaker: Boolean) {
|
private fun setDeltaMovement(living: Entity, center: Vec3, distance: Double, weaker: Boolean) {
|
||||||
//final double mult = Math.min(2, (30 * this.gravitation_strength) / Math.max(1, Math.pow(distance, 2)));
|
//final double mult = Math.min(2, (30 * this.gravitation_strength) / Math.max(1, Math.pow(distance, 2)));
|
||||||
// Сила притяжения
|
// Сила притяжения
|
||||||
@ -204,9 +200,8 @@ class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mattery
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun clientTick() {
|
fun clientTick() {
|
||||||
sleepTicks--
|
if (--sleepTicks > 0) return
|
||||||
if (sleepTicks > 0) return
|
val ply = Minecraft.getInstance().player ?: return
|
||||||
val ply = Minecraft.getInstance().player!!
|
|
||||||
val center = Vec3.atCenterOf(blockPos)
|
val center = Vec3.atCenterOf(blockPos)
|
||||||
|
|
||||||
if (!ply.abilities.mayfly && ply.getItemBySlot(EquipmentSlot.CHEST).item != MItems.PORTABLE_GRAVITATION_STABILIZER) {
|
if (!ply.abilities.mayfly && ply.getItemBySlot(EquipmentSlot.CHEST).item != MItems.PORTABLE_GRAVITATION_STABILIZER) {
|
||||||
@ -222,10 +217,8 @@ class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mattery
|
|||||||
|
|
||||||
override fun tick() {
|
override fun tick() {
|
||||||
super.tick()
|
super.tick()
|
||||||
sleepTicks--
|
if (--sleepTicks > 0) return
|
||||||
if (sleepTicks > 0) return
|
|
||||||
val level = level as? ServerLevel ?: return
|
val level = level as? ServerLevel ?: return
|
||||||
suppressUpdates = false
|
|
||||||
|
|
||||||
val center = Vec3.atCenterOf(blockPos)
|
val center = Vec3.atCenterOf(blockPos)
|
||||||
|
|
||||||
@ -311,13 +304,6 @@ class BlackHoleBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mattery
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var lastSphereSizeOuter = 0
|
|
||||||
private var sphereIndexOuter = 0
|
|
||||||
|
|
||||||
init {
|
|
||||||
mass = mass
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val ITERATIONS = 30_000
|
const val ITERATIONS = 30_000
|
||||||
val BASELINE_MASS = Decimal(50_000)
|
val BASELINE_MASS = Decimal(50_000)
|
||||||
|
Loading…
Reference in New Issue
Block a user