Texture packs are working
This commit is contained in:
		@@ -1,11 +1,17 @@
 | 
			
		||||
import curses
 | 
			
		||||
from .mapdisplay import MapDisplay
 | 
			
		||||
from .texturepack import TexturePack
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Display:
 | 
			
		||||
    def __init__(self, game, screen):
 | 
			
		||||
        self.screen = screen
 | 
			
		||||
        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):
 | 
			
		||||
        self.mapDisplay.refresh()
 | 
			
		||||
 
 | 
			
		||||
@@ -1,25 +1,22 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
import curses
 | 
			
		||||
from dungeonbattle.interfaces import Map
 | 
			
		||||
from dungeonbattle.settings import Settings
 | 
			
		||||
import .texturepack as tp
 | 
			
		||||
from .texturepack import TexturePack
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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.height = height
 | 
			
		||||
        self.map = m
 | 
			
		||||
        self.pad = curses.newpad(m.height, m.width+1)
 | 
			
		||||
        if self.settings.TEXTURE_PACK == 'ASCII' :
 | 
			
		||||
            self.textures = tp.ascii_textures
 | 
			
		||||
        if self.settings.TEXTURE_PACK == 'SQUIRREL' :
 | 
			
		||||
            self.textures = tp.squirrel_textures
 | 
			
		||||
        self.pack = pack
 | 
			
		||||
 | 
			
		||||
    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:
 | 
			
		||||
            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):
 | 
			
		||||
        deltay, deltax = (self.height // 2) + 1, (self.width //2) + 1
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,34 @@
 | 
			
		||||
ascii_textures = {
 | 
			
		||||
    "EMPTY" : ' ',
 | 
			
		||||
    "WALL" : '#',
 | 
			
		||||
    "FLOOR" : '.',
 | 
			
		||||
    "PLAYER" : '@'
 | 
			
		||||
    }
 | 
			
		||||
class TexturePack:
 | 
			
		||||
    _packs = dict()
 | 
			
		||||
 | 
			
		||||
squirrel_textures = {
 | 
			
		||||
    "EMPTY" : ' ',
 | 
			
		||||
    "WALL" : '█',
 | 
			
		||||
    "FLOOR" : '.',
 | 
			
		||||
    "PLAYER" : '🐿️'
 | 
			
		||||
    }
 | 
			
		||||
    name: str
 | 
			
		||||
    EMPTY: str
 | 
			
		||||
    WALL: str
 | 
			
		||||
    FLOOR: str
 | 
			
		||||
    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='🐿️',
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,8 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
from enum import Enum
 | 
			
		||||
from dungeonbattle.settings import Settings
 | 
			
		||||
from dungeonbattle.display.texturepack import texturepack
 | 
			
		||||
from enum import Enum, auto
 | 
			
		||||
 | 
			
		||||
from dungeonbattle.display.texturepack import TexturePack
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Map:
 | 
			
		||||
    """
 | 
			
		||||
@@ -44,24 +45,22 @@ class Map:
 | 
			
		||||
        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
 | 
			
		||||
        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)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Tile(Enum):
 | 
			
		||||
    if self.settings.TEXTURE_PACK == 'ASCII' :
 | 
			
		||||
        self.textures = tp.ascii_textures
 | 
			
		||||
    if self.settings.TEXTURE_PACK == 'SQUIRREL' :
 | 
			
		||||
        self.textures = tp.squirrel_textures
 | 
			
		||||
    
 | 
			
		||||
    EMPTY = self.textures["EMPTY"]
 | 
			
		||||
    WALL = self.textures["WALL"]
 | 
			
		||||
    FLOOR = self.textures["FLOOR"]
 | 
			
		||||
    EMPTY = auto()
 | 
			
		||||
    WALL = auto()
 | 
			
		||||
    FLOOR = auto()
 | 
			
		||||
 | 
			
		||||
    def char(self, pack: TexturePack) -> str:
 | 
			
		||||
        return getattr(pack, self.name)
 | 
			
		||||
 | 
			
		||||
    def is_wall(self) -> bool:
 | 
			
		||||
        return self == Tile.WALL
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ class Settings:
 | 
			
		||||
        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_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:
 | 
			
		||||
        superattribute = super().__getattribute__(item)
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ class TestSettings(unittest.TestCase):
 | 
			
		||||
        self.assertEqual(settings.KEY_DOWN_SECONDARY, 'KEY_DOWN')
 | 
			
		||||
        self.assertEqual(settings.KEY_LEFT_SECONDARY, 'KEY_LEFT')
 | 
			
		||||
        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), 'Pack de textures utilisé')
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user