/
Map.py
209 lines (131 loc) · 3.93 KB
/
Map.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import sys
import os
import libtcodpy as libtcod
import copy
class TilesetListener:
def __init__(self,tileset):
self.tempData=[0 for i in range(7)]
self.tileset=tileset
def new_struct(self,str,name):
if libtcod.struct_get_name(str)=='tile':
self.tempdata=[0 for i in range(7)]
self.tempdata[1]=name
return True
else:
return False
def new_flag(self,name):
return False
def new_property(self,name,type,value):
if name=='id':
self.tempdata[0]=value
elif name=='chara':
self.tempdata[2]=value
elif name=='path':
self.tempdata[3]=value
elif name=='view':
self.tempdata[4]=value
elif name=='ccolor':
self.tempdata[5]=(value.r,value.g,value.b)
elif name=='bcgcolor':
self.tempdata[6]=(value.r,value.g,value.b)
else:
return False
#print name, value
return True
def end_struct(self,struct,name):
#print self.tempdata
self.tileset.tile_data[self.tempdata[0]]=copy.deepcopy(self.tempdata)
return True
def error(self,msg):
print msg
return True
def __del__(self):
print 'listener destroyed'
class Tile:
def __init__(self,passable,viewable,char,color,bkgnd_color):
self.path=passable
self.view=viewable
self.char=char
self.color=color
self.background=bkgnd_color
def set_id(self,id):
'''
meant to be used by create_tile() in Tileset classe
'''
self.id=id
class Tileset:
"""
class to handle tileset, load, store and access tiles properties
"""
def __init__(self,size):
"""
initialize Tileset object
:param size: the numer of different tiles handled
"""
self.tile_data=[0 for i in range(size)]
self.size=size
def load(self,file):
parser=libtcod.parser_new()
tile_struc=libtcod.parser_new_struct(parser,"tile")
libtcod.struct_add_property(tile_struc,"id",libtcod.TYPE_INT,True)
libtcod.struct_add_property(tile_struc,"chara",libtcod.TYPE_CHAR,True)
libtcod.struct_add_property(tile_struc,"path",libtcod.TYPE_BOOL,True)
libtcod.struct_add_property(tile_struc,"view",libtcod.TYPE_BOOL,True)
libtcod.struct_add_property(tile_struc,"ccolor",libtcod.TYPE_COLOR,True)
libtcod.struct_add_property(tile_struc,"bcgcolor",libtcod.TYPE_COLOR,True)
libtcod.parser_run(parser,file,TilesetListener(self))
self.empty=0
print "ended"
libtcod.parser_delete(parser)
def create_tile(self,id):
a= Tile(self.tile_data[id][3],self.tile_data[id][4],self.tile_data[id][2],libtcod.Color(*self.tile_data[id][5]),libtcod.Color(*self.tile_data[id][6]))
a.id=id
return a
def set_empty(self,id):
self.empty=id
def get_empty(self):
return self.create_tile(self.empty)
class Map:
def __init__(self,width,height):
self.width=width
self.height=height
self.data=[[Tile(False,False,chr(219),libtcod.light_blue,libtcod.black) for X in range(width)] for Y in range(height)]
self.pathdata=libtcod.map_new(width,height)
self.path=libtcod.path_new_using_map(self.pathdata)
def empty(self,tileset):
self.data=[[tileset.get_empty() for X in range(self.width)] for Y in range(self.height)]
def get_tile(self,x,y):
return self.data[y][x]
def set_tile(self,x,y,tile):
self.data[y][x]=tile
def save(self,name):
new_fichier=open('Saves/'+name +'.txt','w')
#: ;
string=''
string+=str(self.width)
string+=':'
string+=str(self.height)
string+=';'
for Y in range(self.height):
for X in range(self.width):
string += str(self.get_tile(X,Y).id)
string += ':'
string += ';'
new_fichier.write(string)
new_fichier.close()
def load(self,name,tileset):
fichier=open('Saves/'+name +'.txt','r')
data=fichier.read()
lines=data.split(';')
prop=lines[0].split(':')
self.width=int(prop[0])
self.height=int(prop[1])
for Y in range(self.height):
cels=lines[Y+1].split(':')
for X in range(self.width):
tile=tileset.create_tile(int(cels[X]))
self.set_tile(X,Y,tile)
libtcod.map_set_properties(self.pathdata,X,Y,tile.view,tile.path)
fichier.close()
def __del__(self):
print "Map deleted"