############################################################################# ## This file is part of TunnelHack II. ## ##-------------------------------------------------------------------------## ## Copyright 2007-2008 Bryce Schroeder ## ## bryce.schroeder@gmail.com ## ## http://www.ferazelhosting.net/~bryce/ ## ##-------------------------------------------------------------------------## ## This program 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. ## ## This program 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 this program. If not, see . ## ############################################################################# from .standard_interpreters import Resource, R from . import g from .interface import IEventHandler #import interface from .windowgroup import WindowedGroup import cStringIO import pygame class MapInstance(IEventHandler): """ This class represents the map when the game is using it. It also impelements the event handler interface. """ def __init__(self, map, window=pygame.rect.Rect(0,0,800,600)): self.map = map # self.scrolled = False self.props = WindowedGroup() self.effects = WindowedGroup() self.status_info = WindowedGroup() self.prop_names = {} # viewing offset self.cords = 0,0 self.window = window # someday... self.lighting = None def scroll(self, dx, dy): w,h = self.map.get_surface().get_size() self.cords = ( max(min(self.cords[0]+dx,w-self.window.w),0), max(min(self.cords[1]+dy,h-self.window.h),0)) # self.scrolled = True def draw(self, target, dirty = []): surf = self.map.get_surface() for x in dirty: target.blit(surf, x, (x.x+self.cords[0], x.y+self.cords[1],x.w,x.h)) dirty = [] dirty.extend(self.props.draw(target, self.window, surf, self.cords)) dirty.extend(self.effects.draw(target, self.window, surf, self.cords)) dirty.extend(self.status_info.draw(target,self.window,surf,self.cords)) return dirty def clear(self, target): target.blit(self.map.get_surface(),(0,0)) pygame.display.flip() def add_prop(self, prop, name='default', x=0, y=0): self.props.add(prop) prop.map = self prop.name = name self.prop_names[name] = prop prop.rect.x, prop.rect.y = x,y def remove_prop(self, name): self.props.remove(self.prop_names[name]) del self.prop_names[name] def remove_prop_by_value(self, prop): pname = prop.name self.props.remove(prop) del self.prop_names[pname] def get_prop(self, name): return self.prop_names[name] # Event handling functions def update(self): self.props.update() self.effects.update() self.status_info.update() def raw_event(self, pe): if pe.type == pygame.QUIT: g.done = True return True #def accept_key(self, uni, key, mods): # return True def accept_drop(self, item, x ,y): #print "ASKED" #print "DROPPED", item.name # TEST CODE. boom = g.resources.get("Prop", "MBoom2").instance() g.level.add_prop(boom, x=x-64,y=y-64) boom.add_order('run') # END TEST CODE return True def handle_drop(self, item): pass def process_mouse(self, x, y): return True def request_focus(self): self.window_manager.request_focus(None) def handle_key(self, uni, key, mods): # very temporary code pass class Cell(object): def __init__(self, parent, data, x, y): self.x = x self.y = y self.parent = parent self.floor = g.resources.get("Tileset", self.parent.palette[data[0]]).get( int(data[1:3],16)) if data[3] != '-': self.wall = g.resources.get("Tileset", self.parent.palette[data[3]]).get( int(data[4:6],16)) else: self.wall = None self.passability = int(data[6],16) def render(self, target): self.floor.blit(target, self.x*32, self.y*32) if self.wall: self.wall.blit(target, self.x*32, self.y*32) class Map(Resource): MANDATORY = ['level', 'palette'] #OPTIONAL = ['meta'] def init(self): self.meta = {} if self.parts.has_key('meta'): self.meta = self.process_meta(self.parts['meta']) self.palette = {} for row in self.parts['palette'].split("\n"): if not row or row[0] == '#': continue op, opr1, opr2 = row.split() if op == 'set': self.palette[opr1] = opr2 # load the file into nested lists. self.image = None self.dirty = True def integrate(self): self.matrix = [ [Cell(self,cell,x,y) for x, cell in enumerate(row.split())] for y,row in enumerate(self.parts['level'].split('\n'))] self.theight = len(self.matrix) self.twidth = len(self.matrix[0]) def render(self): if not self.image: self.image = pygame.Surface((32*self.twidth, 32*self.theight), pygame.SRCALPHA) for row in self.matrix: for cell in row: cell.render(self.image) self.dirty = False def get_surface(self): if self.dirty: self.render() return self.image def instance(self): return MapInstance(self) #def load_palette(self):