-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtriggers.py
More file actions
118 lines (86 loc) · 3.58 KB
/
triggers.py
File metadata and controls
118 lines (86 loc) · 3.58 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
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
from typing import Dict, Any, Tuple, NoReturn, Callable
import game_error as err
import json
import game
import hud.hud as hud
import sound_manager
import sounds
from hud import pc_hud
LOAD_LVL = "LOAD_LVL"
DIALOGUE = "DIALOGUE"
PC = "PC"
class Trigger(object):
def __init__(self, _id: str, data: Dict[str, Any]):
self.id_ = _id
if "collision_side" not in data:
raise err.TriggerParseError("No collision_side in file wth id {}".format(_id))
self.side: Dict[str, Tuple[Any]] = {}
if "loc" not in data:
raise err.TriggerParseError("No location in trigger with id {}".format(_id))
self.loc = data["loc"][0:4]
for key, value in data["collision_side"].items():
self.side[key] = self.dat(value)
def get_property(self, data: Dict[str, Any], key: str, cast_check=None) -> Any:
if key not in data:
raise err.TriggerParseError("No {} in trigger id: {}".format(key, self.id_))
value = data[key]
if cast_check and not isinstance(value, cast_check):
raise err.TriggerParseError(
"Value for {} in {} isn't good type need be {}".format(key, self.id_, cast_check))
return value
def dat(self, data) -> Tuple:
return ()
def trigger(self, data) -> NoReturn:
pass
class LoadLvlTrigger(Trigger):
def __init__(self, _id: str, data: Dict[str, Any]):
super().__init__(_id, data)
def dat(self, data) -> Tuple:
return (
self.get_property(data, "lvl_name", str),
self.get_property(data, "player_x"),
self.get_property(data, "player_y")
)
def trigger(self, data) -> NoReturn:
game.game_instance.unload_level()
game.game_instance.load_level(data[0], data[1], data[2])
pass
class PCTrigger(Trigger):
def __init__(self, _id: str, data: Dict[str, Any]):
super().__init__(_id, data)
def dat(self, data) -> Tuple:
return ()
def trigger(self, data) -> NoReturn:
if game.game_instance.player.is_action_press and game.game_instance.player.current_menu is None:
sound_manager.start_in_first_empty_taunt(sounds.PC_OPEN)
game.game_instance.player.open_menu(pc_hud.PCHud(game.game_instance.player))
class ShowTextTrigger(Trigger):
def __init__(self, _id: str, data: Dict[str, Any]):
super().__init__(_id, data)
def dat(self, data) -> Tuple:
return (
self.get_property(data, "need_action", bool),
self.get_property(data, "text", str),
)
def trigger(self, data) -> NoReturn:
if (not data[0]) or game.game_instance.player.is_action_press and (
not game.game_instance.player.current_dialogue):
game.game_instance.player.open_dialogue(hud.QuestionDialog(
hud.Dialog.split(game.game_instance.get_message("test")),
self.callback, ["oui", "non", "jsp"], speed_skip=True), over=False, check_last_open=1000)
def callback(self, value: str, index: int) -> NoReturn:
return False
TRIGGERS: Dict[str, Callable[[str, Dict[str, Any]], Trigger]] = {
LOAD_LVL: LoadLvlTrigger,
DIALOGUE: ShowTextTrigger,
PC: PCTrigger
}
def load(data: dict[str, Any]) -> Trigger:
# with open("data/trigger/{}.json".format(path), 'r', encoding='utf-8') as file:
# data = json.load(file)
if not data:
raise err.TriggerParseError("No data")
if "type" not in data:
raise err.TriggerParseError("No type in data")
type_ = data["type"]
return TRIGGERS[type_](type_, data)