edit: clarified my question. I also realized that I didn't pick a very conclusive question title, which unfortunately I cannot change anymore, sorry.
Hey everyone,
to improve my programming skills a little and getting familiar with good design patterns, I'm trying to program a litte turn based strategy game, and I am at the point to choose a good data structure for the configuration like whichof different terraintypes and unittypes etc. which do exist etc.
What I have thought quiteimplemented so far is a whilebig "Game Object" with knowledge about this, and also searchedeverything in the internet a lotgame, but I couldn't come up with a satisfying solution. I foundand many of its properties are big associative arrays this question(I'm using PHP, but it didn'tmy question doesn't really helphave to do much with the language), for example an arrays of units, a map array.... What my issuearray structure is comparable to is the type object pattern mentioned here.
To quickly get everything goingExample: A single unit is of a certain type, until now I have basically created some multi-dimensional arrays to storesay 'Infantry', and shares many data with all necessary informationthe other infantry untis on the field. Instead of copying all these values, I give every unit a "type" so it can look up the details there. Similar with terraintypes. Example:
terraintypes = array()
terraintypes[0] = array()
terraintypes[0]['name'] = 'Forest'
terraintypes[0]['capturable'] = false
sometile = array();
sometile['coordinates'] = [3, 7]
sometile['type'] = 0; //there is a Forest at (3,7)
unittypes = array()
unittypes[0] = array()
unittypes[0]['name'] = 'Infantry'
unittypes[0]['movecost'] = array()
unittypes[0]['movecost'][0] = 5 //Movementpoints needed for forest
unittypes[0]['movecost'][1] = 7 //Movementpoints needed for terraintype 1
A specific unit or tile on the map wouldn't know its movement costs or name, but would be able to look that up in the unittypes array. Example:
sometile = array();
sometile['owner'] = whatever
sometile['type'] = 0; //this tile is a forest
someunit = array()
someunit['type'] = 0 //This is an "Infantry".
The problem is that getting any kind of information will produce very verbose code, for example finding out how much it costs for a unitunit to move onto a tiletile will be
So I thought I could get rid of all these keys by switchingam trying right now to move to using more objects and link themclasses, likeso if I give a unit a reference to its unittype, it can lookup information itself, for example
forestterraintypes[0] = new Terraintype('Forest')
infantryunittypes[0] = new Unittype('Infantry')
someguysomeunit = new Unit()
someguy.setType(infantryunittypes[0])
Now, if I ask a unit something only its unittype would now, the unit can look it up itself, e.g. someunit.getName() will be implemented in the unit like this.unittype.getName().
So far so good. However, how could a unittype store its movecost on the different terrains? ItBasically I would need an array indexed with terraintypes, but as that is not possible, I will be difficulthave to realize something likeswhich back to translate everything to numeric keys, and have a movecost value for every pair (terraintype, unittype).
infantry.setMovecost(forest, 5)
without falling backWho assigns these numeric keys and where? Will every single unittype need to usingstore some kind of key internally and storeidentifier which is unique among all the unittypes?
Should I use some constants like const FOREST = 0; const ROAD = 1; const INFANTRY = 0; const VEHICLES = 1 etc?
Another of my problems for finding a good data structure is that if I don't give the game all the data in an arraydirectly, e.gbut break everything down to smaller parts, then many parts will need to know about the same objects.
For example, if the Map object knows about all tiles of the map and the units on it, then the game doesn't really know anything about the units, it only knows about the map, and every command in the game which affects a unit needs to setbe passed to the movecostmap, doeven though its not actually a "map task", like healing units.
infantry.movecost[forest.getKey()] = 5
which is what I wantedThis seems like breaking the single responsibility pattern. However, storing references to get away fromall the units in several places (so both the first placeMap and couldsomething else store references to units) seems to be achievedtroublesome with associative arraysbookkeeping.
Am I missing an "obvious" solution here? Selecting everything by a unique key might become necessary when I start saving gamestate in some way, e.g. inTo sum it up a databaselittle, so is it the waycan you hint me to go anywaydata structure which are suitable to represent what I am describing? Thanks in advance!