Texture packs are working

This commit is contained in:
Yohann D'ANELLO 2020-11-06 17:43:30 +01:00
parent 4115363b74
commit f9dcc8f1c1
6 changed files with 61 additions and 38 deletions

View File

@ -1,11 +1,17 @@
import curses import curses
from .mapdisplay import MapDisplay from .mapdisplay import MapDisplay
from .texturepack import TexturePack
class Display: class Display:
def __init__(self, game, screen): def __init__(self, game, screen):
self.screen = screen self.screen = screen
self.game = game self.game = game
self.mapDisplay = MapDisplay(game.m, game.settings, curses.LINES, curses.COLS * 4/5) self.mapDisplay = MapDisplay(game.m,
TexturePack.get_pack(
game.settings.TEXTURE_PACK),
curses.LINES,
curses.COLS * 4 // 5)
def refresh(self): def refresh(self):
self.mapDisplay.refresh() self.mapDisplay.refresh()

View File

@ -1,25 +1,22 @@
#!/usr/bin/env python #!/usr/bin/env python
import curses import curses
from dungeonbattle.interfaces import Map from dungeonbattle.interfaces import Map
from dungeonbattle.settings import Settings from .texturepack import TexturePack
import .texturepack as tp
class MapDisplay: class MapDisplay:
def __init__(self, m: Map, settings : Settings, height: int, width: int): def __init__(self, m: Map, pack: TexturePack, height: int, width: int):
self.width = width self.width = width
self.height = height self.height = height
self.map = m self.map = m
self.pad = curses.newpad(m.height, m.width+1) self.pad = curses.newpad(m.height, m.width+1)
if self.settings.TEXTURE_PACK == 'ASCII' : self.pack = pack
self.textures = tp.ascii_textures
if self.settings.TEXTURE_PACK == 'SQUIRREL' :
self.textures = tp.squirrel_textures
def update_pad(self): def update_pad(self):
self.pad.addstr(0, 0, self.map.draw_string()) self.pad.addstr(0, 0, self.map.draw_string(self.pack))
for e in self.map.entities: for e in self.map.entities:
self.pad.addstr(e.y, e.x, self.textures[e.name]) self.pad.addstr(e.y, e.x, self.pack.PLAYER)
def display(self, y, x): def display(self, y, x):
deltay, deltax = (self.height // 2) + 1, (self.width //2) + 1 deltay, deltax = (self.height // 2) + 1, (self.width //2) + 1

View File

@ -1,13 +1,34 @@
ascii_textures = { class TexturePack:
"EMPTY" : ' ', _packs = dict()
"WALL" : '#',
"FLOOR" : '.',
"PLAYER" : '@'
}
squirrel_textures = { name: str
"EMPTY" : ' ', EMPTY: str
"WALL" : '', WALL: str
"FLOOR" : '.', FLOOR: str
"PLAYER" : '🐿️' PLAYER: str
}
def __init__(self, name: str, **kwargs):
self.name = name
self.__dict__.update(**kwargs)
TexturePack._packs[name] = self
@classmethod
def get_pack(cls, name: str) -> "TexturePack":
return cls._packs[name]
TexturePack.ASCII_PACK = TexturePack(
name="ascii",
EMPTY=' ',
WALL='#',
FLOOR='.',
PLAYER='@',
)
TexturePack.SQUIRREL_PACK = TexturePack(
name="squirrel",
EMPTY=' ',
WALL='',
FLOOR='.',
PLAYER='🐿️',
)

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
from enum import Enum from enum import Enum, auto
from dungeonbattle.settings import Settings
from dungeonbattle.display.texturepack import texturepack from dungeonbattle.display.texturepack import TexturePack
class Map: class Map:
""" """
@ -44,24 +45,22 @@ class Map:
return Map(width, height, tiles, []) return Map(width, height, tiles, [])
def draw_string(self) -> str: def draw_string(self, pack: TexturePack) -> str:
""" """
Draw the current map as a string object that can be rendered Draw the current map as a string object that can be rendered
in the window. in the window.
""" """
return "\n".join("".join(tile.value for tile in line) return "\n".join("".join(tile.char(pack) for tile in line)
for line in self.tiles) for line in self.tiles)
class Tile(Enum): class Tile(Enum):
if self.settings.TEXTURE_PACK == 'ASCII' : EMPTY = auto()
self.textures = tp.ascii_textures WALL = auto()
if self.settings.TEXTURE_PACK == 'SQUIRREL' : FLOOR = auto()
self.textures = tp.squirrel_textures
def char(self, pack: TexturePack) -> str:
EMPTY = self.textures["EMPTY"] return getattr(pack, self.name)
WALL = self.textures["WALL"]
FLOOR = self.textures["FLOOR"]
def is_wall(self) -> bool: def is_wall(self) -> bool:
return self == Tile.WALL return self == Tile.WALL

View File

@ -19,7 +19,7 @@ class Settings:
self.KEY_LEFT_SECONDARY = ['KEY_LEFT', 'Touche secondaire pour aller vers la gauche'] self.KEY_LEFT_SECONDARY = ['KEY_LEFT', 'Touche secondaire pour aller vers la gauche']
self.KEY_RIGHT_PRIMARY = ['d', 'Touche principale pour aller vers la droite'] self.KEY_RIGHT_PRIMARY = ['d', 'Touche principale pour aller vers la droite']
self.KEY_RIGHT_SECONDARY = ['KEY_RIGHT', 'Touche secondaire pour aller vers la droite'] self.KEY_RIGHT_SECONDARY = ['KEY_RIGHT', 'Touche secondaire pour aller vers la droite']
self.TEXTURE_PACK = ['ASCII', 'Pack de textures utilisé'] self.TEXTURE_PACK = ['ascii', 'Pack de textures utilisé']
def __getattribute__(self, item: str) -> Any: def __getattribute__(self, item: str) -> Any:
superattribute = super().__getattribute__(item) superattribute = super().__getattribute__(item)

View File

@ -17,7 +17,7 @@ class TestSettings(unittest.TestCase):
self.assertEqual(settings.KEY_DOWN_SECONDARY, 'KEY_DOWN') self.assertEqual(settings.KEY_DOWN_SECONDARY, 'KEY_DOWN')
self.assertEqual(settings.KEY_LEFT_SECONDARY, 'KEY_LEFT') self.assertEqual(settings.KEY_LEFT_SECONDARY, 'KEY_LEFT')
self.assertEqual(settings.KEY_RIGHT_SECONDARY, 'KEY_RIGHT') self.assertEqual(settings.KEY_RIGHT_SECONDARY, 'KEY_RIGHT')
self.assertEqual(settings.TEXTURE_PACK, 'ASCII') self.assertEqual(settings.TEXTURE_PACK, 'ascii')
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), settings.get_comment('TEXTURE_PACK')) self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), settings.get_comment('TEXTURE_PACK'))
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), 'Pack de textures utilisé') self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), 'Pack de textures utilisé')