Is this indexed approach to java rts game development valid and how can I improve it?

I have class GameState laid out to basically hold an Array of Players, an Array of StarSystems, and a few other fields that need to persist from save to save. Each GameObject keeps track of its own id, which is also its index in its array as well as the id’s of relevant gameobjects.

public class GameState implements Json.Serializable
    private Array<StarSystem> systems;
    private Array<Player> players;

    //Methods used to access data and some utilities

Each StarSystem in the GameState stores an Array of Planet‘s and a Star or three, which in turn hold information on temperature and habitablity. Class Star extends from Planet and has 0 habitablity. The StarSystem class also has references to all Fleets present in the system, their owners. Fleet class holds a list of Ship instances that are in the fleet and relevant information for each ship.

I designed this system for two reasons:

  1. I wanted to be able to call `Json.toJson(Object)` once when I save the game rather than go through and individually save every object.
  2. Before this my code was very unorganized and I had weird references to various things strewn through the code. This is cleaner.

Unfortunately this has also dropped the FPS down from 35-40 to 20-25, which ticks me off and would probably tick any user off as well. In other words, I am asking how I can improve this design?

Notes: The indexed nature means that for a Planet.ProductionQueue to add a new Fighter to Fleet SomeFleet of Player A, it must first call GameInstance.getInstance().getState().getSystem(parent_system).getFleet(fleet_target_id).addShip(new Fighter(owner_id, getLeastPowerfulCommandShip().getID()))

Note that I am also using libGdx, and that the Json class I spoke of is part of the library.

The problem is not the hierarchy of your game, the problem is that you are descending the entire hierarchy for every request.

I’m guessing that players look at a single system at a time, and that a player would “select” a fleet to add a Fighter to it.

Therefore, store local variables: StarSystem current, and Fleet selected Update these variables when the player changes the current fleet/star system. You don’t need to descend the entire hierarchy to access an object.

Note: the objects selected should still be stored in the data structures you mentioned.


Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *