Esempio n. 1
0
class World(object):
    cell_class = Cell

    def __init__(self):
        self.cells = {}
        self.leaderboard_names = []
        self.leaderboard_groups = []
        self.top_left = Vec(0, 0)
        self.bottom_right = Vec(0, 0)
        self.reset()

    def reset(self):
        """
        Clears the `cells` and leaderboards, and sets all corners to `0,0`.
        """
        self.cells.clear()
        del self.leaderboard_names[:]
        del self.leaderboard_groups[:]
        self.top_left.set(0, 0)
        self.bottom_right.set(0, 0)

    def create_cell(self, cid):
        """
        Creates a new cell in the world.
        Override to use a custom cell class.
        """
        self.cells[cid] = self.cell_class()

    @property
    def center(self):
        return Vec(self.size.x/2,self.size.y/2)

    @property
    def size(self):
        return self.top_left.abs() + self.bottom_right.abs()

    def __eq__(self, other):
        """Compares two worlds by comparing their leaderboards."""
        for ls, lo in zip(self.leaderboard_names, other.leaderboard_names):
            if ls != lo:
                return False
        for ls, lo in zip(self.leaderboard_groups, other.leaderboard_groups):
            if ls != lo:
                return False
        if self.top_left != other.top_left:
            return False
        if self.bottom_right != other.bottom_right:
            return False
        return True
Esempio n. 2
0
class Cell(object):
    def __init__(self, *args, **kwargs):
        self.pos = Vec()
        self.update(*args, **kwargs)

    def update(self, cid=-1, x=0, y=0, size=0, name='',
               color=(1, 0, 1), is_virus=False, is_agitated=False, skin_url=''):
        self.cid = cid
        self.pos.set(x, y)
        self.size = size
        self.mass = size ** 2 / 100.0
        self.name = getattr(self, 'name', name) or name
        self.color2 = color
        self.color = tuple(map(lambda rgb: rgb / 255.0, color))
        self.is_virus = is_virus
        self.is_agitated = is_agitated
        self.skin_url = skin_url

    @property
    def is_food(self):
        return self.size < 20 and not self.name

    @property
    def is_ejected_mass(self):
        return self.size in (37, 38) and not self.name

    def same_player(self, other):
        """
        Compares name and color.
        Returns True if both are owned by the same player.
        """
        return self.name == other.name \
            and self.color == other.color

    def __lt__(self, other):
        if self.mass != other.mass:
            return self.mass < other.mass
        return self.cid < other.cid