202 lines
5.9 KiB
C#
202 lines
5.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Spaceshooter.Combat;
|
|
using Spaceshooter.Weapons;
|
|
using Spaceshooter.Managers;
|
|
|
|
namespace Spaceshooter.Player
|
|
{
|
|
[RequireComponent(typeof(Collider2D))]
|
|
public class PlayerShip3D : MonoBehaviour
|
|
{
|
|
public static PlayerShip3D Instance { get; private set; }
|
|
|
|
[Header("Health")]
|
|
[SerializeField] private float maxHealth = 100f;
|
|
[SerializeField] private float currentHealth;
|
|
|
|
[Header("Economy")]
|
|
[SerializeField] private int scrapCount = 0;
|
|
|
|
[Header("Projectiles & Muzzle")]
|
|
[SerializeField] private GameObject projectilePrefab;
|
|
[SerializeField] private Transform muzzlePoint;
|
|
|
|
[Header("Weapons")]
|
|
[SerializeField] private List<WeaponSlot> weaponSlots = new List<WeaponSlot>();
|
|
[SerializeField] private int activeSlotIndex = 0;
|
|
|
|
public float MaxHealth => maxHealth;
|
|
public float CurrentHealth => currentHealth;
|
|
public int ScrapCount => scrapCount;
|
|
public List<WeaponSlot> WeaponSlots => weaponSlots;
|
|
public int ActiveSlotIndex => activeSlotIndex;
|
|
|
|
public event Action OnStatsChanged;
|
|
public event Action OnWeaponSlotChanged;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
Instance = this;
|
|
|
|
currentHealth = maxHealth;
|
|
InitializeDefaultLoadout();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
OnStatsChanged?.Invoke();
|
|
OnWeaponSlotChanged?.Invoke();
|
|
}
|
|
|
|
public void InitializeDefaultLoadout()
|
|
{
|
|
weaponSlots = new List<WeaponSlot>
|
|
{
|
|
new WeaponSlot(0, WeaponData.CreateDefault(WeaponType.StarterBlaster)),
|
|
new WeaponSlot(1, null),
|
|
new WeaponSlot(2, null)
|
|
};
|
|
activeSlotIndex = 0;
|
|
}
|
|
|
|
public void ResetShip()
|
|
{
|
|
currentHealth = maxHealth;
|
|
scrapCount = 0;
|
|
InitializeDefaultLoadout();
|
|
OnStatsChanged?.Invoke();
|
|
OnWeaponSlotChanged?.Invoke();
|
|
}
|
|
|
|
public void SetActiveSlot(int index)
|
|
{
|
|
if (index >= 0 && index < weaponSlots.Count)
|
|
{
|
|
if (weaponSlots[index].IsOccupied)
|
|
{
|
|
activeSlotIndex = index;
|
|
OnWeaponSlotChanged?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
public WeaponSlot GetActiveSlot()
|
|
{
|
|
if (activeSlotIndex >= 0 && activeSlotIndex < weaponSlots.Count)
|
|
{
|
|
return weaponSlots[activeSlotIndex];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public bool FireAt(Asteroid3D target, Vector2 hitPoint)
|
|
{
|
|
WeaponSlot slot = GetActiveSlot();
|
|
if (slot == null || !slot.CanFire) return false;
|
|
|
|
if (projectilePrefab == null)
|
|
{
|
|
Debug.LogWarning("PlayerShip3D: Projectile Prefab is not assigned.");
|
|
return false;
|
|
}
|
|
|
|
Vector3 spawnPos = muzzlePoint != null ? muzzlePoint.position : (transform.position + Vector3.right * 0.8f);
|
|
|
|
GameObject projObj = Instantiate(projectilePrefab, spawnPos, Quaternion.identity);
|
|
Projectile3D proj = projObj.GetComponent<Projectile3D>();
|
|
if (proj != null)
|
|
{
|
|
proj.Initialize(
|
|
hitPoint,
|
|
slot.weaponData.damage,
|
|
slot.weaponData.projectileSpeed,
|
|
slot.weaponData.projectileColor,
|
|
slot.weaponData.projectileScale
|
|
);
|
|
}
|
|
|
|
slot.RecordFired();
|
|
OnWeaponSlotChanged?.Invoke();
|
|
return true;
|
|
}
|
|
|
|
public void TakeDamage(float amount)
|
|
{
|
|
currentHealth = Mathf.Max(0f, currentHealth - amount);
|
|
OnStatsChanged?.Invoke();
|
|
|
|
if (currentHealth <= 0f)
|
|
{
|
|
if (GameManager.Instance != null)
|
|
{
|
|
GameManager.Instance.TriggerGameOver();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddScrap(int amount)
|
|
{
|
|
scrapCount += amount;
|
|
OnStatsChanged?.Invoke();
|
|
}
|
|
|
|
public bool SpendScrap(int amount)
|
|
{
|
|
if (scrapCount >= amount)
|
|
{
|
|
scrapCount -= amount;
|
|
OnStatsChanged?.Invoke();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool RepairHull(float amount, int cost)
|
|
{
|
|
if (currentHealth >= maxHealth) return false;
|
|
if (SpendScrap(cost))
|
|
{
|
|
currentHealth = Mathf.Min(maxHealth, currentHealth + amount);
|
|
OnStatsChanged?.Invoke();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool BuyWeapon(WeaponData newWeapon)
|
|
{
|
|
if (newWeapon == null || !SpendScrap(newWeapon.cost)) return false;
|
|
|
|
for (int i = 0; i < weaponSlots.Count; i++)
|
|
{
|
|
if (weaponSlots[i].IsOccupied && weaponSlots[i].weaponData.type == newWeapon.type)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < weaponSlots.Count; i++)
|
|
{
|
|
if (!weaponSlots[i].IsOccupied)
|
|
{
|
|
weaponSlots[i].Equip(newWeapon);
|
|
activeSlotIndex = i;
|
|
OnWeaponSlotChanged?.Invoke();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
weaponSlots[activeSlotIndex].Equip(newWeapon);
|
|
OnWeaponSlotChanged?.Invoke();
|
|
return true;
|
|
}
|
|
}
|
|
}
|