Template: Wheel Of Fortune¶
The Wheel of Fortune mechanic in games involves a spinning wheel divided into different sections, each representing a prize, reward, or action. Players spin the wheel, and the section the wheel lands on determines what the player receives or has to do next. This mechanic is often used in games as a way to distribute random rewards and can add an element of luck and excitement to the gameplay.
Wheels¶
The Wheel Of Fortune template allows you to create multiple configs. You can provide different rules for each of the configs. The configs are called Documents in Balancy. One of the parameters in Wheel of Fortune is Condition. We are going to use it to give different configs for players, depending on their level, but you can come up with any other segmentation.
Each document also has a parameter Drop, where you can specify the content of the wheel with all the chances. We are using Weight instead of chance because it's easier to work with. Meanwhile, engineers can always normalize weights to convert them to the chances. This is exactly what we are going to do in the code below.
Section for programmers¶
Extension for WheelOfFortune¶
using Balancy.Models.SmartObjects;
namespace Balancy.Models
{
public static class WheelOfFortuneExtension
{
public static ItemWithAmount DropRandomItem(this WheelOfFortune wheel)
{
var allWeight = wheel.CalculateTotalDropWeight();
var random = Balancy.Random.Range(0, allWeight);
foreach (var dropInfo in wheel.Drop)
{
if (random < dropInfo.Weight)
return dropInfo.Item;
random -= dropInfo.Weight;
}
return null;
}
private static int CalculateTotalDropWeight(this WheelOfFortune wheel)
{
var allWeight = 0;
foreach (var dropInfo in wheel.Drop)
allWeight += dropInfo.Weight;
return allWeight;
}
}
}
Example¶
using Balancy;
using Balancy.Models;
using Balancy.Models.SmartObjects;
public class WheelExample
{
public WheelOfFortune GetActiveWheel()
{
return DataEditor.WheelOfFortune.Get();
}
public ItemWithAmount Spin()
{
return GetActiveWheel()?.DropRandomItem();
}
}
What's next¶
- Import the Template.
- Deploy the changes.
- Open the Unity project and generate the code.
- Add the files to your project, written above.
- Initialize Balancy.
- Add the following code in the callback.
var wheel = new WheelExample();
for (int i = 0; i < 10; i++)
{
var reward = wheel.Spin();
Debug.Log($"{i}) REWARD = {wheel.GetActiveWheel()?.Name} : {reward?.Item.Name} = {reward?.Count}");
}