using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameController : MonoBehaviour
{
// משתנים לניהול הרובים, התלבושות והאויבים
public GameObject guns;
private int currentGunIndex = 0;
public GameObject outfits;
private int currentOutfitIndex = 0;
public Text fpsDisplay;
// משתנים לאויבים
public GameObject enemyPrefab;
public Transform[] spawnPoints;
private float spawnRate = 2f;
// ניקוד
public int score = 0;
// משתנים לירי
private float fireRate = 0.2f;
private float lastFireTime;
private float timeSinceLastUpdate = 0.0f;
private int frameCount = 0;
private float fps = 0.0f;
// בניית מבנים
public GameObject[] buildingPrefabs;
public Transform buildPoint;
void Start()
{
SwitchGun(0);
SwitchOutfit(0);
StartCoroutine(SpawnEnemies());
}
void Update()
{
// ירי
if (Input.GetButtonDown("Fire1") && Time.time > lastFireTime + fireRate)
{
FireWeapon();
}
// החלפת רובים
if (Input.GetKeyDown(KeyCode.Alpha1)) SwitchGun(0);
if (Input.GetKeyDown(KeyCode.Alpha2)) SwitchGun(1);
if (Input.GetKeyDown(KeyCode.Alpha3)) SwitchGun(2);
// החלפת תלבושות
if (Input.GetKeyDown(KeyCode.O)) SwitchOutfit((currentOutfitIndex + 1) % outfits.Length);
// עדכון FPS
timeSinceLastUpdate += Time.deltaTime;
frameCount++;
if (timeSinceLastUpdate >= 0.5f)
{
fps = frameCount / timeSinceLastUpdate;
timeSinceLastUpdate -= 0.5f;
frameCount = 0;
}
fpsDisplay.text = "FPS: " + Mathf.Ceil(fps).ToString();
// בניית מבנים - כפתור B או Circle (PlayStation)
if (Input.GetKeyDown(KeyCode.B) || Input.GetButtonDown("Build"))
{
BuildStructure();
}
}
void FireWeapon()
{
Debug.Log("Firing " + guns[currentGunIndex].name);
lastFireTime = Time.time;
}
void SwitchGun(int index)
{
guns[currentGunIndex].SetActive(false);
currentGunIndex = index;
guns[currentGunIndex].SetActive(true);
}
void SwitchOutfit(int index)
{
outfits[currentOutfitIndex].SetActive(false);
currentOutfitIndex = index;
outfits[currentOutfitIndex].SetActive(true);
}
IEnumerator SpawnEnemies()
{
while (true)
{
SpawnEnemy();
yield return new WaitForSeconds(spawnRate);
}
}
void SpawnEnemy()
{
Transform spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
}
public void IncreaseScore(int points)
{
score += points;
Debug.Log("Score: " + score);
}
// פונקציה לבניית מבנים
void BuildStructure()
{
int randomIndex = Random.Range(0, buildingPrefabs.Length);
Instantiate(buildingPrefabs[randomIndex], buildPoint.position, Quaternion.identity);
}
}
public class PlayerController : MonoBehaviour
{
// משתנים לתנועה וקפיצה
public float moveSpeed = 5f;
public float jumpForce = 10f;
public Transform groundCheck;
public LayerMask groundLayer;
private Rigidbody rb;
private bool isGrounded;
private float groundDistance = 0.2f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
isGrounded = Physics.Raycast(groundCheck.position, Vector3.down, groundDistance, groundLayer);
// קפיצה
if (isGrounded && (Input.GetButtonDown("Jump") || Input.GetKeyDown(KeyCode.Space)))
{
Jump();
}
}
void Jump()
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
void FixedUpdate()
{
// תנועה אופקית עם מקשים W, A, S, D או ג'ויסטיק
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical) * moveSpeed * Time.deltaTime;
rb.MovePosition(transform.position + movement);
}
}
public class Enemy : MonoBehaviour
{
// משתנים לבריאות ואפקט מוות
public int health = 10;
public int points = 100;
public GameObject deathEffect;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
TakeDamage(1);
}
}
void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
void Die()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
FindObjectOfType<GameController>().IncreaseScore(points);
Destroy(gameObject);
}
}
public class Bullet : MonoBehaviour
{
// משתנים למהירות וזמן חיים של קליע
public float speed = 20f;
public float lifetime = 2f;
void Start()
{
Destroy(gameObject, lifetime);
}
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
}
public class FPSDisplay : MonoBehaviour
{
private float fps = 0.0f;
private int frameCount = 0;
private float timeSinceLastUpdate = 0.0f;
void Update()
{
timeSinceLastUpdate += Time.deltaTime;
frameCount++;
if (timeSinceLastUpdate >= 0.5f)
{
fps = frameCount / timeSinceLastUpdate;
timeSinceLastUpdate -= 0.5f;
frameCount = 0;
}
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), "FPS: " + Mathf.Ceil(fps).ToString());
}
}
public class BuildingController : MonoBehaviour
{
// משתנים לבניית מבנים כמו חומה, רמפה וכו’
public GameObject buildingPrefabs;
public Transform buildPoint;
void Update()
{
if (Input.GetKeyDown(KeyCode.B) || Input.GetButtonDown("Build")) // כפתור בנייה
{
BuildStructure();
}
}
void BuildStructure()
{
int randomIndex = Random.Range(0, buildingPrefabs.Length);
Instantiate(buildingPrefabs[randomIndex], buildPoint.position, Quaternion.identity);
}
}