diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96403d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/* diff --git a/map.py b/map.py new file mode 100644 index 0000000..915a2a4 --- /dev/null +++ b/map.py @@ -0,0 +1,88 @@ +class TextureParsingError(BaseException): + pass + +class Texture: + def __init__(self, ttype, reference, name): + self.ttype = ttype + self.tref = reference + self.name = name + if ttype == "char": + self.texture = [reference] + self.loaded = True + self.dims = [1, 1] + elif ttype == "mchar": + self.loaded = False + self.texture = None + self.dims = None + # lazy load with Texture().load() + def load(self): + if self.loaded: + return + try: + with open("textures/" + self.tref + ".texture") as fd: + self.texture = [i.rstrip() for i in fd.readlines()] + except FileNotFoundError: + raise TextureParsingError("error parsing " + self.tref) + + # check if texture is 'jagged', e.g. + # there are multiple line lengths + j = None + for i in self.texture: + if not j: + j = len(i) + elif len(i) != j: + self.loaded = False + raise TextureParsingError("error parsing " + self.tref) + + # provide dimensions (j is still in scope) + self.dims = [len(self.texture), j] + self.loaded = True + +class TileError(BaseException): + pass + +class Tile: + def __init__(self, pos, ttype): + self.x = pos[0] + self.y = pos[1] + self.ttype = ttype + self.textures = {} + self.dims = None + self.animation = 0 + def getCoords(self): + return [self.x, self.y] + def addTexture(self, texture): + # check if texture is consistent + # with other textures' dimensions + + if not self.dims: + self.dims = texture.dims + else: + if not self.dims == texture.dims: + raise TileError("texture dimensions are not consistent") + + self.textures[texture.name] = texture + def update(self, tiles): + """ + This method is designed to be replaced. + + This method should be called exactly once + for each rendered frame, regardless if it is + visible. + """ + pass + def render(self): + """ + This method is designed to be replaced. + + This method should be called on every frame + this Tile is visible. + """ + try: + return self.textures[0] + except IndexError: + raise TileError("no textures to render!") + + def use(self): + """ This method is designed to be replaced. """ + pass diff --git a/render.py b/render.py new file mode 100644 index 0000000..ae76006 --- /dev/null +++ b/render.py @@ -0,0 +1,47 @@ +class RendererLoopBreak(BaseException): + pass + +class Renderer: + """ + Class responsible for updating tiles, their states, animations, etc. + + In the future, also takes in a WaterLayer and updates it with the game map. + """ + def __init__(self): + self.tiles = [] + # location of the top left corner of the viewpoint + self.pos = [0, 0] + self.viewport = [80, 24] + + def addTile(self, tile): + self.tiles.append(tile) + + def massUpdate(self): + for i in self.tiles: + if i.ttype == "standard": + i.update(self.tiles) + + def selectForRendering(self): + """ + Returns what tiles are in the viewport. + """ + + tilesInScene = [] + + for i in self.tiles: + dims = i.dims + pos = i.getCoords() + + try: + for j in range(dims[0]): + for k in range(dims[1]): + if self.inViewport([pos[0] + j, pos[1] + k]): + tilesInScene.append(i) + raise RendererLoopBreak + except RendererLoopBreak: + pass + + self.cachedTiles = tilesInScene + + return tilesInScene + diff --git a/textures/simple.texture b/textures/simple.texture new file mode 100644 index 0000000..de55ac7 --- /dev/null +++ b/textures/simple.texture @@ -0,0 +1,3 @@ +-+- +|X| ++-+