This commit is contained in:
JonatanRek
2020-02-01 13:36:07 +01:00
27 changed files with 3449 additions and 824 deletions

View File

@@ -4,7 +4,7 @@ using UnityEngine;
public class BarricadeManager : MonoBehaviour
{
public int health = 0;
public float health = 0;
public List<GameObject> barricadePlanks = new List<GameObject>();
// Start is called before the first frame update
@@ -17,6 +17,10 @@ public class BarricadeManager : MonoBehaviour
void Update()
{
viewHealthAsPlanks();
if (health < 0)
{
health = 0;
}
}
public void addPlank()
@@ -26,6 +30,13 @@ public class BarricadeManager : MonoBehaviour
}
}
public void Damage(float damage)
{
if (health > 0) {
health -= damage;
}
}
void viewHealthAsPlanks()
{
if (barricadePlanks.Count > 0)

View File

@@ -4,19 +4,42 @@ using UnityEngine;
public class BlockManager : MonoBehaviour
{
public enum BlockType {None, Barricade};
public enum BlockType {None, Barricade, Door, Wood, Ammo};
public BlockType blockType = BlockType.None;
public float health = 100;
private Animator animator;
private bool action = false;
// Start is called before the first frame update
void Start()
{
if (blockType == BlockType.Door)
{
animator = this.GetComponent<Animator>();
}
}
// Update is called once per frame
void Update()
{
}
public void Action()
{
if (blockType == BlockType.Door)
{
if (!action)
{
this.GetComponent<BoxCollider>().isTrigger = true;
action = true;
}
else
{
this.GetComponent<BoxCollider>().isTrigger = false;
action = false;
}
animator.ResetTrigger("Door");
animator.SetTrigger("Door");
}
}
}

View File

@@ -6,14 +6,19 @@ using UnityEngine.AI;
public class EnemyManager : MonoBehaviour
{
public float health = 100;
public float actualHealth;
public float damage = 10;
public float attackRate = 2;
private float attackTime = 2;
private float actualHealth;
private NavMeshAgent navMeshAgent;
private PlayerManager player;
private BarricadeManager barricadeManager;
public List<GameObject> bodyParts = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
attackTime = attackRate;
actualHealth = health;
player = FindObjectOfType<PlayerManager>();
navMeshAgent = GetComponent<NavMeshAgent>();
@@ -22,34 +27,37 @@ public class EnemyManager : MonoBehaviour
// Update is called once per frame
void Update()
{
navMeshAgent.SetDestination(player.transform.position);
DropBodyPart();
if (actualHealth <= 0)
{
if (bodyParts.Count > 0) {
foreach (GameObject bodyPart in bodyParts)
{
Destroy(bodyPart, 3);
}
}
navMeshAgent.isStopped = true;
navMeshAgent.enabled = false;
gameObject.AddComponent<Rigidbody>();
Rigidbody rigidBodyEnemy = gameObject.GetComponent<Rigidbody>() as Rigidbody;
rigidBodyEnemy.velocity = -gameObject.transform.forward;
rigidBodyEnemy.mass = 50;
Destroy(this.gameObject, 3);
Destroy(this.gameObject);
}
DestroyBarricades();
if (navMeshAgent != null && barricadeManager == null)
{
navMeshAgent.SetDestination(player.transform.position);
}
else if (navMeshAgent != null && barricadeManager != null)
{
navMeshAgent.SetDestination(barricadeManager.transform.position);
}
}
void FixedUpdate()
{
if (actualHealth > 0)
if (actualHealth > 0 && barricadeManager == null)
{
Vector3 _dir = player.transform.position - transform.position;
_dir.Normalize();
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_dir), 5 * Time.deltaTime);
}
else if (actualHealth > 0 && barricadeManager != null)
{
Vector3 _dir = barricadeManager.transform.position - transform.position;
_dir.Normalize();
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_dir), 5 * Time.deltaTime);
}
}
public void Damage()
@@ -60,6 +68,28 @@ public class EnemyManager : MonoBehaviour
}
}
void DestroyBarricades()
{
Debug.Log(navMeshAgent.pathPending);
if (navMeshAgent.pathStatus == NavMeshPathStatus.PathInvalid)
{
BarricadeManager[] barricadeManagers = FindObjectsOfType<BarricadeManager>();
foreach(BarricadeManager localBarricadeManager in barricadeManagers)
{
if (barricadeManager == null)
{
barricadeManager = localBarricadeManager;
}
else
{
if (Vector3.Distance(barricadeManager.transform.position, transform.position) > Vector3.Distance(localBarricadeManager.transform.position, transform.position)) {
barricadeManager = localBarricadeManager;
}
}
}
}
}
void DropBodyPart()
{
if (bodyParts.Count > 0)
@@ -69,11 +99,42 @@ public class EnemyManager : MonoBehaviour
{
if ((health - actualHealth) / number > i)
{
bodyParts[i].transform.parent = null;
bodyParts[i].AddComponent<Rigidbody>();
Rigidbody rigidBodyEnemy = bodyParts[i].GetComponent<Rigidbody>() as Rigidbody;
bodyParts[i].AddComponent<BoxCollider>();
rigidBodyEnemy.mass = 10;
if (bodyParts[i] != null && bodyParts[i].transform.parent != null) {
bodyParts[i].transform.parent = null;
bodyParts[i].AddComponent<Rigidbody>();
Rigidbody rigidBodyEnemy = bodyParts[i].GetComponent<Rigidbody>() as Rigidbody;
bodyParts[i].AddComponent<MeshCollider>();
MeshCollider meshCollider = bodyParts[i].GetComponent<MeshCollider>() as MeshCollider;
meshCollider.convex = true;
if (i == bodyParts.Count - 1) {
navMeshAgent.isStopped = true;
navMeshAgent.enabled = false;
navMeshAgent = null;
rigidBodyEnemy.velocity = bodyParts[i].transform.forward;
}
rigidBodyEnemy.mass = 10 * bodyParts.Count;
Destroy(bodyParts[i], 4);
}
}
}
}
}
void Attack()
{
RaycastHit hit;
if (Physics.Raycast(transform.position + new Vector3(0,1,0),transform.forward,out hit,2) && attackTime <= Time.time)
{
if (hit.transform.CompareTag("Player"))
{
player.Damage(damage);
}
else if (hit.transform.CompareTag("BarricadeField"))
{
BarricadeManager barricadeManager = hit.transform.GetComponent<BarricadeManager>();
if (barricadeManager != null)
{
barricadeManager.Damage(damage);
}
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerManager : MonoBehaviour
{
@@ -10,6 +11,8 @@ public class PlayerManager : MonoBehaviour
public float rotateSpeed = 5;
public float mouseSensitive = 200;
public float buildRate = 0.5f;
public float health = 100;
private float actualHealth = 100;
private float buildTime;
private bool run = false;
private Camera playerCamera;
@@ -23,6 +26,7 @@ public class PlayerManager : MonoBehaviour
// Start is called before the first frame update
void Start()
{
actualHealth = health;
buildTime = buildRate;
playerCamera = GetComponentInChildren<Camera>();
rigidBody = GetComponent<Rigidbody>();
@@ -37,7 +41,12 @@ public class PlayerManager : MonoBehaviour
RunSwitch();
SelectWeapon();
BuildBlock();
BuildBarricade();
Interact();
if (actualHealth <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
}
}
void FixedUpdate()
@@ -54,6 +63,11 @@ public class PlayerManager : MonoBehaviour
);
}
public void Damage(float damage)
{
actualHealth -= damage;
}
void Rotate()
{
rigidBody.freezeRotation = false;
@@ -162,7 +176,7 @@ public class PlayerManager : MonoBehaviour
}
}
void BuildBarricade()
void Interact()
{
Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
@@ -175,6 +189,11 @@ public class PlayerManager : MonoBehaviour
//barricadeMaterials--; //než bude jistota že uděláme zbírání materiálu
buildTime = buildRate + Time.time;
}
if (Input.GetAxisRaw("Build") > 0 && hit.transform.tag == "Interact")
{
BlockManager blockManager = hit.transform.GetComponent<BlockManager>();
blockManager.Action();
}
}
}
}