If it ever happens, avoid dividing by zero

This commit is contained in:
DBotThePony 2023-07-12 14:41:40 +07:00
parent 37974d40b9
commit 6105ca797b
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -362,9 +362,15 @@ fun IMatteryEnergyStorage.transferChecked(other: IMatteryEnergyStorage, amount:
} }
fun IMatteryEnergyStorage.getBarWidth(): Int { fun IMatteryEnergyStorage.getBarWidth(): Int {
return ((batteryLevel / maxBatteryLevel).toFloat().coerceAtLeast(0f).coerceAtMost(1f) * 13f).roundToInt() if (!maxBatteryLevel.isPositive)
return 0
return ((batteryLevel / maxBatteryLevel).toFloat().coerceIn(0f, 1f) * 13f).roundToInt()
} }
fun IMatteryEnergyStorage.getBarColor(): Int { fun IMatteryEnergyStorage.getBarColor(): Int {
if (!maxBatteryLevel.isPositive)
return 0
return RGBAColor.LOW_POWER.linearInterpolation((batteryLevel / maxBatteryLevel).toFloat(), RGBAColor.FULL_POWER).toRGB() return RGBAColor.LOW_POWER.linearInterpolation((batteryLevel / maxBatteryLevel).toFloat(), RGBAColor.FULL_POWER).toRGB()
} }