GlobalGameJame/Assets/Scripts/PlayerManager.cs

267 lines
9.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerManager : MonoBehaviour
{
public float speed = 8;
public float runSpeed = 20;
public float rotateSpeed = 5;
public float mouseSensitive = 100;
public float buildRate = 0.5f;
public float health = 100;
public float actualHealth = 100;
private float buildTime;
private bool run = false;
private Camera playerCamera;
private Rigidbody rigidBody;
public int barricadeMaterials = 50;
public int ammo = 50;
public List<GameObject> weaponList = new List<GameObject>();
private GameObject createWeapon;
public GameObject spikes;
public Sprite spikesImage;
public Sprite handImage;
public GuiManager guiManager;
public int selectedWeapon = 0;
private float selectRate = 0.1f;
private float selectTime = 0.1f;
public AudioSource audio;
public AudioClip lose;
private float nextMapeTime = 3;
private float nextMapeTime2 = 3;
// Start is called before the first frame update
void Start()
{
selectTime = selectRate;
actualHealth = health;
buildTime = buildRate;
playerCamera = GetComponentInChildren<Camera>();
rigidBody = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit(0);
}
guiManager.Wood.text = barricadeMaterials.ToString();
guiManager.Ammo.text = ammo.ToString();
Move();
RunSwitch();
SelectWeapon();
Interact();
if (actualHealth <= 0)
{
if (audio != null && lose != null && (nextMapeTime2 + 1) <= Time.time)
{
playerCamera.transform.parent = null;
rigidBody.freezeRotation = true;
speed = 0;
runSpeed = 0;
audio.Stop();
audio.volume = 0.5f;
audio.PlayOneShot(lose);
nextMapeTime2 = nextMapeTime + Time.time;
}
if (nextMapeTime2 <= Time.time) {
SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
}
}
float hpInPrcent = (actualHealth / (health / 100.0f));
guiManager.SetHealth((1.0f / 100.0f) * hpInPrcent);
}
void FixedUpdate()
{
Rotate();
}
void Move()
{
rigidBody.MovePosition(
transform.position +
(transform.forward * (run ? runSpeed : speed) * Input.GetAxis("Vertical") * Time.deltaTime) +
(transform.right * (run ? runSpeed : speed) * Input.GetAxis("Horizontal") * Time.deltaTime)
);
}
public void Damage(float damage)
{
actualHealth -= damage;
}
void Rotate()
{
rigidBody.freezeRotation = false;
transform.Rotate(new Vector3(0, rotateSpeed * Input.GetAxis("Mouse X") * mouseSensitive * Time.deltaTime, 0));
if (
((playerCamera.transform.localEulerAngles.x >= 270 && playerCamera.transform.localEulerAngles.x <= 360) &&
playerCamera.transform.localEulerAngles.y == 0 &&
playerCamera.transform.localEulerAngles.z == 0) ||
((playerCamera.transform.localEulerAngles.x <= 90 && playerCamera.transform.localEulerAngles.x >= 0) &&
playerCamera.transform.localEulerAngles.y == 0 &&
playerCamera.transform.localEulerAngles.z == 0)
) {
playerCamera.transform.Rotate(new Vector3(rotateSpeed * -Input.GetAxis("Mouse Y") * mouseSensitive * Time.deltaTime, 0, 0));
} else if (
((playerCamera.transform.localEulerAngles.x >= 270 && playerCamera.transform.localEulerAngles.x <= 360) &&
playerCamera.transform.localEulerAngles.y == 180 &&
playerCamera.transform.localEulerAngles.z == 180)
) {
playerCamera.transform.Rotate(new Vector3(rotateSpeed * (-Input.GetAxis("Mouse Y") > 0 ? -Input.GetAxis("Mouse Y") : 0) * mouseSensitive * Time.deltaTime, 0, 0));
} else if (
((playerCamera.transform.localEulerAngles.x <= 90 && playerCamera.transform.localEulerAngles.x >= 0) &&
playerCamera.transform.localEulerAngles.y == 180 &&
playerCamera.transform.localEulerAngles.z == 180)
) {
playerCamera.transform.Rotate(new Vector3(rotateSpeed * (-Input.GetAxis("Mouse Y") < 0 ? -Input.GetAxis("Mouse Y") : 0) * mouseSensitive * Time.deltaTime, 0, 0));
}
else
{
playerCamera.transform.Rotate(new Vector3(rotateSpeed * -Input.GetAxis("Mouse Y") * mouseSensitive * Time.deltaTime, 0, 0));
}
rigidBody.freezeRotation = true;
}
void RunSwitch()
{
if (Input.GetAxisRaw("Run") > 0)
{
run = true;
}
else
{
run = false;
}
}
void SelectWeapon()
{
if (weaponList.Count > 0)
{
WeaponManager weaponManager = weaponList[selectedWeapon].GetComponent<WeaponManager>();
if (weaponManager != null) {
weaponManager.player = this;
Debug.Log(weaponList.Count);
if (Input.GetAxis("Mouse ScrollWheel") > 0 && selectTime < Time.time)
{
if (weaponManager != null && createWeapon != null)
{
Destroy(createWeapon);
}
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0 && selectTime < Time.time)
{
if (weaponManager != null && createWeapon != null)
{
Destroy(createWeapon);
}
}
weaponManager.player = this;
if (weaponManager != null && createWeapon == null)
{
createWeapon = Instantiate(weaponList[selectedWeapon]) as GameObject;
createWeapon.transform.parent = playerCamera.transform;
createWeapon.transform.localPosition = Vector3.zero;
createWeapon.transform.localEulerAngles = Vector3.zero;
}
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 && selectTime < Time.time)
{
if (weaponList.Count - 1 > selectedWeapon)
{
selectedWeapon += 1;
}
else
{
selectedWeapon = 0;
}
selectTime = selectRate + Time.time;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0 && selectTime < Time.time)
{
if (selectedWeapon > 0)
{
selectedWeapon -= 1;
}
else
{
selectedWeapon = weaponList.Count - 1;
}
selectTime = selectRate + Time.time;
}
}
}
void Interact()
{
Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 2))
{
if (hit.transform.CompareTag("BarricadeField") || hit.transform.CompareTag("Interact") || hit.transform.gameObject.layer == LayerMask.NameToLayer("BuildPlace"))
{
BlockManager blockManager = hit.transform.GetComponent<BlockManager>();
if (blockManager == null || blockManager.blockType != BlockManager.BlockType.Barricade) {
guiManager.InteractVerifier(true, handImage);
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("BuildPlace"))
{
guiManager.InteractVerifier(true, spikesImage);
}
}
else
{
guiManager.InteractVerifier(false, handImage);
}
}
else
{
guiManager.InteractVerifier(false, handImage);
}
if (Input.GetAxisRaw("Build") > 0 && buildTime < Time.time && barricadeMaterials > 0 && hit.transform.tag == "BarricadeField")
{
BarricadeManager barricadeManager = hit.transform.GetComponent<BarricadeManager>();
if (barricadeManager.health < (barricadeManager.barricadePlanks.Count * 50))
{
barricadeManager.addPlank();
barricadeMaterials--;
buildTime = buildRate + Time.time;
}
}
if (Input.GetAxisRaw("Build") > 0 && hit.transform.tag == "Interact")
{
}
if (Input.GetKeyDown(KeyCode.E) && hit.transform.tag == "Interact")
{
BlockManager blockManager = hit.transform.GetComponent<BlockManager>();
blockManager.Action();
}
else if (hit.transform.gameObject.layer == LayerMask.NameToLayer("BuildPlace") && Input.GetKeyDown(KeyCode.E) && buildTime < Time.time && barricadeMaterials > 2)
{
BlockManager blockManager = spikes.GetComponent<BlockManager>();
if (blockManager != null)
{
barricadeMaterials-=3;
Instantiate<GameObject>(spikes, hit.point, transform.rotation);
buildTime = buildRate + Time.time;
}
}
}
else
{
guiManager.InteractVerifier(false);
}
}
}