LaurierCS / PHI-TextBasedGameEngine

A text based game engine for creating games
4 stars 1 forks source link

Context Manager #33

Open jbheard opened 3 years ago

jbheard commented 3 years ago

Create a global game context with basic functionality. This has a few parts:

  1. Base class for all GameObjects

    • The current approach is to have a list of enemies, items, etc. However, this is not very scalable. A better approach may be to have a base GameObject class and extend with enemy, item, etc specific behaviours. See below for a very simple example class.
    • This means our context can have a list of GameObjects and some base functionality without caring what they actually are
  2. Separation of game and context

    • Currently our Game object stores all game information, and leans towards maintaining context as well. Ideally, we should have a GameContext object that maintains the game context, and the Game class is only responsible
  3. Separation of template objects from instance objects

    • Since enemies & items are loaded into the game as templates and then used to spawn instances, we need some way to differentiate between an object being in-game, and just a template for creating an object.
    • Perhaps we could just store a list of yaml objects and have a method load_from_template implemented for all GameObjects? See example below.

GameObject Skeleton:

import abc

class GameObject():
    id = 1

    def __init__(self):
        self.id = GameObject.id
        GameObject.id += 1

    @abc.abstractmethod
    @staticmethod
    def load_from_template(template: dict):
        return

For the method load_from_template above, see here. It seems like static & abstract don't mingle very well on their own and need a custom fix.

jbheard commented 3 years ago

We will also want some object resolving functionality in the context manager. For example functions:

# Name is unique to templates, not instances, this can return many
def get_by_name(self, name: str) -> GameObject:
    return [obj for obj in self.objects if obj.name == name]

# IDs are unique to instances, this will only ever return 1
def get_by_id(self, id: int) -> GameObject:
    for obj in self.objects:
        if obj.id == id:
            return obj
    return None
jbheard commented 3 years ago

Added GameObject class in #34. The changes to Item also rely on having a sort of global game context.

Now we need to decide whether to add the context as:

I'm leaning towards making it instantiable and each Game object will have a single context. I'm not sure if this is the right way to go about things though. I doubt we will ever need to load multiple games simultaneously, but this approach would allow it.