Make map display empty squares
This commit is contained in:
parent
f5166319e1
commit
7521d7b4ab
4 changed files with 153 additions and 11 deletions
16
chunks.py
16
chunks.py
|
|
@ -1,3 +1,5 @@
|
|||
import threading
|
||||
|
||||
class ChunkDiff():
|
||||
"""
|
||||
Represents differences between two chunks (changes to be made to a chunk).
|
||||
|
|
@ -24,4 +26,18 @@ class ChunkPool():
|
|||
Load chunks it doesn't know.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._lock = threading.RLock()
|
||||
|
||||
def __enter__(self):
|
||||
self._lock.acquire()
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, tb):
|
||||
self._lock.release()
|
||||
|
||||
def load_list(self, coords):
|
||||
pass
|
||||
|
||||
def get(self, x, y):
|
||||
pass
|
||||
|
|
|
|||
28
client.py
28
client.py
|
|
@ -1,23 +1,35 @@
|
|||
import curses
|
||||
import sys
|
||||
import threading
|
||||
from maps import Map
|
||||
from chunks import ChunkPool
|
||||
|
||||
# import fron chunks, maps, clientchunkpool
|
||||
|
||||
class Client():
|
||||
def __init__(self, address):
|
||||
self.address = address
|
||||
self.clock = threading.RLock()
|
||||
#self.pool = Chunkpool()
|
||||
self.pool = ChunkPool()
|
||||
#self.map_ = Map(sizex, sizey, self.pool)
|
||||
#self.chunkmap = Chunkmap(sizex, sizey, self.pool) # size changeable by +/-?
|
||||
|
||||
#self.sock = socket.Socket(...)
|
||||
|
||||
def launch(self):
|
||||
# try to connect
|
||||
# launch socket thread
|
||||
# update display
|
||||
# -> launch input thread
|
||||
def launch(self, stdscr):
|
||||
sizey, sizex = stdscr.getmaxyx()
|
||||
self.map_ = Map(sizex, sizey, self.pool)
|
||||
|
||||
stdscr.noutrefresh()
|
||||
self.map_.draw()
|
||||
curses.doupdate()
|
||||
stdscr.getkey()
|
||||
self.map_.worldx += 1
|
||||
self.map_.cursorx += 2
|
||||
self.map_.cursory += 1
|
||||
stdscr.noutrefresh()
|
||||
self.map_.draw()
|
||||
curses.doupdate()
|
||||
stdscr.getkey()
|
||||
|
||||
def get_input(self, scr):
|
||||
pass
|
||||
|
|
@ -32,7 +44,7 @@ def main(argv):
|
|||
return
|
||||
|
||||
client = Client(argv[1])
|
||||
client.launch()
|
||||
curses.wrapper(client.launch)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv)
|
||||
|
|
|
|||
96
maps.py
96
maps.py
|
|
@ -1,15 +1,111 @@
|
|||
import curses
|
||||
import math
|
||||
from utils import CHUNK_HEIGHT, CHUNK_WIDTH, chunkx, chunky, inchunkx, inchunky, Position
|
||||
|
||||
import sys
|
||||
|
||||
class Map():
|
||||
"""
|
||||
A map which displays chunks and a cursor on the screen.
|
||||
Allows for user to modify chunks in an intuitive way.
|
||||
"""
|
||||
|
||||
def __init__(self, width, height, chunkpool):
|
||||
self.worldx = 0
|
||||
self.worldy = 0
|
||||
self.cursorx = 0
|
||||
self.cursory = 0
|
||||
self.chunkpreload = 0
|
||||
|
||||
self.chunkpool = chunkpool
|
||||
|
||||
self._pad = curses.newpad(5, 5)
|
||||
self.resize(width, height)
|
||||
|
||||
def draw(self):
|
||||
with self.chunkpool as pool:
|
||||
for x in range(chunkx(self.width) + 1):
|
||||
for y in range(chunky(self.height) + 1):
|
||||
chunk = pool.get(x+chunkx(self.worldx), y+chunky(self.worldy))
|
||||
if chunk:
|
||||
chunk.draw_to(x*CHUNK_WIDTH, y*CHUNK_HEIGHT, self._pad)
|
||||
else:
|
||||
self.draw_empty_to(x*CHUNK_WIDTH, y*CHUNK_HEIGHT)
|
||||
|
||||
# set cursor position in world
|
||||
self._pad.move(
|
||||
self.cursory - chunky(self.worldy)*CHUNK_HEIGHT,
|
||||
self.cursorx - chunkx(self.worldx)*CHUNK_WIDTH
|
||||
)
|
||||
|
||||
#sys.stderr.write("{} {} 0 0 {} {} | {} {}\n".format(inchunky(self.worldy), inchunkx(self.worldx), self.height, self.width, self.py, self.px))
|
||||
self._pad.noutrefresh(inchunky(self.worldy), inchunkx(self.worldx), 0, 0, self.height-1, self.width-1)
|
||||
#self._pad.noutrefresh(0, 0, 0, 0, 10, 10)
|
||||
|
||||
def draw_empty_to(self, x, y):
|
||||
if False and curses.has_colors():
|
||||
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
|
||||
for dy in range(CHUNK_HEIGHT):
|
||||
self._pad.addstr(y+dy, x, " "*CHUNK_WIDTH, curses.color_pair(1))
|
||||
else:
|
||||
for dy in range(CHUNK_HEIGHT):
|
||||
s = "."*(CHUNK_WIDTH)
|
||||
#sys.stderr.write("Writing {!r} at {} {} ownsize {} {}\n".format(s, x, y+dy, self.px, self.py))
|
||||
self._pad.addstr(y+dy, x, s)
|
||||
|
||||
def resize(self, width, height):
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
self._pad.resize(
|
||||
(chunky(height) + 1)*CHUNK_HEIGHT,
|
||||
(chunkx(width) + 1)*CHUNK_WIDTH + 1 # workaround for addwstr issue when drawing
|
||||
)
|
||||
|
||||
with self.chunkpool as pool:
|
||||
pool.load_list(self.visible_chunk_coords())
|
||||
|
||||
def visible_chunk_coords(self):
|
||||
coords = []
|
||||
|
||||
xstart = chunkx(self.worldx) - self.chunkpreload
|
||||
ystart = chunky(self.worldy) - self.chunkpreload
|
||||
xend = xstart + chunkx(self.width) + 1 + 2*self.chunkpreload
|
||||
yend = ystart + chunky(self.height) + 1 + 2*self.chunkpreload
|
||||
|
||||
for x in range(xstart, xend):
|
||||
for y in range(ystart, yend):
|
||||
coords.append(Position(x, y))
|
||||
|
||||
return coords
|
||||
|
||||
def write(self, char):
|
||||
pass
|
||||
|
||||
def move_cursor(self, dx, dy):
|
||||
pass
|
||||
|
||||
def scroll(self, dx, dy):
|
||||
pass
|
||||
|
||||
|
||||
class ChunkMap():
|
||||
"""
|
||||
A map that shows which chunks are currently loaded.
|
||||
Might show additional details too (i.e. if a chunk has been modified).
|
||||
"""
|
||||
|
||||
def __init__(self, chunkpool):
|
||||
self.cpool = chunkpool
|
||||
#
|
||||
# def draw(self):
|
||||
# pass
|
||||
#
|
||||
# def resize(self, size):
|
||||
# pass
|
||||
#
|
||||
def move(self, x, y, corner):
|
||||
pass
|
||||
|
||||
def toggle(self):
|
||||
pass
|
||||
|
|
|
|||
18
utils.py
Normal file
18
utils.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
from collections import namedtuple
|
||||
|
||||
Position = namedtuple("Position", "x y")
|
||||
|
||||
CHUNK_WIDTH = 16
|
||||
CHUNK_HEIGHT = 8
|
||||
|
||||
def chunkx(value):
|
||||
return value//CHUNK_WIDTH
|
||||
|
||||
def chunky(value):
|
||||
return value//CHUNK_HEIGHT
|
||||
|
||||
def inchunkx(value):
|
||||
return value%CHUNK_HEIGHT
|
||||
|
||||
def inchunky(value):
|
||||
return value%CHUNK_WIDTH
|
||||
Loading…
Add table
Add a link
Reference in a new issue