Implement enemy spawning and following waypoints

This commit is contained in:
2024-08-12 14:21:11 +02:00
parent 5104b336ca
commit 3f715b2bbc
44 changed files with 1894 additions and 52 deletions

View File

@@ -0,0 +1,27 @@
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class AnimatorComponent : MonoBehaviour
{
[SerializeField]
[ReadOnly]
private Animator animator;
[SerializeField]
[ReadOnly]
private float velocity;
private void Awake()
{
animator = GetComponent<Animator>();
}
public void UpdateVelocity(float velocity)
{
this.velocity = velocity;
animator.SetFloat("Velocity", velocity);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e22e730d77a8fa1488817a22b3acee38
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -7,6 +7,11 @@ using UnityEngine.EventSystems;
public class HealthComponent : MonoBehaviour
{
[SerializeField]
[Required]
[OnValueChanged("Setup")]
private Enemy enemy;
[ReadOnly]
[SerializeField]
private int health;
@@ -20,7 +25,7 @@ public class HealthComponent : MonoBehaviour
[Button]
public void Setup()
{
health = 100;
health = enemy.health;
isDead = false;
}

View File

@@ -0,0 +1,76 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using UnityEngine.AI;
using UnityEngine.Events;
[RequireComponent(typeof(NavMeshAgent))]
public class NavMeshComponent : MonoBehaviour
{
[SerializeField]
[ReadOnly]
public int currentWaypointIndex = 0;
[SerializeField]
[ReadOnly]
public WaypointsGroup waypointGroup;
[SerializeField]
[ReadOnly]
private NavMeshAgent agent;
[SerializeField]
private UnityEvent<float> onVelocityChanged;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
public void MoveTo(WaypointsGroup waypointGroup)
{
this.waypointGroup = waypointGroup;
MoveToNext();
}
private void MoveToNext()
{
currentWaypointIndex++;
agent.destination = waypointGroup.waypoints[currentWaypointIndex]
.GetPosition();
}
private void Update()
{
var velocity = agent.velocity.magnitude / agent.speed;
onVelocityChanged.Invoke(velocity);
if (waypointGroup == null)
return;
if (ReachedDestinationOrGaveUp())
{
if (currentWaypointIndex != waypointGroup.waypoints.Count - 1)
{
MoveToNext();
}
}
}
private bool ReachedDestinationOrGaveUp()
{
if (agent.pathPending)
return false;
if (agent.remainingDistance > agent.stoppingDistance)
return false;
if (!(!agent.hasPath || agent.velocity.sqrMagnitude == 0f))
return false;
return true;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3f8cfc01629a2a4d8e8eeba3344c8f8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnComponent : MonoBehaviour
{
public void Spawn(SpawnInformation info)
{
var gameObject = Instantiate(info.prefab, transform);
gameObject.transform.position = info.position;
gameObject.name = info.prefab.name;
info.onSpawned.Invoke(gameObject);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 435f0c23cdef1c340bb304e83dc5d8a9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: