GlobalGameJame/Assets/Scripts/PlayerManager.cs
Kotrba Filip 5f189178bc push 50
2020-02-01 18:17:29 +01:00

217 lines
7.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerManager : MonoBehaviour
{
public float speed = 10;
public float runSpeed = 25;
public float rotateSpeed = 5;
public float mouseSensitive = 200;
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 = 10;
public List<GameObject> weaponList = new List<GameObject>();
private GameObject createWeapon;
public GuiManager guiManager;
[HideInInspector]public int selectedWeapon = 0;
// Start is called before the first frame update
void Start()
{
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()
{
Move();
RunSwitch();
SelectWeapon();
BuildBlock();
Interact();
if (actualHealth <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
}
}
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)
{
if (Input.mouseScrollDelta.y > 0)
{
WeaponManager weaponManager = weaponList[selectedWeapon].GetComponent<WeaponManager>();
if (weaponManager != null && createWeapon != null)
{
Destroy(createWeapon);
}
if (weaponList.Count > selectedWeapon + 1) {
selectedWeapon++;
}
else
{
selectedWeapon = 0;
}
}
else if (Input.mouseScrollDelta.y < 0)
{
WeaponManager weaponManager = weaponList[selectedWeapon].GetComponent<WeaponManager>();
if (weaponManager != null && createWeapon != null)
{
Destroy(createWeapon);
}
if (selectedWeapon > 0)
{
selectedWeapon--;
}
else
{
selectedWeapon = weaponList.Count - 1;
}
}
WeaponManager weaponManager2 = weaponList[selectedWeapon].GetComponent<WeaponManager>();
if (weaponManager2 != null && createWeapon == null)
{
createWeapon = Instantiate(weaponList[selectedWeapon]) as GameObject;
createWeapon.transform.parent = playerCamera.transform;
createWeapon.transform.localPosition = Vector3.zero;
createWeapon.transform.localEulerAngles = Vector3.zero;
}
}
}
void BuildBlock ()
{
if (Input.GetAxisRaw("Fire1") > 0 && buildTime < Time.time && weaponList.Count > 0)
{
Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("BuildPlace")))
{
BlockManager blockManager = weaponList[selectedWeapon].GetComponent<BlockManager>();
if (blockManager != null) {
Instantiate(weaponList[selectedWeapon], hit.point, transform.rotation);
buildTime = buildRate + 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"))
{
guiManager.InteractVerifier(true);
}
else
{
guiManager.InteractVerifier(false);
}
if (Input.GetAxisRaw("Build") > 0 && buildTime < Time.time && barricadeMaterials > 0 && hit.transform.tag == "BarricadeField")
{
BarricadeManager barricadeManager = hit.transform.GetComponent<BarricadeManager>();
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
{
guiManager.InteractVerifier(false);
}
}
}