161 lines
5.8 KiB
C#
Executable File
161 lines
5.8 KiB
C#
Executable File
using System;
|
|
using UnityEngine;
|
|
|
|
public class Building : MonoBehaviour
|
|
{
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"data: {buildingData.ToString()} , level:{curLevel}";
|
|
}
|
|
public BuildingData buildingData;
|
|
public int curLevel;
|
|
public int[] additionalLevels;
|
|
public Outline[] outlines;
|
|
public bool autoGetOutlines = true;
|
|
public DateTime lastCollected{ get{if(!DBmanager.buildingStates.ContainsKey(buildingData.buildingName)){Debug.LogError($"No state for this building : {buildingData.buildingName}\n{BuildingManager.instance.ToString()}");} return DBmanager.buildingStates[buildingData.buildingName].lastCollectedTimestamp;}set{
|
|
_lastCollected = value.ToString();
|
|
Debug.Log("Setting last collected to " + value);
|
|
DBmanager.buildingStates[buildingData.buildingName].lastCollectedTimestamp = value;
|
|
DBmanager.UpdateBuildingsToServer();
|
|
}}
|
|
[SerializeField]private string _lastCollected;
|
|
|
|
ParticleSystem[] levelUps;
|
|
|
|
void OnDrawGizmos() {
|
|
Gizmos.color = Color.blue;
|
|
//Gizmos.DrawWireSphere(transform.position + new Vector3(0,spaceRadius,0), spaceRadius);
|
|
Collider col = GetComponent<Collider>();
|
|
Gizmos.DrawWireCube(col.bounds.center, col.bounds.size);
|
|
}
|
|
|
|
void Awake(){
|
|
//if(outline == null){outline = GetComponent<Outline>();}
|
|
if(autoGetOutlines){ outlines = GetComponentsInChildren<Outline>(); }
|
|
ToggleOutlines(false);
|
|
Selector.OnSelectedChanged.AddListener(OnSelectedChanged);
|
|
transform.tag = "Building";
|
|
|
|
levelUps = GetComponentsInChildren<ParticleSystem>();
|
|
foreach(ParticleSystem particle in levelUps){
|
|
particle.Stop();
|
|
}
|
|
}
|
|
|
|
void OnSelectedChanged(){
|
|
if(Selector.selectedBuilding == null){ToggleOutlines(false); return;}
|
|
|
|
ToggleOutlines(Selector.selectedBuilding == this);
|
|
//GetComponent<Collider>().isTrigger=(Selector.movingBuilding==this);
|
|
|
|
}
|
|
|
|
void ToggleOutlines(bool value){
|
|
bool isMoving = Selector.movingBuilding==this;
|
|
foreach(Outline outline in outlines){
|
|
// outline.enabled = value;
|
|
outline.enabled=true;
|
|
outline.OutlineColor = value ? Color.yellow : new Color(0.68f,0.68f,0.68f);
|
|
outline.OutlineWidth = 5;
|
|
// outline.OutlineColor = (isMoving) ? Color.green : Color.yellow;
|
|
}
|
|
}
|
|
|
|
public void ChangeOutlineColor(Color color){
|
|
foreach(Outline outline in outlines){
|
|
outline.OutlineColor = color;
|
|
}
|
|
}
|
|
|
|
public void Upgrade(int newLevel =-1, int gold_cost =-1, int metal_cost = -1){
|
|
if(curLevel >= buildingData.levels.Count-1){Debug.Log("Already max");return;}
|
|
|
|
if(gold_cost<0){gold_cost = buildingData.levels[curLevel+1].price;}
|
|
if(metal_cost<0){metal_cost = buildingData.levels[curLevel+1].metal_price;}
|
|
|
|
Debug.Log("Upgrading " + buildingData.buildingName + " for " + gold_cost + " coins and " + metal_cost + " energy");
|
|
|
|
switch (curLevel)
|
|
{
|
|
case 0: Debug.Log("1 Star"); break;
|
|
case 1: Debug.Log("2 Stars"); break;
|
|
}
|
|
|
|
if(DBmanager.Coins < gold_cost && DBmanager.Metal < metal_cost){
|
|
MessageDialog.instance.ShowMessage("Failed", "Insufficient Resources to complete the transaction");
|
|
return;
|
|
}
|
|
DBmanager.SetCoins(DBmanager.Coins- gold_cost);
|
|
DBmanager.SetMetal(DBmanager.Metal - metal_cost);
|
|
if(newLevel <0){
|
|
curLevel = Mathf.Clamp(curLevel+1,0, buildingData.levels.Count-1);
|
|
}else{
|
|
curLevel = newLevel;
|
|
}
|
|
|
|
DBmanager.UpgradeBuilding(buildingData.buildingName, curLevel);
|
|
SelectedItemMenu.instance.HideUpgradeMenu();
|
|
// UpgradeEffect.instance.Show(Camera.main.WorldToScreenPoint(transform.position));
|
|
Debug.Log("Showing upgrade effect");
|
|
DBmanager.SetXp(DBmanager.Xp + buildingData.levels[curLevel].xpGain);
|
|
|
|
AudioManager.instnace.UpgradeBuilding();
|
|
foreach(ParticleSystem particle in levelUps){
|
|
particle.Play();
|
|
}
|
|
}
|
|
//
|
|
// public List<Collider> buildingsInsideMe = new List<Collider>();
|
|
// public bool locationInvalid {get{return buildingsInsideMe.Count == 0;}}
|
|
//
|
|
// void OnTriggerEnter(Collider other) {
|
|
// Debug.Log("Trigger enter : " + other.name);
|
|
// if(other.GetComponent<Collider>().tag == "Building" && other != GetComponent<Collider>()){
|
|
// if(buildingsInsideMe.Contains(other)){
|
|
// //Already got him
|
|
// }else{
|
|
// buildingsInsideMe.Add(other);ChangeOutlineColor(locationInvalid ? Color.red : Color.green);
|
|
// }
|
|
// }
|
|
//
|
|
//
|
|
// }
|
|
//
|
|
// void OnTriggerExit(Collider other){
|
|
// Debug.Log("Trigger exit : " + other.name);
|
|
// if(other.GetComponent<Collider>().tag == "Building" && other != GetComponent<Collider>()){
|
|
// if(buildingsInsideMe.Contains(other)){
|
|
// buildingsInsideMe.Remove(other);
|
|
// ChangeOutlineColor(locationInvalid ? Color.green : Color.red);
|
|
// }else{
|
|
// //Nothing to remove
|
|
// }
|
|
// }
|
|
//
|
|
// }
|
|
|
|
|
|
/* */
|
|
}
|
|
|
|
|
|
[System.Serializable]
|
|
public class BuildingState{
|
|
public string id;
|
|
public int level;
|
|
public Vector3 position;
|
|
public DateTime lastCollectedTimestamp;
|
|
|
|
public BuildingState(string m_id, int m_level, Vector3 m_position, DateTime _lastCollectedTimestamp){
|
|
id = m_id;
|
|
level = m_level;
|
|
position = m_position;
|
|
lastCollectedTimestamp= _lastCollectedTimestamp;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return JsonUtility.ToJson(this);
|
|
}
|
|
} |