void TryUseItem(UsableItem itemData) { if (itemData.keepUsingWhileButtonDown || Input.GetMouseButtonDown(0)) { if (IsHandsOrItemWithValidDurability(selection)) { Vector3 lookAt = look.lookPositionRaycasted; Usability usability = itemData.CanUseHotbar(player, selection, lookAt); if (usability == Usability.Usable) { CmdUseItem(selection, lookAt); if (player.isNonHostLocalPlayer) { OnUsedItem(itemData, lookAt); } } else if (usability == Usability.Empty) { if (Input.GetMouseButtonDown(0)) { if (itemData.emptySound) { audioSource.PlayOneShot(itemData.emptySound); } } } } } }
public override Usability CanUseInventory(Player player, int inventoryIndex) { Usability baseUsable = base.CanUseInventory(player, inventoryIndex); if (baseUsable != Usability.Usable) { return(baseUsable); } return(FindEquipableSlotFor(player.equipment, inventoryIndex) != -1 ? Usability.Usable : Usability.Never); }
public override Usability CanUseHotbar(Player player, int hotbarIndex, Vector3 lookAt) { Usability baseUsable = base.CanUseHotbar(player, hotbarIndex, lookAt); if (baseUsable != Usability.Usable) { return(baseUsable); } return(player.reloading.ReloadTimeRemaining() > 0 ? Usability.Cooldown : Usability.Usable); }
// TODO this is weird to pass slotindex too. needed because hands option though. void TryUseItem(UsableItem itemData, int slotIndex) { // note: no .amount > 0 check because it's either an item or hands // use current item or hands // repeated or one time use while holding mouse down? if (itemData.keepUsingWhileButtonDown || Input.GetMouseButtonDown(0)) { // get the exact look position on whatever object we aim at Vector3 lookAt = look.lookPositionRaycasted; // use it Usability usability = itemData.CanUse(this, slotIndex, lookAt); if (usability == Usability.Usable) { // attack by using the weapon item //Debug.DrawLine(Camera.main.transform.position, lookAt, Color.gray, 1); UseItem(slotIndex, lookAt); // simulate OnUsed locally without waiting for the Rpc to avoid // latency effects: // - usedEndTime would be synced too slowly, hence fire interval // would be too slow on clients // - TryUseItem would be called immediately again afterwards // because useEndTime wouldn't be reset yet due to latency // - decals/muzzle flash would be delayed by latency and feel // bad OnUsedItem(itemData, lookAt); } else if (usability == Usability.Empty) { // play empty sound locally (if any) // -> feels best to only play it when clicking the mouse button once, not while holding if (Input.GetMouseButtonDown(0)) { if (itemData.emptySound) { audioSource.PlayOneShot(itemData.emptySound); } } } // do nothing if on cooldown (just wait) or if not usable at all } }
public override Usability CanUseHotbar(Player player, int hotbarIndex, Vector3 lookAt) { Usability baseUsable = base.CanUseHotbar(player, hotbarIndex, lookAt); if (baseUsable != Usability.Usable) { return(baseUsable); } if (player.reloading.ReloadTimeRemaining() > 0) { return(Usability.Cooldown); } if (requiredAmmo != null && player.hotbar.slots[hotbarIndex].item.ammo == 0) { return(Usability.Empty); } return(Usability.Usable); }
public override Usability CanUseHotbar(Player player, int hotbarIndex, Vector3 lookAt) { Usability baseUsable = base.CanUseHotbar(player, hotbarIndex, lookAt); if (baseUsable != Usability.Usable) { return(baseUsable); } Vector3 lookDirection = (lookAt - player.look.headPosition).normalized; Vector3 position = player.construction.CalculatePreviewPosition(this, player.look.headPosition, lookDirection); Quaternion rotation = player.construction.CalculatePreviewRotation(this); /* * Bounds bounds = new Bounds(); * Bounds originalBounds = structurePrefab.GetComponentInChildren<Renderer>().bounds; * Vector3 p0 = new Vector3(originalBounds.center.x - bounds.size.x, * originalBounds.center.y - bounds.size.y, * originalBounds.center.z - bounds.size.z); * Vector3 p1 = new Vector3(originalBounds.center.x + bounds.size.x, * originalBounds.center.y - bounds.size.y, * originalBounds.center.z - bounds.size.z); * Vector3 p2 = new Vector3(originalBounds.center.x - bounds.size.x, * originalBounds.center.y + bounds.size.y, * originalBounds.center.z - bounds.size.z); * Vector3 p3 = new Vector3(originalBounds.center.x - bounds.size.x, * originalBounds.center.y - bounds.size.y, * originalBounds.center.z + bounds.size.z); * Vector3 p4 = new Vector3(originalBounds.center.x + bounds.size.x, * originalBounds.center.y + bounds.size.y, * originalBounds.center.z - bounds.size.z); * Vector3 p5 = new Vector3(originalBounds.center.x + bounds.size.x, * originalBounds.center.y - bounds.size.y, * originalBounds.center.z + bounds.size.z); * Vector3 p6 = new Vector3(originalBounds.center.x - bounds.size.x, * originalBounds.center.y + bounds.size.y, * originalBounds.center.z + bounds.size.z); * Vector3 p7 = new Vector3(originalBounds.center.x + bounds.size.x, * originalBounds.center.y + bounds.size.y, * originalBounds.center.z + bounds.size.z); * bounds.Encapsulate(position + rotation * p0); * bounds.Encapsulate(position + rotation * p1); * bounds.Encapsulate(position + rotation * p2); * bounds.Encapsulate(position + rotation * p3); * bounds.Encapsulate(position + rotation * p4); * bounds.Encapsulate(position + rotation * p5); * bounds.Encapsulate(position + rotation * p6); * bounds.Encapsulate(position + rotation * p7); */ Vector3 prefabPosition = structurePrefab.transform.position; Quaternion prefabRotation = structurePrefab.transform.rotation; structurePrefab.transform.position = position; structurePrefab.transform.rotation = rotation; Bounds bounds = structurePrefab.GetComponentInChildren <Renderer>().bounds; structurePrefab.transform.position = prefabPosition; structurePrefab.transform.rotation = prefabRotation; return(CanBuildThere(player.look.headPosition, bounds, player.look.raycastLayers) ? Usability.Usable : Usability.Empty); }