summaryrefslogtreecommitdiff
path: root/asciifarm
diff options
context:
space:
mode:
Diffstat (limited to 'asciifarm')
-rw-r--r--asciifarm/client/client.py14
-rw-r--r--asciifarm/client/display/__init__.py45
-rw-r--r--asciifarm/client/display/fieldpad.py7
-rw-r--r--asciifarm/client/display/healthpad.py33
-rw-r--r--asciifarm/client/display/infopad.py7
-rw-r--r--asciifarm/client/display/screen.py15
6 files changed, 82 insertions, 39 deletions
diff --git a/asciifarm/client/client.py b/asciifarm/client/client.py
index feb7057..5ad91e0 100644
--- a/asciifarm/client/client.py
+++ b/asciifarm/client/client.py
@@ -13,10 +13,8 @@ from .display.screen import Screen
import string
from .display import Display
-
#logging.basicConfig(filename="client.log", filemode='w', level=logging.DEBUG)
-
class Client:
def __init__(self, stdscr, display, name, connection, keybindings):
@@ -33,7 +31,8 @@ class Client:
for key, action in self.commands.items()
if chr(key) in string.printable)
- self.info = {}
+ self.display.showInfo(self.controlsString)
+
def start(self):
@@ -82,15 +81,12 @@ class Client:
self.display.setFieldCenter(msg[1])
if msgType == "health":
- self.display.showHealth(*msg[1])
+ self.display.setHealth(*msg[1])
if msgType == "inventory":
- self.info["inventory"] = msg[1]
+ self.display.setInventory(msg[1])
if msgType == "ground":
- self.info["ground"] = msg[1]
+ self.display.setGround(msg[1])
- infostring = json.dumps(self.info, indent=2)
- infostring += "\n\n" + self.controlsString
- self.display.showInfo(infostring)
self.display.update()
diff --git a/asciifarm/client/display/__init__.py b/asciifarm/client/display/__init__.py
index 41383d6..80e2904 100644
--- a/asciifarm/client/display/__init__.py
+++ b/asciifarm/client/display/__init__.py
@@ -4,8 +4,15 @@ import curses
from .fieldpad import FieldPad
from .infopad import InfoPad
from .healthpad import HealthPad
+from .inventorypad import InventoryPad
from .screen import Screen
+
+SIDEWIDTH = 20
+HEALTHHEIGHT = 2
+INVENTORYHEIGHT = 12
+
+
class Display:
def __init__(self, stdscr, charMap, colours=False):
@@ -15,9 +22,12 @@ class Display:
self.characters = charMap["mapping"]
self.defaultChar = charMap.get("default", "?")
self.infoPad = InfoPad((100, 100))
- self.healthPad = HealthPad((20, 1))
+ self.healthPad = HealthPad(20, ("@",39), ("-",23), colours)
+ self.inventoryPad = InventoryPad("Inventory", 16)
+ self.groundPad = InventoryPad("Ground", 8)
self.lastinfostring = None
self.colours = colours
+ self.changed = False
if colours:
curses.use_default_colors()
for i in range(0, min(256, curses.COLORS, curses.COLOR_PAIRS)):
@@ -32,28 +42,49 @@ class Display:
(x, y), spriteName = cell
sprite = self.getChar(spriteName)
self.fieldPad.changeCell(x, y, *sprite)
- self.screen.change()
+ self.change()
def setFieldCenter(self, pos):
self.fieldPad.setCenter(pos)
- def showHealth(self, health, maxHealth):
+ def setHealth(self, health, maxHealth):
self.healthPad.setHealth(health, maxHealth)
- self.screen.change()
+ self.change()
def showInfo(self, infostring):
if infostring != self.lastinfostring:
self.infoPad.showString(infostring)
- self.screen.change()
+ self.change()
self.lastinfostring = infostring
+ def setInventory(self, items):
+ self.inventoryPad.setInventory(items)
+ self.change()
+
+ def setGround(self, items):
+ self.groundPad.setInventory(items)
+ self.change()
+
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)
+ def change(self):
+ self.changed = True
+ def update(self):
+ if self.changed:
+ fieldRight = min(self.fieldPad.getWidth(), self.screen.getWidth()-SIDEWIDTH-1)
+ healthBottom = self.healthPad.getHeight()
+ inventoryBottom = healthBottom + self.inventoryPad.getHeight()
+ groundBottom = inventoryBottom + self.groundPad.getHeight()
+ self.fieldPad.update(self, 0,0,fieldRight, min(self.fieldPad.getHeight(), self.screen.getHeight()))
+ self.healthPad.update(self, fieldRight+1,0, self.screen.getWidth(), healthBottom)
+ self.inventoryPad.update(self, fieldRight+1, healthBottom, self.screen.getWidth(), min(self.screen.getHeight(), inventoryBottom))
+ self.groundPad.update(self, fieldRight+1, inventoryBottom, self.screen.getWidth(), min(self.screen.getHeight(), groundBottom))
+ self.infoPad.update(self, fieldRight+1,groundBottom+1, self.screen.getWidth(), self.screen.getHeight())
+ curses.doupdate()
+ self.changed = False
diff --git a/asciifarm/client/display/fieldpad.py b/asciifarm/client/display/fieldpad.py
index ac69678..461cb57 100644
--- a/asciifarm/client/display/fieldpad.py
+++ b/asciifarm/client/display/fieldpad.py
@@ -12,6 +12,8 @@ class FieldPad:
self.charSize = charSize
self.center = (0, 0)
self.colours = colours
+ self.changed = False
+ self.lastView = None
def resize(self, width, height):
self.size = (width, height)
@@ -22,6 +24,7 @@ class FieldPad:
self.pad.addstr(y, x*self.charSize, char, curses.color_pair(colour))
else:
self.pad.addstr(y, x*self.charSize, char)
+ self.changed = True
def setCenter(self, pos):
self.center = pos
@@ -33,6 +36,10 @@ class FieldPad:
return self.size[1]
def update(self, screen, x, y, xmax, ymax):
+ if not self.changed and (x, y, xmax, ymax) == self.lastView:
+ return
+ self.lastView = (x, y, xmax, ymax)
+ self.changed = False
width = xmax-x
height = ymax-y
self.pad.noutrefresh(
diff --git a/asciifarm/client/display/healthpad.py b/asciifarm/client/display/healthpad.py
index 17baf35..64838d3 100644
--- a/asciifarm/client/display/healthpad.py
+++ b/asciifarm/client/display/healthpad.py
@@ -1,21 +1,38 @@
import curses
-
-
class HealthPad:
-
-
- def __init__(self, size=(1,1), *args):
- self.pad = curses.newpad(size[1], size[0])
- self.size = size
+ def __init__(self, width=1, char=('@',0), emptyChar=('-',0), colours=False):
+ self.char = char
+ self.emptyChar = emptyChar
+ self.pad = curses.newpad(2, width+1)
+ self.width = width
+ self.changed = False
+ self.lastView = None
+ self.colours = colours
def setHealth(self, health, maxHealth):
self.pad.erase()
- self.pad.addstr(0,0,"Health: {}/{}".format(health, maxHealth))
+ barEnd = round(health/maxHealth * self.width)
+ self.pad.addstr(0,0,"Health: {}/{}".format(health, maxHealth)[:self.width])
+ if self.colours:
+ self.pad.addstr(1,0, self.char[0]*barEnd, curses.color_pair(self.char[1]))
+ self.pad.addstr(1,barEnd, self.emptyChar[0]*(self.width-barEnd), curses.color_pair(self.emptyChar[1]))
+ else:
+ self.pad.addstr(1,0, self.char[0]*barEnd)
+ self.pad.addstr(1,barEnd, self.emptyChar[0]*(self.width-barEnd))
+
+ self.changed = True
+
+ def getHeight(self):
+ return 2
def update(self, screen, x, y, xmax, ymax):
+ if not self.changed and (x, y, xmax, ymax) == self.lastView:
+ return
+ self.lastView = (x, y, xmax, ymax)
+ self.changed = False
self.pad.noutrefresh(
0,
0,
diff --git a/asciifarm/client/display/infopad.py b/asciifarm/client/display/infopad.py
index 6fd8655..6de3692 100644
--- a/asciifarm/client/display/infopad.py
+++ b/asciifarm/client/display/infopad.py
@@ -10,12 +10,19 @@ class InfoPad:
def __init__(self, size=(1,1), *args):
self.pad = curses.newpad(size[1], size[0])
self.size = size
+ self.changed = False
+ self.lastView = None
def showString(self, string):
self.pad.clear()
self.pad.addstr(0,0,string)
+ self.changed = True
def update(self, screen, x, y, xmax, ymax):
+ if not self.changed and (x, y, xmax, ymax) == self.lastView:
+ return
+ self.lastView = (x, y, xmax, ymax)
+ self.changed = False
self.pad.noutrefresh(
0,
0,
diff --git a/asciifarm/client/display/screen.py b/asciifarm/client/display/screen.py
index ec49d34..ee4ba94 100644
--- a/asciifarm/client/display/screen.py
+++ b/asciifarm/client/display/screen.py
@@ -4,8 +4,6 @@ from .fieldpad import FieldPad
import signal
-SIDEWIDTH = 20
-HEALTHHEIGHT = 1
class Screen:
@@ -14,7 +12,6 @@ class Screen:
curses.curs_set(0)
self.stdscr = stdscr
self.height, self.width = self.stdscr.getmaxyx()
- self.changed = False
signal.signal(signal.SIGWINCH, self.updateSize)
def updateSize(self, *args):
@@ -29,16 +26,4 @@ class Screen:
def getHeight(self):
return self.height
-
- def change(self):
- self.changed = True
-
- def update(self, fieldPad, infoPad, healthPad):
- if self.changed:
- fieldEnd = min(fieldPad.getWidth(), self.getWidth()-SIDEWIDTH-1)
- fieldPad.update(self, 0,0,fieldEnd, min(fieldPad.getHeight(), self.getHeight()))
- healthPad.update(self, fieldEnd+1,0, self.getWidth(), HEALTHHEIGHT)
- infoPad.update(self, fieldEnd+1,HEALTHHEIGHT, self.getWidth(), self.getHeight())
- curses.doupdate()
- self.changed = False