Files
project-reset/Assets/Scripts/Units/PlayerCombat.cs
2026-01-16 17:45:13 -05:00

92 lines
2.9 KiB
C#

using System;
using Reset.Core;
using Reset.Items;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Profiling;
namespace Reset.Units{
public class PlayerCombat : UnitCombat{
public CombatType currentCombatType;
private IEquipable currentWeapon;
private GameObject currentWeaponItem;
public void DrawWeapon(){
if (Unit.UnitIsNetworked()) {
CreatePlayerWeaponRpc();
} else {
CreatePlayerWeapon();
}
}
[Rpc(SendTo.Everyone)]
public void CreatePlayerWeaponRpc(){
CreatePlayerWeapon();
}
public void CreatePlayerWeapon(){
// Remove a current weapon
DisposeCurrentWeapon();
// Remove a current weapon
if (currentWeapon != null) {
Destroy(currentWeaponItem);
currentWeaponItem = null;
}
// Switch which weapopn gets pulled out
Weapon weaponType = null;
switch ((Unit.Combat as PlayerCombat).currentCombatType) {
case CombatType.Melee:
weaponType = ((PlayerInventory)Unit.Inventory).meleeWeapon;
break;
case CombatType.Ranged:
weaponType = ((PlayerInventory)Unit.Inventory).rangedWeapon;
break;
default:
throw new ArgumentOutOfRangeException();
}
// Add weapon to status and hand
currentWeapon = weaponType;
currentWeaponItem = weaponType.InstantiateItemObject();
// Move item to hand
currentWeaponItem.transform.SetParent((Unit.Animation as PlayerAnimation).rightHand);
currentWeaponItem.transform.localPosition = weaponType.handPositionOffset;
currentWeaponItem.transform.rotation = (Unit.Animation as PlayerAnimation).rightHand.rotation * Quaternion.Euler(weaponType.handRotationOffset);
// Add the weapon script for this weapon
(currentWeapon as Weapon).AddActorScript();
}
public GameObject GetCurrentWeaponItem(){
return currentWeaponItem;
}
public void HolsterWeapon(){
DisposeCurrentWeapon();
}
public void DisposeCurrentWeapon(){
// Return if no weapon active
if (currentWeapon == null) {
return;
}
// Destroy physical mesh
Destroy(currentWeaponItem);
// Destroy weapon actor
if ((GetComponent<WeaponActor>()) != null) {
Destroy(GetComponent<WeaponActor>());
}
// Remove references
currentWeaponItem = null;
currentWeapon = null;
}
}
}