Hy guys,
I am working on a simple game architecture / strategy. Before I actually started writing lots of code I want to make sure. simple system works.
Basically I have:
Manage Class - which I thought will create Objects and will keep track of all of them in the List or Dictionary so other systems will access only this Manager to get information.
Factory Class - actually creates / instantiates GO into the scene
Controller Class - on each GO to control their behaviour
Entity Custom Class - object data holder
I have a couple of questions you might be able to help with from your experience :
-
How Manager Class should add objects to its list if objects were created by Factory Class?
Currently my manager class sends events to the rest -
Also I will have functionality within Controller class to change GO parameters (color, scale …)
These has to be passed on back to manager.
But I believe this will just work because I will be changing referenced objects so it will just work.? -
What is the best way to link actual Scene GameObject with custom Entity Class? Currently I just have GameObject field within Constructor
And here is my code
Manage Class
public class BuildingManager : MonoBehaviour
{
public List<Building> buildings;
public delegate void BuildingAdded();
public static event BuildingAdded onBuildingAdded;
public delegate void BuildingUpdate(float value);
public static event BuildingUpdate onBuildingUpdate;
private void Start()
{
buildings = new List<Building>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
AddBuilding();
Debug.Log("Building was created and added to the list");
}
if (Input.GetKeyDown(KeyCode.R))
{
Debug.Log(buildings.Count);
}
}
public void AddBuilding()
{
buildings.Add(new Building());
onBuildingAdded?.Invoke();
}
public void UpdateBuildingHeight(float value)
{
onBuildingUpdate?.Invoke(value);
}
}
Factory Class
public class BuildingFactory : MonoBehaviour
{
public GameObject buildingPrefab;
// Start is called before the first frame update
void Start()
{
BuildingManager.onBuildingAdded += MakeBuilding;
}
private void MakeBuilding()
{
GameObject buildingGO = Instantiate(buildingPrefab, Vector3.zero, Quaternion.identity, transform);
}
}
Controller Class
public class BuildingController : MonoBehaviour
{
private void Start()
{
BuildingManager.onBuildingUpdate += UpdateHeight;
}
public void UpdateHeight(float value)
{
transform.localScale = new Vector3(1, value, 1);
Debug.Log("I am changing the height: " + value);
}
}
Entity Custom Class
public class Building
{
public string id;
public float height;
public GameObject buildingGeo;
public Building()
{
}
public Building(string id, float height, GameObject buildingGeo)
{
this.id = id;
this.height = height;
this.buildingGeo = buildingGeo;
}
}
As you can see these are very simple , I just need to connect the dots between their communication. So I have a clear way forward.