-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_loader.py
60 lines (52 loc) · 1.96 KB
/
map_loader.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
import json
import os
import pyray
from blocks.block import Block
from blocks.speedboostblock import SpeedBoostBlock
from blocks.jumpboostblock import JumpBoostBlock
from blocks.lavablock import LavaBlock
from entities.enemy import Enemy
def load_map(file_path):
with open(file_path, 'r') as file:
data = json.load(file)
blocks = []
enemies = []
for item in data['blocks']:
block_type = item['type']
height = item['height']
width = item['width']
x = item['x']
y = item['y']
color_data = item['color']
color = pyray.Color(color_data['r'], color_data['g'], color_data['b'], color_data['a'])
if block_type == 'Block':
blocks.append(Block(height, width, x, y, color))
elif block_type == 'SpeedBoostBlock':
speed = item['speed']
blocks.append(SpeedBoostBlock(height, width, x, y, color, speed))
elif block_type == 'JumpBoostBlock':
jump = item['jump']
blocks.append(JumpBoostBlock(height, width, x, y, color, jump))
elif block_type == 'LavaBlock':
blocks.append(LavaBlock(height, width, x, y, color))
for item in data.get('enemies', []):
height = item['height']
width = item['width']
x = item['x']
y = item['y']
color_data = item['color']
color = pyray.Color(color_data['r'], color_data['g'], color_data['b'], color_data['a'])
health = item['health']
inventory_data = item.get('inventory', [])
enemy = Enemy(height, width, x, y, color, health, None, inventory_data=inventory_data)
enemies.append(enemy)
player_data = data['player']
inventory_data = player_data.get('inventory', [])
player_data['inventory'] = inventory_data
return {
'blocks': blocks,
'enemies': enemies,
'player': player_data
}
def list_maps(directory):
return [f for f in os.listdir(directory) if f.endswith('.json')]