package Screens { import flash.display.*; import flash.events.*; import Gameplay.*; import flash.filters.*; import Services.LevelService; import Services.UserService; import Services.WeaponPanelService; public class BuyScreen extends MovieClip { private var widget : w_buy = new w_buy(); private var current_selection : int = 0; //Weapon the player is currently looking to purchase private var owns_weapon : int = -1; //True if current_selection is already owned by player private var weapons : Array = new Array(); private var items : Array = new Array(); private var icon : DisplayObject = icon; // Variables for Continue Button private var cont_status : int = I_LEVEL; public static const I_FAILED : int = 0; public static const I_SUCCESS : int = 1; public static const I_LEVEL : int = 2; //Services private var level : LevelService = SystemController.getInstance().levelService; private var user : UserService = SystemController.getInstance().userService; private var panel : WeaponPanelService = SystemController.getInstance().weaponPanelService; public function BuyScreen() { addEventListener(Event.ADDED_TO_STAGE, init); addChild(widget); widget.continue_btn.addEventListener(MouseEvent.CLICK, gotoNextScreen); widget.mainmenu_btn.addEventListener(MouseEvent.CLICK, gotoMainMenu); widget.buy_btn.addEventListener(MouseEvent.CLICK, buyWeapon); for (var i : int = 0; i < WeaponIndex.number_of_weapons; i++) { var weapon : Weapon = new Weapon(i); var temp : weapon_item = new weapon_item(); temp.addChild(weapon.graphic); weapon.graphic.scaleX = .3; weapon.graphic.scaleY = .3; weapons.push(weapon); items.push(temp); widget.addChild(temp); temp.weapon_name.text = weapon.name; temp.ammo.text = weapon.ammoLoaded + "/" + weapon.ammoLeft; temp.glowred.visible = false temp.addEventListener(MouseEvent.CLICK, weaponClicked); temp.x = (i % 6)*92 - 332; temp.y = Math.floor(i / 6) * 86 - 150; temp.addEventListener(MouseEvent.ROLL_OVER, highlight); temp.addEventListener(MouseEvent.ROLL_OUT, unhighlight); } showWeaponStats(0); } private function init(e:Event):void { user.syncAll(); widget.addChild(panel.widget); widget.x = stage.stageWidth / 2; widget.y = stage.stageHeight / 2; widget.cash_amount.text = "$" + user.money; panel.update(); } //User clicks on a weapon to view details before purchasing it private function weaponClicked(e:MouseEvent):void { showWeaponStats(items.indexOf(e.currentTarget)); } //Shows purchasing details about the gun private function showWeaponStats(index : int):void { current_selection = index; var weapon : Weapon = Weapon(weapons[index]); var pistol : Weapon = Weapon(weapons[WeaponIndex.PISTOL]); widget.damage.text = Math.round(10.0*weapon.damage / pistol.damage).toString(); widget.rate_of_fire.text = Math.round(300.0/weapon.frames_per_bullet).toString(); widget.penetration.text = weapon.penetration.toString(); widget.clip_size.text = weapon.clipSize.toString(); userOwnsGun(index); if (owns_weapon > -1) { // User owns gun, buy ammo widget.buy_btn.label.text = "Buy Ammo"; widget.price.text = "$" + Math.round(weapon.cost / 12.0); widget.price_description.text = "Price of Clip"; } else { // User does not own gun, buy the gun widget.buy_btn.label.text = "Buy Gun"; widget.price.text = "$" + weapon.cost; widget.price_description.text = "Price of Gun"; } // Show weapon if (icon != null) widget.removeChild(icon); icon = (new Weapon(weapon.type)).graphic; icon.x = -270; icon.y = 160; icon.scaleX = .85; icon.scaleY = .85; widget.addChild(icon); widget.weapon_name.text = weapon.name; } //Check to see if user owns a gun of type index, stores answer in owns_weapon private function userOwnsGun(index : int):void { var users_guns : Array = user.weapons; for (var i : int = 0; i < users_guns.length; i++) { if (Weapon(users_guns[i]).type == index) { owns_weapon = i; return; } } owns_weapon = -1; } //User clicks the "Buy" Button private function buyWeapon(e:MouseEvent):void { if (owns_weapon > -1) { // Try to buy ammo if (user.money >= Math.round(Weapon(weapons[current_selection]).cost / 12.0)) { Weapon(user.weapons[owns_weapon]).ammoLeft += Weapon(user.weapons[owns_weapon]).clipSize; user.money -= Math.round(Weapon(weapons[current_selection]).cost / 12.0); widget.cash_amount.text = "$" + user.money; panel.update(); user.syncWeapons(); user.syncCash(); } } else { // Try to buy gun if (user.money >= Weapon(weapons[current_selection]).cost) { user.weapons.push(new Weapon(current_selection)); user.money -= Weapon(weapons[current_selection]).cost; widget.cash_amount.text = "$" + user.money; panel.update(); user.syncWeapons(); user.syncCash(); showWeaponStats(current_selection); } } } private function highlight(e:MouseEvent):void { var filter : GlowFilter = new GlowFilter(0xAA0000, 1, 6, 6, 2, 3, true); weapon_item(e.currentTarget).glow.visible = true; } private function unhighlight(e:MouseEvent):void { weapon_item(e.currentTarget).glow.visible = false; } // Sets the message to be displayed on this screen public function setMessage(msg : int):void { cont_status = msg; switch (cont_status) { case I_FAILED: widget.continue_btn.label.text = "Retry"; break; case I_LEVEL: widget.continue_btn.label.text = "Go Back"; break; case I_SUCCESS: widget.continue_btn.label.text = "Next Level"; break; } } // Player clicked the "Continue" button private function gotoNextScreen(e:Event):void { switch (cont_status) { case I_LEVEL: SystemController.getInstance().animateChangeScreen(this, ScreenIndex.LEVEL, true); break; case I_SUCCESS: level.setLevel(level.levelIndex + 1); SystemController.getInstance().fadeChangeScreen(this, ScreenIndex.SINGLE); break; case I_FAILED: level.setLevel(level.levelIndex); SystemController.getInstance().fadeChangeScreen(this, ScreenIndex.SINGLE); break; default: SystemController.getInstance().animateChangeScreen(this, ScreenIndex.MAIN, true); } } // Player clicked the "Main Menu" button private function gotoMainMenu(e:Event):void { SystemController.getInstance().animateChangeScreen(this, ScreenIndex.MAIN, true); } } }