GlobalGameJame/Assets/Scripts/PlayerManager.cs

246 lines
9.0 KiB
C#
Raw Normal View History

2020-01-31 20:47:30 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2020-02-01 11:11:56 +00:00
using UnityEngine.SceneManagement;
2020-01-31 20:47:30 +00:00
public class PlayerManager : MonoBehaviour
{
public float speed = 10;
public float runSpeed = 25;
public float rotateSpeed = 5;
public float mouseSensitive = 200;
2020-01-31 21:32:05 +00:00
public float buildRate = 0.5f;
2020-02-01 11:11:56 +00:00
public float health = 100;
2020-02-01 15:20:10 +00:00
public float actualHealth = 100;
2020-01-31 21:32:05 +00:00
private float buildTime;
2020-01-31 20:47:30 +00:00
private bool run = false;
private Camera playerCamera;
private Rigidbody rigidBody;
2020-02-01 17:53:28 +00:00
public int barricadeMaterials = 50;
public int ammo = 50;
2020-01-31 20:47:30 +00:00
public List<GameObject> weaponList = new List<GameObject>();
2020-01-31 23:53:03 +00:00
private GameObject createWeapon;
2020-02-01 21:54:57 +00:00
public GameObject spikes;
public Sprite spikesImage;
public Sprite handImage;
2020-02-01 17:17:29 +00:00
public GuiManager guiManager;
2020-02-01 19:53:49 +00:00
public int selectedWeapon = 0;
private float selectRate = 1;
private float selectTime = 1;
2020-01-31 20:47:30 +00:00
// Start is called before the first frame update
void Start()
{
2020-02-01 19:53:49 +00:00
selectTime = selectRate;
2020-02-01 11:11:56 +00:00
actualHealth = health;
2020-01-31 21:32:05 +00:00
buildTime = buildRate;
2020-01-31 20:47:30 +00:00
playerCamera = GetComponentInChildren<Camera>();
rigidBody = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
2020-02-01 18:49:53 +00:00
guiManager.Wood.text = barricadeMaterials.ToString();
guiManager.Ammo.text = ammo.ToString();
2020-01-31 20:47:30 +00:00
Move();
RunSwitch();
2020-01-31 21:32:05 +00:00
SelectWeapon();
2020-02-01 11:11:56 +00:00
Interact();
if (actualHealth <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
}
float hpInPrcent = (actualHealth / (health / 100.0f));
guiManager.SetHealth((1.0f / 100.0f) * hpInPrcent);
2020-01-31 20:47:30 +00:00
}
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)
);
}
2020-02-01 11:11:56 +00:00
public void Damage(float damage)
{
actualHealth -= damage;
}
2020-01-31 20:47:30 +00:00
void Rotate()
{
2020-02-01 07:52:46 +00:00
rigidBody.freezeRotation = false;
2020-01-31 20:47:30 +00:00
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));
}
2020-01-31 23:53:03 +00:00
else
{
playerCamera.transform.Rotate(new Vector3(rotateSpeed * -Input.GetAxis("Mouse Y") * mouseSensitive * Time.deltaTime, 0, 0));
}
2020-02-01 07:52:46 +00:00
rigidBody.freezeRotation = true;
2020-01-31 20:47:30 +00:00
}
void RunSwitch()
{
if (Input.GetAxisRaw("Run") > 0)
{
run = true;
}
else
{
run = false;
}
}
2020-01-31 21:32:05 +00:00
void SelectWeapon()
2020-01-31 20:47:30 +00:00
{
2020-01-31 21:32:05 +00:00
if (weaponList.Count > 0)
2020-01-31 20:47:30 +00:00
{
2020-02-01 19:53:49 +00:00
WeaponManager weaponManager = weaponList[selectedWeapon].GetComponent<WeaponManager>();
if (weaponManager != null) {
2020-02-01 18:49:53 +00:00
weaponManager.player = this;
2020-02-01 19:53:49 +00:00
Debug.Log(weaponList.Count);
if (Input.GetAxis("Mouse ScrollWheel") > 0 && selectTime < Time.time)
2020-01-31 23:53:03 +00:00
{
2020-02-01 19:53:49 +00:00
if (weaponManager != null && createWeapon != null)
{
Destroy(createWeapon);
}
2020-01-31 23:53:03 +00:00
}
2020-02-01 19:53:49 +00:00
else if (Input.GetAxis("Mouse ScrollWheel") < 0 && selectTime < Time.time)
{
if (weaponManager != null && createWeapon != null)
{
Destroy(createWeapon);
}
2020-01-31 20:47:30 +00:00
}
2020-02-01 19:53:49 +00:00
weaponManager.player = this;
if (weaponManager != null && createWeapon == null)
2020-01-31 20:47:30 +00:00
{
2020-02-01 19:53:49 +00:00
createWeapon = Instantiate(weaponList[selectedWeapon]) as GameObject;
createWeapon.transform.parent = playerCamera.transform;
createWeapon.transform.localPosition = Vector3.zero;
createWeapon.transform.localEulerAngles = Vector3.zero;
2020-01-31 20:47:30 +00:00
}
}
2020-02-01 19:53:49 +00:00
if (Input.GetAxis("Mouse ScrollWheel") > 0 && selectTime < Time.time)
2020-01-31 20:47:30 +00:00
{
2020-02-01 19:53:49 +00:00
if (weaponList.Count - 1 > selectedWeapon)
{
selectedWeapon += 1;
}
else
2020-01-31 23:53:03 +00:00
{
2020-02-01 19:53:49 +00:00
selectedWeapon = 0;
2020-01-31 23:53:03 +00:00
}
2020-02-01 19:53:49 +00:00
selectTime = selectRate + Time.time;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0 && selectTime < Time.time)
{
2020-01-31 20:47:30 +00:00
if (selectedWeapon > 0)
{
2020-02-01 19:53:49 +00:00
selectedWeapon -= 1;
2020-01-31 20:47:30 +00:00
}
else
{
selectedWeapon = weaponList.Count - 1;
}
2020-02-01 19:53:49 +00:00
selectTime = selectRate + Time.time;
2020-01-31 23:53:03 +00:00
}
2020-01-31 21:32:05 +00:00
}
}
2020-02-01 11:11:56 +00:00
void Interact()
2020-01-31 23:53:03 +00:00
{
Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
2020-02-01 13:26:43 +00:00
if (Physics.Raycast(ray, out hit, 2))
2020-01-31 23:53:03 +00:00
{
2020-02-01 21:54:57 +00:00
if (hit.transform.CompareTag("BarricadeField") || hit.transform.CompareTag("Interact") || hit.transform.gameObject.layer == LayerMask.NameToLayer("BuildPlace"))
2020-02-01 17:17:29 +00:00
{
2020-02-01 21:54:57 +00:00
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);
}
2020-02-01 17:17:29 +00:00
}
else
{
2020-02-01 21:54:57 +00:00
guiManager.InteractVerifier(false, handImage);
2020-02-01 17:17:29 +00:00
}
2020-01-31 23:53:03 +00:00
if (Input.GetAxisRaw("Build") > 0 && buildTime < Time.time && barricadeMaterials > 0 && hit.transform.tag == "BarricadeField")
{
BarricadeManager barricadeManager = hit.transform.GetComponent<BarricadeManager>();
2020-02-01 18:49:53 +00:00
if (barricadeManager.health < (barricadeManager.barricadePlanks.Count * 50))
{
barricadeManager.addPlank();
barricadeMaterials--;
buildTime = buildRate + Time.time;
}
2020-01-31 23:53:03 +00:00
}
2020-02-01 11:11:56 +00:00
if (Input.GetAxisRaw("Build") > 0 && hit.transform.tag == "Interact")
2020-02-01 15:20:10 +00:00
{
}
if (Input.GetKeyDown(KeyCode.E) && hit.transform.tag == "Interact")
2020-02-01 11:11:56 +00:00
{
BlockManager blockManager = hit.transform.GetComponent<BlockManager>();
blockManager.Action();
2020-02-01 21:54:57 +00:00
}
else if (hit.transform.gameObject.layer == LayerMask.NameToLayer("BuildPlace") && Input.GetKeyDown(KeyCode.E) && buildTime < Time.time && barricadeMaterials > 0)
2020-02-01 21:54:57 +00:00
{
BlockManager blockManager = spikes.GetComponent<BlockManager>();
if (blockManager != null)
{
barricadeMaterials-=3;
2020-02-01 21:54:57 +00:00
Instantiate<GameObject>(spikes, hit.point, transform.rotation);
buildTime = buildRate + Time.time;
}
2020-02-01 11:11:56 +00:00
}
2020-01-31 20:47:30 +00:00
}
2020-02-01 17:17:29 +00:00
else
{
guiManager.InteractVerifier(false);
}
2020-01-31 20:47:30 +00:00
}
}