Improved scripting

-Added accurate rotationPerSec & secondPerRot values!... Up to a speed
of ~720.
--Used for new idle expgain calculation in Load()
-Added scripting for assigning button events within
InitializeButtonArrays()
--stored all buttons to arrays depending on contents of the parent UI
panel's name
--mainButtons[], rotationButtons[], colorButtons[]... etc
--Future buttons will automatically exist within the array, only events
need assigning.
--Buttons do not include tick boxes, etc!
--Buttons cannot have onClick() even assigned through editor (Unity
bug?)
This commit is contained in:
Shaun Reed 2017-09-04 13:53:03 -04:00
parent ea55db656d
commit c868d738b9
4 changed files with 149 additions and 46 deletions

Binary file not shown.

View File

@ -32,11 +32,6 @@ public class ExperienceBar : MonoBehaviour {
fillAmount = currentExp / currentRequirement; fillAmount = currentExp / currentRequirement;
} }
public void OnDestroy()
{
GameObject.FindGameObjectWithTag("GameController").GetComponent<GameManager>().Save();
}
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
fillAmount = currentExp / currentRequirement; fillAmount = currentExp / currentRequirement;

View File

@ -18,6 +18,7 @@ public class GameManager : MonoBehaviour {
public float requirement; public float requirement;
public float speed; public float speed;
public float increment; public float increment;
public float rotationsPerSec;
public DateTime currentTime; public DateTime currentTime;
} }
@ -50,6 +51,7 @@ public class GameManager : MonoBehaviour {
int currentLevel = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentLevel; int currentLevel = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentLevel;
float currentExp = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentExp; float currentExp = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentExp;
float currentRequirement = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentRequirement; float currentRequirement = GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentRequirement;
float rotationPerSec = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().rotationPerSec;
float currentSpeed = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentSpeed; float currentSpeed = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentSpeed;
float currentIncrement = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentIncrement; float currentIncrement = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentIncrement;
@ -59,6 +61,7 @@ public class GameManager : MonoBehaviour {
data.requirement = currentRequirement; data.requirement = currentRequirement;
data.speed = currentSpeed; data.speed = currentSpeed;
data.increment = currentIncrement; data.increment = currentIncrement;
data.rotationsPerSec = rotationPerSec;
data.currentTime = DateTime.Now; data.currentTime = DateTime.Now;
bf.Serialize(file, data); bf.Serialize(file, data);
@ -75,6 +78,7 @@ public class GameManager : MonoBehaviour {
PlayerData data = (PlayerData)bf.Deserialize(file); PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close(); file.Close();
GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().rotationPerSec = data.rotationsPerSec;
GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentSpeed = data.speed; GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentSpeed = data.speed;
GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentIncrement = data.increment; GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentIncrement = data.increment;
GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentLevel = data.level; GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentLevel = data.level;
@ -82,9 +86,9 @@ public class GameManager : MonoBehaviour {
GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentRequirement = data.requirement; GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentRequirement = data.requirement;
DateTime loadTime = DateTime.Now; DateTime loadTime = DateTime.Now;
int secondsPassed = GetIdleRewards(data.currentTime, loadTime); int secondsPassed = GetIdleTime(data.currentTime, loadTime);
float radianSpeed = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentSpeed * Mathf.Deg2Rad; float radianSpeed = GameObject.FindGameObjectWithTag("Player").GetComponent<SpinningCube>().currentSpeed * Mathf.Deg2Rad;
float idleExp = radianSpeed * secondsPassed; float idleExp = (data.rotationsPerSec * secondsPassed) * data.increment;
GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentExp += idleExp; GameObject.FindGameObjectWithTag("ExpGained").GetComponent<ExperienceBar>().currentExp += idleExp;
Debug.Log("Loaded"); Debug.Log("Loaded");
@ -92,7 +96,7 @@ public class GameManager : MonoBehaviour {
} }
} }
public int GetIdleRewards(DateTime saveTime, DateTime loadTime) public int GetIdleTime(DateTime saveTime, DateTime loadTime)
{ {
int daysPassed = 0; int daysPassed = 0;
int hoursPassed = 0; int hoursPassed = 0;

View File

@ -1,19 +1,21 @@
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using System.Collections; using System.Collections;
using System.Collections.Generic;
public class ToggleMenus : MonoBehaviour public class ToggleMenus : MonoBehaviour
{ {
//Menus //Menus
public GameObject RotMenu; public GameObject rotMenu;
public GameObject ColorMenu; public GameObject colorMenu;
public GameObject ShapesMenu; public GameObject shapesMenu;
public GameObject LightingMenu; public GameObject lightingMenu;
public GameObject ExpMenu; public GameObject expMenu;
private bool ActiveMenu = false; private bool ActiveMenu = false;
//Shapes - Prefabs //Shapes - Prefabs
public GameObject player;
public GameObject Square; public GameObject Square;
public GameObject Sphere; public GameObject Sphere;
public GameObject Cylinder; public GameObject Cylinder;
@ -28,18 +30,22 @@ public class ToggleMenus : MonoBehaviour
//private GameObject lighting = GameObject.FindGameObjectWithTag("Lighting"); //private GameObject lighting = GameObject.FindGameObjectWithTag("Lighting");
public float lightRotX, lightRotY, lightRotZ; public float lightRotX, lightRotY, lightRotZ;
public GameObject[] buttons;
// Use this for initialization // Use this for initialization
void Start() void Start()
{ {
player = GameObject.FindGameObjectWithTag("Player");
InitializeButtonArrays();
Temp = Square; Temp = Square;
lightRotX = 0; lightRotX = 0;
lightRotY = 0; lightRotY = 0;
lightRotZ = 0; lightRotZ = 0;
EditLightingRotation(); //EditLightingRotation();
//EditLightingLocation(); //EditLightingLocation();
} }
public void Update() public void Update()
@ -49,57 +55,57 @@ public class ToggleMenus : MonoBehaviour
public void ToggleRotationMenu() public void ToggleRotationMenu()
{ {
if (RotMenu.gameObject.active) if (rotMenu.gameObject.activeSelf)
{ {
RotMenu.gameObject.SetActive(false); rotMenu.gameObject.SetActive(false);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
else if (!RotMenu.gameObject.active && ActiveMenu) else if (!rotMenu.gameObject.activeSelf && ActiveMenu)
{ {
CloseAll(); CloseAll();
RotMenu.gameObject.SetActive(true); rotMenu.gameObject.SetActive(true);
} }
else else
{ {
RotMenu.gameObject.SetActive(true); rotMenu.gameObject.SetActive(true);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
} }
public void ToggleColorMenu() public void ToggleColorMenu()
{ {
if (ColorMenu.gameObject.active) if (colorMenu.activeSelf)
{ {
ColorMenu.gameObject.SetActive(false); colorMenu.gameObject.SetActive(false);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
else if (!ColorMenu.gameObject.active && ActiveMenu) else if (!colorMenu.activeSelf && ActiveMenu)
{ {
CloseAll(); CloseAll();
ColorMenu.gameObject.SetActive(true); colorMenu.SetActive(true);
} }
else else
{ {
ColorMenu.gameObject.SetActive(true); colorMenu.SetActive(true);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
} }
public void ToggleShapesMenu() public void ToggleShapesMenu()
{ {
if (ShapesMenu.gameObject.active) if (shapesMenu.gameObject.activeSelf)
{ {
ShapesMenu.gameObject.SetActive(false); shapesMenu.gameObject.SetActive(false);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
else if (!ShapesMenu.gameObject.active && ActiveMenu) else if (!shapesMenu.gameObject.activeSelf && ActiveMenu)
{ {
CloseAll(); CloseAll();
ShapesMenu.gameObject.SetActive(true); shapesMenu.gameObject.SetActive(true);
} }
else else
{ {
ShapesMenu.gameObject.SetActive(true); shapesMenu.gameObject.SetActive(true);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
@ -107,50 +113,50 @@ public class ToggleMenus : MonoBehaviour
public void ToggleLightingMenu() public void ToggleLightingMenu()
{ {
if (LightingMenu.gameObject.active) if (lightingMenu.gameObject.activeSelf)
{ {
LightingMenu.gameObject.SetActive(false); lightingMenu.gameObject.SetActive(false);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
else if (!LightingMenu.gameObject.active && ActiveMenu) else if (!lightingMenu.gameObject.activeSelf && ActiveMenu)
{ {
CloseAll(); CloseAll();
LightingMenu.gameObject.SetActive(true); lightingMenu.gameObject.SetActive(true);
} }
else else
{ {
LightingMenu.gameObject.SetActive(true); lightingMenu.gameObject.SetActive(true);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
} }
public void ToggleExpMenu() public void ToggleExpMenu()
{ {
if (ExpMenu.gameObject.active) if (expMenu.gameObject.activeSelf)
{ {
ExpMenu.gameObject.SetActive(false); expMenu.gameObject.SetActive(false);
//check if another menu is open to avoid overlapping //check if another menu is open to avoid overlapping
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
else if (!ExpMenu.gameObject.active && ActiveMenu) else if (!expMenu.gameObject.activeSelf && ActiveMenu)
{ {
CloseAll(); CloseAll();
ExpMenu.gameObject.SetActive(true); expMenu.gameObject.SetActive(true);
} }
else else
{ {
ExpMenu.gameObject.SetActive(true); expMenu.gameObject.SetActive(true);
ActiveMenu = !ActiveMenu; ActiveMenu = !ActiveMenu;
} }
} }
public void CloseAll() public void CloseAll()
{ {
ShapesMenu.gameObject.SetActive(false); shapesMenu.gameObject.SetActive(false);
RotMenu.gameObject.SetActive(false); rotMenu.gameObject.SetActive(false);
ColorMenu.gameObject.SetActive(false); colorMenu.gameObject.SetActive(false);
LightingMenu.gameObject.SetActive(false); lightingMenu.gameObject.SetActive(false);
ExpMenu.gameObject.SetActive(false); expMenu.gameObject.SetActive(false);
} }
public void ChangeShapeSquare() public void ChangeShapeSquare()
@ -188,7 +194,7 @@ public class ToggleMenus : MonoBehaviour
Active = "Cylinder(Clone)"; Active = "Cylinder(Clone)";
Debug.Log(Active); Debug.Log(Active);
} }
/*
public void EditLightingRotation() public void EditLightingRotation()
{ {
buttons = GameObject.FindGameObjectsWithTag("RotationMenuButtons"); buttons = GameObject.FindGameObjectsWithTag("RotationMenuButtons");
@ -197,9 +203,11 @@ public class ToggleMenus : MonoBehaviour
gameObject.GetComponent<Button>().onClick.RemoveAllListeners(); gameObject.GetComponent<Button>().onClick.RemoveAllListeners();
} }
/*
GameObject.Find("X Button").GetComponent<Button>().onClick.AddListener(() => { EditLightingRotationX(); }); GameObject.Find("X Button").GetComponent<Button>().onClick.AddListener(() => { EditLightingRotationX(); });
GameObject.Find("Y Button").GetComponent<Button>().onClick.AddListener(() => { EditLightingRotationY(); }); GameObject.Find("Y Button").GetComponent<Button>().onClick.AddListener(() => { EditLightingRotationY(); });
GameObject.Find("Z Button").GetComponent<Button>().onClick.AddListener(() => { EditLightingRotationZ(); }); GameObject.Find("Z Button").GetComponent<Button>().onClick.AddListener(() => { EditLightingRotationZ(); });
} }
public void EditLightingLocation() public void EditLightingLocation()
@ -236,4 +244,100 @@ public class ToggleMenus : MonoBehaviour
{ {
lightRotZ = 20 * Time.deltaTime; lightRotZ = 20 * Time.deltaTime;
} }
*/
public void InitializeButtonArrays()
{
List<Button> mainButtons = new List<Button>();
List<Button> rotationButtons = new List<Button>();
List<Button> colorButtons = new List<Button>();
List<Button> shapesButtons = new List<Button>();
List<Button> lightingButtons = new List<Button>();
List<Button> expButtons = new List<Button>();
//Component spinningCube = player.GetComponent<SpinningCube>();
Button[] allButtons = GameObject.Find("UI Canvas").GetComponentsInChildren<Button>(true);
foreach(Button b in allButtons)
{
if (b.gameObject.transform.parent.name.Contains("Main"))
{
mainButtons.Add(b.gameObject.GetComponent<Button>());
}
if (b.gameObject.transform.parent.name.Contains("Rotation"))
{
rotationButtons.Add(b.gameObject.GetComponent<Button>());
}
if (b.gameObject.transform.parent.name.Contains("Color"))
{
colorButtons.Add(b.gameObject.GetComponent<Button>());
}
if (b.gameObject.transform.parent.name.Contains("Shapes"))
{
shapesButtons.Add(b.gameObject.GetComponent<Button>());
}
if (b.gameObject.transform.parent.name.Contains("Lighting"))
{
lightingButtons.Add(b.gameObject.GetComponent<Button>());
}
if (b.gameObject.transform.parent.name.Contains("Exp"))
{
expButtons.Add(b.gameObject.GetComponent<Button>());
}
}
//5 (0-4)
Debug.Log("mainButtons: " + mainButtons.Count);
//5 (0-4)
Debug.Log("rotationButtons: " + rotationButtons.Count);
//8 (0-7)
Debug.Log("colorButtons: " + colorButtons.Count);
//4 (0-3)
Debug.Log("shapesButtons: " + shapesButtons.Count);
//3 (0-2)
Debug.Log("lightingButtons: " + lightingButtons.Count);
//3 (0-2)
Debug.Log("expButtons: " + expButtons.Count);
mainButtons[0].onClick.AddListener(() => { ToggleRotationMenu(); });
mainButtons[1].onClick.AddListener(() => { ToggleColorMenu(); });
mainButtons[2].onClick.AddListener(() => { ToggleShapesMenu(); });
mainButtons[3].onClick.AddListener(() => { ToggleLightingMenu(); });
mainButtons[4].onClick.AddListener(() => { ToggleExpMenu(); });
rotationButtons[0].onClick.AddListener(() => { player.GetComponent<SpinningCube>().RaiseRotationSpeed(); });
rotationButtons[1].onClick.AddListener(() => { player.GetComponent<SpinningCube>().LowerRotationSpeed(); });
rotationButtons[2].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ToggleRotation(); });
rotationButtons[3].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ToggleRotationDirection(); });
rotationButtons[4].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ResetRotationSpeed(); });
colorButtons[0].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ChangeColorBlack(); });
colorButtons[1].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ChangeColorWhite(); });
colorButtons[2].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ChangeColorRed(); });
colorButtons[3].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ChangeColorGreen(); });
colorButtons[4].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ChangeColorBlue(); });
colorButtons[5].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ChangeColorYellow(); });
colorButtons[6].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ChangeColorCyan(); });
colorButtons[7].onClick.AddListener(() => { player.GetComponent<SpinningCube>().ChangeColorMagenta(); });
shapesButtons[0].onClick.AddListener(() => { ChangeShapeCylinder(); });
shapesButtons[1].onClick.AddListener(() => { ChangeShapeCapsule(); });
shapesButtons[2].onClick.AddListener(() => { ChangeShapeSquare(); });
shapesButtons[3].onClick.AddListener(() => { ChangeShapeSphere(); });
//Temporary save/load hidden in lighting for debug
lightingButtons[0].onClick.AddListener(() => { GameObject.Find("EventSystem").GetComponent<GameManager>().Save(); });
lightingButtons[1].onClick.AddListener(() => { GameObject.Find("EventSystem").GetComponent<GameManager>().Load(); });
//lightingButtons[2].onClick.AddListener(() => { function(); });
expButtons[0].onClick.AddListener(() => { GameObject.Find("Gained Image").GetComponent<ExperienceBar>().ExpMore(); });
expButtons[1].onClick.AddListener(() => { GameObject.Find("Gained Image").GetComponent<ExperienceBar>().ExpLess(); });
expButtons[2].onClick.AddListener(() => { GameObject.Find("Gained Image").GetComponent<ExperienceBar>().ResetExp(); });
}
} }