Browse Source

Add graphics

master
Josh Gordon 4 years ago
parent
commit
7c9b2bfb13
  1. 57
      app/dungeon.py

57
app/dungeon.py

@ -1,16 +1,22 @@
from random import randint, choice
import numpy as np
import cv2
#algorithm: http://blankhead.fi/blog/index.php/2019/06/01/cell-flow-dungeon-layout-generation/
CELL_SIZE = 10
CELL_SIZE = 5
RETRY_CELL_PLACEMENT = True
SCALE_FACTOR = 32
GRID_SIZE = 10
FLOW_COLOR = np.array([0,255,0]) #red
def randomColor():
b = randint(0,255)
g = randint(0,255)
r = randint(0,255)
return np.array([r,g,b])
return np.array([b,g,r])
class Cell(object):
@ -45,13 +51,18 @@ class Cell(object):
else:
pass #write this when interested
def getColor(self):
return self.color
def getFlow(self):
return self.flow
class Grid(object):
def __init__(self,w,h):
self.w = w
self.h = h
self.spaces = np.zeros((w,h))
self.spaces = np.zeros((w,h),dtype=np.int)
self.cells = []
def isOpen(self,x,y):
@ -86,8 +97,13 @@ class Grid(object):
def addCell(self):
if len(self.cells) == 0:
c = Cell(1)
for i in range(CELL_SIZE):
sx,sy = GRID_SIZE//2,GRID_SIZE//2
c = Cell(len(self.cells)+1)
c.spaces = [(sx,sy)]
c.flow = []
self.spaces[sx,sy] = c.ID
for i in range(CELL_SIZE-1):
c.grow(self)
self.cells.append(c)
else:
@ -101,7 +117,7 @@ class Grid(object):
if len(free) == 0:
continue
sx,sy = choice(free)
c = Cell(len(self.cells))
c = Cell(len(self.cells)+1)
c.spaces = [(sx,sy)]
c.flow = [(sx,sy,x0,y0)]
self.spaces[sx,sy] = c.ID
@ -114,13 +130,40 @@ class Grid(object):
def __str__(self):
return str(self.spaces)
def exportImage(self):
imBase = np.zeros((self.w,self.h,3))
for x in range(self.w):
for y in range(self.h):
if not self.spaces[x,y] == 0:
#print(self.cells[self.spaces[x,y]-1].getColor())
imBase[x,y,:] = self.cells[self.spaces[x,y]-1].getColor()/255
imBase = np.kron(imBase,np.ones((SCALE_FACTOR,SCALE_FACTOR,1)))
return imBase
def getCells(self):
return self.cells
def drawFlow(im,grid):
for cell in grid.getCells():
for (y1,x1,y0,x0) in cell.getFlow():
cv2.arrowedLine(im,(x1*SCALE_FACTOR + SCALE_FACTOR//2,y1*SCALE_FACTOR + SCALE_FACTOR//2),(x0*SCALE_FACTOR + SCALE_FACTOR//2,y0*SCALE_FACTOR + SCALE_FACTOR//2),100)
#driver code
g = Grid(10,10)
g = Grid(GRID_SIZE,GRID_SIZE)
g.addCell()
g.addCell()
g.addCell()
g.addCell()
g.addCell()
print(g)
im = g.exportImage()
#print(im)
print(im.shape)
drawFlow(im,g)
cv2.imshow("Here",im)
cv2.waitKey(0)
Loading…
Cancel
Save