1
0
mirror of https://github.com/chylex/Hardcore-Ender-Expansion-2.git synced 2025-04-14 12:15:44 +02:00

Refactor Energy Cluster particle & add base particle class for Energy transfers

This commit is contained in:
chylex 2019-01-19 03:00:36 +01:00
parent 1545f62ff3
commit 7bcb025bcb
4 changed files with 123 additions and 51 deletions

View File

@ -18,6 +18,7 @@ import chylex.hee.game.mechanics.energy.IEnergyQuantity.Internal
import chylex.hee.game.mechanics.energy.IEnergyQuantity.Units
import chylex.hee.game.mechanics.energy.ProximityHandler
import chylex.hee.game.particle.ParticleEnergyCluster
import chylex.hee.game.particle.base.ParticleBaseEnergy.ClusterParticleDataGenerator
import chylex.hee.game.particle.spawner.IParticleSpawner
import chylex.hee.game.particle.spawner.ParticleSpawnerCustom
import chylex.hee.game.particle.util.IOffset.InBox
@ -85,7 +86,7 @@ class TileEntityEnergyCluster : TileEntityBase(), ITickable{
val wasUsedRecently: Boolean
get() = world.totalWorldTime - lastUseTick < 20L
// Variables
// Fields
private var ticksToRegen = 20
private var lastUseTick = 0L
@ -96,6 +97,9 @@ class TileEntityEnergyCluster : TileEntityBase(), ITickable{
private var particle: Pair<IParticleSpawner, IShape>? = null
private val particleSkipTest = ParticleEnergyCluster.newCountingSkipTest()
var particleDataGenerator: ClusterParticleDataGenerator? = null
private set
var breakWithoutExplosion = false
// Interactions
@ -260,6 +264,8 @@ class TileEntityEnergyCluster : TileEntityBase(), ITickable{
}
}
else if (context == NETWORK){
particleDataGenerator = ClusterParticleDataGenerator(this@TileEntityEnergyCluster)
val particleSpawner = ParticleSpawnerCustom(
type = ParticleEnergyCluster,
data = ParticleEnergyCluster.Data(this@TileEntityEnergyCluster),

View File

@ -1,11 +1,5 @@
package chylex.hee.game.particle
import chylex.hee.game.block.entity.TileEntityEnergyCluster
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthOverride.POWERED
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthOverride.REVITALIZING
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthStatus.DAMAGED
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthStatus.TIRED
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthStatus.UNSTABLE
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthStatus.WEAKENED
import chylex.hee.game.particle.base.ParticleBaseEnergy
import chylex.hee.game.particle.spawner.factory.IParticleData
import chylex.hee.game.particle.spawner.factory.IParticleMaker
@ -13,8 +7,6 @@ import chylex.hee.game.particle.util.ParticleSetting
import chylex.hee.game.particle.util.ParticleSetting.ALL
import chylex.hee.game.particle.util.ParticleSetting.DECREASED
import chylex.hee.game.particle.util.ParticleSetting.MINIMAL
import chylex.hee.game.render.util.IColor
import chylex.hee.game.render.util.RGB
import chylex.hee.init.ModBlocks
import chylex.hee.system.util.Pos
import chylex.hee.system.util.floorToInt
@ -25,6 +17,7 @@ import net.minecraft.client.particle.Particle
import net.minecraft.world.World
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
import org.apache.commons.lang3.ArrayUtils.EMPTY_INT_ARRAY
import java.util.Random
object ParticleEnergyCluster : IParticleMaker{
@ -35,51 +28,13 @@ object ParticleEnergyCluster : IParticleMaker{
// Particle data
class Data(cluster: TileEntityEnergyCluster) : IParticleData{
private companion object{
private val COLOR_GRAY = adjustColorComponents(RGB(60u))
private fun adjustColorComponents(color: IColor): Int{
val rgb = color.toRGB()
return(rgb.red.coerceIn(64, 224) shl 16) or (rgb.green.coerceIn(64, 224) shl 8) or rgb.blue.coerceIn(64, 224)
}
}
private val level = cluster.energyLevel.floating.value
private val capacity = cluster.energyBaseCapacity.floating.value
private val health = cluster.currentHealth
private val colorPrimary = adjustColorComponents(cluster.color.primary(100F, 42F))
private val colorSecondary = adjustColorComponents(cluster.color.secondary(90F, 42F))
class Data(private val cluster: TileEntityEnergyCluster) : IParticleData{
override fun generate(rand: Random): IntArray{
val useSecondaryHue = when(health){
REVITALIZING, UNSTABLE -> true
POWERED -> rand.nextBoolean()
else -> rand.nextInt(4) == 0
}
val turnGray = useSecondaryHue && when(health){
WEAKENED -> rand.nextInt(100) < 25
TIRED -> rand.nextInt(100) < 75
DAMAGED, UNSTABLE -> true
else -> false
}
val finalColor = when{
turnGray -> COLOR_GRAY
useSecondaryHue -> colorSecondary
else -> colorPrimary
}
val finalScale = when(useSecondaryHue){
true -> (0.6F + (capacity * 0.07F) + (level * 0.008F)) * (if (health == POWERED) 1.6F else 1F)
false -> 0.5F + (capacity * 0.03F) + (level * 0.06F)
}
val data = cluster.particleDataGenerator?.next(rand) ?: return EMPTY_INT_ARRAY
return intArrayOf(
finalColor,
(finalScale * 100F).floorToInt()
data.color,
(data.scale * 100F).floorToInt()
)
}
}

View File

@ -1,5 +1,14 @@
package chylex.hee.game.particle.base
import chylex.hee.game.block.entity.TileEntityEnergyCluster
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthOverride.POWERED
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthOverride.REVITALIZING
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthStatus.DAMAGED
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthStatus.TIRED
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthStatus.UNSTABLE
import chylex.hee.game.mechanics.energy.IClusterHealth.HealthStatus.WEAKENED
import chylex.hee.game.render.util.GL
import chylex.hee.game.render.util.IColor
import chylex.hee.game.render.util.RGB
import chylex.hee.system.Resource
import net.minecraft.client.Minecraft
import net.minecraft.client.particle.Particle
@ -14,6 +23,7 @@ import net.minecraft.world.World
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
import org.lwjgl.opengl.GL11.GL_QUADS
import java.util.Random
@SideOnly(Side.CLIENT)
abstract class ParticleBaseEnergy(world: World, posX: Double, posY: Double, posZ: Double, motX: Double, motY: Double, motZ: Double) : ParticleBaseFloating(world, posX, posY, posZ, motX, motY, motZ){
@ -30,6 +40,52 @@ abstract class ParticleBaseEnergy(world: World, posX: Double, posY: Double, posZ
private val mc = Minecraft.getMinecraft()
private var lastInterpolationFixTime = 0L
private val COLOR_GRAY = adjustColorComponents(RGB(60u))
private fun adjustColorComponents(color: IColor): Int{
val rgb = color.toRGB()
return(rgb.red.coerceIn(64, 224) shl 16) or (rgb.green.coerceIn(64, 224) shl 8) or rgb.blue.coerceIn(64, 224)
}
}
data class ClusterParticleData(val color: Int, val scale: Float)
class ClusterParticleDataGenerator(cluster: TileEntityEnergyCluster){
private val level = cluster.energyLevel.floating.value
private val capacity = cluster.energyBaseCapacity.floating.value
private val health = cluster.currentHealth
private val colorPrimary = adjustColorComponents(cluster.color.primary(100F, 42F))
private val colorSecondary = adjustColorComponents(cluster.color.secondary(90F, 42F))
fun next(rand: Random): ClusterParticleData{
val useSecondaryHue = when(health){
REVITALIZING, UNSTABLE -> true
POWERED -> rand.nextBoolean()
else -> rand.nextInt(4) == 0
}
val turnGray = useSecondaryHue && when(health){
WEAKENED -> rand.nextInt(100) < 25
TIRED -> rand.nextInt(100) < 75
DAMAGED, UNSTABLE -> true
else -> false
}
val finalColor = when{
turnGray -> COLOR_GRAY
useSecondaryHue -> colorSecondary
else -> colorPrimary
}
val finalScale = when(useSecondaryHue){
true -> (0.6F + (capacity * 0.07F) + (level * 0.008F)) * (if (health == POWERED) 1.6F else 1F)
false -> 0.5F + (capacity * 0.03F) + (level * 0.06F)
}
return ClusterParticleData(color = finalColor, scale = finalScale)
}
}
init{

View File

@ -0,0 +1,55 @@
package chylex.hee.game.particle.base
import net.minecraft.util.math.Vec3d
import net.minecraft.world.World
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
@SideOnly(Side.CLIENT)
abstract class ParticleBaseEnergyTransfer(world: World, posX: Double, posY: Double, posZ: Double) : ParticleBaseEnergy(world, posX, posY, posZ, 0.0, 0.0, 0.0){
protected abstract val targetPos: Vec3d
init{
particleMaxAge = 200
}
protected fun loadColor(color: Int){
particleRed = ((color shr 16) and 255) / 255F
particleGreen = ((color shr 8) and 255) / 255F
particleBlue = (color and 255) / 255F
}
protected fun setupMotion(speed: Double){
val motion = targetPos.subtract(posX, posY, posZ).normalize().scale(speed)
motionX = motion.x
motionY = motion.y
motionZ = motion.z
}
override fun onUpdate(){
val prevMotX = motionX
val prevMotY = motionY
val prevMotZ = motionZ
if (targetPos.squareDistanceTo(posX, posY, posZ) <= Vec3d(motionX, motionY, motionZ).lengthSquared()){
setExpired()
}
super.onUpdate()
if (isExpired){
return
}
if (targetPos.squareDistanceTo(posX, posY, posZ) >= targetPos.squareDistanceTo(prevPosX, prevPosY, prevPosZ)){
posX = prevPosX
posY = prevPosY
posZ = prevPosZ
setExpired()
}
else{
motionX = prevMotX
motionY = prevMotY
motionZ = prevMotZ
}
}
}