Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using CryEngine;
- using System.Collections.Generic;
- namespace CryEngine.Game
- {
- [EntityComponent(Guid="0bbe85a3-8e95-594e-3782-31797a4a23be")]
- public class WeaponSwitching : EntityComponent
- {
- /// <summary>
- /// Called at the start of the game.
- /// </summary>
- ///
- [EntityProperty]
- public string Gun1 { get; set; } = "Gun1";
- [EntityProperty]
- public string Gun2 { get; set; } = "Gun2";
- [EntityProperty]
- public string Gun3 { get; set; } = "Gun3";
- [EntityProperty]
- public string Gun4 { get; set; } = "Gun4";
- // public string[] WeaponNames { get; set; }
- public List<Entity> Weapons = new List<Entity>();
- public List<int> Inventory = new List<int>();
- public int InventoryLimit = 0;
- public int selectedWeapon;
- [EntityProperty]
- public int MaxLimit { get; set; } = 2;
- protected override void OnGameplayStart()
- {
- selectedWeapon = 0;
- AddWeapon(0);
- }
- protected override void OnUpdate(float frameTime)
- {
- if (Input.KeyDown(KeyId.Z))
- {
- if(Inventory.Count > 1)
- {
- SwitchWeapon();
- }
- }
- }
- // here we find all the weapons by name
- // and set there entitys to your WeaponList
- public void ConstructWeaponList()
- {
- List<string> WeaponNames = new List<string>();
- WeaponNames.Add(Gun1);
- WeaponNames.Add(Gun2);
- WeaponNames.Add(Gun3);
- WeaponNames.Add(Gun4);
- for (int i = 0; i < WeaponNames.Count; i++)
- {
- Weapons.Add(Entity.Find(WeaponNames[i]));
- }
- }
- // switches the weapons
- public void SwitchWeapon()
- {
- selectedWeapon += 1;
- if(selectedWeapon > Inventory.Count - 1)
- {
- selectedWeapon = 0;
- }
- SelectWeapon(Inventory[selectedWeapon]);
- }
- // this adds an new weapon based on
- // the Weapons index
- public void AddWeapon(int index)
- {
- // if we already have the weapon fill the amo
- if(Inventory.Contains(index))
- {
- // get the entity that was got and fill ammo
- for (int i = 0; i < Inventory.Count; i++)
- {
- if (Inventory[i] == index)
- {
- selectedWeapon = i;
- Weapons[i].GetComponent<Gun>().FillAmmo();
- }
- }
- SelectWeapon(index);
- return;
- }
- if (InventoryLimit <= MaxLimit)
- {
- Inventory.Add(index);
- SelectWeapon(index);
- selectedWeapon = InventoryLimit;
- InventoryLimit += 1;
- }
- else
- {
- for (int i = 0; i < Inventory.Count; i++)
- {
- if (i == Inventory[selectedWeapon]){
- Inventory.Remove(i);
- break;
- }
- }
- Inventory.Add(index);
- SelectWeapon(index);
- selectedWeapon = 0;
- }
- }
- void SelectWeapon(int index)
- {
- foreach(Entity weapon in Weapons)
- {
- // enable selected weapon
- if (weapon.GetComponent<Gun>() != null)
- {
- weapon.GetComponent<Gun>().GunEnabled = false;
- weapon.Hidden = true;
- if (weapon == Weapons[index])
- {
- weapon.GetComponent<Gun>().GunEnabled = true;
- weapon.Hidden = false;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement