Module game_layers
[hide private]
[frames] | no frames]

Source Code for Module game_layers

  1  from cocos import euclid, collision_model 
  2  from cocos.director import director 
  3  from cocos.actions import Show, MoveTo, Hide 
  4  from cocos.layer.base_layers import Layer 
  5  from cocos.sprite import Sprite 
  6  from cocos.text import Label 
  7  from constants import ACTION_BUTTON_WIDTH, MENU_BUTTON_Z, ACTIONMENU_X_OFFSET, \ 
  8      ACTIONMENU_Y_OFFSET, TIMER_X_OFFSET, TIMER_Y_OFFSET, TIMER_SCALE, WINDOW_WIDTH, \ 
  9      WINDOW_HEIGHT, MINMAP_CIRCLE_Z, STATUS_BAR_HEIGHT, SOUND, MUSIC 
 10  from imageLayer import ImageLayer 
 11  from maps import MiniMap, MiniMapCircle 
 12  from constants import * 
 13  import objects 
 14  from utils import aabb_to_aa_rect 
 15  import os 
 16  import pyglet 
 17  from music import theme_player 
 18  import pyglet.window 
 19  import constants 
 20  import cocos.rect as rect 
 21  from cocos.menu import CENTER, Menu, MenuItem, EntryMenuItem 
 22  from pyglet.resource import media, image 
 23  from cocos.actions.interval_actions import ScaleTo 
 24   
 25   
 26  # Loading the font directory 
 27  pyglet.font.add_directory('fonts') 
 28   
 29   
30 -class StatusMenu(Layer):
31 is_event_handler = True 32
33 - def __init__(self,settingsLayer,player):
34 super(StatusMenu,self).__init__() 35 self.position = (0,WINDOW_HEIGHT-STATUS_BAR_HEIGHT) 36 self.controller = objects.Objects.get_controller() 37 self.cm = collision_model.CollisionManagerBruteForce() 38 self.settingsMenu = settingsLayer 39 self.player = player
40
41 - def on_enter(self):
42 super(StatusMenu,self).on_enter() 43 self.add(Sprite(os.path.join("images","background.png"),scale=2,position=(500,30)),z=-1) 44 #self.add(Sprite(os.path.join("images","background.png"),scale=2,opacity=255,position=(500,30)),z=-1) #changed the sprite to be darker 45 #self.add(Sprite(os.path.join("images","background.png"),scale=2,opacity=255,position=(500,30)),z=-1) 46 text1 = Label(text="# Units: ",font_name='Rabiohead',anchor_y='center',position=(20,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 47 self.add(text1) 48 text2 = Label(text="# Idle CPUs: ",font_name='Rabiohead',anchor_y='center',position=(195,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 49 self.add(text2) 50 51 self.selectedUnitText = "" 52 53 self.selectedUnitLabel = Label(text="Selected Unit: " + self.selectedUnitText,font_name='Rabiohead',anchor_y='center',position=(440,STATUS_BAR_HEIGHT/2),font_size=24,color=(255,255,255,255),multiline=False) 54 self.add(self.selectedUnitLabel) 55 # self.health = Label(text="Health: ",font_name="Rabiohead",anchor_y="center",position=(20,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 56 # self.add(self.health) 57 # health = 0 58 # totalhealth = 0 59 # for building in self.player.units: 60 # health += self.player.units[building].health 61 # totalhealth += self.player.units[building].health 62 # self.curHealth = int((float(health) / totalhealth) * 100.0) 63 # self.curHealthLabel = Label(text=str(self.curHealth) + "%",font_name='Rabiohead',anchor_y='center',position=(130,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 64 # self.add(self.curHealthLabel) 65 self.menuButton = MenuButton(960,10) 66 self.add(self.menuButton,z=1) 67 self.cm.add(self.menuButton) 68 self.musicButton = ToggleMusicButton(885,18) 69 self.add(self.musicButton,z=1) 70 self.cm.add(self.musicButton) 71 self.soundButton = ToggleSoundButton(820,15) 72 self.add(self.soundButton,z=1) 73 self.cm.add(self.soundButton) 74 75 self.oldunitCount = len(self.controller.player.units) 76 self.oldCpuCount = len(self.controller.player.idleCPUs) 77 self.unitCountLabel = Label(text=str(self.oldunitCount),font_name='Rabiohead',anchor_y='center',position=(130,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 78 self.cpuCountLabel = Label(text=str(self.oldCpuCount),font_name='Rabiohead',anchor_y='center',position=(390,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 79 self.add(self.unitCountLabel) 80 self.add(self.cpuCountLabel) 81 self.schedule(self.step) 82 self.soundX = Label(text="X",font_name='Rabiohead',anchor_y='center',position=(795,37),font_size=50,bold=True,color=(255,255,255,255),multiline=False) 83 self.musicX = Label(text="X",font_name='Rabiohead',anchor_y='center',position=(867,37),font_size=50,bold=True,color=(255,255,255,255),multiline=False) 84 if self.settingsMenu.settingsMenuOn: 85 self.add(settingsMenu)
86
87 - def on_mouse_release(self, x, y, buttons, modifiers):
88 # Mouse released 89 clicked_objects = self.cm.objs_touching_point(x, WINDOW_HEIGHT - y) 90 for item in clicked_objects: 91 if type(item) == MenuButton: 92 self.settingsMenu.toggle_settings_menu() 93 94 elif type(item) == ToggleSoundButton: 95 if constants.SOUND: 96 self.add(self.soundX,z=2) 97 else: 98 self.remove(self.soundX) 99 constants.SOUND = not constants.SOUND 100 101 elif type(item) == ToggleMusicButton: 102 constants.MUSIC = not constants.MUSIC 103 if not constants.MUSIC: 104 theme_player.stop() 105 self.add(self.musicX,z=2) 106 else: 107 theme_player.play() 108 self.remove(self.musicX)
109
110 - def step(self,dt):
111 from models import Server 112 if len(self.controller.player.units) != self.oldunitCount: 113 self.oldunitCount = len(self.controller.player.units) 114 self.remove(self.unitCountLabel) 115 self.unitCountLabel = Label(text=str(self.oldunitCount),font_name='Rabiohead',anchor_y='center',position=(130,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 116 self.add(self.unitCountLabel) 117 118 if len(self.controller.player.idleCPUs) != self.oldCpuCount: 119 self.oldCpuCount = len(self.controller.player.idleCPUs) 120 self.remove(self.cpuCountLabel) 121 self.cpuCountLabel = Label(text=str(self.oldCpuCount),font_name='Rabiohead',anchor_y='center',position=(390,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 122 self.add(self.cpuCountLabel) 123 124 if len(self.controller.selectedUnits) > 0: 125 self.remove(self.selectedUnitLabel) 126 self.selectedUnitText = str(self.controller.selectedUnits[0].__class__.__name__) 127 self.selectedUnitLabel = Label(text="Selected Unit: " + self.selectedUnitText,font_name='Rabiohead',anchor_y='center',position=(440,STATUS_BAR_HEIGHT/2),font_size=24,color=(255,255,255,255),multiline=False) 128 self.add(self.selectedUnitLabel) 129 self.selectedUnitText = "" 130 elif self.selectedUnitText == "": 131 self.remove(self.selectedUnitLabel) 132 self.selectedUnitLabel = Label(text="Selected Unit: " + self.selectedUnitText,font_name='Rabiohead',anchor_y='center',position=(440,STATUS_BAR_HEIGHT/2),font_size=24,color=(255,255,255,255),multiline=False) 133 self.selectedUnitText = "0" 134 self.add(self.selectedUnitLabel)
135 136 137 138 # health = 0 139 # totalHealth = 0 140 # for building in self.player.units: 141 # health += self.player.units[building].health 142 # totalHealth += self.player.units[building].initialHealth 143 # try: 144 # healthPercent = int(float(health) / totalHealth * 100) 145 # except: 146 # pass 147 148 # if healthPercent != self.curHealth: 149 # self.remove(self.curHealthLabel) 150 # self.curHealthLabel = Label(text=str(self.curHealth) + "%",font_name='Rabiohead',anchor_y='center',position=(130,STATUS_BAR_HEIGHT/2),font_size=27,color=(255,255,255,255),multiline=False) 151 # self.add(self.curHealthLabel) 152 # self.curHealth = healthPercent 153
154 -class SettingsMenuSprite(Sprite):
155 - def __init__(self,x,y):
156 super(SettingsMenuSprite, self).__init__(os.path.join("images", 157 "maps", "minimap_bg.png")) 158 self.position = euclid.Vector2(x,y) 159 self.opacity = 255 160 self.scale = 0.6 161 self.cshape = aabb_to_aa_rect(self.get_AABB())
162 163
164 -def set_menu_theme(menu):
165 menu.font_title['font_name'] = 'Rabiohead' 166 menu.font_title['font_size'] = 50 167 menu.font_title['color'] = (0, 0, 0, 255) 168 169 menu.font_item['font_name'] = 'Rabiohead' 170 menu.font_item['font_size'] = 18 171 menu.font_item['color'] = (0, 0, 0, 255) 172 173 menu.font_item_selected['font_name'] = 'Rabiohead' 174 menu.font_item_selected['font_size'] = 18 175 menu.font_item_selected['color'] = (0, 0, 0, 255) 176 177 menu.menu_valign = CENTER 178 menu.menu_halign = CENTER
179
180 -def zoom_in():
181 return ScaleTo(1.2, duration=0.2)
182 183
184 -def zoom_out():
185 return ScaleTo(1.0, duration=0.2)
186
187 -class SettingsMenu(Menu):
188 activate_sound = media( 189 os.path.join('sounds', "Menu.wav"), streaming=False) 190 select_sound = media( 191 os.path.join('sounds', "Menu.wav"), streaming=False) 192
193 - def __init__(self):
194 super(SettingsMenu, self).__init__() 195 196 set_menu_theme(self) 197 198 # then add the items 199 self.home = MenuItem('Home', self.on_home) 200 self.fullscreen = MenuItem('Toggle Full Screen', self.on_fullscreen) 201 self.tutorial = MenuItem('Toggle Tutorial', self.on_tutorial) 202 203 self.items = [ 204 (self.home), 205 (self.fullscreen), 206 (self.tutorial), 207 ] 208 209 self.create_menu(self.items, zoom_in(), zoom_out())
210
211 - def on_home(self):
212 if constants.MUSIC: 213 theme_player.stop() 214 director.pop()
215
216 - def on_fullscreen(self):
217 director.window.set_fullscreen(not director.window.fullscreen)
218 219
220 - def on_tutorial(self):
222 223
224 -class SettingsLayer(Layer):
225 is_event_handler = True 226
227 - def __init__(self):
228 super(SettingsLayer, self).__init__() 229 self.cm = collision_model.CollisionManagerBruteForce() 230 self.settingsMenuOn = False 231 self.settingsMenu = SettingsMenu() 232 self.settingsMenuSprite = SettingsMenuSprite(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)
233 # self.homeButton = HomeButton(WINDOW_WIDTH / 2 - 30, WINDOW_HEIGHT / 2 + 60) 234 # self.toggleTutorialButton = ToggleTutorialButton(WINDOW_WIDTH / 2 - 50, WINDOW_HEIGHT / 2, 'Toggle Tutorial On') 235 # self.toggleFullScreenButton = ToggleFullScreenButton(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 - 60, 'Toggle Full Screen On') 236
237 - def toggle_settings_menu(self):
238 # Toggles the settings on/off 239 if self.settingsMenuOn: 240 self.remove(self.settingsMenu) 241 self.remove(self.settingsMenuSprite) 242 # print "menu on" 243 # self.remove(self.settingsMenu) 244 # self.remove(self.homeButton) 245 # self.remove(self.toggleTutorialButton) 246 # self.remove(self.toggleFullScreenButton) 247 # self.cm.remove_tricky(self.settingsMenu) 248 # self.cm.remove_tricky(self.homeButton) 249 # self.cm.remove_tricky(self.toggleTutorialButton) 250 # self.cm.remove_tricky(self.toggleFullScreenButton) 251 252 else: 253 self.add(self.settingsMenu,z=25) 254 # print "menu off" 255 self.add(self.settingsMenuSprite,z=20) 256 # self.add(self.homeButton,z=25) 257 # self.add(self.toggleTutorialButton,z=20) 258 # self.add(self.toggleFullScreenButton,z=20) 259 # self.cm.add(self.settingsMenu) 260 # self.cm.add(self.homeButton) 261 # self.cm.add(self.toggleTutorialButton) 262 # self.cm.add(self.toggleFullScreenButton) 263 264 self.settingsMenuOn = not self.settingsMenuOn
265
266 - def on_key_press(self, key, modifiers):
267 if key == pyglet.window.key.SPACE: 268 self.toggle_settings_menu()
269
270 - def on_mouse_release(self, x, y, buttons, modifiers):
271 # Mouse released - check to see if we clicked on the menu and need 272 # to set screen focus. 273 if self.settingsMenuOn: 274 clicked_objects = self.cm.objs_touching_point( 275 x - self.position[0], y - self.position[1]) 276 for item in clicked_objects: 277 if type(item) == HomeButton: 278 if constants.MUSIC: 279 theme_player.stop() 280 director.pop() 281 elif type(item) == ToggleTutorialButton: 282 print "Tutorial Toggled" 283 constants.SHOW_TUTORIAL = not constants.SHOW_TUTORIAL 284 elif type(item) == ToggleFullScreenButton: 285 print "Full Screen Toggled" 286 director.window.set_fullscreen(not director.window.fullscreen)
287
288 -class ActionButton(Sprite):
289
290 - def __init__(self, x, y, name, unitParent):
291 fontSize = 8 292 self.is_text = False 293 294 try: 295 image = os.path.join("images", "menus", BUTTON_DICTIONARY[name]) 296 except: 297 image = os.path.join("images", "menus", "unit_action_button.png") 298 self.is_text = True 299 textOffsetX = 13 300 textOffsetY = -5 301 super(ActionButton, self).__init__(image) 302 303 # Calculate action time in number of seconds. We could also add this 304 # info to the name (or to self.actionList some other way) 305 self.actionTime = { 306 "DEL": 3, 307 "TPING": 8, 308 "TCONS": 15, 309 "BDB": 10, 310 "BCPU": 20 311 }.get(name, None) 312 313 decryptType = "" 314 if name == "Decrypt": 315 decryptType = str(unitParent.originalType.__class__.__name__) 316 317 self.buttonName = { 318 "TPing":"Create Ping", 319 "TAPTGet":"Create APT-Get", 320 "TDOS":"Create DOS", 321 "TInstaller":"Create Installer", 322 "TSQLInjection":"Create SQLInjection", 323 "TDNSPoison":"Create DNSPoison", 324 "THandshake":"Create Handshake", 325 "TSpoof":"Create Spoof", 326 "TBufferOverflow":"Create Buffer Overflow", 327 "BDB":"Install Database", 328 "BCPU":"Program FPGA", 329 "BDatabase":"Install Database", 330 "BAlgorithmFactory":"Allocate Algorithms", 331 "BSoftwareUpdater":"Download Software Updater", 332 "BFirewall":"Enable Firewall", 333 "BRSA":"Write RSA", 334 "BHandshake":"Build Handshake", 335 "BDNSPoison":"Build DNS Poison", 336 "BSpoofedBuilding": "Build Spoofed Building", 337 "UPingOfDeath":"Upgrade to Ping of Death", 338 "UNMap":"Upgrade to NMap", 339 "USinkhole":"Upgrade to Sinkhole", 340 "RPortScanner":"Port Scanner", 341 "RHandshake":"Research Handshake", 342 "RBigData":"Research Big Data", 343 "RAdvancedAlgorithms":"Research Advanced Algorithms", 344 "RPingResearch": "Advanced Ping", 345 "RNetworkTopology": "Research Network Topology", 346 "RFPGA":"Research FPGA", 347 "RRSA":"Research RSA", 348 "ROverclocking":"Research Overclocking", 349 "ROverflow":"Research Overflow", 350 "DSpoof":"Spoofed Building", 351 "Attack":"Attack", 352 "Shake":"Shake", 353 "Encrypt":"Encrypt", 354 "Decrypt":"Decrypt To " + decryptType, 355 "GenKey":"Generate Key", 356 "Ping":"Execute Ping", 357 "NMap":"Perform NMap" 358 }.get(name, []) # Determine the name to display on the button 359 360 if self.buttonName == []: 361 quit() 362 363 self.position = euclid.Vector2(x, y) 364 self.text = Label(self.buttonName, position=( 365 x - textOffsetX, y - textOffsetY), color=(50, 50, 116, 255), font_size=fontSize, multiline = True, font_name='Rabiohead', width=ACTION_BUTTON_WIDTH - 5) 366 self.name = name 367 self.cshape = aabb_to_aa_rect(self.get_AABB()) 368 self.unitParent = unitParent
369 # self.cshape.center = self.position 370
371 - def add(self, game_map, cm):
372 game_map.add(self, z=MENU_BUTTON_Z) 373 if self.is_text == True: 374 game_map.add(self.text, z=MENU_BUTTON_Z) 375 cm.add(self)
376
377 - def remove(self, game_map, cm):
378 game_map.remove(self) 379 if self.is_text == True: 380 game_map.remove(self.text) 381 cm.remove_tricky(self)
382 # This is the sprite for the background to the action menu - I'm making a 383 # primitive version of this that we will need to change once final visuals 384 # are added. 385
386 -class ActionMenu(Sprite):
387
388 - def __init__(self, x, y, actions, unitParent):
389 from models import Troop 390 if issubclass(type(unitParent), Troop): 391 image = os.path.join("images", "menus", "unit_action_menu.png") 392 # x = x - ACTIONMENU_X_OFFSET 393 # y = y - ACTIONMENU_Y_OFFSET 394 # elif type(unitParent) == SpoofedBuilding: 395 # image = os.path.join("images", "menus", "spoofed_building_action_menu.png") 396 else: 397 image = os.path.join("images", "menus", "building_action_menu.png") 398 super(ActionMenu, self).__init__(image) 399 self.position = euclid.Vector2(x, y) 400 self.actionNames = actions 401 self.cshape = aabb_to_aa_rect(self.get_AABB()) 402 self.actionList = [] 403 self.unitParent = unitParent 404 405 if issubclass(type(unitParent), Troop): 406 self.slots = [euclid.Vector2(x - 43, y), 407 euclid.Vector2(x + 0, y), 408 euclid.Vector2(x + 43, y)] # HOW MANY SLOTS SHOULD TROOPS HAVE? 3? 409 else: 410 self.slots = [euclid.Vector2(x + 50, y - 0), 411 euclid.Vector2(x - 50, y - 0), 412 euclid.Vector2(x - 0, y - 50), 413 euclid.Vector2(x - 0, y + 50), 414 euclid.Vector2(x + 35.11, y - 35.11), 415 euclid.Vector2(x + 35.11, y + 35.11), 416 euclid.Vector2(x - 35.11, y - 35.11), 417 euclid.Vector2(x - 35.11, y + 35.11)] 418 419 # (Easily Changed) 420 self.make_action_buttons()
421
422 - def make_action_buttons(self):
423 x = self.position[0] 424 y = self.position[1] 425 426 for i in range(len(self.actionNames)): 427 action = ActionButton(self.slots[i][0], self.slots[ 428 i][1], self.actionNames[i], self.unitParent) 429 self.actionList.append(action)
430
431 - def add_action_buttons(self, game_map, cm):
432 for button in self.actionList: 433 button.add(game_map, cm)
434
435 - def remove_action_buttons(self, game_map, cm):
436 for button in self.actionList: 437 button.remove(game_map, cm)
438 439
440 -class TransTimer(Sprite):
441 - def __init__(self, sec, pos, opacity=1):
442 super(TransTimer, self).__init__(os.path.join("images", 443 "maps", "transtimer.png")) 444 self.position = euclid.Vector2( 445 pos[0] - TIMER_X_OFFSET, pos[1] - TIMER_Y_OFFSET) 446 self.scale = TIMER_SCALE 447 self.opacity = opacity * 255 448 self.duration = float(sec)
449
450 - def get_move_action(self, action=None):
451 x, y = self.position 452 y = y + 40 453 theLocation = euclid.Vector2(x, y) 454 if action == None: 455 moveAction = Show() 456 else: 457 moveAction = action + Show() 458 459 moveAction += MoveTo(theLocation, self.duration / 4) 460 x = x + 40 461 theLocation = euclid.Vector2(x, y) 462 moveAction += MoveTo(theLocation, self.duration / 4) 463 y = y - 40 464 theLocation = euclid.Vector2(x, y) 465 moveAction += MoveTo(theLocation, self.duration / 4) 466 x = x - 40 467 theLocation = euclid.Vector2(x, y) 468 moveAction += MoveTo(theLocation, self.duration / 4) 469 moveAction += Hide() 470 471 return moveAction
472 483
484 -class HomeButton(Label):
485 - def __init__(self, x, y):
486 super(HomeButton, self).__init__(text='Home',font_name='Rabiohead', font_size=24) 487 self.visible = True 488 self.scale = 1 489 width = 50 490 height = 20 491 self.rect = rect.Rect(x - width/2, - height/2, x + width/2, x + height/2) 492 493 self.position = euclid.Vector2(x,y) 494 self.cshape = aabb_to_aa_rect(self.rect) 495 self.cshape.center = self.position
496
497 -class ToggleFullScreenButton(Label):
498 - def __init__(self, x, y, text):
499 super(ToggleFullScreenButton, self).__init__(text=text,font_name='Rabiohead', font_size=24) 500 self.visible = True 501 self.scale = 1 502 width = 50 503 height = 20 504 self.rect = rect.Rect(x - width/2, - height/2, x + width/2, x + height/2) 505 self.position = euclid.Vector2(x,y) 506 self.cshape = aabb_to_aa_rect(self.rect) 507 self.cshape.center = self.position
508
509 -class StickyNote(Sprite):
510 - def __init__(self, levelNumber, levelFiveCounter = 0):
511 512 notePosition = { 513 "2":euclid.Vector2(320,1080), 514 "3":euclid.Vector2(350,1700), 515 "4":euclid.Vector2(400,1270), 516 "5":euclid.Vector2(10,1000) 517 }[levelNumber] 518 519 self.levelFiveCounter = levelFiveCounter 520 521 if levelNumber == "5": 522 self.levelFiveCounter += 1 523 else: 524 self.levelFiveCounter = "" 525 526 527 super(StickyNote, self).__init__(os.path.join("images", "tutorial", "level" + levelNumber + "_instruction" + str(self.levelFiveCounter) + ".png"), position = notePosition)
528
529 -class TutorialXButton(Sprite):
530 - def __init__(self, stickyNoteParent):
531 super(TutorialXButton, self).__init__(os.path.join("images", "tutorial", "x.png")) 532 x = stickyNoteParent.position[0] + 84 533 y = stickyNoteParent.position[1] + 112 534 self.position = euclid.Vector2(x, y) 535 self.stickyNoteParent = stickyNoteParent 536 self.cshape = aabb_to_aa_rect(self.get_AABB()) 537 self.cshape.center = self.position
538 539 540
541 -class ToggleTutorialButton(Label):
542 - def __init__(self, x, y, text):
543 super(ToggleTutorialButton, self).__init__(text=text,font_name='Rabiohead',font_size=24) 544 self.visible = True 545 self.scale = 1 546 width = 50 547 height = 20 548 self.rect = rect.Rect(x - width/2, - height/2, x + width/2, x + height/2) 549 self.position = euclid.Vector2(x,y) 550 self.cshape = aabb_to_aa_rect(self.rect) 551 self.cshape.center = self.position
552 553
554 -class ToggleSoundButton(Sprite):
555 - def __init__(self, x, y):
556 557 super(ToggleSoundButton, self).__init__(os.path.join( 558 "images", "maps", "speaker.png")) 559 self.visible = True 560 self.scale = 1 561 562 self.position = euclid.Vector2(x,y) 563 self.cshape = aabb_to_aa_rect(self.get_AABB()) 564 self.cshape.center = self.position
565 566
567 -class ToggleMusicButton(Sprite):
568 - def __init__(self, x, y):
569 super(ToggleMusicButton, self).__init__(os.path.join( 570 "images", "maps", "music.png")) 571 self.visible = True 572 self.scale = 1 573 574 self.position = euclid.Vector2(x,y) 575 self.cshape = aabb_to_aa_rect(self.get_AABB()) 576 self.cshape.center = self.position
577 578 579
580 -class SurrenderButton(Sprite):
581 - def __init__(self, x, y, isVisible=True):
582 super(SurrenderButton, self).__init__(os.path.join( 583 "images", "maps", "surrender.png")) 584 self.visible = isVisible 585 self.position = euclid.Vector2(x, y) 586 self.cshape = aabb_to_aa_rect(self.get_AABB()) 587 self.cshape.center = self.position 588 self.scale = 0.2 # Arbitrary
589
590 -class MessageInstruction(object):
591 - def __init__(self, parent_layer, message, has_image_background, x, y, width, height, font):
592 self.parent_layer = parent_layer 593 self.background_layer = ImageLayer(os.path.join('images','tutorial','background.png')) 594 self.message = message 595 self.has_image_background = has_image_background 596 self.message_label = Label( 597 self.message, font_name='Rabiohead', font_size=font, 598 width=width, height=height, 599 color=(0, 0, 0, 255), 600 anchor_x='center', anchor_y='center', multiline = True) 601 602 if(self.has_image_background == True): 603 # Position needs to be worked out 604 self.background_layer.postition = 300, 500 605 self.parent_layer.add(self.background_layer) 606 self.message_label.position = 50, 50 607 self.background_layer.add(self.message_label) 608 else: 609 self.message_label.position = x, y 610 self.parent_layer.add(self.message_label)
611
612 - def removeMessage(self):
613 if(self.has_image_background == True): 614 self.parent_layer.kill() 615 else: 616 self.message_label.kill()
617 618
619 -class MessageAlert(Layer):
620 - def __init__(self, parent_layer, message, image):
621 super(MessageAlert, self).__init__() 622 self.message_layer = ImageLayer(image) 623 self.message_layer_height = 400 # sets the constants 624 self.message_layer_width = 300 # set the constants 625 self.parent_layer = parent_layer 626 self.message = message 627 self.message_label = Label( 628 self.message, font_name='Rabiohead', font_size=26, 629 x=self.message_layer_width / 2, y=self.message_layer_height / 2, 630 width=self.message_layer_width - 100, height=self.message_layer_height - 100, 631 color=(0, 0, 0, 255), 632 anchor_x='center', anchor_y='center', multiline = True) 633 634 # added text to specified parent layer(should be the 635 # self.game_controller_layer of controller.py) 636 self.message_label.position = 30, -240 637 self.message_layer.add(self.message_label) 638 self.message_layer.position = 0, 0 639 self.parent_layer.add(self.message_layer)
640
641 - def removeMessage(self):
642 self.message_layer.kill()
643 644
645 -class InfoLayer(Layer):
646 is_event_handler = True 647
648 - def __init__(self, gameMap, scroller,player):
649 super(InfoLayer, self).__init__() 650 self.map = gameMap 651 self.scroller = scroller 652 self.cm = collision_model.CollisionManagerBruteForce( 653 ) # Grid having problems... 654 self.miniMapToggled = False 655 self.player = player 656 # Also hard coded. 657 self.position = (3 * WINDOW_WIDTH / 4.0 + 90, WINDOW_HEIGHT / 4.0 - 20) 658 self.visibleCircles = [] 659 # Define the minimap rectangle sprite that we toggle on/off. 660 self.miniMap = MiniMap( 661 self.map.AS, self.map.w, self.map.h, self.map.edges, self.player) 662 self.map.minimap = self.miniMap # adds this MiniMap instance to the map
663
664 - def toggle_mini_map(self):
665 # Toggles the minimap on/off 666 if self.miniMapToggled: 667 self.remove(self.miniMap) 668 for circle in self.visibleCircles: 669 self.remove(circle) 670 self.cm.remove_tricky(circle) 671 self.visibleCircles = [] 672 673 #if curVertex.building: 674 # self.remove(self.miniMap.minimapBuildings[vid]) 675 # self.cm.remove_tricky(self.miniMap.minimapBuildings[vid]) 676 677 else: 678 self.add(self.miniMap) 679 for vid in self.miniMap.miniMapCircles: 680 curVertex = self.map.vertices[vid] 681 if curVertex.visibilityState != 0: 682 circle = self.miniMap.miniMapCircles[vid] 683 self.add(circle, z=MINMAP_CIRCLE_Z) 684 self.visibleCircles.append(circle) 685 self.cm.add(circle) 686 687 #if self.map.vertices[vid].building: 688 #building = Sprite(os.path.join('images', 'maps', 'minimap_building.png'), scale=0.1,position=euclid.Vector2(float(self.miniMap.miniMapCircles[vid].position[0]), float(self.miniMap.miniMapCircles[vid].position[1]))) 689 #self.add(building, z=MINMAP_CIRCLE_Z) 690 #self.miniMap.minimapBuildings[vid] = building 691 # self.cm.add(building) 692 693 self.miniMapToggled = not self.miniMapToggled
694
695 - def on_key_press(self, key, modifiers):
696 if key == 65289: 697 # Pressed tab. Toggle minimap. 698 self.toggle_mini_map()
699
700 - def on_mouse_release(self, x, y, buttons, modifiers):
701 # Mouse released - check to see if we clicked on the menu and need 702 # to set screen focus. 703 if self.miniMapToggled: 704 clicked_objects = self.cm.objs_touching_point( 705 x - self.position[0], y - self.position[1]) 706 for item in clicked_objects: 707 if type(item) == MiniMapCircle: 708 self.scroller.set_focus( 709 item.asPosition[0] - WINDOW_WIDTH / 2.0, 710 item.asPosition[1] - WINDOW_HEIGHT / 2.0)
711 712 713 keyboard = key.KeyStateHandler() 714
715 -class HotkeysLayer(Layer):
716 is_event_handler = True 717
718 - def __init__(self, hotkeys, scroller):
719 super(HotkeysLayer, self).__init__() 720 self.scroller = scroller 721 self.cm = collision_model.CollisionManagerBruteForce() 722 self.hotkeysMenuOn = False 723 self.hotkeys = hotkeys 724 # self.hotkeysMenu = HotkeysMenu(self.hotkeys, 190, 200) 725 self.hotkeysList = [] 726 self.keyNumList = []
727
728 - def update_hotkeys_menu(self):
729 # Toggles the hotkeys menu on/off 730 731 if self.hotkeysMenuOn: 732 for key in self.keyNumList: 733 self.remove(key) 734 for key in self.hotkeysList: 735 self.remove(key) 736 self.cm.remove_tricky(key) 737 self.keyNumList = [] 738 self.hotkeysList = [] 739 x = 10 740 y = 600 741 for hotkey in self.hotkeys: 742 if self.hotkeys[hotkey]: 743 y -= 50 744 keyNum = hotkey - 48 745 newKey = Label(text="%s:" % keyNum,font_name='Rabiohead',anchor_y='center',position=(x,y),font_size=14,color=(0,0,0,255),multiline=False) 746 x+=40 747 newLabel = HotkeyLabel((x,y),self.hotkeys[hotkey]) 748 self.hotkeysList.append(newLabel) 749 self.cm.add(newLabel) 750 self.keyNumList.append(newKey) 751 self.add(newLabel,z=25) 752 self.add(newKey,z=25) 753 x -= 40
754 755
756 - def toggle_hotkeys_menu(self):
757 # Toggles the hotkeys menu on/off 758 759 if self.hotkeysMenuOn: 760 # self.remove(self.hotkeysMenu) 761 # self.remove(self.title) 762 763 for key in self.hotkeysList: 764 self.remove(key) 765 self.cm.remove_tricky(key) 766 for num in self.keyNumList: 767 self.remove(num) 768 self.hotkeysList = [] 769 self.keyNumList = [] 770 771 else: 772 # self.add(self.hotkeysMenu,z=20) 773 # self.title = Label(text="Hotkeys:",font_name='Rabiohead',anchor_y='center',position=(10,630),font_size=20,color=(0,0,0,255),multiline=False) 774 # self.add(self.title,z=25) 775 x = 10 776 y = 600 777 for hotkey in self.hotkeys: 778 if self.hotkeys[hotkey]: 779 y -= 50 780 keyNum = hotkey - 48 781 newKey = Label(text="Key: %s" % keyNum,font_name='Rabiohead',anchor_y='center',position=(x,y),font_size=12,color=(0,0,0,255),multiline=False) 782 x+=70 783 newLabel = HotkeyLabel((x,y),self.hotkeys[hotkey]) 784 self.hotkeysList.append(newLabel) 785 self.cm.add(newLabel) 786 self.keyNumList.append(newKey) 787 self.add(newLabel,z=25) 788 self.add(newKey,z=25) 789 x -= 70 790 791 self.hotkeysMenuOn = not self.hotkeysMenuOn
792
793 - def on_key_press(self, key, modifiers):
794 if key == pyglet.window.key.SLASH and modifiers == pyglet.window.key.MOD_SHIFT: 795 self.toggle_hotkeys_menu()
796
797 - def on_mouse_release(self, x, y, buttons, modifiers):
798 # Mouse released - check to see if we clicked on the menu and need 799 # to set screen focus. 800 if self.hotkeysMenuOn: 801 clicked_objects = self.cm.objs_touching_point(x, y) 802 for item in clicked_objects: 803 if type(item) == HotkeyLabel: 804 self.scroller.set_focus( 805 item.hotkeyPos[0] - WINDOW_WIDTH / 2.0, 806 item.hotkeyPos[1] - WINDOW_HEIGHT / 2.0)
807 808
809 -class HotkeysMenu(Sprite):
810 - def __init__(self, hotkeys, x, y):
811 super(HotkeysMenu, self).__init__(os.path.join("images", 812 "maps", "minimap_bg.png")) 813 self.hotkeys = hotkeys 814 self.position = euclid.Vector2(float(x), float(y)) 815 self.opacity = 255 816 self.scale = 0.6 817 self.cshape = aabb_to_aa_rect(self.get_AABB())
818
819 - def setup(self):
820 pass
821 822
823 -class HotkeyLabel(Sprite):
824 - def __init__(self, position, hotkey):
825 super(HotkeyLabel, self).__init__(hotkey.imageOutline) 826 self.hotkeyPos = hotkey.position 827 self.scale = 0.7 828 self.position = euclid.Vector2(float(position[0]), float(position[1])) 829 self.cshape = collision_model.CircleShape( 830 euclid.Vector2(x=self.position[0], y=self.position[1]), 14)
831 832
833 -class HotkeyUnit(Sprite):
834 - def __init__(self, position, asPosition, color, scale, building=False):
835 super(MiniMapCircle, self).__init__(os.path.join("images", 836 "maps", "minimap_circle.png")) 837 self.color = color 838 self.scale = scale 839 self.position = euclid.Vector2(float(position[0]), float(position[1])) 840 self.asPosition = asPosition # position of AS associated with this circle 841 # self.cshape = aabb_to_aa_rect(self.get_AABB()) 842 self.cshape = collision_model.CircleShape( 843 euclid.Vector2(x=self.position[0], y=self.position[1]), 14) 844 if building: 845 self.building = Sprite(os.path.join( 846 'images', 'maps', 'minimap_building.png'), position=euclid.Vector2(float(position[0]), float(position[1])))
847 # self.batch_add(circle, z=MINMAP_CIRCLE_Z) Circle is undefined 848