using UnityEngine;
using Photon.Pun;
using System.Collections;
public class GameManager : MonoBehaviourPunCallbacks
{
public Transform[] spawnPoints;
public GameObject playerPrefab;
[System.Serializable]
public class Weapon
{
public string name;
public GameObject weaponModel;
public int maxAmmo = 100;
public float fireRate = 0.1f; // Time between shots
public int damage = 25;
public float reloadTime = 2f;
public bool usesEnergy = false; // Set true for futuristic weapons
public bool armorPenetration = false; // Special for M82B or Groza
public float overheatTime = 3f; // Plasma weapons only
}
public Weapon[] weapons = new Weapon[]
{
new Weapon { name = "Scar", maxAmmo = 30, fireRate = 0.1f, damage = 22,
reloadTime = 2f },
new Weapon { name = "Groza", maxAmmo = 30, fireRate = 0.08f, damage = 28,
reloadTime = 2.2f, armorPenetration = true },
new Weapon { name = "AWM", maxAmmo = 5, fireRate = 1.2f, damage = 100,
reloadTime = 3.5f },
new Weapon { name = "M1887", maxAmmo = 2, fireRate = 0.9f, damage = 90,
reloadTime = 2.5f },
new Weapon { name = "MP40", maxAmmo = 40, fireRate = 0.05f, damage = 18,
reloadTime = 1.8f },
new Weapon { name = "Plasma Gun", maxAmmo = 0, fireRate = 0.06f, damage =
20, usesEnergy = true, overheatTime = 5f },
new Weapon { name = "M82B", maxAmmo = 8, fireRate = 0.5f, damage = 90,
reloadTime = 2.8f, armorPenetration = true }
};
private int currentWeaponIndex = 0;
private Weapon currentWeapon;
public Camera fpsCamera;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
private int currentAmmo;
private bool isReloading = false;
private float energyCooldown = 0f; // For Plasma Gun
// Player Stats
public int maxHealth = 200;
private int currentHealth;
// Enemy AI
public GameObject zombiePrefab;
public Transform[] zombieSpawnPoints;
public int maxZombies = 20;
public float zombieSpawnRate = 3f;
// UI Reference
private UIManager uiManager;
void Start()
{
uiManager = FindObjectOfType<UIManager>();
currentHealth = maxHealth;
EquipWeapon(0);
if (PhotonNetwork.IsMasterClient)
{
StartCoroutine(SpawnZombies());
}
}
void Update()
{
if (isReloading) return;
// Shooting mechanics
if (Input.GetButton("Fire1") && currentWeapon.usesEnergy == false &&
currentAmmo > 0)
{
Shoot();
}
else if (currentWeapon.usesEnergy && Time.time > energyCooldown)
{
Shoot();
energyCooldown = Time.time + currentWeapon.fireRate;
// Overheat logic for Plasma Gun
if (currentWeapon.name == "Plasma Gun")
{
StartCoroutine(OverheatPlasmaGun());
}
}
// Reload weapon
if (Input.GetKeyDown(KeyCode.R) && currentAmmo < currentWeapon.maxAmmo)
{
StartCoroutine(Reload());
}
// Switch weapons
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
EquipWeapon((currentWeaponIndex + 1) % weapons.Length);
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
EquipWeapon((currentWeaponIndex - 1 + weapons.Length) %
weapons.Length);
}
}
void EquipWeapon(int weaponIndex)
{
currentWeaponIndex = weaponIndex;
currentWeapon = weapons[weaponIndex];
currentAmmo = currentWeapon.maxAmmo;
foreach (var weapon in weapons)
{
weapon.weaponModel.SetActive(false);
}
weapons[weaponIndex].weaponModel.SetActive(true);
if (uiManager != null)
{
uiManager.UpdateWeaponName(currentWeapon.name);
uiManager.UpdateAmmo(currentAmmo, currentWeapon.maxAmmo);
}
}
void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCamera.transform.position,
fpsCamera.transform.forward, out hit))
{
EnemyAI enemy = hit.transform.GetComponent<EnemyAI>();
if (enemy != null)
{
int finalDamage = currentWeapon.armorPenetration ?
currentWeapon.damage * 2 : currentWeapon.damage;
enemy.TakeDamage(finalDamage);
}
Instantiate(impactEffect, hit.point,
Quaternion.LookRotation(hit.normal));
}
currentAmmo--;
uiManager.UpdateAmmo(currentAmmo, currentWeapon.maxAmmo);
}
IEnumerator Reload()
{
isReloading = true;
yield return new WaitForSeconds(currentWeapon.reloadTime);
currentAmmo = currentWeapon.maxAmmo;
isReloading = false;
uiManager.UpdateAmmo(currentAmmo, currentWeapon.maxAmmo);
}
IEnumerator OverheatPlasmaGun()
{
yield return new WaitForSeconds(currentWeapon.overheatTime);
energyCooldown = Time.time + 5f; // Cooldown after overheat
}
IEnumerator SpawnZombies()
{
while (true)
{
if (GameObject.FindGameObjectsWithTag("Zombie").Length < maxZombies)
{
int spawnIndex = Random.Range(0, zombieSpawnPoints.Length);
PhotonNetwork.Instantiate(zombiePrefab.name,
zombieSpawnPoints[spawnIndex].position, Quaternion.identity);
}
yield return new WaitForSeconds(zombieSpawnRate);
}
}
}