Wave System - nickc01/Corrupted-Kin GitHub Wiki
Wave System
This page will go over how the Wave System from the Corrupted Kin mod works, and the individual components that make it up. If you want to see all the relevant code that makes it up, look here: Wave System Code Folder
How it's generated (Math Warning)
The basic idea for how the wave is created is that it's comprised of a series of points, referred to as Wave Points. In the example below, we have a set of 25 points, with each point connected with a line. The left-most point is at (-6,0) and the right-most point is at (6,0):
So far doesn't look very interesting, but what we can do is control the height of each of the points. This can be done using a function, such as the function "f(x) = abs(x)". For the first point, which has an x of -6, passing -6 into this function will result in 6. Thus, the y value (the height) of the first point will be 6. For the second point, which has an x of -5.5, this function will produce a y value of 5.5. If this is done for all of the points, we get something like this:
So in essence, applying a function over a series of points allows us to create some interesting shapes. What if instead of using "f(x) = abs(x)", we try "f(x) = sin(x)" instead? The shape we get is this:
Looks alot like a wave doesn't it? But there's one more piece to this puzzle, and that is to make it move. Doing this is pretty simple, we just add another variable to the function: "f(x) = sin(x + t)".
What's special about "t" here is that it changes over time. It increases by 1 every second. Now if we look at the wave, this happens:
Nice! We can even have multiple functions by adding them together, creating even wilder effects. Here's the example "f(x) = sin(x) + abs(x/10)"
All of this is essentially how the waves are created. You can view the example graph here
Generators
Generators are what generate the waves, just like the functions in the example graph. There are several different kinds of generators that are used during the Corrupted Kin fight:
Ambient Wave
This wave creates the default ambient motion that is always active throughout the fight:
Left and Right Slam Waves
These are massive waves that occur when Corrupted Kin slams into the ground or when a bomb lands on the ground. The Left and Right waves are separate generators, but in this fight, they are both used at the same time to create two large waves that go in opposite directions.
Burrow Wave
The burrow wave is a special wave that is only used during the move when the boss burrows under the ground and jumps out of the infection ocean
Every frame, the Wave System will loop over all the wave points and recalculate their height values. This calculation occurs in the CalculateWave function:
//Used for calculating the height of a wave at a certain point
float CalculateWave(float x)
{
//Converts the x value from a range of (-WaveWidth/2 : +WaveWidth/2) to a range of (0 : WaveWidth)
x += WaveWidth / 2f;
float value = 0f;
//Loop over all the wave generators and calculate the height of a wave at the particular x position
for (int i = 0; i < generators.Count; i++)
{
value = generators[i].Calculate(x, value);
}
return value;
}
For each wave point, it will loop over all of the currently active wave generators, and use them to calculate the new height for the particular point. It uses all the generators so that they can interact with each other. The generators can customize how they interact, and in Corrupted Kin's case, most of them simply add together. That way, when two waves come crashing into each other, they sum up to form an even taller wave.