2019-08-09 01:44:59 +00:00
|
|
|
package net.shadowfacts.asmr.program.blocks.math
|
|
|
|
|
2019-08-10 02:39:01 +00:00
|
|
|
import net.minecraft.util.Identifier
|
|
|
|
import net.shadowfacts.asmr.ASMR
|
2019-08-11 22:53:02 +00:00
|
|
|
import net.shadowfacts.asmr.program.*
|
2019-08-12 00:05:09 +00:00
|
|
|
import net.shadowfacts.asmr.program.execution.SimpleExecutableBlock
|
2019-08-09 01:44:59 +00:00
|
|
|
import java.lang.RuntimeException
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
class BinaryOperatorBlock<Type: Any>(
|
|
|
|
val type: ProgramType<Type>,
|
|
|
|
val operation: Operation
|
2019-08-12 00:05:09 +00:00
|
|
|
): SimpleExecutableBlock(
|
2019-08-10 23:21:26 +00:00
|
|
|
Identifier(ASMR.modid, "binary_operator")
|
|
|
|
) {
|
2019-08-09 01:44:59 +00:00
|
|
|
|
|
|
|
init {
|
2019-08-09 02:44:19 +00:00
|
|
|
if (type != ProgramType.INT && type != ProgramType.FLOAT) {
|
2019-08-09 01:44:59 +00:00
|
|
|
throw RuntimeException("BinaryOperatorBlock type must be int or float")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 23:21:26 +00:00
|
|
|
val left = ProgramBlockInput(Identifier(ASMR.modid, "binary_operator.left_operand"), type, this)
|
|
|
|
val right = ProgramBlockInput(Identifier(ASMR.modid, "binary_operator.right_operand"), type, this)
|
2019-08-09 01:44:59 +00:00
|
|
|
|
2019-08-10 23:21:26 +00:00
|
|
|
val output = ProgramBlockOutput(Identifier(ASMR.modid, "binary_operator.result"), type, this)
|
2019-08-09 01:44:59 +00:00
|
|
|
|
|
|
|
override val inputs: Array<ProgramBlockInput<*>> = arrayOf(left, right)
|
|
|
|
override val outputs: Array<ProgramBlockOutput<*>> = arrayOf(output)
|
|
|
|
|
|
|
|
override fun execute() {
|
2019-08-09 02:44:19 +00:00
|
|
|
if (type == ProgramType.INT) {
|
2019-08-09 01:44:59 +00:00
|
|
|
val left = left.value as Int
|
|
|
|
val right = right.value as Int
|
|
|
|
output.value = when (operation) {
|
2019-08-09 02:44:19 +00:00
|
|
|
Operation.ADD -> left + right
|
|
|
|
Operation.SUBTRACT -> left - right
|
|
|
|
Operation.MULTIPLY -> left * right
|
|
|
|
Operation.DIVIDE -> left / right
|
2019-08-09 01:44:59 +00:00
|
|
|
} as Type
|
2019-08-09 02:44:19 +00:00
|
|
|
} else if (type == ProgramType.FLOAT) {
|
2019-08-09 01:44:59 +00:00
|
|
|
val left = left.value as Float
|
|
|
|
val right = right.value as Float
|
|
|
|
output.value = when (operation) {
|
2019-08-09 02:44:19 +00:00
|
|
|
Operation.ADD -> left + right
|
|
|
|
Operation.SUBTRACT -> left - right
|
|
|
|
Operation.MULTIPLY -> left * right
|
|
|
|
Operation.DIVIDE -> left / right
|
2019-08-09 01:44:59 +00:00
|
|
|
} as Type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class Operation {
|
2019-08-09 02:44:19 +00:00
|
|
|
ADD, SUBTRACT, MULTIPLY, DIVIDE
|
2019-08-09 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|