Skip to content

Commit

Permalink
Refactoring #3
Browse files Browse the repository at this point in the history
  • Loading branch information
BabichMikhail committed May 13, 2018
1 parent 2beb8b8 commit 2d00ccf
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 136 deletions.
3 changes: 2 additions & 1 deletion Assets/Resources/scripts/ClockController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ private void Awake()
initializedAt = Time.time;
}

void Update () {
private void Update ()
{
var startedAt = (int)(Time.time - initializedAt);
float minutes = startedAt / 60;
float seconds = startedAt - minutes * 60;
Expand Down
15 changes: 15 additions & 0 deletions Assets/Resources/scripts/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@
public const int SEND_UNIT_INTERVAL = 200;
public const float ADD_MONEY_INTERVAL = 0.333f;
public const int MAIN_TOWER_HEALTH = 10000;

public const float AUDIO_SOURCE_PROJECTILE_SOUND_TIME = 5.6f;

public const string PROJECTILE_CONTAINER_TAG_NAME = "ProjectileContainer";
public const string ROUTE_CONTAINER_TAG_NAME = "RouteContainer";
public const string TOWER_CONTAINER_TAG_NAME = "TowerContainer";
public const string UNIT_CONTAINER_TAG_NAME = "UnitContainer";
public const string CREATE_TOWER_CANVAS_TAG_NAME = "CreateTowerCanvas";
public const string MISSING_COLIDERS_CONTAINER_TAG_NAME = "MissingColliders";

public const string TOWER_POSITION_SELECTED_MODE_MATERIAL = "materials/Location/Gear";
public const string TOWER_POSITION_SELECTABLE_MODE_MATERIAL = "materials/Location/Teapot Tower Material";
public const string TOWER_POSITION_NOT_SELECTABLE_MODE_MATERIAL = "materials/DarkRedMaterial";

public const string TOWER_POSITION_TAP_TAG_NAME = "TowerPositionChild";
}
60 changes: 14 additions & 46 deletions Assets/Resources/scripts/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,23 @@
using UnityEngine;

public class Container {
private static GameObject unitContainer;
private static GameObject projectileContainer;
private static GameObject routeContainer;
private static GameObject towerContainer;
private static Canvas createTowerCanvas;
private static List<GameObject[]> towers = new List<GameObject[]>();
private Dictionary<int, GameObject[]> towersByType = new Dictionary<int, GameObject[]>();

private static Container instance;
public static Container Instance { get; set; }

private Container() {}

public static void Reset() { instance = null; }

public static Container GetInstance()
public static GameObject GetContainer(string tagName)
{
if (instance == null)
instance = new Container();
return instance;
var container = GameObject.FindGameObjectWithTag(tagName);
Debug.Assert(container != null);
return container;
}

public void AddTowers(GameObject[] towers) { Container.towers.Add(towers); }
public GameObject[] GetTowers(int type) { return towers[type]; }
public GameObject GetUnitContainer()
{
if (unitContainer == null)
unitContainer = GameObject.FindGameObjectWithTag("UnitContainer");
return unitContainer;
}
public GameObject GetProjectileContainer()
{
if (projectileContainer == null)
projectileContainer = GameObject.FindGameObjectWithTag("ProjectileContainer");
return projectileContainer;
}
public GameObject GetRouteContainer()
{
if (routeContainer == null)
routeContainer = GameObject.FindGameObjectWithTag("RouteContainer");
return routeContainer;
}
public GameObject GetTowerContainer() {
if (towerContainer == null)
towerContainer = GameObject.FindGameObjectWithTag("TowerContainer");
return towerContainer;
}
public Canvas GetCreateTowerCanvas()
{
if (createTowerCanvas == null)
createTowerCanvas = GameObject.FindGameObjectWithTag("CreateTowerCanvas").GetComponent<Canvas>();
return createTowerCanvas;
}
public GameObject UnitContainer { get; set; }
public GameObject ProjectileContainer { get; set; }
public GameObject RouteContainer { get; set; }
public GameObject TowerContainer { get; set; }
public Canvas CreateTowerCanvas { get; set; }

public void AddTowers(GameObject[] towers, int type) { towersByType[type] = towers; }
public GameObject[] GetTowers(int type) { return towersByType[type]; }
}
56 changes: 22 additions & 34 deletions Assets/Resources/scripts/CurrentTowerDefenceState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,45 @@
using UnityEngine;

public class CurrentTowerDefenceState {
private static CurrentTowerDefenceState instance;
private GameObject currentTower;
private GameObject selectedTower;
private Dictionary<string, TowerController> createdTowers = new Dictionary<string, TowerController>();
private int balance = Config.START_BALANCE;


public enum UpdateTypes { UPDATE_SPEED, UPDATE_DAMAGE }

private CurrentTowerDefenceState() {}
public static void Reset() { instance = null; }

public static CurrentTowerDefenceState GetInstance()
{
if (instance == null)
instance = new CurrentTowerDefenceState();
return instance;
}
public static CurrentTowerDefenceState Instance { get; set; }

public bool TowerIsCurrent(GameObject tower)
{
return currentTower != null && currentTower.name == tower.name;
return selectedTower != null && selectedTower.name == tower.name;
}

public void SetCurrentTower(GameObject tower)
public void SetSelectedTower(GameObject tower)
{
if (currentTower != null)
currentTower.GetComponentInChildren<MeshRenderer>().material = Resources.Load<Material>("materials/Location/Teapot Tower Material");
currentTower = tower;
if (currentTower != null)
currentTower.GetComponentInChildren<MeshRenderer>().material = Resources.Load<Material>("materials/Location/Gear");
if (selectedTower != null)
selectedTower.GetComponentInChildren<MeshRenderer>().material = Resources.Load<Material>(Config.TOWER_POSITION_SELECTABLE_MODE_MATERIAL);
selectedTower = tower;
if (selectedTower != null)
selectedTower.GetComponentInChildren<MeshRenderer>().material = Resources.Load<Material>(Config.TOWER_POSITION_SELECTED_MODE_MATERIAL);
}

public GameObject GetCurrentTower()
public GameObject GetSelectedTower()
{
return currentTower;
return selectedTower;
}

public Canvas GetCurrentCanvas()
public Canvas GetConstructionCanvas()
{
return Container.GetInstance().GetCreateTowerCanvas();
return Container.Instance.CreateTowerCanvas;
}

public void ResetTower()
{
GetCurrentCanvas().enabled = false;
SetCurrentTower(null);
GetConstructionCanvas().enabled = false;
SetSelectedTower(null);
}

public GameObject CreateTower(GameObject towerPrefab, GameObject projectile)
{
var tower = GetCurrentTower();
var tower = GetSelectedTower();
Debug.Assert(tower != null);
Debug.Assert(!createdTowers.ContainsKey(tower.name));
var newTower = Object.Instantiate(towerPrefab, tower.transform);
Expand All @@ -76,7 +64,7 @@ public void ChangeBalance(int delta)

public bool CanCreateNextTower(int idx, int type)
{
return Container.GetInstance().GetTowers(type).Length > idx;
return Container.Instance.GetTowers(type).Length > idx;
}

public bool CanCreateTower()
Expand All @@ -88,8 +76,8 @@ public void CreateNextTower(TowerPositionController controller)
{
var idx = controller.GetCurrentTowerToCreateIndex();
Debug.Assert(CanCreateNextTower(idx, controller.type));
var towerPrefab = Container.GetInstance().GetTowers(controller.type)[idx];
Object.Instantiate(towerPrefab, GetCurrentTower().transform);
var towerPrefab = Container.Instance.GetTowers(controller.type)[idx];
Object.Instantiate(towerPrefab, GetSelectedTower().transform);
controller.IncreaseTowerToCreateIndex();
if (!CanCreateNextTower(controller.GetCurrentTowerToCreateIndex(), controller.type))
controller.DisableInitialComponents();
Expand All @@ -100,11 +88,11 @@ public void CreateNextTower(TowerPositionController controller)

public void UpdateTowerPositionMaterials()
{
var material = Resources.Load<Material>("materials/DarkRedMaterial");
var material = Resources.Load<Material>(Config.TOWER_POSITION_NOT_SELECTABLE_MODE_MATERIAL);
if (balance >= Config.CONSTRUCT_TOWER_COST)
material = currentTower == null ? Resources.Load<Material>("materials/Location/Teapot Tower Material") : Resources.Load<Material>("materials/Location/Gear");
material = selectedTower == null ? Resources.Load<Material>(Config.TOWER_POSITION_SELECTABLE_MODE_MATERIAL) : Resources.Load<Material>(Config.TOWER_POSITION_SELECTED_MODE_MATERIAL);

GameObject[] objects = GameObject.FindGameObjectsWithTag("TowerPositionChild");
GameObject[] objects = GameObject.FindGameObjectsWithTag(Config.TOWER_POSITION_TAP_TAG_NAME);
for (int i = 0; i < objects.Length; ++i) {
var renderer = objects[i].GetComponent<MeshRenderer>();
renderer.material = material;
Expand Down
1 change: 0 additions & 1 deletion Assets/Resources/scripts/GearZRotator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using UnityEngine;


public class GearZRotator : MonoBehaviour {
public float randomRotationSpeed;

Expand Down
32 changes: 20 additions & 12 deletions Assets/Resources/scripts/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ public class MainController : MonoBehaviour {

private void Awake()
{
Container.GetInstance().AddTowers(towers0);
Container.GetInstance().AddTowers(towers1);
var missingCollidersObject = GameObject.FindGameObjectWithTag("MissingColliders");
Container.Instance = new Container();
Container.Instance.AddTowers(towers0, 0);
Container.Instance.AddTowers(towers1, 1);
Container.Instance.ProjectileContainer = Container.GetContainer(Config.PROJECTILE_CONTAINER_TAG_NAME);
Container.Instance.RouteContainer = Container.GetContainer(Config.ROUTE_CONTAINER_TAG_NAME);
Container.Instance.TowerContainer = Container.GetContainer(Config.TOWER_CONTAINER_TAG_NAME);
Container.Instance.UnitContainer = Container.GetContainer(Config.UNIT_CONTAINER_TAG_NAME);
Container.Instance.CreateTowerCanvas = Container.GetContainer(Config.CREATE_TOWER_CANVAS_TAG_NAME).GetComponent<Canvas>();

CurrentTowerDefenceState.Instance = new CurrentTowerDefenceState();

var missingCollidersObject = GameObject.FindGameObjectWithTag(Config.MISSING_COLIDERS_CONTAINER_TAG_NAME);
if (missingCollidersObject != null)
missingCollidersObject.SetActive(false);
}
Expand All @@ -54,7 +63,7 @@ private void Start()
lastIncreaseMoneyTime = startedAt = Time.time;
Time.timeScale = 1.0f;

var routeContainer = Container.GetInstance().GetRouteContainer();
var routeContainer = Container.Instance.RouteContainer;
var collider = mainTower.GetComponentInChildren<Collider>();
Debug.Assert(collider != null);
for (var i = 0; i < routeContainer.transform.childCount; ++i) {
Expand All @@ -66,7 +75,7 @@ private void Start()
router.points.Add(routerObject.transform.GetChild(j).transform.position);
router.points.Add(mainTower.transform.position);
router.targetCollider = collider;
routerController.SetRouter(router);
routerController.Router = router;
routerControllers.Add(routerController);
}
for (int i = 0; i < routeContainer.transform.childCount; ++i)
Expand All @@ -76,14 +85,13 @@ private void Start()

private void Update()
{
Music.Update();
TryToSendUnit();
var balanceDelta = 0;
while (Time.time - lastIncreaseMoneyTime > Config.ADD_MONEY_INTERVAL) {
++balanceDelta;
lastIncreaseMoneyTime += Config.ADD_MONEY_INTERVAL;
}
CurrentTowerDefenceState.GetInstance().ChangeBalance(balanceDelta);
CurrentTowerDefenceState.Instance.ChangeBalance(balanceDelta);
}

public void TryToSendUnit()
Expand All @@ -101,24 +109,24 @@ public void TryToSendUnit()
private void SendUnit(int respawnIndex)
{
var routerController = routerControllers[respawnIndex];
var router = routerController.GetRouter().CopyInstance();
var router = routerController.Router.CopyInstance();
var units = routerController.units;
var unit = Instantiate(units[Random.Range(0, units.Length)], Container.GetInstance().GetUnitContainer().transform);
var unit = Instantiate(units[Random.Range(0, units.Length)], Container.Instance.UnitContainer.transform);
unit.GetComponent<UnitController>().SetUp(router, mainTower);
router.SetPosition(unit.transform, router.GetInitialPoint());
routerController.EnableUnit(unit);
}

public void CreateOrUpdateTower()
{
var state = CurrentTowerDefenceState.GetInstance();
var state = CurrentTowerDefenceState.Instance;
Debug.Assert(state.GetBalance() >= Config.CONSTRUCT_TOWER_COST);
var controller = state.GetCurrentTower().GetComponent<TowerPositionController>();
var controller = state.GetSelectedTower().GetComponent<TowerPositionController>();
state.CreateNextTower(controller);
}

public void DeselectTower()
{
CurrentTowerDefenceState.GetInstance().ResetTower();
CurrentTowerDefenceState.Instance.ResetTower();
}
}
13 changes: 9 additions & 4 deletions Assets/Resources/scripts/MainMenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
public class MainMenuController : MonoBehaviour {
public RawImage line;

public void Awake()
{
Music.Instance = new Music();
}

public void Update()
{
Music.Update();
Music.Instance.Update();
}

public void OnClickGame()
Expand All @@ -23,7 +28,7 @@ public void OnClickContinue()

public void OnClickSound()
{
Music.GetInstance().SwitchSound(line);
Music.Instance.SwitchSound(line);
}

public void OnClickExit()
Expand All @@ -41,8 +46,8 @@ public void OnClickRestart()
{
Time.timeScale = 0.0f;
SceneManager.LoadScene("Main");
Container.Reset();
CurrentTowerDefenceState.Reset();
Container.Instance = null;
CurrentTowerDefenceState.Instance = null;
Time.timeScale = 1.0f;
}
}
18 changes: 4 additions & 14 deletions Assets/Resources/scripts/Music.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@
using UnityEngine.UI;

class Music {
private Music() {}

private static Music instance;

public static Music GetInstance()
{
if (instance == null)
instance = new Music();
return instance;
}
public static Music Instance { get; set; }

private float volume;
private float time;
Expand Down Expand Up @@ -44,11 +35,10 @@ public void PlayGlobalMusic(GameObject newParent)
audio.Play();
}

public static void Update(bool force = false)
public void Update(bool force = false)
{
instance = GetInstance();
if ((playing || force) && (instance.audio == null || instance.audio.isPlaying))
instance.PlayGlobalMusic(GameObject.FindGameObjectWithTag("MainCamera"));
if ((playing || force) && (audio == null || audio.isPlaying))
PlayGlobalMusic(GameObject.FindGameObjectWithTag("MainCamera"));
}

public bool SwitchSound(RawImage line)
Expand Down
8 changes: 4 additions & 4 deletions Assets/Resources/scripts/ProjectileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public enum TargetType { TOWER, UNIT }
private void Awake()
{
var audioSource = gameObject.GetComponent<AudioSource>();
if (audioSource != null) // Settings for specific sound
audioSource.time = 5.6f;
if (audioSource != null)
audioSource.time = Config.AUDIO_SOURCE_PROJECTILE_SOUND_TIME;
}

private void Start()
Expand All @@ -34,7 +34,7 @@ private void Start()

speedVector = new Vector3(
route.x / route.magnitude * speed,
g*t1,
g * t1,
route.z / route.magnitude * speed
);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ private void Update()
}

if (targetType == TargetType.UNIT) {
var unitCollection = Container.GetInstance().GetUnitContainer();
var unitCollection = Container.Instance.UnitContainer;
for (int i = 0; i < unitCollection.transform.childCount; ++i) {
var childObject = unitCollection.transform.GetChild(i).gameObject;
if ((childObject.transform.position - destinationPoint).sqrMagnitude < damageRadius * damageRadius)
Expand Down
Loading

0 comments on commit 2d00ccf

Please sign in to comment.