simple map and graphics logic
This commit is contained in:
parent
444f621aa0
commit
413a3a19ca
|
@ -0,0 +1 @@
|
||||||
|
__pycache__/*
|
|
@ -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
|
|
@ -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
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
-+-
|
||||||
|
|X|
|
||||||
|
+-+
|
Loading…
Reference in New Issue