2021-09-11 15:12:53 +02:00
|
|
|
from gamedata.objects.base import BaseObject
|
|
|
|
from math import sqrt
|
|
|
|
import random
|
|
|
|
|
|
|
|
# Tiles object, displaying and managing the play grid
|
|
|
|
|
|
|
|
class Tiles(BaseObject):
|
|
|
|
|
2021-09-13 22:16:02 +02:00
|
|
|
def __init__(self,x,y,game,nbcells=22,cellsize=32):
|
2021-09-11 15:12:53 +02:00
|
|
|
|
|
|
|
super().__init__(x,y,game,nbcells*cellsize,nbcells*cellsize)
|
|
|
|
|
|
|
|
self.nbcells = nbcells
|
|
|
|
self.cellsize = cellsize
|
|
|
|
|
2021-09-17 15:09:01 +02:00
|
|
|
self.saferadius = 3
|
2021-09-11 15:12:53 +02:00
|
|
|
|
2021-09-13 22:16:02 +02:00
|
|
|
self.grid = self.gengrid(game.sprite_lib["collisionmap.png"])
|
2021-09-13 10:53:42 +02:00
|
|
|
self.spawns = self.getspawnpoints(self.grid,saferadius=self.saferadius)
|
|
|
|
|
2021-09-13 22:16:02 +02:00
|
|
|
self.debugcell = game.pygame.Surface([cellsize]*2)
|
|
|
|
self.debugcell.fill([255]*3)
|
|
|
|
self.debugcell.set_alpha(100)
|
2021-09-11 15:12:53 +02:00
|
|
|
|
2021-09-13 22:16:02 +02:00
|
|
|
def gengrid(self,sprite):
|
|
|
|
# Generating a list of lists based on an image
|
|
|
|
width,height = sprite.get_size()
|
|
|
|
grid = []
|
|
|
|
for y in range(height):
|
|
|
|
line = []
|
|
|
|
for x in range(width):
|
|
|
|
if sprite.get_at((x,y))!=(0,0,0,255):
|
|
|
|
line.append(1)
|
|
|
|
else:
|
|
|
|
line.append(0)
|
|
|
|
grid.append(line)
|
2021-09-11 15:12:53 +02:00
|
|
|
|
|
|
|
return grid
|
|
|
|
|
2021-09-13 07:41:32 +02:00
|
|
|
def getspawnpoints(self,grid,saferadius=2):
|
|
|
|
spawns = []
|
|
|
|
for y in range(saferadius,len(grid)-saferadius):
|
|
|
|
for x in range(saferadius,len(grid[y])-saferadius):
|
|
|
|
canspawn = True
|
|
|
|
for safey in range(-saferadius,saferadius+1):
|
|
|
|
for safex in range(-saferadius,saferadius+1):
|
|
|
|
if grid[y+safey][x+safex]==0: # If a pit is inside the radius, can't spawn here
|
|
|
|
canspawn = False
|
|
|
|
if canspawn:
|
2021-09-13 10:34:34 +02:00
|
|
|
spawns.append([self.rect[0]+(x+0.5)*self.cellsize,self.rect[1]+(y+0.5)*self.cellsize])
|
2021-09-13 07:41:32 +02:00
|
|
|
return spawns
|
|
|
|
|
2021-09-11 15:12:53 +02:00
|
|
|
def draw(self):
|
2021-09-13 22:16:02 +02:00
|
|
|
# Drawing the ground
|
2021-09-14 17:01:58 +02:00
|
|
|
self.game.window.blit(self.game.sprite_lib["ingame.png"],(-self.game.globals["camerax"],-self.game.globals["cameray"]))
|
2021-09-13 07:41:32 +02:00
|
|
|
if self.game.globals["debug"]:
|
2021-09-13 22:16:02 +02:00
|
|
|
# Drawing the grid
|
|
|
|
for y in range(len(self.grid)):
|
|
|
|
for x in range(len(self.grid[y])):
|
|
|
|
if self.grid[y][x]==1:
|
2021-09-14 17:01:58 +02:00
|
|
|
self.game.window.blit(self.debugcell,[self.rect[0]+x*self.cellsize-self.game.globals["camerax"],self.rect[1]+y*self.cellsize-self.game.globals["cameray"]])
|
2021-09-13 22:16:02 +02:00
|
|
|
# Draw spawn points
|
2021-09-13 10:34:34 +02:00
|
|
|
for point in self.spawns:
|
2021-09-14 17:01:58 +02:00
|
|
|
self.game.pygame.draw.circle(self.game.window,[100,100,0],[point[0]-self.game.globals["camerax"],point[1]-self.game.globals["cameray"]],radius=3)
|