summaryrefslogtreecommitdiff
path: root/asciifarm/client/display/__init__.py
blob: 41383d638a022037bbe25bb6503b00c10f153d55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59


import curses
from .fieldpad import FieldPad
from .infopad import InfoPad
from .healthpad import HealthPad
from .screen import Screen

class Display:
    
    def __init__(self, stdscr, charMap, colours=False):
        
        self.screen = Screen(stdscr)
        self.fieldPad = FieldPad((64, 32), charMap.get("charwidth", 1), colours)
        self.characters = charMap["mapping"]
        self.defaultChar = charMap.get("default", "?")
        self.infoPad = InfoPad((100, 100))
        self.healthPad = HealthPad((20, 1))
        self.lastinfostring = None
        self.colours = colours
        if colours:
            curses.use_default_colors()
            for i in range(0, min(256, curses.COLORS, curses.COLOR_PAIRS)):
                curses.init_pair(i, i%16, i//16)
            
    
    def resizeField(self, size):
        self.fieldPad.resize(*size)
    
    def drawFieldCells(self, cells):
        for cell in cells:
            (x, y), spriteName = cell
            sprite = self.getChar(spriteName)
            self.fieldPad.changeCell(x, y, *sprite)
        self.screen.change()
    
    def setFieldCenter(self, pos):
        self.fieldPad.setCenter(pos)
    
    def showHealth(self, health, maxHealth):
        self.healthPad.setHealth(health, maxHealth)
        self.screen.change()
    
    def showInfo(self, infostring):
        if infostring != self.lastinfostring:
            self.infoPad.showString(infostring)
            self.screen.change()
            self.lastinfostring = infostring
    
    def getChar(self, sprite):
        char = self.characters.get(sprite, self.defaultChar)
        if isinstance(char, str):
            return [char]
        return char
    
    def update(self):
        self.screen.update(self.fieldPad, self.infoPad, self.healthPad)