asciigame/map.py

90 lines
2.6 KiB
Python
Raw Normal View History

2022-04-27 17:58:19 -05:00
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:
2022-04-29 16:07:21 -05:00
def __init__(self, pos, ttype, name="none"):
2022-04-27 17:58:19 -05:00
self.x = pos[0]
self.y = pos[1]
self.ttype = ttype
self.textures = {}
2022-04-29 16:07:21 -05:00
self.name = name
2022-04-27 17:58:19 -05:00
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[list(self.textures.keys())[0]].texture
2022-04-27 17:58:19 -05:00
except IndexError:
raise TileError("no textures to render!")
def use(self):
""" This method is designed to be replaced. """
pass