-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
199 lines (166 loc) · 6.46 KB
/
main.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
import json
import sys
from typing import *
import os
import json
import os
class VisualNovelEngine:
def __init__(self, game_data: list[dict]):
self.game_data = game_data
self.min_id = min(game_data, key=lambda x: x.get("id", float("inf")))["id"]
self.max_id = max(game_data, key=lambda x: x.get("id", float("-inf")))["id"]
self.current_state = 0
self.variables = {}
# Assuming each action dict always has an "id" key
def find_label_id(self,var: str) -> int:
for action in self.game_data:
if action.get("type") == "label" and action.get("label") == var:
return action.get("id")
return None
def get_dict_by_id(self,target_id: int) -> dict:
for action in self.game_data:
if action.get("id") == target_id:
return action
return None
def clear_screen(self):
os.system('cls' if os.name == 'nt' else 'clear')
def show_sprite(self, sprite_path: str):
print(f"\n[Showing sprite: {sprite_path}]\n")
self.current_state +=1
def show_dialogue(self, label: str, content: str):
print(f"\n{label}: {content}")
input()
self.current_state +=1
def process_command(self,value):
if value["action"] == "get_gamemode":
return "Survival"
elif value["action"] == "custom_command":
return "Nothing for now"
else:
return "Nothing for now"
def modify_variable(self, variable: str, operation: str, value: Any):
if isinstance(value,dict):
value = self.process_command(value)
if operation == "increment_var":
self.variables[variable] += value
elif operation == "substract_var":
self.variables[variable] -= value
else:
self.variables[variable] = value
self.current_state +=1
def give_item(self, item: str, amount: int):
print(f"\n[Received {amount}x {item}]\n")
input("Press Enter to continue...")
self.current_state +=1
def process_jump(self, action):
self.current_state = self.find_label_id(action["label"])
def process_conditional(self, condition: dict):
var = self.variables[condition["var"]]
value = condition["value"]
end = condition["end"]
if value is dict:
value = self.process_command(value)
print(value)
print(var)
match condition["condition"]:
case "equal":
if var == value:
self.current_state += 1
else:
self.current_state = end
case "not_equal":
if var != value:
self.current_state += 1
else:
self.current_state = end
case "less_than":
if var < value:
self.current_state += 1
else:
self.current_state = end
case "greater_than":
if var > value:
self.current_state += 1
else:
self.current_state = end
def show_choices(self, choices: list[dict]) -> str:
print("\nChoices:")
for number, choice in enumerate(choices, 1):
print(f"{number}. {choice['display']}")
while True:
try:
choice = int(input("\nEnter your choice (number): ")) - 1
if 0 <= choice < len(choices):
target_label = choices[choice]['label']
self.current_state = self.find_label_id(target_label)
break
except ValueError:
pass
print("Invalid choice. Please try again.")
def create_variable(self,varName,varInit):
self.variables[varName] = varInit
self.current_state +=1
def process_meta(self,action:dict[str,any]):
action_type = action["action"]
if action_type == "create_var":
self.create_variable(action["var"],action["init"])
else:
self.current_state +=1
def process_action(self, action: dict[str, any]) -> str:
action_type = action["type"]
if action_type == "show_sprite":
self.show_sprite(action["sprite"])
elif action_type == "dialogue":
self.show_dialogue(action["label"], action["content"])
elif action_type == "modify_variable":
self.modify_variable( action["var"], action["action"], action["value"])
elif action_type == "give_item":
self.give_item(action["item"], action["amount"])
elif action_type == "conditional":
self.process_conditional(action)
elif action_type == "transition" and action.get("action") == "jump":
self.process_jump(action)
elif action_type == "choice":
self.show_choices(action["choice"])
elif action_type == "command":
self.process_command(action)
elif action_type == "label":
self.current_state+=1
elif action_type == "finish_dialogue":
return True
else:
self.current_state +=1
return False
def run(self):
print("HENLO~ \n\n")
self.clear_screen()
while True:
action = self.get_dict_by_id(self.current_state)
if action["type"]=="meta":
self.process_meta(action)
else:
if self.process_action(action):
return "END"
def main():
if len(sys.argv) != 2:
print("Usage: python vn_engine.py <script.json>")
sys.exit(1)
try:
with open(sys.argv[1], 'r', encoding='utf-8') as f:
game_data = json.load(f)
print("=== Terminal Visual Novel ===")
print(f"Loading script: {sys.argv[1]}")
input("Press Enter to start...")
engine = VisualNovelEngine(game_data)
engine.run()
print("\nGame finished! Thanks for playing!")
except FileNotFoundError:
print(f"Error: Could not find file '{sys.argv[1]}'")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON format in '{sys.argv[1]}': {e}")
sys.exit(1)
except KeyboardInterrupt:
print("\nGame terminated by user.")
if __name__ == "__main__":
main()