weapon system done
This commit is contained in:
1 parent
1af59b921f
commit
f07d687be8
20 files changed
+1504
-52
No files matched your search
@@ -26,10 +26,12 @@ public class HitscanWeapon : MonoBehaviour
|
||||
// 給 HUD / 之後的記錄器用
|
||||
public int Ammo { get; private set; }
|
||||
public bool IsReloading => reloadEndTime > 0f;
|
||||
public float ReloadRemaining => IsReloading ? Mathf.Max(0f, reloadEndTime - Time.time) : 0f; // 換彈剩餘秒數
|
||||
public int RecoilSeed { get; private set; } // 後座力隨機部分的種子,記錄下來就能重現
|
||||
|
||||
public event Action Fired;
|
||||
public event Action<BodyPart, float> HitLanded; // 部位、實際傷害
|
||||
public event Action<Vector3> ShotTraced; // 每顆子彈 (彈丸) 停下的位置,給彈道線用
|
||||
|
||||
private float nextFireTime;
|
||||
private float lastShotTime = -999f;
|
||||
@@ -157,10 +159,29 @@ public class HitscanWeapon : MonoBehaviour
|
||||
return new Vector2(horizontal, vertical);
|
||||
}
|
||||
|
||||
// 依距離由近到遠檢查 : 自己 → 略過 ; 命中框 → 命中 ; 機器人的移動膠囊 → 略過 ; 其他 (牆) → 擋住
|
||||
void ShootRay(Vector3 direction)
|
||||
{
|
||||
Vector3 origin = aimCamera.transform.position;
|
||||
Vector3 end = origin + direction * data.range; // 什麼都沒打到就畫到射程盡頭
|
||||
|
||||
RaycastHit? stop = FindFirstHit(origin, direction);
|
||||
if (stop.HasValue)
|
||||
{
|
||||
end = stop.Value.point;
|
||||
|
||||
if (stop.Value.collider.TryGetComponent(out Hitbox hitbox))
|
||||
{
|
||||
float dealt = hitbox.Hit(data.damage);
|
||||
if (dealt > 0f) HitLanded?.Invoke(hitbox.part, dealt);
|
||||
}
|
||||
}
|
||||
|
||||
ShotTraced?.Invoke(end);
|
||||
}
|
||||
|
||||
// 依距離由近到遠檢查 : 自己 → 略過 ; 命中框 → 命中 ; 機器人的移動膠囊 → 略過 ; 其他 (牆) → 擋住
|
||||
RaycastHit? FindFirstHit(Vector3 origin, Vector3 direction)
|
||||
{
|
||||
int count = Physics.RaycastNonAlloc(origin, direction, hitBuffer, data.range, ~0, QueryTriggerInteraction.Collide);
|
||||
Array.Sort(hitBuffer, 0, count, HitDistanceComparer.Instance);
|
||||
|
||||
@@ -169,24 +190,21 @@ public class HitscanWeapon : MonoBehaviour
|
||||
Collider col = hitBuffer[i].collider;
|
||||
|
||||
if (playerRoot != null && col.transform.IsChildOf(playerRoot)) continue;
|
||||
|
||||
if (col.TryGetComponent(out Hitbox hitbox))
|
||||
{
|
||||
float dealt = hitbox.Hit(data.damage);
|
||||
if (dealt > 0f) HitLanded?.Invoke(hitbox.part, dealt);
|
||||
return;
|
||||
}
|
||||
|
||||
if (col.TryGetComponent(out Hitbox _)) return hitBuffer[i];
|
||||
if (col.isTrigger || col.GetComponentInParent<BotHealth>() != null) continue;
|
||||
|
||||
return; // 被牆或其他東西擋住
|
||||
return hitBuffer[i]; // 被牆或其他東西擋住
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void StartReload()
|
||||
{
|
||||
if (IsReloading || Ammo >= data.magazineSize) return;
|
||||
reloadEndTime = Time.time + data.reloadTime;
|
||||
|
||||
float duration = Ammo == 0 && data.emptyReloadTime > 0f ? data.emptyReloadTime : data.reloadTime;
|
||||
reloadEndTime = Time.time + duration;
|
||||
}
|
||||
|
||||
void UpdateReload()
|
||||
|
||||
Reference in new issue
Block a user