/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Utility.Builders
{
using Opsive.Shared.Inventory;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Game;
using Opsive.UltimateCharacterController.Items;
using Opsive.UltimateCharacterController.Items.Actions;
using Opsive.UltimateCharacterController.Items.Actions.PerspectiveProperties;
using Opsive.UltimateCharacterController.Inventory;
using UnityEngine;
///
/// Builds a new item.
///
public class ItemBuilder
{
///
/// The type of action to create.
///
public enum ActionType {
ShootableWeapon, // The item uses a ShootableWeapon.
MeleeWeapon, // The item uses a MeleeWeapon.
Shield, // The item uses a Shield.
MagicItem, // The item uses a MagicItem.
ThrowableItem, // The item uses a ThrowableItem.
GrenadeItem, // The item uses a GrenadeItem.
Flashlight, // The item uses a Flashlight.
Nothing // The item doesn't have any actions.
}
///
/// Builds the item with the specified parameters.
///
/// The name of the item.
/// The ItemDefinition that the item uses (optional).
/// The ID of the item within the animator.
/// The character that the item should be attached to (optional).
/// The ID of the slot that the item is parented to.
/// Should the item be added to the character's default loadout?
/// Should the first person perspective be added?
/// A reference to the GameObject used in first person view.
/// A reference to the animator controller added to the first person object. Can be null.
/// A reference to the visible first person item. Can be null.
/// A reference to the ItemSlot to add the visible item to.
/// A reference to the animator controller added to the first person visible item. Can be null.
/// Should the third person perspective be added?
/// A reference to the GameObject used in third person view.
/// A reference to the ItemSlot to add the third person item to.
/// A reference to the animator controller added to the third person object. Can be null.
/// A reference to the invisible shadow caster material. This is only used for first person characters.
/// The type of item to create.
/// The ItemDefinition that the action uses (optional).
public static GameObject BuildItem(string name, ItemDefinitionBase itemDefinition, int animatorItemID, GameObject character, int slotID, bool addToDefaultLoadout, bool addFirstPersonPerspective,
GameObject firstPersonObject, RuntimeAnimatorController firstPersonObjectAnimatorController, GameObject firstPersonVisibleItem, ItemSlot firstPersonItemSlot,
RuntimeAnimatorController firstPersonVisibleItemAnimatorController, bool addThirdPersonPerspective, GameObject thirdPersonObject, ItemSlot thirdPersonItemSlot,
RuntimeAnimatorController thirdPersonObjectAnimatorController, Material invisibleShadowCasterMaterial, ActionType actionType, ItemDefinitionBase actionItemDefinition)
{
var itemGameObject = new GameObject(name);
var itemSlotID = (character == null || (firstPersonItemSlot == null && thirdPersonItemSlot == null)) ? slotID :
(firstPersonItemSlot != null ? firstPersonItemSlot.ID : thirdPersonItemSlot.ID);
// If character is null then a prefab will be created.
if (character != null) {
// The attach to object must have an ItemPlacement component.
var itemPlacement = character.GetComponentInChildren();
if (itemPlacement == null) {
Debug.LogError("Error: Unable to find the ItemPlacement component within " + character.name + ".");
return null;
}
// Organize the main item GameObject under the ItemPlacement GameObject.
itemGameObject.transform.SetParentOrigin(itemPlacement.transform);
// The item can automatically be added to the inventory's default loadout.
if (itemDefinition != null && addToDefaultLoadout) {
var inventory = character.GetComponent();
var defaultLoadout = inventory.DefaultLoadout;
if (defaultLoadout == null) {
defaultLoadout = new ItemDefinitionAmount[0];
}
var hasItemDefinition = false;
for (int i = 0; i < defaultLoadout.Length; ++i) {
// If the ItemIdentifier has already been added then a new ItemIdentifier doesn't need to be added.
if (defaultLoadout[i].ItemDefinition == itemDefinition) {
defaultLoadout[i].Amount++;
hasItemDefinition = true;
break;
}
}
if (!hasItemDefinition) {
System.Array.Resize(ref defaultLoadout, defaultLoadout.Length + 1);
defaultLoadout[defaultLoadout.Length - 1] = new ItemDefinitionAmount(itemDefinition, 1);
}
// The actionItemIdentifier should also be added.
if (actionItemDefinition != null) {
hasItemDefinition = false;
for (int i = 0; i < defaultLoadout.Length; ++i) {
// If the ItemIdentifier has already been added then a new action ItemDefinition doesn't need to be added.
if (defaultLoadout[i].ItemDefinition == actionItemDefinition) {
hasItemDefinition = true;
break;
}
}
if (!hasItemDefinition) {
System.Array.Resize(ref defaultLoadout, defaultLoadout.Length + 1);
defaultLoadout[defaultLoadout.Length - 1] = new ItemDefinitionAmount(actionItemDefinition, 100);
}
}
inventory.DefaultLoadout = defaultLoadout;
// The ItemIdentifier should be added to the ItemSetManager as well.
var itemSetManager = character.GetComponent();
if (itemSetManager != null && itemDefinition.GetItemCategory() != null) {
itemSetManager.Initialize(false);
var index = itemSetManager.CategoryToIndex(itemDefinition.GetItemCategory());
if (index > -1) {
var category = itemSetManager.CategoryItemSets[index];
hasItemDefinition = false;
for (int j = 0; j < category.ItemSetList.Count; ++j) {
if (category.ItemSetList[j].Slots[itemSlotID] == itemDefinition) {
hasItemDefinition = true;
break;
}
}
if (!hasItemDefinition) {
category.ItemSetList.Add(new ItemSet(Mathf.Max(inventory.SlotCount, itemSlotID + 1), itemSlotID, itemDefinition, null, string.Empty));
}
}
}
}
}
var item = itemGameObject.AddComponent- ();
item.ItemDefinition = itemDefinition;
item.SlotID = itemSlotID;
item.AnimatorItemID = animatorItemID;
#if FIRST_PERSON_CONTROLLER
// Add the first person object.
if (addFirstPersonPerspective) {
AddFirstPersonObject(character, name, itemGameObject, ref firstPersonObject, firstPersonObjectAnimatorController, ref firstPersonVisibleItem, firstPersonItemSlot,
firstPersonVisibleItemAnimatorController);
// If the character doesn't have an animator then the item should be equipped by a timer.
if (character != null && character.GetComponent() == null) {
item.EquipEvent.WaitForAnimationEvent = false;
}
}
#endif
// Add the third person object. The character will always have a third person object if the character has an animator.
if (addThirdPersonPerspective) {
AddThirdPersonObject(character, name, itemGameObject, ref thirdPersonObject, thirdPersonItemSlot, thirdPersonObjectAnimatorController, invisibleShadowCasterMaterial,
!addFirstPersonPerspective || firstPersonObject != null || firstPersonVisibleItem != null);
}
// Add the specified action type.
AddAction(itemGameObject, addFirstPersonPerspective, firstPersonObject, firstPersonVisibleItem, addThirdPersonPerspective, thirdPersonObject, actionType, actionItemDefinition);
return itemGameObject;
}
///
/// Creates a GameObject as the child of the parent.
///
/// The name of the GameObject.
/// The parent of the new GameObject.
/// The Trasnform of the non duplicate GameObject.
private static Transform CreateGameObject(string name, Transform parent)
{
var gameObject = new GameObject(name);
gameObject.transform.SetParentOrigin(parent);
return gameObject.transform;
}
#if FIRST_PERSON_CONTROLLER
///
/// Adds the first person object to the specified item.
///
/// The character that the first person object is being added to.
/// The name of the item.
/// A reference to the item's GameObject.
/// A reference to the GameObject used in first person view.
/// A reference to the animator controller added to the first person object. Can be null.
/// A reference to the visible first person item. Can be null.
/// A reference to the ItemSlot to add the visible item to.
/// A reference to the animator controller added to the first person visible item. Can be null.
public static void AddFirstPersonObject(GameObject character, string name, GameObject itemGameObject,
ref GameObject firstPersonObject, RuntimeAnimatorController firstPersonObjectAnimatorController, ref GameObject firstPersonVisibleItem, ItemSlot firstPersonItemSlot,
RuntimeAnimatorController firstPersonVisibleItemAnimatorController)
{
var parentFirstPersonObject = false;
if (firstPersonObject != null && (character == null || !firstPersonObject.transform.IsChildOf(character.transform))) {
parentFirstPersonObject = true;
var origFirstPersonPerspectiveItem = firstPersonVisibleItem;
var visibleItemName = string.Empty;
var visibleItemSearchName = string.Empty;
// The visible item is a child of the object. When the object is instantiated the new visible item should be found again.
// This is done by giving the visible item a unique name.
if (firstPersonVisibleItem != null) {
visibleItemName = firstPersonVisibleItem.name;
firstPersonVisibleItem.name += Random.value.ToString();
// Remember the path so the newly created visible item can be found again.
var parent = firstPersonVisibleItem.transform.parent;
visibleItemSearchName = firstPersonVisibleItem.name;
while (parent != firstPersonObject.transform && parent != null) {
visibleItemSearchName = parent.name + "/" + visibleItemSearchName;
parent = parent.parent;
}
}
firstPersonObject = GameObject.Instantiate(firstPersonObject);
if (character == null) {
firstPersonObject.name = "First Person " + name;
} else {
firstPersonObject.name = firstPersonObject.name.Substring(0, firstPersonObject.name.Length - 7); // Remove "(Clone)".
}
AddFirstPersonArms(character, firstPersonObject, firstPersonObjectAnimatorController);
// An ItemSlot must also be added to the base object if no visible item exists.
if (firstPersonVisibleItem == null) {
firstPersonObject.AddComponent();
}
// A new visible item would have been created.
if (firstPersonVisibleItem != null) {
var foundVisibleItem = firstPersonObject.transform.Find(visibleItemSearchName);
if (foundVisibleItem != null) {
// The newly created visible item is now the main visible item.
firstPersonVisibleItem = foundVisibleItem.gameObject;
} else {
// The visible item may not have been a child of the first person object GameObject.
firstPersonVisibleItem = GameObject.Instantiate(firstPersonVisibleItem);
// The ItemSlot reference also needs to be updated.
var itemSlots = firstPersonObject.GetComponentsInChildren();
for (int i = 0; i < itemSlots.Length; ++i) {
if (itemSlots[i].ID == firstPersonItemSlot.ID) {
firstPersonItemSlot = itemSlots[i];
break;
}
}
firstPersonVisibleItem.transform.SetParentOrigin(firstPersonItemSlot.transform);
}
origFirstPersonPerspectiveItem.name = firstPersonVisibleItem.name = visibleItemName;
}
} else if (firstPersonVisibleItem != null) {
firstPersonVisibleItem = GameObject.Instantiate(firstPersonVisibleItem);
firstPersonVisibleItem.name = (character == null ? "First Person " : "") + name;
}
var perspectiveItem = itemGameObject.AddComponent();
perspectiveItem.Object = firstPersonObject;
perspectiveItem.VisibleItem = firstPersonVisibleItem;
if (firstPersonVisibleItem != null) {
if (firstPersonVisibleItem.GetComponent() == null) {
var audioSource = firstPersonVisibleItem.AddComponent();
audioSource.playOnAwake = false;
audioSource.spatialBlend = 1;
audioSource.maxDistance = 20;
}
}
// The visible item can use an animator.
if (firstPersonVisibleItemAnimatorController != null && firstPersonVisibleItem != null) {
Animator animator;
if ((animator = firstPersonVisibleItem.GetComponent()) == null) {
animator = firstPersonVisibleItem.AddComponent();
}
animator.applyRootMotion = false;
animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
animator.runtimeAnimatorController = firstPersonVisibleItemAnimatorController;
if (firstPersonVisibleItem.GetComponent() == null) {
firstPersonVisibleItem.AddComponent();
}
}
Transform parentTransform = null;
if (character != null) {
// The object should be a child of the First Person Objects GameObject.
if (firstPersonObject != null && parentFirstPersonObject) {
var firstPersonObjects = character.GetComponentInChildren();
if (firstPersonObjects == null) {
Debug.LogError("Error: Unable to find the FirstPersonObjects component within " + character.name + ".");
return;
} else {
parentTransform = firstPersonObjects.transform;
}
} else if (firstPersonVisibleItem != null) {
parentTransform = firstPersonItemSlot.transform;
}
} else {
// The object should be a child of the item GameObject.
parentTransform = itemGameObject.transform;
}
// Assign the transform. The object will contain the visible item if it exists.
var obj = firstPersonObject && parentFirstPersonObject ? firstPersonObject : firstPersonVisibleItem;
if (obj != null) {
obj.transform.SetParentOrigin(parentTransform);
// The item's object should be on the first person overlay layer so it'll render over all other objects.
obj.transform.SetLayerRecursively(LayerManager.Overlay);
} else if (firstPersonVisibleItem != null) {
firstPersonVisibleItem.transform.SetLayerRecursively(LayerManager.Overlay);
}
// Add any properties for actions which have already been added.
AddPropertiesToActions(itemGameObject, firstPersonObject, firstPersonVisibleItem, null);
}
///
/// Adds the FirstPersonBaseObject to the arms.
///
/// The character that contains the FirstPersonObject.
/// A reference to the GameObject used in first person view.
/// A reference to the animator controller added to the first person object. Can be null.
public static void AddFirstPersonArms(GameObject character, GameObject firstPersonObject, RuntimeAnimatorController firstPersonObjectAnimatorController)
{
var maxID = -1;
if (character != null && firstPersonObject.GetComponent() == null) {
// The base object ID must be unique.
var baseObjects = character.GetComponentsInChildren();
for (int i = 0; i < baseObjects.Length; ++i) {
if (baseObjects[i].ID > maxID) {
maxID = baseObjects[i].ID;
}
}
}
var baseObject = firstPersonObject.AddComponent();
baseObject.ID = maxID + 1;
firstPersonObject.transform.SetLayerRecursively(LayerManager.Overlay);
if (firstPersonObjectAnimatorController != null) {
Animator animator;
if ((animator = firstPersonObject.GetComponent()) == null) {
animator = firstPersonObject.AddComponent();
}
animator.applyRootMotion = false;
animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
animator.runtimeAnimatorController = firstPersonObjectAnimatorController;
if (firstPersonObject.GetComponent() == null) {
firstPersonObject.AddComponent();
}
}
}
///
/// Removes the third person item.
///
/// The item to remove.
public static void RemoveFirstPersonObject(FirstPersonController.Items.FirstPersonPerspectiveItem firstPersonPerspectiveItem)
{
// Remove any properties which use the first person object.
var itemProperties = firstPersonPerspectiveItem.GetComponents();
for (int i = itemProperties.Length - 1; i > -1; --i) {
Object.DestroyImmediate(itemProperties[i], true);
}
if (firstPersonPerspectiveItem.VisibleItem != null) {
Object.DestroyImmediate(firstPersonPerspectiveItem.VisibleItem, true);
}
Object.DestroyImmediate(firstPersonPerspectiveItem, true);
}
#endif
///
/// Adds the third person object to the specified item.
///
/// The character that the third person object is being added to.
/// The name of the item.
/// A reference to the item's GameObject.
/// A reference to the GameObject used in third person view.
/// A reference to the ItemSlot to add the third person item to.
/// A reference to the animator controller added to the third person object. Can be null.
/// A reference to the invisible shadow caster material. This is only used for first person characters.
/// Should the ThirdPersonObject component be added to the object?
public static void AddThirdPersonObject(GameObject character, string name, GameObject itemGameObject, ref GameObject thirdPersonObject, ItemSlot thirdPersonItemSlot,
RuntimeAnimatorController thirdPersonObjectAnimatorController, Material invisibleShadowCasterMaterial, bool defaultAddThirdPersonObject)
{
var visibleItem = itemGameObject.AddComponent();
if (thirdPersonObject != null) {
thirdPersonObject = GameObject.Instantiate(thirdPersonObject);
thirdPersonObject.name = (character == null ? "Third Person " : "") + name;
visibleItem.Object = thirdPersonObject;
var addThirdPersonObject = defaultAddThirdPersonObject;
#if THIRD_PERSON_CONTROLLER
if (character != null && !addThirdPersonObject) {
var characterLocomotion = character.GetComponent();
var movementTypes = characterLocomotion.GetSerializedMovementTypes();
if (movementTypes != null) {
for (int i = 0; i < movementTypes.Length; ++i) {
if (characterLocomotion.MovementTypes[i].GetType().FullName.Contains("ThirdPerson")) {
addThirdPersonObject = true;
break;
}
}
}
#if ULTIMATE_CHARACTER_CONTROLLER_MULTIPLAYER
var networkInfo = character.GetComponent();
if (networkInfo != null) {
addThirdPersonObject = true;
}
#endif
}
#else
addThirdPersonObject = false;
#endif
if (addThirdPersonObject) {
// The ThirdPersonObject component is added so the PerspectiveMonitor knows what objects should use the invisible shadow caster material.
thirdPersonObject.AddComponent();
} else {
// If the ThirdPersonObject isn't added then the renderer should be directly attached.
var renderers = thirdPersonObject.GetComponentsInChildren();
for (int i = 0; i < renderers.Length; ++i) {
var materials = renderers[i].sharedMaterials;
for (int j = 0; j < materials.Length; ++j) {
materials[j] = invisibleShadowCasterMaterial;
}
renderers[i].sharedMaterials = materials;
}
}
if (thirdPersonObject.GetComponent() == null) {
var audioSource = thirdPersonObject.AddComponent();
audioSource.playOnAwake = false;
audioSource.spatialBlend = 1;
audioSource.maxDistance = 20;
}
// Optionally add the animator.
if (thirdPersonObjectAnimatorController != null) {
Animator animator;
if ((animator = thirdPersonObject.GetComponent()) == null) {
animator = thirdPersonObject.AddComponent();
}
animator.applyRootMotion = false;
animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
animator.runtimeAnimatorController = thirdPersonObjectAnimatorController;
if (thirdPersonObject.GetComponent() == null) {
thirdPersonObject.AddComponent();
}
}
Transform parentTransform = null;
if (character != null) {
parentTransform = thirdPersonItemSlot.transform;
} else {
// The object should be a child of the item GameObject.
parentTransform = itemGameObject.transform;
}
// Assign the transform position and layer.
thirdPersonObject.transform.SetParentOrigin(parentTransform);
thirdPersonObject.transform.SetLayerRecursively(LayerManager.SubCharacter);
}
// Add any properties for actions which have already been added.
AddPropertiesToActions(itemGameObject, null, null, thirdPersonObject);
}
///
/// Adds the properties to any actions already created.
///
/// A reference to the item's GameObject.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
private static void AddPropertiesToActions(GameObject itemGameObject, GameObject firstPersonObject, GameObject firstPersonVisibleItem, GameObject thirdPersonObject)
{
var actions = itemGameObject.GetComponents();
for (int i = 0; i < actions.Length; ++i) {
#if ULTIMATE_CHARACTER_CONTROLLER_SHOOTER
if (actions[i] is ShootableWeapon) {
AddShootableWeaponProperties(itemGameObject, actions[i].ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
continue;
}
#endif
#if ULTIMATE_CHARACTER_CONTROLLER_MELEE
if (actions[i] is MeleeWeapon) {
AddMeleeWeaponProperties(itemGameObject, actions[i].ID, (firstPersonObject != null || firstPersonVisibleItem != null), firstPersonVisibleItem, thirdPersonObject != null, thirdPersonObject);
continue;
}
#endif
if (actions[i] is GrenadeItem) {
AddGrenadeItemProperties(itemGameObject, actions[i].ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
continue;
}
if (actions[i] is ThrowableItem) {
AddThrowableItemProperties(itemGameObject, actions[i].ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
continue;
}
if (actions[i] is Flashlight) {
AddFlashlightProperties(itemGameObject, actions[i].ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
continue;
}
}
}
///
/// Removes the third person item.
///
/// The item to remove.
public static void RemoveThirdPersonObject(ThirdPersonController.Items.ThirdPersonPerspectiveItem thirdPersonVisibleItem)
{
// Remove any properties which use the third person object.
var itemProperties = thirdPersonVisibleItem.GetComponents();
for (int i = itemProperties.Length - 1; i > -1; --i) {
Object.DestroyImmediate(itemProperties[i], true);
}
Object.DestroyImmediate(thirdPersonVisibleItem.Object, true);
Object.DestroyImmediate(thirdPersonVisibleItem, true);
}
///
/// Adds the specified ActionType to the item.
///
/// The GameObject to add the action to.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
/// The type of action to add.
/// The ItemDefinition that the action uses (optional).
public static void AddAction(GameObject itemGameObject, bool addFirstPersonPerspective, GameObject firstPersonObject, GameObject firstPersonVisibleItem,
bool addThirdPersonPerspective, GameObject thirdPersonObject, ActionType actionType, ItemDefinitionBase actionItemDefinition)
{
// The action ID must be unique.
var maxID = -1;
var actions = itemGameObject.GetComponents();
for (int i = 0; i < actions.Length; ++i) {
if (actions[i].ID > maxID) {
maxID = actions[i].ID;
}
}
switch (actionType) {
#if ULTIMATE_CHARACTER_CONTROLLER_SHOOTER
case ActionType.ShootableWeapon:
var shootableWeapon = itemGameObject.AddComponent();
shootableWeapon.ID = maxID + 1;
shootableWeapon.ConsumableItemDefinition = actionItemDefinition;
AddShootableWeaponProperties(itemGameObject, shootableWeapon.ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
break;
#endif
#if ULTIMATE_CHARACTER_CONTROLLER_MELEE
case ActionType.MeleeWeapon:
var meleeWeapon = itemGameObject.AddComponent();
meleeWeapon.ID = maxID + 1;
meleeWeapon.FaceTarget = false;
AddMeleeWeaponProperties(itemGameObject, meleeWeapon.ID, addFirstPersonPerspective, firstPersonVisibleItem, addThirdPersonPerspective, thirdPersonObject);
break;
case ActionType.Shield:
var shield = itemGameObject.AddComponent();
shield.ID = maxID + 1;
var shieldAttributeManager = itemGameObject.AddComponent();
shieldAttributeManager.Attributes[0].Name = "Durability"; // Rename the Health attribute to Durability.
AddShieldProperties(shield, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
// The Block ability should be added if it isn't already.
var characterLocomotion = itemGameObject.GetComponentInParent();
if (characterLocomotion != null) {
var blockAbility = characterLocomotion.GetAbility();
if (blockAbility == null) {
AbilityBuilder.AddAbility(characterLocomotion, typeof(Character.Abilities.Items.Block));
}
}
break;
#endif
case ActionType.MagicItem:
var magicItem = itemGameObject.AddComponent();
var item = itemGameObject.GetComponent
- ();
item.EquipEvent = new AnimationEventTrigger(false, 0);
item.UnequipEvent = new AnimationEventTrigger(false, 0);
AddMagicItemProperties(itemGameObject, magicItem.ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
break;
case ActionType.ThrowableItem:
var throwableItem = itemGameObject.AddComponent();
throwableItem.ID = maxID + 1;
throwableItem.CanEquipEmptyItem = false;
AddThrowableItemProperties(itemGameObject, throwableItem.ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
break;
case ActionType.GrenadeItem:
var grenadeItem = itemGameObject.AddComponent();
grenadeItem.ID = maxID + 1;
grenadeItem.CanEquipEmptyItem = false;
AddGrenadeItemProperties(itemGameObject, grenadeItem.ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
break;
case ActionType.Flashlight:
var flashLight = itemGameObject.AddComponent();
flashLight.ID = maxID + 1;
flashLight.UseEvent = new AnimationEventTrigger(false, 0);
flashLight.UseCompleteEvent = new AnimationEventTrigger(false, 0);
var flashlightAttributeManager = itemGameObject.AddComponent();
flashlightAttributeManager.Attributes[0].Name = "Battery"; // Rename the Health attribute to Battery.
AddFlashlightProperties(itemGameObject, flashLight.ID, firstPersonObject, firstPersonVisibleItem, thirdPersonObject);
break;
}
}
#if ULTIMATE_CHARACTER_CONTROLLER_SHOOTER
///
/// Adds the ShootableWeaponProperties to the specified GameObject.
///
/// The GameObject to add the properties to.
/// The ActionID of the properties.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
private static void AddShootableWeaponProperties(GameObject itemGameObject, int actionID, GameObject firstPersonObject, GameObject firstPersonVisibleItem, GameObject thirdPersonObject)
{
#if FIRST_PERSON_SHOOTER
if (firstPersonObject != null || firstPersonVisibleItem != null) {
var parent = firstPersonVisibleItem != null ? firstPersonVisibleItem.transform : firstPersonObject.transform;
var shootableProperties = itemGameObject.AddComponent();
// Setup the standard references.
shootableProperties.ActionID = actionID;
shootableProperties.FirePointLocation = CreateGameObject("Fire Point", parent);
shootableProperties.MuzzleFlashLocation = CreateGameObject("Muzzle Flash", parent);
shootableProperties.ShellLocation = CreateGameObject("Shell Eject Point", parent);
}
#endif
if (thirdPersonObject != null) {
var shootableProperties = itemGameObject.AddComponent();
// Setup the standard references.
shootableProperties.ActionID = actionID;
shootableProperties.FirePointLocation = CreateGameObject("Fire Point", thirdPersonObject.transform);
shootableProperties.MuzzleFlashLocation = CreateGameObject("Muzzle Flash", thirdPersonObject.transform);
shootableProperties.ShellLocation = CreateGameObject("Shell Eject Point", thirdPersonObject.transform);
}
}
#endif
#if ULTIMATE_CHARACTER_CONTROLLER_MELEE
///
/// Adds the MeleeWeaponProperties to the specified GameObject.
///
/// The GameObject to add the properties to.
/// The ActionID of the properties.
/// Should the first person perspective be added?
/// A reference to the visible first person item.
/// Should the third person perspective be added?
/// A reference to the GameObject used in third person view.
private static void AddMeleeWeaponProperties(GameObject itemGameObject, int actionID, bool addFirstPersonPerspective, GameObject firstPersonVisibleItem,
bool addThirdPersonPerspective, GameObject thirdPersonObject)
{
#if FIRST_PERSON_MELEE
if (itemGameObject) {
var meleeWeaponProperties = itemGameObject.AddComponent();
meleeWeaponProperties.ActionID = actionID;
if (firstPersonVisibleItem != null) {
BoxCollider boxCollider;
if ((boxCollider = firstPersonVisibleItem.GetComponent()) == null) {
boxCollider = firstPersonVisibleItem.AddComponent();
}
meleeWeaponProperties.Hitboxes = new MeleeWeapon.MeleeHitbox[] { new MeleeWeapon.MeleeHitbox(boxCollider) };
}
}
#endif
if (addThirdPersonPerspective) {
var meleeWeaponProperties = itemGameObject.AddComponent();
meleeWeaponProperties.ActionID = actionID;
if (thirdPersonObject != null) {
BoxCollider boxCollider;
if ((boxCollider = thirdPersonObject.GetComponent()) == null) {
boxCollider = thirdPersonObject.AddComponent();
}
meleeWeaponProperties.Hitboxes = new MeleeWeapon.MeleeHitbox[] { new MeleeWeapon.MeleeHitbox(boxCollider) };
}
}
}
///
/// Adds the shield properties to the specified GameObject.
///
/// A reference to the parent Shield component.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
private static void AddShieldProperties(Shield shield, GameObject firstPersonObject, GameObject firstPersonVisibleItem, GameObject thirdPersonObject)
{
#if FIRST_PERSON_CONTROLLER
if (firstPersonObject != null || firstPersonVisibleItem != null) {
var shieldCollider = firstPersonVisibleItem.AddComponent();
shieldCollider.Shield = shield;
shieldCollider.FirstPersonPerspective = true;
if (firstPersonVisibleItem.GetComponent() == null) {
firstPersonVisibleItem.AddComponent();
}
}
#endif
if (thirdPersonObject != null) {
var shieldCollider = thirdPersonObject.AddComponent();
shieldCollider.Shield = shield;
if (thirdPersonObject.GetComponent() == null) {
thirdPersonObject.AddComponent();
}
}
}
#endif
///
/// Adds the MagicItemProperties to the specified GameObject.
///
/// The GameObject to add the properties to.
/// The ActionID of the properties.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
private static void AddMagicItemProperties(GameObject itemGameObject, int actionID, GameObject firstPersonObject, GameObject firstPersonVisibleItem, GameObject thirdPersonObject)
{
#if FIRST_PERSON_CONTROLLER
if (firstPersonObject != null || firstPersonVisibleItem != null) {
var parent = firstPersonVisibleItem != null ? firstPersonVisibleItem.transform : firstPersonObject.transform;
var magicItemProperties = itemGameObject.AddComponent();
// Setup the standard references.
magicItemProperties.ActionID = actionID;
magicItemProperties.OriginLocation = CreateGameObject("Origin", parent);
}
#endif
var character = itemGameObject.GetComponentInParent();
if (thirdPersonObject != null || (character != null && character.GetComponent() != null)) {
var magicItemProperties = itemGameObject.AddComponent();
// Setup the standard references.
magicItemProperties.ActionID = actionID;
magicItemProperties.OriginLocation = CreateGameObject("Origin", thirdPersonObject.transform);
}
}
///
/// Adds the ThrowableItemProperties to the specified GameObject.
///
/// The GameObject to add the properties to.
/// The ActionID of the properties.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
private static void AddThrowableItemProperties(GameObject itemGameObject, int actionID, GameObject firstPersonObject, GameObject firstPersonVisibleItem, GameObject thirdPersonObject)
{
#if FIRST_PERSON_CONTROLLER
if (firstPersonObject != null || firstPersonVisibleItem != null) {
var throwableProperties = itemGameObject.AddComponent();
// Setup the standard references.
throwableProperties.ActionID = actionID;
throwableProperties.ThrowLocation = throwableProperties.TrajectoryLocation = (firstPersonVisibleItem != null ? firstPersonVisibleItem : firstPersonObject).transform;
}
#endif
if (thirdPersonObject != null) {
var throwableProperties = itemGameObject.AddComponent();
// Setup the standard references.
throwableProperties.ActionID = actionID;
throwableProperties.ThrowLocation = throwableProperties.TrajectoryLocation = thirdPersonObject.transform;
}
// Throwable items should be completely dropped.
var item = itemGameObject.GetComponent
- ();
item.FullInventoryDrop = true;
}
///
/// Adds the GrenadeItemProperties to the specified GameObject.
///
/// The GameObject to add the properties to.
/// The ActionID of the properties.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
private static void AddGrenadeItemProperties(GameObject itemGameObject, int actionID, GameObject firstPersonObject, GameObject firstPersonVisibleItem, GameObject thirdPersonObject)
{
#if FIRST_PERSON_CONTROLLER
if (firstPersonObject != null || firstPersonVisibleItem != null) {
var grenadeProperties = itemGameObject.AddComponent();
// Setup the standard references.
grenadeProperties.ActionID = actionID;
grenadeProperties.ThrowLocation = grenadeProperties.TrajectoryLocation = (firstPersonVisibleItem != null ? firstPersonVisibleItem : firstPersonObject).transform;
// The Grenade component should not exist on the first person visible item.
if (firstPersonVisibleItem != null && firstPersonVisibleItem.GetComponent() != null) {
GameObject.DestroyImmediate(firstPersonVisibleItem.GetComponent(), true);
// If the grenade component exists then a collider does as well.
if (firstPersonVisibleItem.GetComponent() != null) {
GameObject.DestroyImmediate(firstPersonVisibleItem.GetComponent(), true);
}
}
}
#endif
if (thirdPersonObject != null) {
var grenadeProperties = itemGameObject.AddComponent();
// Setup the standard references.
grenadeProperties.ActionID = actionID;
grenadeProperties.ThrowLocation = grenadeProperties.TrajectoryLocation = thirdPersonObject.transform;
// The Grenade component should not exist on the third person object.
if (thirdPersonObject.GetComponent() != null) {
GameObject.DestroyImmediate(thirdPersonObject.GetComponent(), true);
// If the grenade component exists then a collider does as well.
if (thirdPersonObject.GetComponent() != null) {
GameObject.DestroyImmediate(thirdPersonObject.GetComponent(), true);
}
}
}
// Grenades should be completely dropped.
var item = itemGameObject.GetComponent
- ();
item.FullInventoryDrop = true;
}
///
/// Adds the FlashlightProperties to the specified GameObject.
///
/// The GameObject to add the properties to.
/// The ActionID of the properties.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
private static void AddFlashlightProperties(GameObject itemGameObject, int actionID, GameObject firstPersonObject, GameObject firstPersonVisibleItem, GameObject thirdPersonObject)
{
#if FIRST_PERSON_CONTROLLER
if (firstPersonObject != null || firstPersonVisibleItem != null) {
var flashlight = itemGameObject.AddComponent();
// Setup the standard references.
flashlight.ActionID = actionID;
var lightGameObject = new GameObject("Light", typeof(Light));
lightGameObject.transform.SetParentOrigin((firstPersonVisibleItem != null ? firstPersonVisibleItem : firstPersonObject).transform);
flashlight.Light = lightGameObject;
}
#endif
if (thirdPersonObject != null) {
var flashlight = itemGameObject.AddComponent();
// Setup the standard references.
flashlight.ActionID = actionID;
var lightGameObject = new GameObject("Light", typeof(Light));
lightGameObject.transform.SetParentOrigin(thirdPersonObject.transform);
flashlight.Light = lightGameObject;
}
}
///
/// Adds the specified ActionType to the item.
///
/// The GameObject to add the action to.
/// The type of action to add.
/// The ItemDefinition that the action uses (optional).
public static void AddAction(GameObject itemGameObject, ActionType actionType, ItemDefinitionBase actionItemDefinition)
{
GameObject firstPersonObject = null, firstPersonVisibleItemGameObject = null, thirdPersonObject = null;
PopulatePerspectiveObjects(itemGameObject, ref firstPersonObject, ref firstPersonVisibleItemGameObject, ref thirdPersonObject);
AddAction(itemGameObject, (firstPersonObject != null || firstPersonVisibleItemGameObject != null), firstPersonObject, firstPersonVisibleItemGameObject, thirdPersonObject != null, thirdPersonObject, actionType, actionItemDefinition);
}
///
/// Populates the first and third person objects for the specified item GameObject.
///
/// The GameObject to get the first and third person references of.
/// A reference to the GameObject used in first person view.
/// A reference to the visible first person item.
/// A reference to the GameObject used in third person view.
private static void PopulatePerspectiveObjects(GameObject itemGameObject, ref GameObject firstPersonObject, ref GameObject firstPersonVisibleItemGameObject, ref GameObject thirdPersonObject)
{
#if FIRST_PERSON_CONTROLLER
var firstPersonVisibleItem = itemGameObject.GetComponent();
if (firstPersonVisibleItem != null) {
firstPersonObject = firstPersonVisibleItem.Object;
firstPersonVisibleItemGameObject = firstPersonVisibleItem.VisibleItem;
}
#endif
var thirdPersonVisibleItem = itemGameObject.GetComponent();
if (thirdPersonVisibleItem != null) {
thirdPersonObject = thirdPersonVisibleItem.Object;
}
}
///
/// Removes the specified action.
///
/// The action to remove.
public static void RemoveAction(ItemAction itemAction)
{
// Remove the matching perspective properties first so the ID can be matched.
RemovePerspectiveProperties(itemAction.gameObject, itemAction.ID);
Object.DestroyImmediate(itemAction, true);
}
///
/// Removes the perspective properties on the item with the specified ID.
///
/// The GameObject which has the ItemPerpsectiveProperties.
private static void RemovePerspectiveProperties(GameObject itemGameObject, int actionID)
{
var perspectiveProperties = itemGameObject.GetComponents();
for (int i = perspectiveProperties.Length - 1; i > -1; --i) {
if (perspectiveProperties[i].ActionID != actionID) {
continue;
}
#if ULTIMATE_CHARACTER_CONTROLLER_SHOOTER
var shootableWeaponPerspectiveProperties = perspectiveProperties[i] as IShootableWeaponPerspectiveProperties;
if (shootableWeaponPerspectiveProperties != null) {
var propertyTransform = shootableWeaponPerspectiveProperties.FirePointLocation;
if (propertyTransform != null) {
Object.DestroyImmediate(propertyTransform.gameObject, true);
}
propertyTransform = shootableWeaponPerspectiveProperties.MuzzleFlashLocation;
if (propertyTransform != null) {
Object.DestroyImmediate(propertyTransform.gameObject, true);
}
propertyTransform = shootableWeaponPerspectiveProperties.ShellLocation;
if (propertyTransform != null) {
Object.DestroyImmediate(propertyTransform.gameObject, true);
}
propertyTransform = shootableWeaponPerspectiveProperties.SmokeLocation;
if (propertyTransform != null) {
Object.DestroyImmediate(propertyTransform.gameObject, true);
}
}
#endif
Object.DestroyImmediate(perspectiveProperties[i], true);
}
}
}
}