-
Notifications
You must be signed in to change notification settings - Fork 0
/
ship_mods.py
271 lines (233 loc) · 9.04 KB
/
ship_mods.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import json
import pyglet
import cocos
from cocos.menu import *
from cocos.scenes import *
from cocos.director import director
import battle, gui, entity, serializer
def set_fonts(menu):
"Called from a menu to set the default fonts used"
#
# Menu font options
#
menu.font_title = {
'font_name':'Classic Robot',
'font_size':28,
'color':(200, 200, 200, 255)
}
menu.font_item= {
'font_name':'Classic Robot',
'font_size':16,
'bold':False,
'italic':False,
'anchor_y':'center',
'anchor_x':'left',
'color':(192,192,192,255),
'dpi':96,
}
menu.font_item_selected = {
'font_name':'Classic Robot',
'font_size':16,
'bold':False,
'italic':False,
'anchor_y':'center',
'anchor_x':'left',
'color':(192,192,0,255),
'dpi':96,
}
class ShipMod(cocos.layer.Layer):
"Layer containing the different menu elements"
def __init__(self):
super(ShipMod, self).__init__()
self.player = entity.Player.load()
self.inventory = self.player.inventory
w, h = director.get_window_size()
# The fleet list
self.add(ShipList(), name="ship_list")
self.selected = None
# Page title
self.add(cocos.text.Label("Ship Modifications",
font_name = "Classic Robot",
font_size = 36,
color = (200, 200, 200, 255),
anchor_y = "top",
anchor_x = "center",
x=w//2,
y=h)
)
# The ship display window
self.ship_info = gui.ShipInfoLayer((250, h-400 ), 550, 300, show_all_weapons=True)
self.add(self.ship_info, name="ship_info")
# The list of modifications in inventory
self.add(ModList(), name="mod_list")
def on_enter(self):
super(ShipMod, self).on_enter()
self.get("ship_list").push_handlers(self)
def on_exit(self):
super(ShipMod, self).on_exit()
self.get("ship_list").pop_handlers()
def on_selected(self, ship):
self.selected = ship
self.ship_info.set_model( ship )
try:
self.remove("slot_menu_mob")
self.remove("slot_menu_def")
self.remove("slot_menu_wea")
except Exception:
pass
slot_menu = SlotMenu(ship.slots['mobility'], 250)
self.add(slot_menu, name="slot_menu_mob")
slot_menu = SlotMenu(ship.slots['defense'], 450)
self.add(slot_menu, name="slot_menu_def")
slot_menu = SlotMenu(ship.slots['weapon'], 650)
self.add(slot_menu, name="slot_menu_wea")
def on_mod_selected(self, mod):
if not self.selected:
return
slot_weapon = self.get("slot_menu_wea")
selected_weapon = slot_weapon.selected_mod
i=1
while selected_weapon.type != "weapon":
selected_weapon = slot_weapon.mod_at_index(slot_weapon.selected_index - i)
i += 1
if mod.type in self.selected.slots and \
self.selected.add_mod(mod):
self.inventory.remove(mod)
self.get("mod_list").on_change()
elif mod.type in selected_weapon.slots and \
selected_weapon.add_mod(mod):
self.inventory.remove(mod)
self.get("mod_list").on_change()
def on_mod_deselected(self, mod):
if self.selected.remove_mod(mod):
self.player.add_mod_to_inventory(mod)
self.get("mod_list").on_change()
class SlotMenu(gui.SubMenu):
def __init__(self, slot, hmargin):
self.slot = slot
super(SlotMenu, self).__init__()
w, h = director.get_window_size()
set_fonts(self)
self.font_title['font_size'] = 20
self.font_item['font_size'] = self.font_item_selected ['font_size'] = 12
self.menu_halign = LEFT
self.menu_valign = TOP
self.menu_hmargin = hmargin
self.menu_vmargin = 400
def on_enter(self):
super(SlotMenu, self).on_enter()
self.slot.parent.push_handlers(self)
self._create_menu_items()
def _create_menu_items(self):
l = []
self._append_mod_menu_item(l, self.slot, 0)
if not l:
l = [MenuItem("None", None)]
self.title = "{slot.type} ({count}/{slot.max_count})".format(slot=self.slot,
count=len(self.slot.mods) )
self.create_menu(l)
def _append_mod_menu_item(self, l, slot, level):
for mod in slot.mods:
menu_item = MenuItem(" "*level + mod.name,
self.parent.on_mod_deselected, mod)
menu_item.mod = mod
l.append(menu_item)
# Check for mods of mod.
for slot in mod.slots.itervalues():
self._append_mod_menu_item(l, slot, level+1)
def on_exit(self):
super(SlotMenu, self).on_exit()
self.slot.parent.pop_handlers()
map(self.remove, (child for z,child in self.children) )
def on_key_press(self, s, m):
return False
def on_change(self):
old_selected_mod = self.selected_mod
map(self.remove, (child for z,child in self.children) )
self._create_menu_items()
if old_selected_mod is not None:
new_idx = self.index_of_mod(old_selected_mod)
self._select_item(new_idx)
def on_mouse_motion( self, x, y, dx, dy ):
"Do not select item when hovering over them."
pass
def on_mouse_release( self, x, y, buttons, modifiers ):
"Only select items when clicking them."
super(SlotMenu, self).on_mouse_motion(x, y, 0, 0)
if buttons == pyglet.window.mouse.LEFT:
return super(SlotMenu, self).on_mouse_release(x, y, buttons, modifiers)
@property
def selected_mod(self):
"Returns the selected mod in this slot."
# If list is empty, there is only one child which is a MenuItem("None", None)
if self.children[self.selected_index][1].callback_func is None:
return None
# ItemMenu represents a mod contained in its mod attribute.
return self.children[self.selected_index][1].mod
@property
def mod_at_index(self, idx):
"Returns the mod at the given index"
return self.children[idx][1].mod
def index_of_mod(self, mod):
"Finds the index of the MenuItem displaying this mod."
# If selected item is MenuItem("None", None), return 0
if self.selected_mod is None: return 0
for idx, (z, child) in enumerate(self.children):
if child.mod is mod:
return idx
return 0
class ShipList(gui.SubMenu, pyglet.event.EventDispatcher):
def __init__(self):
super(ShipList, self).__init__()
self.title = _("""Fleet""")
self.menu_halign = LEFT
self.menu_valign = TOP
self.menu_hmargin = 20
self.menu_vmargin = 100
set_fonts(self)
def on_enter(self):
super(ShipList, self).on_enter()
self.buttons = []
for ship in self.parent.player.fleet:
ship_button = MenuItem(ship.ship_type, self.dispatch_event, "on_selected", ship)
self.buttons.append(ship_button)
self.buttons.append( MenuItem("Save...", self.on_save) )
self.buttons.append( MenuItem("Go to the Battle", self.on_quit) )
self.create_menu(self.buttons, selected_effect=zoom_in(),
unselected_effect=zoom_out())
def on_exit(self):
super(ShipList, self).on_exit()
map(self.remove, (child for z,child in self.children) )
def on_quit(self):
my_battle = battle.Battle()
battle_scene = cocos.scene.Scene(my_battle)
director.replace(FadeTransition(battle_scene, duration = 3))
def on_save(self):
with open("player.json", "w") as fp:
json.dump(self.parent.player, fp, cls=serializer.SpaceEncoder, indent=4, sort_keys=True)
def show(self, ship):
self.parent.ship_info.set_model( ship )
ShipList.register_event_type("on_selected")
class ModList(gui.SubMenu):
def __init__(self):
super(ModList, self).__init__()
self.title = _("""Modifications""")
self.menu_halign = RIGHT
self.menu_valign = TOP
self.menu_hmargin = 20
self.menu_vmargin = 100
set_fonts(self)
def on_enter(self):
super(ModList, self).on_enter()
l = [MenuItem(mod.name, self.parent.on_mod_selected, mod) for mod in self.parent.inventory]
if not l:
l = [MenuItem("None", None)]
self.create_menu(l)
def on_exit(self):
super(ModList, self).on_exit()
map(self.remove, (child for z,child in self.children) )
def on_key_press(self, s, m):
return False
def on_change(self):
self.on_exit()
self.on_enter()