Skip to content

Commit 769d368

Browse files
committed
Create StandAloneCharacterPointSystem.cs
1 parent 284f18b commit 769d368

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

StandAloneCharacterPointSystem.cs

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// This code is part of the Fungus library (https://github.com/snozbot/fungus)
2+
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
3+
4+
using UnityEngine;
5+
using UnityEngine.UI;
6+
using System.Collections;
7+
8+
namespace Fungus
9+
{
10+
/// <summary>
11+
/// Loads a saved value and stores it in a Boolean, Integer, Float or String variable. If the key is not found then the variable is not modified.
12+
/// </summary>
13+
[CommandInfo("Variable",
14+
"Standalone Character's Point System",
15+
"Point/affinity system like in HP, MP or point systems for characters in visual novel. IMPORTANT: Use On-Update only when this command works as a standalone/in disconnected block ")]
16+
[AddComponentMenu("")]
17+
public class StandAloneCharacterPointSystem : Command
18+
{
19+
[HideInInspector] protected string key = "";
20+
[Tooltip("Variable to store the value in.")]
21+
[VariableProperty(typeof(IntegerVariable),
22+
typeof(FloatVariable))]
23+
[SerializeField] protected Variable variable;
24+
[SerializeField] protected float maxValueOf;
25+
[SerializeField] protected Slider slider;
26+
[SerializeField] protected bool useTextUI = false;
27+
[Tooltip("If Text component is used, Slider component will be ignored")]
28+
[SerializeField] protected Text textComponent;
29+
[Header("Realtime Update. MUST be standalone/disconnected block")]
30+
[Tooltip("Use this only when this command works as a standalone(separate block/disconnected block)")]
31+
[SerializeField] protected bool onUpdate = true;
32+
33+
//Slight delay
34+
float time = 0.1f;
35+
36+
void Update()
37+
{
38+
if(onUpdate)
39+
{
40+
if (time >= 0)
41+
{
42+
time -= Time.deltaTime;
43+
return;
44+
}
45+
else
46+
{
47+
GetCharacterPoint();
48+
}
49+
}
50+
}
51+
52+
public void GetCharacterPoint()
53+
{
54+
if(variable != null)
55+
{
56+
key = variable.name;
57+
if (key == "" ||
58+
variable == null)
59+
{
60+
Continue();
61+
return;
62+
}
63+
var flowchart = GetFlowchart();
64+
65+
// Prepend the current save profile (if any)
66+
string prefsKey = SetSaveProfile.SaveProfile + "_" + flowchart.SubstituteVariables(key);
67+
System.Type variableType = variable.GetType();
68+
69+
if (variableType == typeof(IntegerVariable))
70+
{
71+
72+
IntegerVariable integerVariable = variable as IntegerVariable;
73+
if(textComponent != null && useTextUI == true)
74+
{
75+
textComponent.text = integerVariable.Value.ToString();
76+
}
77+
if(slider != null && useTextUI == false)
78+
{
79+
slider.maxValue = maxValueOf;
80+
slider.wholeNumbers = true;
81+
//slider.value = (float)integerVariable.Value;
82+
//Debug.Log(integerVariable.Value);
83+
var tmpfinttofloat = slider.GetComponent<Slider>().value;
84+
//Debug.Log(tmpfinttofloat);
85+
//float animfloat =
86+
slider.value = (float)integerVariable.Value;
87+
88+
}
89+
}
90+
else if (variableType == typeof(FloatVariable))
91+
{
92+
FloatVariable floatVariable = variable as FloatVariable;
93+
if(textComponent != null && useTextUI == true)
94+
{
95+
textComponent.text = floatVariable.Value.ToString();
96+
}
97+
if(slider != null && useTextUI == false)
98+
{
99+
slider.maxValue = maxValueOf;
100+
slider.wholeNumbers = true;
101+
var tmpfinttofloat = slider.GetComponent<Slider>().value;
102+
slider.value = floatVariable.Value;
103+
104+
}
105+
}
106+
}
107+
}
108+
109+
#region Public members
110+
public override void OnEnter()
111+
{
112+
if(!onUpdate)
113+
{
114+
GetCharacterPoint();
115+
}
116+
Continue();
117+
}
118+
public override string GetSummary()
119+
{
120+
if (variable == null)
121+
{
122+
return "Error: No variable selected";
123+
}
124+
if(textComponent == null && textComponent == true)
125+
{
126+
return "Error: No Text component available";
127+
}
128+
if(slider == null && textComponent == false)
129+
{
130+
return "Error: No Slider component available";
131+
}
132+
return variable.Key;
133+
}
134+
135+
public override Color GetButtonColor()
136+
{
137+
return new Color32(235, 191, 217, 255);
138+
}
139+
140+
public override bool HasReference(Variable in_variable)
141+
{
142+
return this.variable == in_variable ||
143+
base.HasReference(in_variable);
144+
}
145+
146+
#endregion
147+
#region Editor caches
148+
#if UNITY_EDITOR
149+
protected override void RefreshVariableCache()
150+
{
151+
base.RefreshVariableCache();
152+
var f = GetFlowchart();
153+
f.DetermineSubstituteVariables(key, referencedVariables);
154+
}
155+
#endif
156+
#endregion Editor caches
157+
}
158+
}

0 commit comments

Comments
 (0)