-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstructure.py
More file actions
71 lines (54 loc) · 2.37 KB
/
structure.py
File metadata and controls
71 lines (54 loc) · 2.37 KB
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
from typing import Dict, Any, Tuple, NoReturn, Callable, List
import displayer
import game_error as er
import json
import game
import pygame
import collision as co
FLOOR = "floor"
BUILD = "build"
DEFAULT_SIZE = [1, 1]
EMPTY_COLLISION = [0, 0, 0, 0]
class Structure(object):
def __init__(self, name: str, data: Dict[str, Any]):
self.name: str = name
if not data["display"]:
raise er.StructureParseError("No display for {}".format(name))
self.fake_floor = data.get("fake_floor", False)
self.display: List['displayer.Displayer'] = [displayer.parse(d, name) for d in data["display"]]
self.collision: List[int] = data["collision"] if "collision" in data else EMPTY_COLLISION
if len(self.collision) != 4:
raise er.StructureParseError("Invalid collision size ({}) in {} need 4 arg".format(len(self.collision), name))
self.collision_side: Dict[str, 'co.CollisionEvent'] = {}
if "collision_side" in data:
for key, value in data["collision_side"].items():
name = value["id"]
self.collision_side[key] = co.EVENTS[name](value)
self.have_collision: bool = self.collision != EMPTY_COLLISION
def get_display(self, random: List[List[int]], x: int, y: int) -> 'displayer.Displayer':
if len(self.display) == 1:
return self.display[0]
v = int(random[y][x] * len(self.display))
return self.display[v]
def render(self, screen: pygame.Surface, x: int, y: int, cx: int, cy: int,
random: List[List[int]], collision: 'co.Collision') -> NoReturn:
d = self.get_display(random, cx, cy)
d.display(screen, x, y)
if self.have_collision:
collision.add(co.SquaredCollisionBox(
x + self.collision[0] * game.CASE_SIZE,
y + self.collision[1] * game.CASE_SIZE,
x + self.collision[2] * game.CASE_SIZE,
y + self.collision[3] * game.CASE_SIZE,
self.collision_side
))
def __del__(self):
# safe delete
self.display.clear()
def parse(path: str):
with open("data/build/{}.json".format(path), "r", encoding='utf-8') as file:
data = json.load(file)
if not data:
raise er.StructureParseError("No data in {}".format(path))
# same for the moment
return Structure(path, data)