Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using System;
  2. using CryEngine;
  3. using System.Collections.Generic;
  4.  
  5. namespace CryEngine.Game
  6. {
  7.     [EntityComponent(Guid="0bbe85a3-8e95-594e-3782-31797a4a23be")]
  8.     public class WeaponSwitching : EntityComponent
  9.     {
  10.         /// <summary>
  11.         /// Called at the start of the game.
  12.         /// </summary>
  13.         ///
  14.  
  15.  
  16.         [EntityProperty]
  17.         public string Gun1 { get; set; } = "Gun1";
  18.         [EntityProperty]
  19.         public string Gun2 { get; set; } = "Gun2";
  20.         [EntityProperty]
  21.         public string Gun3 { get; set; } = "Gun3";
  22.         [EntityProperty]
  23.         public string Gun4 { get; set; } = "Gun4";
  24.  
  25.  
  26.         //  public string[] WeaponNames { get; set; }
  27.  
  28.         public List<Entity> Weapons = new List<Entity>();
  29.         public List<int> Inventory = new List<int>();
  30.         public int InventoryLimit = 0;
  31.         public int selectedWeapon;
  32.  
  33.         [EntityProperty]
  34.         public int MaxLimit { get; set; } = 2;
  35.  
  36.  
  37.         protected override void OnGameplayStart()
  38.         {
  39.             selectedWeapon = 0;
  40.             AddWeapon(0);
  41.         }
  42.  
  43.         protected override void OnUpdate(float frameTime)
  44.         {
  45.  
  46.             if (Input.KeyDown(KeyId.Z))
  47.             {
  48.                 if(Inventory.Count > 1)
  49.                 {
  50.                     SwitchWeapon();
  51.                 }
  52.             }
  53.         }
  54.  
  55.         // here we find all the weapons by name
  56.         // and set there entitys to your WeaponList
  57.         public void ConstructWeaponList()
  58.         {
  59.  
  60.             List<string> WeaponNames = new List<string>();
  61.  
  62.             WeaponNames.Add(Gun1);
  63.             WeaponNames.Add(Gun2);
  64.             WeaponNames.Add(Gun3);
  65.             WeaponNames.Add(Gun4);
  66.  
  67.             for (int i = 0; i < WeaponNames.Count; i++)
  68.             {
  69.                 Weapons.Add(Entity.Find(WeaponNames[i]));
  70.             }
  71.         }
  72.  
  73.         // switches the weapons
  74.         public void SwitchWeapon()
  75.         {
  76.             selectedWeapon += 1;
  77.             if(selectedWeapon > Inventory.Count - 1)
  78.             {
  79.                 selectedWeapon = 0;
  80.             }
  81.             SelectWeapon(Inventory[selectedWeapon]);
  82.         }
  83.  
  84.  
  85.         // this adds an new weapon based on
  86.         // the Weapons index
  87.         public void AddWeapon(int index)
  88.         {
  89.  
  90.             // if we already have the weapon fill the amo
  91.             if(Inventory.Contains(index))
  92.             {
  93.                 // get the entity that was got and fill ammo
  94.                 for (int i = 0; i < Inventory.Count; i++)
  95.                 {
  96.                     if (Inventory[i] == index)
  97.                     {
  98.                  
  99.                         selectedWeapon = i;
  100.                         Weapons[i].GetComponent<Gun>().FillAmmo();
  101.                     }
  102.  
  103.                 }
  104.  
  105.                 SelectWeapon(index);
  106.  
  107.                 return;
  108.             }
  109.  
  110.             if (InventoryLimit <= MaxLimit)
  111.             {
  112.                 Inventory.Add(index);
  113.                 SelectWeapon(index);
  114.                 selectedWeapon = InventoryLimit;
  115.                 InventoryLimit += 1;
  116.             }
  117.             else
  118.             {
  119.                 for (int i = 0; i < Inventory.Count; i++)
  120.                 {
  121.                     if (i == Inventory[selectedWeapon]){
  122.  
  123.                         Inventory.Remove(i);
  124.                         break;
  125.                     }
  126.                 }
  127.  
  128.                 Inventory.Add(index);
  129.                 SelectWeapon(index);
  130.                 selectedWeapon = 0;
  131.             }
  132.  
  133.  
  134.         }
  135.  
  136.         void SelectWeapon(int index)
  137.         {
  138.             foreach(Entity weapon in Weapons)
  139.             {
  140.                 // enable selected weapon
  141.                 if (weapon.GetComponent<Gun>() != null)
  142.                 {
  143.                     weapon.GetComponent<Gun>().GunEnabled = false;
  144.                     weapon.Hidden = true;
  145.                     if (weapon == Weapons[index])
  146.                     {
  147.                         weapon.GetComponent<Gun>().GunEnabled = true;
  148.                         weapon.Hidden = false;
  149.                     }
  150.                 }
  151.  
  152.             }
  153.         }
  154.  
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement