-
Notifications
You must be signed in to change notification settings - Fork 11
/
prefabs.py
147 lines (116 loc) · 4.02 KB
/
prefabs.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#Tools for generating buildings (prefabs)
from globals import DATA_DIR
from tiles import *
import graphics as gfx
import menus
import logging
import random
import json
import os
def create_new_prefab(size):
if not len(size) == 3:
raise Exception('Invalid prefab size: Expected 3 arguments.')
_prefab = []
for x in range(size[0]):
_y = []
for y in range(size[1]):
_z = []
for z in range(size[2]):
#if z==0:
# _z.append(create_tile(TALL_GRASS_TILE))
#else:
_z.append(None)
_y.append(_z)
_prefab.append(_y)
logging.debug('Created new prefab of size (%s,%s,%s).' % (size[0],size[1],size[2]))
return {'map': _prefab,'size': size}
def cache_prefab(name, path):
with open(path, 'r') as f:
_prefab = json.loads(''.join(f.readlines()))
_prefab['name'] = name
PREFABS[name] = _prefab
logging.debug('Prefab cached: %s' % name)
def cache_all_prefabs():
logging.debug('Caching all prefabs...')
for (dirpath, dirname, filenames) in os.walk(PREFAB_DIR):
for f in [f for f in filenames if f.count('.json')]:
cache_prefab(f.partition('.')[0], os.path.join(PREFAB_DIR, f))
def save(prefab):
with open(os.path.join(PREFAB_DIR, 'test.json'), 'w') as f:
f.write(json.dumps(prefab))
def _draw_prefab(prefab):
_X_MAX = PREFAB_CAMERA_POS[0]+PREFAB_WINDOW_SIZE[0]
_Y_MAX = PREFAB_CAMERA_POS[1]+PREFAB_WINDOW_SIZE[1]
#DARK_BUFFER[0] = numpy.zeros((MAP_WINDOW_SIZE[1], PREFAB_WINDOW_SIZE[0]))
#LIGHT_BUFFER[0] = numpy.zeros((MAP_WINDOW_SIZE[1], MAP_WINDOW_SIZE[0]))
map = prefab['map']
if _X_MAX>prefab['size'][0]:
_X_MAX = prefab['size'][0]
if _Y_MAX>prefab['size'][1]:
_Y_MAX = prefab['size'][1]
for x in range(PREFAB_CAMERA_POS[0],_X_MAX):
_RENDER_X = x-PREFAB_CAMERA_POS[0]
for y in range(PREFAB_CAMERA_POS[1],_Y_MAX):
_RENDER_Y = y-PREFAB_CAMERA_POS[1]
_drawn = False
for z in range(prefab['size'][2]-1,-1,-1):
if map[x][y][z]:
gfx.blit_tile(_RENDER_X,
_RENDER_Y,
map[x][y][z],
char_buffer=PREFAB_CHAR_BUFFER,
rgb_fore_buffer=PREFAB_RGB_FORE_BUFFER,
rgb_back_buffer=PREFAB_RGB_BACK_BUFFER)
_drawn = True
break
#if z > PREFAB_CAMERA_POS[2] and SETTINGS['draw z-levels above']:
# gfx.blit_tile(_RENDER_X,_RENDER_Y,map[x][y][z])
# gfx.darken_tile(_RENDER_X,_RENDER_Y,abs((PREFAB_CAMERA_POS[2]-z))*30)
# _drawn = True
#elif z == PREFAB_CAMERA_POS[2]:
# if (x,y,z) in SELECTED_TILES[0] and time.time()%1>=0.5:
# gfx.blit_char(_RENDER_X,_RENDER_Y,'X',darker_grey,black)
# else:
# gfx.blit_tile(_RENDER_X,_RENDER_Y,map[x][y][z])
# _drawn = True
#elif z < PREFAB_CAMERA_POS[2] and SETTINGS['draw z-levels below']:
# gfx.blit_tile(_RENDER_X,_RENDER_Y,map[x][y][z])
# gfx.darken_tile(_RENDER_X,_RENDER_Y,abs((PREFAB_CAMERA_POS[2]-z))*30)
# _drawn = True
#if SETTINGS['draw z-levels above'] and _drawn:
# break
if not _drawn:
gfx.blit_tile(_RENDER_X,
_RENDER_Y,
BLANK_TILE,
char_buffer=PREFAB_CHAR_BUFFER,
rgb_fore_buffer=PREFAB_RGB_FORE_BUFFER,
rgb_back_buffer=PREFAB_RGB_BACK_BUFFER)
def draw_prefab_thumbnail(entry):
#VIEWS
key = entry['key']
value = entry['values'][entry['value']]
_prefab = PREFABS[key]
PREFAB_TOP_VIEW_POS = (15, 1)
for y in range(0, _prefab['size'][1]):
for x in range(0, _prefab['size'][0]):
for z in range(0, _prefab['size'][2]):
if not _prefab['map'][x][y][z]:
continue
print _prefab['map'][x][y][z]
#tcod.console_put_char_(0, PREFAB_TOP_VIEW_POS[0]+x, PREFAB_TOP_VIEW_POS[1]+y,
def prefab_selected(entry):
pass
def create_prefab_list():
tcod.console_clear(0)
tcod.console_clear(MAP_WINDOW)
_prefabs = []
for prefab in PREFABS.values():
_prefabs.append(menus.create_item('single', prefab['name'], None))
return menus.create_menu(title='Prefabs',
menu=_prefabs,
padding=(0, 0),
position=(0, 0),
format_str='$k',
on_select=prefab_selected,
on_move=draw_prefab_thumbnail)