Game Design – Architecture for Tower Defense Game with Multiple Towers

Architecturedesigndesign-patterns

I'm trying to create a Tower Defense game where minions move through a labyrinth and you can build towers on top of the labyrinths' walls to stop the minions. I'm personally using Python and pygame, but I'm looking for generic design answers that can be applied to any OOP language.

So, since both minions and towers have a size and a position, as well as a sprite image, I felt like it'd be appropriate to create a common base class:

class Entity:

    def __init__(self, image, size, position):
        self.image = image
        self.size = size
        self.position = position

    def draw(self, surface):
        surface.blit(self.image, self.position, self.size)

Alright so I want my game to work similar to Vector TD, where you have multiple different types of towers that can be build, but you can build multiple instances of each type, and each instance can then be upgraded individually. What this means is that I need some kind of generic way to explain how all towers of similar type work, yet I need each instance to be separated. So I began with a Tower base class with all the properties I can think of:

class Tower(Entity):

    def __init__(self, image, size, position, attack_speed, attack_damage, attack_splash_radius, attack_range, cost):
        super().__init__(image, size, position)
        self.attack_speed = attack_speed
        self.attack_damage = attack_damage
        ...  # etc

But the problem is, all turrets of the same type have same range, same attack speed, same cost, etc. but they're still individual instances, so it would feel stupid to copy all of these common properties with the same values to every instance separately.

The reason I don't just subclass Tower to create my custom tower types is because they don't really add anything to the class. Literally the only thing they would do is change the default values for the __init__'s arguments. Using subclasses like this would also lead into the common values being copied into every instance, so if my subclass GreenTower defaulted range=800 for all my turrets, it would mean that the value 800 would be stored separately into every instance (when they could just use a common value). I just feel like there's a better way.

So my question is entirely about the design/architecture/structure of the system. How should I group these properties together while still allowing individual turrets to be upgraded separately? Ideally I could be able to design these tower types in something like JSON where I can dynamically load them to the game.

Best Answer

It sounds like a use-case for a Flyweight. Essentially pull all the shared details for your turrets as a single flyweight instance and each turret then refers back to that object. Note that you can have more than one flyweight so this doesn't limit you in any way. If you end up with close to one flyweight instance for each turret, then it's probably not worth it, though.

You can also decorate your turret instances so that you get most of the properties from the flyweight but modify specific ones as needed for specific turrets. You can even have flyweights that decorate other flyweights. It all depends on your needs.

Related Topic