-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtilemap_scripts.py
82 lines (69 loc) · 3.08 KB
/
tilemap_scripts.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
import game_sdl
import json
from game_structs import CharacterBehaviors
from tilemap_structs import Tile, Tilemap, TilemapCharacters, Tileset
def load_map(path):
json_data = open(path)
data = json.load(json_data)
tilemap = Tilemap()
tilemap.tilew = data['tilewidth']
tilemap.tileh = data['tileheight']
for t in data['tilesets']:
tileset = Tileset(t['image'], t['imagewidth'], t['imageheight'],
t['tilewidth'], t['tileheight'],
t['margin'], t['spacing'],
t['firstgid'], t['transparentcolor'],
game_sdl.load_image(t['image']))
tilemap.tilesets.append(tileset)
if 'tiles' in t:
for tile_meta in t['tiles']:
tile_id = tile_meta['id']
tile_props = tile_meta['properties']
for tile_prop in tile_props:
if tile_prop['name'] == 'NoCollide' and tile_prop['value'] == '1':
tilemap.no_collision[int(tile_id) + tileset.firstgid] = True
if tile_prop['name'] == 'NoPeak' and tile_prop['value'] == '1':
tilemap.no_peak[int(tile_id) + tileset.firstgid] = True
if tile_prop['name'] == 'CollDy':
tilemap.coll_dy[int(tile_id) + tileset.firstgid] = int(tile_prop['value'])
# Index GUIDs
for tileset_idx in range(0, len(tilemap.tilesets)):
tileset = tilemap.tilesets[tileset_idx]
gid = tileset.firstgid
for i in range(0, int(tileset.h/(tileset.tileh + tileset.spacing))):
for j in range(0, int(tileset.w/(tileset.tilew + tileset.spacing))):
x = j * (tileset.tilew + tileset.spacing) + tileset.margin
y = i * (tileset.tileh + tileset.spacing) + tileset.margin
tilemap.gindex[gid] = Tile(x, y, tileset_idx)
gid += 1
# Load map itself
tilemap.current_height = data['height']
tilemap.current_width = data['width']
tilemap.current_map = []
layer_terrain = 0
layer_characters = 1
map_data = data['layers'][layer_terrain]['data']
for i in range(tilemap.current_height):
row = []
for j in range(tilemap.current_width):
row.append(map_data[i * tilemap.current_width + j])
tilemap.current_map.append(row)
char_data = data['layers'][layer_characters]['objects']
for char in char_data:
char_type = char['type'].upper()
char_x = int(char['x'])
char_y = int(char['y'])
if char_type == 'PLAYER':
char_type = CharacterBehaviors.PLAYER
elif char_type == 'JUMPINGAI':
char_type = CharacterBehaviors.JUMPING_AI
else:
char_type = CharacterBehaviors.BASIC_AI
tilemap.characters_to_spawn.append(TilemapCharacters(char_type, char_x, char_y))
json_data.close()
return tilemap
def get_coll_dy(tilemap: Tilemap, tile_id: int) -> int:
if tile_id not in tilemap.coll_dy:
return 0
else:
return tilemap.coll_dy[tile_id]