-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelGen.py
59 lines (53 loc) · 2.44 KB
/
levelGen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from helpers import *
from levelUnits import *
from gridClasses import *
from random import randint
from gridGeo import dist
from classtypes import inf
def genBoard(x,y,defCell=".",wallCell="x",empty=.1):
x_size=x*7
y_size=y*7
grid=[[defCell for i in range(x_size)] for i in range(y_size)]
def push_gunit(gunit,xo,yo):
for y in range(len(gunit)):
for x in range(len(gunit[0])):
cell = gunit[y][x]
if cell=="x":
if cell!=wallCell:
cell=wallCell
grid[y+yo][x+xo]=cell
for rx in range(0,x):
for ry in range(0,y):
gunit = GUNITS[randint(0,len(GUNITS)-1)]#get random grid peice
if randint(1,100)/100>empty:#chance of empty to not spawn
push_gunit(gunit,rx*7,ry*7)
for row in range(len(grid)):
if row==0 or row==len(grid)-1:
for i in range(len(grid[row])):
grid[row][i]=wallCell
else:
grid[row][0]=wallCell
grid[row][-1]=wallCell
return grid
def spawnEnemies(crang,amount,canSpawnFunc,grid,minPlayerDist=-1,playerPos=[0,0],bad=[]):
spawned=[]
for i in range(amount):
pos=[randint(crang[0][i],crang[1][i]) for i in range(2)]
while (not canSpawnFunc(pos[0],pos[1])) or dist(pos,playerPos)<minPlayerDist or pos in bad:
pos=[randint(crang[0][i],crang[1][i]) for i in range(2)]
spawned.append(Unit(pos,randFrameName(),[randWeaponName(),randWeaponName()],randArmorName(),displayChar="*",kind="enemy"))
return spawned
#spawnAllies
def spawnAllies(crang,amount,canSpawnFunc,grid,maxPlayerDist=inf(),playerPos=[0,0],bad=[]):
spawned=[]
for i in range(amount):
pos=[randint(crang[0][i],crang[1][i]) for i in range(2)]
while (not canSpawnFunc(pos[0],pos[1])) or (dist(pos,playerPos)>maxPlayerDist) or (pos in bad):
pos=[randint(crang[0][i],crang[1][i]) for i in range(2)]
spawned.append(Unit(pos,randFrameName(),[randWeaponName(),randWeaponName()],randArmorName(),displayChar="+",kind="ally"))
return spawned
def get_free_loc(crang,grid,canSpawnFunc,bad=[],playerPos=[0,0],dist_range=[-1,inf()]):
loc=[randint(crang[0][i],crang[1][i]) for i in range(2)]
while (not canSpawnFunc(loc[0],loc[1])) or loc in bad or (not dist_range[0]<dist(loc,playerPos)<dist_range[1]):
loc=[randint(crang[0][i],crang[1][i]) for i in range(2)]
return loc