Yayas Documentation

↳ Calculatables ?

Basics

Calculatables are a Math Formula used to calculate a floating point number at runtime.
Their main application, is having dynamic values defined in Json Files.
They Support Addition, Subtraction, Multiplication and Division. You can also use Brackets for more complex formulas.
You can also put in integral or floating point numbers, hex strings or number strings; in these cases it'll just output the same number when performing the calculation.

    input    => output

69           => 69
"0xfaaaab"   => 0xfaaaab
"16+4"       => 20
"10-5*(1+2)" => -5

Variables

What makes them really useful, are Variables. Variables are replaced by their held value before calculating the Formula.
If a Variable isn't provided, it'll be replaced by 0.
Currently there isn't a way to set variables in json. The available Variables are hardcoded and differ between Calculatables.

example1 = 5
example2 = 3.5

           input           =>    processed   => output

"example1"                 => "5"            => 5
"4+example1*(10-example2)" => "4+5*(10-3.5)" => 58.5

Functions

Another very useful features, are Functions. If simple Math isn't enough for you, you can use the following Functions to spice things up as well:
min(a, b)
Returns the lower of the 2 given Numbers. max(a, b)
Returns the greater of the 2 given Numbers. clamp(value, min, max)
Clamps the given value so it fits in the given bounds. clamp(value, min, max)
Clamps the given value so it fits in the given bounds. rand()
Returns a random number on a Scale of 0-1. rand(max)
Returns a random number on a Scale of 0-max. rand(min, max)
Returns a random number between the given bounds. randb()
Returns either 0 or 1 randomly (50//50 chance). randb(chance)
Returns either 0 or 1 based on the given chance (on a scale from 0-1). The higher chance, the more likely the output is to be 1. pow(a, b)
Returns a to the power of b.
All of the Arguments are calculatable as well of course; so you can nest functions and brackets whatever way you want!

     input     => output

"min(420, 69)" => 69
"pow(4, 2)"    => 16
"rand(5)"      => i dunno, something between 0-5

no idea why I thought a complex example like this would be good
"clamp(69 * (1 - pow(0.9, 2)), 1, 20 / 2)"
↳ "clamp(69 * (0.19), 1, 10)"
↳ "clamp(13.11, 1, 10)"
↳ 10

For Developers

Calculating a Calculatable:

Calculatable example = new Calculatable("var*2");

//no variables supplied; so the calculatable turns into "0*2"
System.out.println(example.calculate(Map.of()));

Map<String, Float> vars = Map.of("var", 6);
//var == 6; so the calculatable turns into "6*2"
System.out.println(example.calculate(vars));

--- Output ---
=> 0
=> 12

Calculatable Range

A Calculatable Range consists of 2 Calculatables which represent the lower and upper bound.
It is defined in a Json Object with following fields:
min (Required Calculatable)
Lower Bound Calculatable. max (Required Calculatable)
Upper Bound Calculatable.

For Developers

CalculatableRange$calculateRange(Map<String, Float>) returns a Vector2f with x being the lower and y the upper bound.
CalculatableRange$getRandomValue(Map<String, Float>, Random) to get a random float within the ranges bounds.

Calculatable Color

A Calculatable Color consists of 3-4 Calculatables which represent the color channels rgba. Each color channel ranges from 0-1.
It is defined in a Json Array. If only 3 Calculatables are given, alpha will default to 1.

For Developers

CalculatableColor$getColor(Map<String, Float>) returns an int representing the color.