half working water source/block

This commit is contained in:
randomuser 2022-04-29 16:07:21 -05:00
parent 1130d6d703
commit 981ebe24c9
5 changed files with 97 additions and 11 deletions

29
demo.py
View File

@ -1,12 +1,15 @@
#!/usr/bin/env python3
from map import *
from render import *
import map
import render
import water
import utils
def main():
textures = []
textures.append(Texture("char", "#", "hash"))
textures.append(Texture("mchar", "simple", "simple"))
textures.append(map.Texture("char", "#", "hash"))
textures.append(map.Texture("mchar", "simple", "simple"))
textures.append(map.Texture("char", "o", "water"))
for i in textures:
i.load()
@ -15,18 +18,26 @@ def main():
for i in range(10, 20):
for j in range(2, 8):
t = Tile([i, j], "placeholder")
t = map.Tile([i, j], "placeholder")
t.addTexture(textures[0])
tiles.append(t)
t = Tile([23, 2], "placeholder")
t = map.Tile([23, 2], "placeholder")
t.addTexture(textures[1])
tiles.append(t)
render = Renderer()
[render.addTile(i) for i in tiles]
t = water.WaterTile([30, 2], source=True)
t.addTexture(textures[2])
tiles.append(t)
renderer = render.Renderer()
[renderer.addTile(i) for i in tiles]
render.finalRender()
while True:
renderer.massUpdate()
renderer.finalRender()
input()
if __name__ == "__main__":
main()

3
map.py
View File

@ -42,11 +42,12 @@ class TileError(BaseException):
pass
class Tile:
def __init__(self, pos, ttype):
def __init__(self, pos, ttype, name="none"):
self.x = pos[0]
self.y = pos[1]
self.ttype = ttype
self.textures = {}
self.name = name
self.dims = None
self.animation = 0
def getCoords(self):

View File

@ -18,7 +18,7 @@ class Renderer:
def massUpdate(self):
for i in self.tiles:
if i.ttype == "standard":
if i.ttype == "standard" or i.ttype == "liquid":
i.update(self.tiles)
def inViewport(self, pos):
@ -37,6 +37,8 @@ class Renderer:
for i in self.tiles:
dims = i.dims
if not dims:
dims = [1, 1]
pos = i.getCoords()
try:

31
utils.py Normal file
View File

@ -0,0 +1,31 @@
class BoardUtils:
def findOccupied(board):
occupied = []
for i in board:
pos = i.getCoords()
dims = i.dims
for j in range(dims[0]):
for k in range(dims[1]):
occupied.append([pos[0] + j, pos[1] + k])
return occupied
def surround(pos, radius=1):
surrounding = []
for i in range(-1 * radius, radius, 1):
for j in range(-1 * radius, radius, 1):
surrounding.append([pos[0] + i, pos[1] + j])
return surrounding
def tileAtPosition(board, pos):
for i in board:
lpos = i.getCoords()
dims = i.dims
if not dims:
dims = [1, 1]
for j in range(dims[0]):
for k in range(dims[1]):
if [lpos[0] + j, lpos[1] + k] == pos:
return i
return None

41
water.py Normal file
View File

@ -0,0 +1,41 @@
import map
import utils
class WaterTile(map.Tile):
def __init__(self, pos, source=False, noupdate=False):
self.source = source
self.noupdate = noupdate
if source:
self.waterlevel = 10 # ten is maximum
else:
self.waterlevel = 0
super(WaterTile, self).__init__(pos, "liquid", "water")
def update(self, tiles):
if self.noupdate:
self.noupdate = False
return
if self.waterlevel == 0 or self.waterlevel == 1:
# we can't spread water, so don't update
return
# get a list of all tiles around current tile
surround = utils.BoardUtils.surround(self.getCoords())
dist = False
for i in surround:
tile = utils.BoardUtils.tileAtPosition(tiles, i)
if not tile:
# there's no tile here, so create one
t = WaterTile(i, noupdate=True)
t.waterlevel = 1
tiles.append(t)
dist = True
elif tile.name == "water":
# we can populate into this tile
tile.waterlevel += 1
dist = True
if dist and not self.source:
self.waterlevel -= 1
def render(self):
return "s"