Advertisement
Guest User

DBStoreController

a guest
Jan 11th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //DBStoreController.js from FPS Construct (Dastardly Banana)
  2.  
  3. //FPS Constructor - Weapons
  4. //Copyright© Dastardly Banana Productions 2010
  5. //This script is distributed exclusively through ActiveDen and it's use is restricted to the terms of the ActiveDen
  6. //licensing agreement.
  7. //
  8. // Questions should be addressed to info@dastardlybanana.com
  9. //
  10. class WeaponClassArrayType{
  11.     var weaponClass : String;
  12.     var WeaponInfoArray : WeaponInfo[];
  13. }
  14.  
  15. static var storeActive : boolean = false;
  16. static var canActivate : boolean = true;
  17. static var singleton : DBStoreController;
  18.  
  19.  var balance: float; // Store account balance
  20.  
  21.  //var scrollPosition : Vector2;
  22. @HideInInspector var WeaponInfoArray : WeaponInfo[] ;
  23. @HideInInspector var WeaponInfoByClass :WeaponClassArrayType[];
  24. @HideInInspector var weaponClassNames : String[];
  25. @HideInInspector var weaponClassNamesPopulated : String [];
  26. @HideInInspector var playerW : PlayerWeapons;
  27. @HideInInspector var nullWeapon : GameObject; //there must be one null weapon as a placeholder to put in an empty slot.
  28. @HideInInspector var slotInfo: SlotInfo;
  29. var canExitWhileEmpty : boolean = false;
  30. static var inStore : boolean = false;
  31.  
  32. function Initialize() {
  33.     singleton = this;
  34.     playerW = FindObjectOfType(PlayerWeapons) as PlayerWeapons;
  35.     slotInfo = FindObjectOfType(SlotInfo) as SlotInfo;     
  36.     WeaponInfoArray = FindObjectsOfType(WeaponInfo) as WeaponInfo[];
  37.     for(var w : WeaponInfo in WeaponInfoArray) {
  38.         if(w.weaponClass == weaponClasses.Null)
  39.             nullWeapon = w.gameObject;
  40.     }
  41.     setupWeaponClassNames();
  42.     setupWeaponInfoByClass();
  43. }
  44.  
  45.  
  46. function getNumOwned(slot: int) {
  47.     //will use the slot info later to restrict count
  48.     var n : int = 0;
  49.     for (var i: int = 0; i < WeaponInfoArray.length; i++) {
  50.         if(WeaponInfoArray[i].owned && slotInfo.isWeaponAllowed(slot,WeaponInfoArray[i]))
  51.             n++;
  52.     }
  53.     return n;
  54. }
  55.  
  56. function getWeaponNamesOwned(slot : int) : String[] {
  57.     var names : String[] = new String[getNumOwned(slot)];
  58.     var n : int = 0;
  59.     for (var i: int = 0; i <  WeaponInfoArray.length; i++) {
  60.         if(WeaponInfoArray[i].owned && slotInfo.isWeaponAllowed(slot,WeaponInfoArray[i])){
  61.             names[n] = WeaponInfoArray[i].gunName;
  62.             n++;
  63.         }
  64.     }
  65.     return names;
  66. }
  67.  
  68. function getWeaponsOwned(slot : int) : WeaponInfo[] {
  69.     var w : WeaponInfo[] = new WeaponInfo[getNumOwned(slot)];
  70.     var n : int = 0;
  71.     for (var i: int = 0; i <  WeaponInfoArray.length; i++) {
  72.         if(WeaponInfoArray[i].owned && slotInfo.isWeaponAllowed(slot,WeaponInfoArray[i])){
  73.             w[n] = WeaponInfoArray[i];
  74.             n++;
  75.         }
  76.     }
  77.     return w;
  78. }
  79.  
  80.  
  81.  
  82. function Update() {
  83.     if(InputDB.GetButtonDown("Store")){
  84.         if(!storeActive && canActivate && !GunScript.takingOut && !GunScript.puttingAway){
  85.             activateStore();
  86.         }else if(storeActive) {
  87.             deActivateStore();
  88.         }
  89.     }
  90. }
  91.  
  92. function setupWeaponClassNames() {
  93.     var names : String[];
  94.     var nameArray = new Array();
  95.    
  96.     for (var w : weaponClasses in weaponClasses.GetValues(weaponClasses) ) {
  97.         nameArray.push(w.ToString().Replace("_", " " ));
  98.     }
  99.     weaponClassNames = nameArray.ToBuiltin(String);
  100. }
  101.  
  102. //Organize Weapon Information by Weapon Class for use within the GUI
  103. //Note: the code assumes the last weapon class is "Null" so the array is one element shorter than the number of
  104. //      weapon classes.
  105.  
  106. function setupWeaponInfoByClass() {
  107.  
  108.     //check to see how many Weapon Classes have one or more weapons
  109.  
  110.     var n : int = 0;
  111.     for (var i : int = 0; i< weaponClassNames.length - 1; i++) {
  112.         for (var j : int = 0; j < WeaponInfoArray.length; j++) {
  113.             if(WeaponInfoArray[j].weaponClass == i) {
  114.                 n++;
  115.                 break;
  116.             }
  117.         }
  118.     }
  119.     weaponClassNamesPopulated = new String[n];
  120.     WeaponInfoByClass = new WeaponClassArrayType[n]; // size array to hold all non-Null weapon classes with at least one weapon
  121.  
  122.         n = 0;
  123.         for(i =0; i < weaponClassNames.length - 1; i++) {
  124.         var arr = new Array(); 
  125.             for (j = 0 ; j<WeaponInfoArray.Length; j++) {
  126.                 if(WeaponInfoArray[j].weaponClass == i) {
  127.                     arr.push(WeaponInfoArray[j]);
  128.                 }
  129.             }
  130.             if(arr.length > 0) {
  131.                 WeaponInfoByClass[n] = new WeaponClassArrayType();
  132.                 WeaponInfoByClass[n].WeaponInfoArray = arr.ToBuiltin(WeaponInfo);
  133.                 WeaponInfoByClass[n].weaponClass = weaponClassNames[i];
  134.                 weaponClassNamesPopulated[n] = weaponClassNames[i];
  135.                 n++;
  136.             }
  137.     }
  138. }
  139.  
  140. function activateStore() {
  141.     PlayerWeapons.HideWeaponInstant();
  142.     Time.timeScale = 0;
  143. //  GUIController.state = GUIStates.store;
  144.     inStore = true;
  145.     //BroadcastMessage("Unlock");
  146.     storeActive = true;
  147.     Screen.lockCursor = false;
  148.     LockCursor.canLock = false;
  149.     //playerW.BroadcastMessage("DeselectWeapon"); //turn off graphics/weapons on entering store
  150.     var player = GameObject.FindWithTag("Player");
  151.     player.BroadcastMessage("Freeze", SendMessageOptions.DontRequireReceiver);
  152. }
  153.  
  154. function deActivateStore() {
  155.     if(PlayerWeapons.HasEquipped() <= 0 && !canExitWhileEmpty)
  156.         return;
  157.     storeActive = false;
  158.     inStore = false;
  159.     //GUIController.state = GUIStates.playing;
  160.     //BroadcastMessage("Lock");
  161.     Time.timeScale = 1;
  162.     Screen.lockCursor=true;
  163.     LockCursor.canLock = true;
  164.     PlayerWeapons.ShowWeapon();
  165.     //playerW.SelectWeapon(playerW.selectedWeapon); // activate graphics on selected weapon
  166.     var player = GameObject.FindWithTag("Player");
  167.     player.BroadcastMessage("UnFreeze", SendMessageOptions.DontRequireReceiver);
  168.    
  169. }
  170.  
  171. function equipWeapon(g : WeaponInfo, slot: int) {
  172.     //if the weapon is equipped in another slot, unequip it
  173.     if(slot < 0)
  174.         return;
  175.     for(var i : int = 0; i < playerW.weapons.length; i++) {
  176.         if(g.gameObject == playerW.weapons[i]) {
  177.             unEquipWeapon(i);
  178.         }
  179.     }
  180.     if(playerW.weapons[playerW.selectedWeapon] == null)
  181.         playerW.selectedWeapon = slot;
  182.     playerW.BroadcastMessage("DeselectWeapon", SendMessageOptions.DontRequireReceiver);
  183.     var tempGScript : GunScript = g.gameObject.GetComponent(GunScript).GetPrimaryGunScript();
  184.     tempGScript.ammoLeft = tempGScript.ammoPerClip;
  185.     playerW.SetWeapon(g.gameObject,slot);
  186. }
  187. function unEquipWeapon(slot: int) {
  188.     playerW.weapons[slot] = null;
  189. }
  190.  
  191. function buyUpgrade(g : WeaponInfo, u : Upgrade) {
  192.     withdraw(u.buyPrice);
  193.     u.owned = true;
  194.     g.ApplyUpgrade();
  195. }
  196.  
  197. function buyWeapon(g : WeaponInfo) {
  198.     withdraw(g.buyPrice);
  199.     g.owned = true;
  200.     g.ApplyUpgrade();
  201.     equipWeapon(g, autoEquipWeapon(g, false));
  202. }
  203.  
  204. function BuyAmmo(g : WeaponInfo) {
  205.     withdraw(g.ammoPrice);
  206.     var temp : GunScript = g.gameObject.GetComponent(GunScript).GetPrimaryGunScript();
  207.     temp.clips = Mathf.Min(temp.clips+temp.ammoPerClip, temp.maxClips);
  208.     temp.ApplyToSharedAmmo();
  209. }
  210.  
  211. function sellWeapon(g: WeaponInfo) {
  212.     if(!g.canBeSold)
  213.         return;
  214.     for(var i : int = 0; i < g.upgrades.length ; i++ ) {
  215.         g.upgrades[i].owned = false;
  216.         g.upgrades[i].RemoveUpgrade(); 
  217.     }
  218.     DropWeapon(g);
  219.     deposit(g.sellPriceUpgraded);
  220.     g.owned = false;
  221. }
  222.  
  223. function getBalance(): float {
  224.     return balance;
  225. }
  226.  
  227. //Function to deposit money - returns the new balance
  228. function deposit(amt : float) : float {
  229.         balance += amt;
  230.         return balance;
  231. }
  232.  
  233. // Function to withdraw money  - returns amount withdrawn
  234. // You can't withdraw more than the balance.
  235. function withdraw(amt : float) : float {
  236.     if (amt <= balance) {
  237.         balance -= amt;
  238.         return amt;
  239.     } else {
  240.         var oldBalance: float = balance;
  241.         balance = 0;
  242.         return oldBalance;
  243.     }
  244.        
  245. }
  246.  
  247. function autoEquipWeapon(w : WeaponInfo, auto : boolean) : int {
  248.     //Slot the weapon is equipped in, -1 if not equipped
  249.     var slot : int = -1;
  250.    
  251.     //find the first empty slot that can hold w
  252.     for (var i: int = 0; i < playerW.weapons.length; i ++) {
  253.         if(slotInfo.isWeaponAllowed(i,w) && playerW.weapons[i] ==null) {
  254.             //equipWeapon(w,i);
  255.             slot = i;
  256.         }
  257.         if(slot >= 0)
  258.             return slot;
  259.     }
  260.     if(!auto)
  261.         return slot;
  262.        
  263.     if(slotInfo.isWeaponAllowed(playerW.selectedWeapon,w)) {
  264.         //equipWeapon(w,i);
  265.         slot = playerW.selectedWeapon;
  266.         return slot;
  267.     }
  268.        
  269.     for ( i = 0; i < playerW.weapons.length; i++) {
  270.         if(slotInfo.isWeaponAllowed(i,w)) {
  271.             //equipWeapon(w,i);
  272.             slot = i;
  273.         }
  274.         if(slot >= 0)
  275.             return slot;   
  276.     }
  277.     return slot;
  278. }
  279.  
  280. function autoEquipWeaponWithReplacement(w : WeaponInfo, auto : boolean) : int {
  281.     //Slot the weapon is equipped in, -1 if not equipped
  282.     var slot : int = -1;
  283.    
  284.     //See if the weapon is already equipped and can be replaced
  285.     for (var i: int = 0; i < PlayerWeapons.PW.weapons.length; i ++) {
  286.         if(slotInfo.isWeaponAllowed(i,w) && (playerW.weapons[i] == null || (playerW.weapons[i].gameObject == w.gameObject))) {
  287.             //equipWeapon(w,i);
  288.             slot = i;
  289.         }
  290.         if(slot >= 0)
  291.             return slot;
  292.     }
  293.    
  294.     //find the first empty slot that can hold w
  295.     for (i = 0; i < playerW.weapons.length; i ++) {
  296.         if(slotInfo.isWeaponAllowed(i,w) && PlayerWeapons.PW.weapons[i] ==null) {
  297.             //equipWeapon(w,i);
  298.             slot = i;
  299.         }
  300.         if(slot >= 0)
  301.             return slot;
  302.     }
  303.     if(!auto)
  304.         return slot;
  305.        
  306.     if(slotInfo.isWeaponAllowed(PlayerWeapons.PW.selectedWeapon,w)) {
  307.         //equipWeapon(w,i);
  308.         slot = playerW.selectedWeapon;
  309.         return slot;
  310.     }
  311.     for ( i = 0; i < PlayerWeapons.PW.weapons.length; i++) {
  312.         if(slotInfo.isWeaponAllowed(i,w)) {
  313.             //equipWeapon(w,i);
  314.             slot = i;
  315.         }
  316.         if(slot >= 0)
  317.             return slot;   
  318.     }
  319.     return slot;
  320. }
  321.  
  322. //Drops weapon at given index in Weapons[]
  323. function DropWeapon (g: WeaponInfo){
  324.     //Weapon Drop
  325.     var wep : int = -1;
  326.     for(var i : int = 0; i<playerW.weapons.length;i++){
  327.         if(playerW.weapons[i].gameObject == g.gameObject){
  328.             wep = i;
  329.             break;
  330.         }
  331.     }
  332.     if(wep < 0) return;
  333.    
  334.     playerW.weapons[wep] = null;
  335.    
  336.     //Ceck if we have a weapon to switch to
  337.     var prevWeapon : int = -1;
  338.     for(i = wep-1; i >= 0; i--){
  339.         if(playerW.weapons[i] != null){
  340.             prevWeapon = i;
  341.             break;
  342.         }
  343.     }
  344.    
  345.     var nextWeapon : int = -1;
  346.     if(prevWeapon == -1){
  347.         for(i = wep+1; i < playerW.weapons.length; i++){
  348.             if(playerW.weapons[i] != null){
  349.                 nextWeapon = i;
  350.                 break;
  351.             }
  352.         }
  353.         prevWeapon = nextWeapon;
  354.        
  355.         if(nextWeapon == -1)
  356.             return;
  357.     }
  358.        
  359.     playerW.selectedWeapon = prevWeapon;
  360.     playerW.SelectWeapon(prevWeapon);
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement