Skip to content

Commit 0874d66

Browse files
V0.0.4 - Randomizer Utilities, Build Handler and USliderComponent
- added SimpleBuildHandler from the UnscriptedLogicLib as a utility class - added the whole RandomLogic class from the UnscriptedLogicLib as a utility class - added USliderComponent for sliders
1 parent 4657c6d commit 0874d66

File tree

9 files changed

+421
-2
lines changed

9 files changed

+421
-2
lines changed

About.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
If you are reading this, you're probably currently digging through the source code of my game, and if you aren't hello anyways.
22

3-
Throughtout my journey as a game developer, I've found a few functions and workflows that are repeated over and over again. So I created UnscriptedLogicLib.dll, my personal library of utility functions. It's kinda like a repairman's toolbox. A whole script of functions that help me do what I wanna do but without having to remember how to do it.
3+
Throughout my journey as a game developer, I've found a few functions and workflows that are repeated over and over again. So I created UnscriptedLogicLib.dll, my personal library of utility functions. It's kinda like a repairman's toolbox. A whole script of functions that help me do what I wanna do but without having to remember how to do it.
44

55
Over time, I found that it wasn't really enough. It's just bits and pieces of code. Sure it helped me in a few game jams but it never really helped me in making bigger games. So I took it up a notch.
66

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@
66
#V0.0.2
77

88
- updated UI binding for better readability by converting the parameter type from 'object' to generic
9-
- shifted the Bindable class to the UObject so that any UnscriptedEngine class can use it
9+
- shifted the Bindable class to the UObject so that any UnscriptedEngine class can use it
10+
11+
#V0.0.3
12+
13+
- updated a few small bugs. Improved overall CanvasController and UI Binding flow
14+
15+
#V0.0.4
16+
17+
- added RandomLogic and SimpleBuildHandler from UnscriptedLogicLib.dll
18+
- added USliderComponent for sliders

UI/Components/USliderComponent.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using UnityEngine.Events;
2+
using UnityEngine.UI;
3+
using UnscriptedEngine;
4+
5+
public class USliderComponent : UUIComponent
6+
{
7+
private Slider slider;
8+
9+
public Slider Slider => slider;
10+
11+
public override void InitializeUIComponent(UCanvasController context)
12+
{
13+
base.InitializeUIComponent(context);
14+
15+
slider = GetComponent<Slider>();
16+
}
17+
18+
public override void OnBindedValueChanged<T>(T value)
19+
{
20+
if (float.TryParse(value.ToString(), out float result))
21+
{
22+
slider.value = result;
23+
}
24+
}
25+
}

UI/Components/USliderComponent.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Utilities.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Utilities/RandomLogic.cs

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace UnscriptedEngine
5+
{
6+
public static class RandomLogic
7+
{
8+
public static float BetFloats(float start = 0f, float end = 100f)
9+
{
10+
return Random.Range(start, end);
11+
}
12+
13+
public static float BetFloats(Vector2 range)
14+
{
15+
return Random.Range(range.x, range.y);
16+
}
17+
18+
public static int BetInts(int start = 0, int end = 100)
19+
{
20+
return Random.Range(start, end);
21+
}
22+
23+
public static int BetInts(Vector2Int range)
24+
{
25+
return Random.Range(range.x, range.y);
26+
}
27+
28+
public static float FloatZeroTo(float value)
29+
{
30+
return Random.Range(0f, value);
31+
}
32+
33+
public static int IntZeroTo(int value)
34+
{
35+
return Random.Range(0, value);
36+
}
37+
38+
public static T FromArray<T>(T[] list)
39+
{
40+
return list[IntZeroTo(list.Length)];
41+
}
42+
43+
public static T FromArray<T>(T[] list, out int index)
44+
{
45+
index = IntZeroTo(list.Length);
46+
return list[index];
47+
}
48+
49+
public static T FromList<T>(List<T> list)
50+
{
51+
return list[IntZeroTo(list.Count)];
52+
}
53+
54+
public static T FromList<T>(List<T> list, out int index)
55+
{
56+
index = IntZeroTo(list.Count);
57+
return list[index];
58+
}
59+
60+
public static Vector2 InArea2D(float x, float y)
61+
{
62+
return InArea2D(new Vector2(x, y));
63+
}
64+
65+
public static Vector2 InArea2D(Vector2 spawnArea)
66+
{
67+
float x = Random.Range((0f - spawnArea.x) / 2f, spawnArea.x / 2f);
68+
float y = Random.Range((0f - spawnArea.y) / 2f, spawnArea.y / 2f);
69+
return new Vector2(x, y);
70+
}
71+
72+
public static Vector3 InArea3D(float x, float y, float z)
73+
{
74+
return InArea3D(new Vector3(x, y, z));
75+
}
76+
77+
public static Vector3 InArea3D(Vector3 spawnArea)
78+
{
79+
float x = Random.Range((0f - spawnArea.x) / 2f, spawnArea.x / 2f);
80+
float y = Random.Range((0f - spawnArea.y) / 2f, spawnArea.y / 2f);
81+
float z = Random.Range((0f - spawnArea.z) / 2f, spawnArea.z / 2f);
82+
return new Vector3(x, y, z);
83+
}
84+
85+
public static Vector2Int InGrid(Vector2Int grid)
86+
{
87+
return InGrid(grid.x, grid.y);
88+
}
89+
90+
public static Vector2Int InGrid(int x, int y)
91+
{
92+
int x2 = IntZeroTo(x);
93+
int y2 = IntZeroTo(y);
94+
return new Vector2Int(x2, y2);
95+
}
96+
97+
public static Vector3Int InGrid3D(int x, int y, int z)
98+
{
99+
int x2 = IntZeroTo(x);
100+
int y2 = IntZeroTo(y);
101+
int z2 = IntZeroTo(z);
102+
return new Vector3Int(x2, y2, z2);
103+
}
104+
105+
public static Vector3 VectorDirAroundY()
106+
{
107+
return IntZeroTo(4) switch
108+
{
109+
0 => Vector3.forward,
110+
1 => Vector3.back,
111+
3 => Vector3.left,
112+
_ => Vector3.right,
113+
};
114+
}
115+
116+
public static Vector3 PointAtCircumferenceXZ(Vector3 center, float radius)
117+
{
118+
float f = FloatZeroTo(360f);
119+
float z = radius * Mathf.Sin(f);
120+
float x = radius * Mathf.Cos(f);
121+
return center + new Vector3(x, 0f, z);
122+
}
123+
124+
public static Vector3 VectorDirectionAny()
125+
{
126+
return IntZeroTo(6) switch
127+
{
128+
0 => Vector3.forward,
129+
1 => Vector3.back,
130+
3 => Vector3.left,
131+
4 => Vector3.right,
132+
5 => Vector3.up,
133+
_ => Vector3.down,
134+
};
135+
}
136+
137+
public static int RandomIndex<T>(T[] list, float[] chances)
138+
{
139+
float[] array = new float[list.Length];
140+
float num = 0f;
141+
for (int i = 0; i < list.Length; i++)
142+
{
143+
array[i] = num + chances[i];
144+
num = array[i];
145+
}
146+
147+
int num2 = Random.Range(0, 100);
148+
for (int j = 0; j < array.Length; j++)
149+
{
150+
float num3 = ((j == array.Length - 1) ? 100f : array[j]);
151+
float num4 = ((j == 0) ? 0f : array[j - 1]);
152+
if ((float)num2 > num4 && (float)num2 < num3)
153+
{
154+
return j;
155+
}
156+
}
157+
158+
return 0;
159+
}
160+
161+
public static void Randomize(this int value, int incluseiveMin = 0, int exclusiveMax = 1)
162+
{
163+
value = Random.Range(incluseiveMin, exclusiveMax);
164+
}
165+
166+
public static void Randomize(this int value, Vector2Int range)
167+
{
168+
value = Random.Range(range.x, range.y);
169+
}
170+
171+
public static void Randomize(this float value, float inclusiveMin = 0f, float exclusiveMax = 1f)
172+
{
173+
value = Random.Range(inclusiveMin, exclusiveMax);
174+
}
175+
176+
public static void Randomize(this float value, Vector2 range)
177+
{
178+
value = Random.Range(range.x, range.y);
179+
}
180+
181+
public static void Randomize(this Vector2 value, Vector2 minInclusive, Vector2 maxExclusive)
182+
{
183+
value.x.Randomize(minInclusive.x, maxExclusive.x);
184+
value.y.Randomize(minInclusive.y, maxExclusive.y);
185+
}
186+
187+
public static void Randomize(this Vector2Int value, Vector2Int minInclusive, Vector2Int maxExclusive)
188+
{
189+
value.x.Randomize(minInclusive.x, maxExclusive.x);
190+
value.y.Randomize(minInclusive.y, maxExclusive.y);
191+
}
192+
193+
public static void Randomize(this Vector3 value, Vector3 minInclusive, Vector3 maxExclusive)
194+
{
195+
value.x.Randomize(minInclusive.x, maxExclusive.x);
196+
value.y.Randomize(minInclusive.y, maxExclusive.y);
197+
value.z.Randomize(minInclusive.z, maxExclusive.z);
198+
}
199+
200+
public static void Randomize(this Vector3Int value, Vector3Int minInclusive, Vector3Int maxExclusive)
201+
{
202+
value.x.Randomize(minInclusive.x, maxExclusive.x);
203+
value.y.Randomize(minInclusive.y, maxExclusive.y);
204+
value.z.Randomize(minInclusive.z, maxExclusive.z);
205+
}
206+
207+
public static T GetRandomElement<T>(this T[] value)
208+
{
209+
return FromArray(value);
210+
}
211+
212+
public static T GetRandomElement<T>(this List<T> value)
213+
{
214+
return FromList(value);
215+
}
216+
}
217+
}

Utilities/RandomLogic.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)