Add NPC and Power-Up features with associated prefabs and effects
This commit is contained in:
66
Assets/Scripts/Infrastructure/Unity/NpcController.cs
Normal file
66
Assets/Scripts/Infrastructure/Unity/NpcController.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using KBCore.Refs;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Infrastructure.Unity
|
||||
{
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class NpcController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float moveSpeed = 6f;
|
||||
[SerializeField] private float changeDirInterval = 1f;
|
||||
[SerializeField] private LayerMask tileLayer;
|
||||
|
||||
[Self] [SerializeField] private Rigidbody rb;
|
||||
|
||||
private Vector3 _currentDir;
|
||||
private float _timer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
PickNewDirection();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_timer += Time.deltaTime;
|
||||
if (_timer >= changeDirInterval)
|
||||
{
|
||||
_timer = 0;
|
||||
PickNewDirection();
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (IsGrounded()) rb.MovePosition(rb.position + _currentDir * (moveSpeed * Time.fixedDeltaTime));
|
||||
|
||||
if (Physics.Raycast(transform.position, Vector3.down, out var hit, 1.5f, tileLayer))
|
||||
{
|
||||
if (hit.collider.TryGetComponent<TileViewAdapter>(out var tile))
|
||||
{
|
||||
tile.OnPlayerStep();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsGrounded()
|
||||
{
|
||||
return Physics.Raycast(transform.position, Vector3.down, out var hit, 1.15f, tileLayer);
|
||||
}
|
||||
|
||||
private void PickNewDirection()
|
||||
{
|
||||
var rand = Random.Range(0, 4);
|
||||
switch (rand)
|
||||
{
|
||||
case 0: _currentDir = Vector3.forward; break;
|
||||
case 1: _currentDir = Vector3.back; break;
|
||||
case 2: _currentDir = Vector3.left; break;
|
||||
case 3: _currentDir = Vector3.right; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user