I'm trying to program a game-simulator in Java using OOP (SC2 to be precise). Basically the games has units (each different unit is a class, each actually created unit is an instance of that class), and has buildings with different roles. Again buildings each have their own class, and when a building is built its class is instantiated. So I'll have u1, u2, u3, b1, b2 b3... etc instances.
All fine for that.
Now, I need to simulate time increment. I know that eventually I need the user to be able to interact with the game (e.g. at key times, that are game-dependant and NOT know before the simulation, I want user-input). This means I will want the game to run for X time increments, then possibly stops for a specific event (when Y resources have been obtained, which allows to create a building/unit, or when a new building has been created and opens up new decisions).
So to sum up here are my (general) classes:
class Unit extends GameElement{
//bunch of attributes
//bunch of units-specific methods, getters & not relevent here
public void timeIncrement () {
//Manages all time-dependant changes for this unit when
//that method is called
}
}
Similarly for building, they will have their own timeIncrement methods, which will manage their own (class-specific) time-dependant behaviours.
Both building classes & unit classes are extension of:
abstract class GameElement {
//Manages time-dependant behaviours
public abstract void timeIncrement();
//Manages the building/creation of the game element
public abstract void building();
}
which defines common methods needed ,e.g. each unit must manage it's time and also its building procedure.
I have problems as to how to define
class TimeManagement{
//Those ArrayList list all objects created with a
//timeIncrement() method that needs to be called
private ArrayList<Units> = new ArrayList<Units>();
private ArrayList<Buildings> = new ArrayList<Buildings>();
//This is the (universal game-wide) timestep. It might change so I
//need to have a single-place to edit it, e.g. global variable
private double TIME_STEP = 0.5;
}
Basically my plan is to have TimeManagement with ArrayList of all the objects it needs to tell that the time has incremented. For each arrayList, it will loop through the objects it contains and call myObject.timeIncrement() method, and then the objects will management he increment however they are programmed to.
My problem is how to define this TimeManagement class. It doesn't make sense to me to instantiate this class. But if I declare it static I can't (unless I am wrong on this - I haven't used static classes very much yet) update it's ArrayList when I build new units, so how will TimeManagement be able to call the timeIncrement for all the object that needs it?
Or should I just create a bogus instance of TimeManagement, so I don't have to declare it static? But this just feels wrong, from a programming point of view.
I would prefer to work something out with this general architecture. It seems to me that it requires something along the lines of this TimeManagement class but I just can't quite put my finger on it it seems....