""" Copyright 2008 Ben Sarsgard This file is part of ZedZed. ZedZed 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. ZedZed 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 ZedZed. If not, see . """ from pygame.sprite import Sprite from helpers import * class Feature(object): DIRECTION_E = 0 DIRECTION_ES = 1 DIRECTION_ESW = 2 DIRECTION_EW = 3 DIRECTION_N = 4 DIRECTION_NE = 5 DIRECTION_NES = 6 DIRECTION_NESW = 7 DIRECTION_NEW = 8 DIRECTION_NS = 9 DIRECTION_NSW = 10 DIRECTION_NW = 11 DIRECTION_S = 12 DIRECTION_SW = 13 DIRECTION_W = 14 DECO_FLOOR_COUNT = 16 DECO_GRASS_COUNT = 3 DECO_WALL_COUNT = 22 BLOCK_GRASS_COUNT = 1 OBJ_FLOOR_COUNT = 7 OBJ_GRASS_COUNT = 1 STORAGE_FLOOR_COUNT = 9 def __init__(self, description, direction=None, image_name='feature.png', color=(0,0,0)): self.description = description self.color = color self.direction = direction self.children = [] self.image_name = image_name self.RefreshImage() def RefreshImage(self): if self.image_name is None: self.image = None else: self.image = load_image(self.image_name, -1) def GetName(self): return "%s" % (self.description) def AppendChild(self, feature): self.children.append(feature) def HasChildren(self): return len(self.children) > 0 def __getstate__(self): #return (self.weight, self.description, self.image_name) self.image = None return self.__dict__ def __setstate__(self, state): #self.weight, self.description, self.image_name = state self.__dict__.update(state) self.RefreshImage() class OpenableFeature(Feature): def __init__(self, description, is_open, direction=None, image_name='feature.png', color=(0,0,0)): self.is_open = is_open Feature.__init__(self, description, direction, image_name, color) def Open(self): self.is_open = True self.RefreshImage() def Close(self): self.is_open = False self.RefreshImage() class Wall(Feature): def __init__(self, direction=None, image_name=None, color=(0,0,255)): #self.child_feature = None Feature.__init__(self, None, direction, image_name, color) def RefreshImage(self): if self.direction == Feature.DIRECTION_E: self.image_name = 'wall_e.png' elif self.direction == Feature.DIRECTION_ES: self.image_name = 'wall_es.png' elif self.direction == Feature.DIRECTION_ESW: self.image_name = 'wall_esw.png' elif self.direction == Feature.DIRECTION_EW: self.image_name = 'wall_ew.png' elif self.direction == Feature.DIRECTION_N: self.image_name = 'wall_n.png' elif self.direction == Feature.DIRECTION_NE: self.image_name = 'wall_ne.png' elif self.direction == Feature.DIRECTION_NES: self.image_name = 'wall_nes.png' elif self.direction == Feature.DIRECTION_NESW: self.image_name = 'wall_nesw.png' elif self.direction == Feature.DIRECTION_NEW: self.image_name = 'wall_new.png' elif self.direction == Feature.DIRECTION_NS: self.image_name = 'wall_ns.png' elif self.direction == Feature.DIRECTION_NSW: self.image_name = 'wall_nsw.png' elif self.direction == Feature.DIRECTION_NW: self.image_name = 'wall_nw.png' elif self.direction == Feature.DIRECTION_S: self.image_name = 'wall_s.png' elif self.direction == Feature.DIRECTION_SW: self.image_name = 'wall_sw.png' elif self.direction == Feature.DIRECTION_W: self.image_name = 'wall_w.png' for feature in self.children: feature.direction = self.direction feature.RefreshImage() if isinstance(feature, Door): self.image_name = None Feature.RefreshImage(self) def AddDoor(self, description): success = False if len(self.children) == 0: # a door can be the only child self.children.append(Door(description, self.direction)) success = True return success def AddWindow(self, description): success = False if len(self.children) == 0: # a window can be the only child self.children.append(Window(description, self.direction)) success = True return success def HasDoor(self): has_door = False for feature in self.children: if isinstance(feature, Door): has_door = True break return has_door def HasWindow(self): has_window = False for feature in self.children: if isinstance(feature, Window): has_window = True break return has_window def GetName(self): return None class Window(Feature): def __init__(self, description, direction, image_name=None, color=(0,0,255)): Feature.__init__(self, description, direction, image_name, color) def RefreshImage(self): if self.direction == Feature.DIRECTION_NS: self.image_name = 'window_ns.png' elif self.direction == Feature.DIRECTION_EW: self.image_name = 'window_ew.png' else: self.image_name = None Feature.RefreshImage(self) def GetName(self): return "Window (%s)" % (self.description) class Door(OpenableFeature): def __init__(self, description, direction, image_name=None, color=(125,0,0)): OpenableFeature.__init__(self, description, False, direction, image_name, color) def GetName(self): status_string = '' if self.is_open: status_string = 'Open' else: status_string = 'Closed' return "Door, %s (%s)" % (status_string, self.description) def RefreshImage(self): if self.is_open: if self.direction == Feature.DIRECTION_NS: self.image_name = 'door_open_ns.png' elif self.direction == Feature.DIRECTION_EW: self.image_name = 'door_open_ew.png' else: self.image_name = None else: if self.direction == Feature.DIRECTION_NS: self.image_name = 'door_closed_ns.png' elif self.direction == Feature.DIRECTION_EW: self.image_name = 'door_closed_ew.png' else: self.image_name = None OpenableFeature.RefreshImage(self)