On Friday, 8 March 2024 at 16:54:48 UTC, cc wrote:
> If you don't want Unit to be a template, you can just have Map derive from a basic interface or abstract class. You can also have every relevant class share similar templates, you just need to remember to supply the template arguments everywhere.
Right. Interfaces. I haven't used this feature yet, but I had read about them, thinking I would likely use them. I forgot about them at the time of making this thread. I will try making Map & Tile an interface, and maybe Unit as well.
Can an interface and a template have the same name, similar to function overloading? In that case, Map!Tile
would refer to the template, while Map
would be the interface. Maybe I'll have figured this out myself by the time someone replies.
> You'll need to think about how much interoperation you want between these classes. Does Unit really need to know what TileType map is using, or can it just trust that when it asks Map to move, Map will handle everything related to tile types? Generally it's best practice to have as much of the inner workings isolated as possible and just provide methods to access functionality.
I don't think Tile
and Unit
will need to know the details of what kind of Tile & Unit derivative the map is using.
As an aside on this topic, I wonder if my Map
class and it's derivative Mission
are doing too many things. I don't know if there is enough of a problem with one "master" class dominating the program that it's worth splitting it up despite the more complex programming.
> > After changing class Unit
to class Unit (TileType)
, it complains about the line Unit* occupant;
in Tile.
Are you sure you need a pointer here? Class objects in D are already reference-type by default.
I'm pretty sure I do, as references can't be null. As an approximation of what I'm doing, think of Map
as a chess board, Tile
as a square on the board, and Unit
as a chess piece. Not every tile on the chess board is occupied by a piece.
If you want, you can see the program I have so far on my GitHub repository. Right now the master
branch is the most up-to-date, but I might soon make a new branch to try out templates and interfaces.