-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_interpreter.py
170 lines (144 loc) · 5.51 KB
/
command_interpreter.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
#-*-coding:utf-8-*
from obj_models import specials_default
from copy import deepcopy
from math import fabs
import res
def enter_command(obj, command, id, coord):
obj = deepcopy(obj)
attr = obj["model"]
if len(command.split(" ")) == 2:
target, value = command.split(" ")
if target == "name":
if value in res.images:
attr["name"] = value
else:
print(" ----- ERREUR IMAGE INEXISTANTE ----- \n")
return obj, False
elif target == "x":
try:
obj["rect"] = obj["rect"].move(int(value), 0)
except ValueError:
print(" ----- ERREUR LA VALEUR DOIT ETRE UN ENTIER ----- \n")
return obj, False
elif target == "y":
try:
obj["rect"] = obj["rect"].move(0, int(value))
except ValueError:
print(" ----- ERREUR LA VALEUR DOIT ETRE UN ENTIER ----- \n")
return obj, False
elif target in ("pts", "p"):
try:
if int(value) < 0:
attr["pts"] = 0
else:
attr["pts"] = int(value)
except ValueError:
print(" ----- ERREUR LA VALEUR DOIT ETRE UN ENTIER ----- \n")
return obj, False
elif target == "sh":
if value == "1" or value == "0":
attr["sh"] = int(value)
else:
print(" ----- ERREUR FORME INVALIDE [0/1] ----- \n")
return obj, False
elif target == "k1":
if value == "1":
attr["k"] = (True,attr["k"][1])
elif value == "0":
attr["k"] = (False,attr["k"][1])
else:
print(" ----- ERREUR PROPRIETE INVALIDE [0/1] ----- \n")
return obj, False
elif target == "k2":
if value == "1":
attr["k"] = (attr["k"][0],True)
elif value == "0":
attr["k"] = (attr["k"][0],False)
else:
print(" ----- ERREUR PROPRIETE INVALIDE [0/1] ----- \n")
return obj, False
elif target == "h":
try:
if int(value) < 0:
attr["h"] = -1
else:
attr["h"] = int(value)
except ValueError:
print(" ----- ERREUR LA VALEUR DOIT ETRE UN ENTIER ----- \n")
return obj, False
elif target == "hb":
try:
value = int(value)
except ValueError:
print(" ----- ERREUR LA VALEUR DOIT ETRE UN NOMBRE ----- \n")
return obj, False
if obj["rect"].width < fabs(value) or obj["rect"].height < fabs(value):
print(" ----- ERREUR HITBOX DISPROPORTIONEE (rect {}x{}) ----- \n".format(obj["rect"].width, obj["rect"].height))
return obj, False
attr["hb"] = value
elif target == "spe":
if len(value.split(":")) == 3:
spe_name, spe_target, spe_value = value.split(":")
try:
if attr["spe"][spe_name][spe_target]:
pass
except KeyError:
print(" ----- ERREUR EXISTENCE ATTRIBUT SPECIAL ----- \n")
return obj, False
if spe_value == "ID":
spe_value = "/".join(id)
elif spe_value == "COORD":
spe_value = coord
elif type(attr["spe"][spe_name][spe_target]) == bool:
if spe_value == "1":
spe_value = True
elif spe_value == "0":
spe_value = False
else:
try:
spe_value = int(spe_value)
except:
try:
spe_value = float(spe_value)
except:
pass
attr["spe"][spe_name][spe_target] = spe_value
else:
print(" ----- ERREUR SYNTAXE SPECIAL ----- \n")
return obj, False
elif target == "rect":
try:
obj["rect"].size = int(value.split("*")[0]), int(value.split("*")[1])
except:
print(" ----- ERREUR VALEUR ----- \n")
return obj, False
elif target == "speAdd":
if value in specials_default:
attr["spe"][value] = specials_default[value]
else:
print(" ----- ERREUR SPECIAL INNEXISTANT ----- \n")
return obj, False
else:
print(" ----- ERREUR COMMANDE INCONNUE ----- \n")
return obj, False
elif len(command.split(" ")) == 1 and "!" in command:
if command == "solid!":
attr["k"] = (True,attr["k"][1])
elif command == "unsolid!":
attr["k"] = (False,attr["k"][1])
elif command == "hazard!":
attr["k"] = (attr["k"][0],True)
elif command == "unhazard!":
attr["k"] = (attr["k"][0],False)
elif command == "rect!":
attr["sh"] = 0
elif command == "circle!":
attr["sh"] = 1
else:
print(" ----- ERREUR COMMANDE INCONNUE ----- \n")
return obj, False
else:
print(" ----- ERREUR SYNTAXE ----- \n")
return obj, False
obj["model"] = attr
return obj, True