Implement enemy spawning and following waypoints
This commit is contained in:
27
Assets/Scripts/Components/AnimatorComponent.cs
Normal file
27
Assets/Scripts/Components/AnimatorComponent.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Components/AnimatorComponent.cs.meta
Normal file
11
Assets/Scripts/Components/AnimatorComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e22e730d77a8fa1488817a22b3acee38
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
76
Assets/Scripts/Components/NavMeshComponent.cs
Normal file
76
Assets/Scripts/Components/NavMeshComponent.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Components/NavMeshComponent.cs.meta
Normal file
11
Assets/Scripts/Components/NavMeshComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3f8cfc01629a2a4d8e8eeba3344c8f8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
14
Assets/Scripts/Components/SpawnComponent.cs
Normal file
14
Assets/Scripts/Components/SpawnComponent.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Components/SpawnComponent.cs.meta
Normal file
11
Assets/Scripts/Components/SpawnComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 435f0c23cdef1c340bb304e83dc5d8a9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user