Source

Fat x Fast / FatxFast / objects / entity.py

Full commit

# This file is part of FatxFast.
#
#    FatxFast is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"


import pygame
import time
from FatxFast.tilemap.editor.tile import TILEWIDTH, TILEHEIGHT
from FatxFast.resource import get_color, resource_manager

class GameEntity(pygame.sprite.Sprite):
    """Object entites base class. Handles all the objects which are not
    part of the map. These objects vary in size and are freely assigned a
    position & size attributes. They require an image & a pygame rect for
    positional information"""

    # Default image for entities
    image = pygame.Surface((TILEWIDTH, TILEHEIGHT))
    def __init__(self, **kwargs):
        super(GameEntity, self).__init__()
        self.entity_id = int(kwargs.get("entity_id", 0))
        self.name = kwargs.get("name", "NONAME_ENTITY")
        self.rect = kwargs.get("rect", pygame.Rect(
            int(kwargs.get("x", 0)), 
            int(kwargs.get("y", 0)),
            int(kwargs.get("width", TILEWIDTH)), 
            int(kwargs.get("height", TILEHEIGHT))))
        if "imagename" in kwargs:
            self.set_image(kwargs["imagename"], 
                get_color(kwargs.get("colorkey", "black")))
        self.invisible = int(kwargs.get("invisible", 0))

    def set_image(self, imagename, colorkey=get_color("black")):
        self.image = resource_manager.load_image(imagename, 
            colorkey)

    def get_position(self):
        return self.rect.topleft

    def set_position(self, pos):
        self.rect.topleft = pos

class Observer(object):
    
    def handle_event(self, event):
        pass

class Observable(Observer):

    def __init__(self):
        self.observers = []
    
    def detach(self, observer):
        self.observers.remove(observer)
    
    def attach(self, observer):
        self.observers.append(observer)
    
    def notify(self, event):
        for obs in self.observers:
            obs.handle_event(event)