Beispiel #1
0
    def __init__(self):
        self.noobs = []
        self.next_id = []
        self.current_frame = 0
        self.game = None

        if USE_DATASTORE:
            self.mongo_conn = pymongo.Connection()
            self.datastore = self.mongo_conn.snockerball
            self.datastore.add_son_manipulator(JSONTransformer())
        else:
            self.datastore = None

        self.metagrid = MetaGrid(self.datastore)
        self.client_manager = ClientManager()
        self.ghosts = {}
Beispiel #2
0
class Engine:
    "The engine controls the whole game, updating entities and shit"

    def __init__(self):
        self.noobs = []
        self.next_id = []
        self.current_frame = 0
        self.game = None

        if USE_DATASTORE:
            self.mongo_conn = pymongo.Connection()
            self.datastore = self.mongo_conn.snockerball
            self.datastore.add_son_manipulator(JSONTransformer())
        else:
            self.datastore = None

        self.metagrid = MetaGrid(self.datastore)
        self.client_manager = ClientManager()
        self.ghosts = {}

    def save_world(self, save_terrain=False):

        if not USE_DATASTORE:
            return
        log("saving world..")

        for cell in self.metagrid.cells:
            self.metagrid.cells[cell].persist()
            if save_terrain:
                self.metagrid.cells[cell].persist_terrain()
        log("saving ghosts.")
        for ghost in self.ghosts:
            log("persisting %s" % self.ghosts[ghost])
            self.ghosts[ghost].persist(self.datastore)
        log("saving complete.")

    def build_world(self):

        generate = True

        if generate:
            log("Generating world.")
            if USE_DATASTORE:
                self.datastore.entities.remove()

        for x in range(METAGRID_SIZE):
            for y in range(METAGRID_SIZE):
                g_x = x * GRID_SIZE
                g_y = y * GRID_SIZE
                # log('new metacell at %d, %d' % (g_x, g_y))
                cell = self.metagrid.add_cell(g_x, g_y)

                if generate and self.game:
                    self.game.new_metagrid_cell(cell)
        if generate:
            if self.game != None:
                self.game.build_world()
            self.save_world(save_terrain=True)
        else:
            log("Loading world")
            entities = self.datastore.entities.find()

            for ent in entities:
                ent_dict = {}
                for var in ent:
                    ent_dict[str(var)] = ent[var]
                guy = entity_class_registry[ent["__class__"]](**ent_dict)
            log("Load complete")

    def add_entity(self, new_guy, moving=False):
        if hasattr(new_guy, "pos") and (new_guy.pos):
            self.metagrid.add_entity(new_guy, moving)
        else:
            if not isinstance(new_guy, Updater):
                log("Why add %s, a ghost that does not update?" % new_guy)
            self.ghosts[new_guy.id] = new_guy

    def remove_entity(self, ent, moving=True):
        if hasattr(ent, "pos") and ent.pos:
            self.metagrid.remove_entity(ent, moving)
        if ent.id in self.ghosts:
            del self.ghosts[ent.id]

    def get_entities(self, x, y):
        return self.metagrid.get_entities(x, y)

    def get_entity(self, ent_id):
        if ent_id in self.ghosts:
            return self.ghosts[ent_id]
        return self.metagrid.get_entity(ent_id)

    def make_id(self):
        return "".join([random.choice(ID_CHARS) for x in range(16)])

    def update(self):
        for id in self.ghosts:
            self.ghosts[id].update()
        self.metagrid.update()
        self.client_manager.update()