Refactor NPC behavior and teleportation logic; enhance tile material properties

This commit is contained in:
2025-12-13 01:16:59 +01:00
parent 53de85a269
commit 189bbb7ae7
7 changed files with 66 additions and 22 deletions

View File

@@ -7,7 +7,7 @@ namespace Core.Domain
public class GameSession
{
private const string HighScoreKey = "HighScore";
private const float NpcSpawnTime = 4f;
private const float NpcSpawnTime = 30f;
private const float PowerUpSpawnInterval = 25f;
public int Score { get; private set; }

View File

@@ -243,7 +243,7 @@ namespace Infrastructure.Unity
if (_playerInstance && hunterNpcPrefab && Random.value < 0.3f)
{
var hunter = Instantiate(hunterNpcPrefab, spawnPos, Quaternion.identity);
hunter.Initialize(_playerInstance);
hunter.Initialize(_playerInstance, () => _gameSession.EndGame(), () => _gameSession.TimeDilation);
}
else if (npcPrefab)
{

View File

@@ -1,3 +1,4 @@
using System;
using UnityEngine;
using KBCore.Refs;
@@ -12,28 +13,32 @@ namespace Infrastructure.Unity
[Self] [SerializeField] private Rigidbody rb;
private PlayerController _target;
private Action _onCaughtCallback;
private Func<float> _timeDilationProvider;
public void Initialize(PlayerController target)
public void Initialize(PlayerController target, Action onCaughtCallback, Func<float> timeDilationProvider)
{
_target = target;
_onCaughtCallback = onCaughtCallback;
_timeDilationProvider = timeDilationProvider;
}
private void FixedUpdate()
{
if (_target == null) return;
if (!_target) return;
var dilation = _timeDilationProvider?.Invoke() ?? 1f;
if (IsGrounded())
{
var direction = (_target.transform.position - transform.position).normalized;
// Flatten direction to avoid flying/digging
direction.y = 0;
direction.Normalize();
rb.MovePosition(rb.position + direction * (moveSpeed * Time.fixedDeltaTime));
rb.MovePosition(rb.position + direction * (moveSpeed * dilation * Time.fixedDeltaTime));
}
// Trigger tiles
if (Physics.Raycast(transform.position, Vector3.down, out var hit, 1.5f, tileLayer))
if (Physics.Raycast(transform.position, Vector3.down, out var hit, 2f, tileLayer))
{
if (hit.collider.TryGetComponent<TileViewAdapter>(out var tile))
{
@@ -44,15 +49,25 @@ namespace Infrastructure.Unity
private bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, out var hit, 1.15f, tileLayer);
return Physics.Raycast(transform.position, Vector3.down, out var hit, 2f, tileLayer);
}
private void OnDrawGizmos()
{
if (_target)
if (!_target) return;
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, _target.transform.position);
}
private void OnCollisionEnter(Collision other)
{
if (!_target) return;
if (other.gameObject == _target.gameObject)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, _target.transform.position);
_onCaughtCallback?.Invoke();
Destroy(gameObject);
}
}
}

View File

@@ -19,6 +19,7 @@ namespace Infrastructure.Unity
[SerializeField] private float floorHeightDistance = 15f;
[SerializeField] private float decayTime = 0.5f;
[SerializeField] private float fallingTime = 2.0f;
[SerializeField] private float minimumDistanceBetweenTeleporters = 3f;
public float FloorHeightDistance => floorHeightDistance;
public int FloorsCount => floorsCount;
@@ -72,6 +73,24 @@ namespace Infrastructure.Unity
var posA = new Vector3(spotA.x - xOffset, yOffset + 0.6f, spotA.y - zOffset);
var posB = new Vector3(spotB.x - xOffset, yOffset + 0.6f, spotB.y - zOffset);
// check if positions are not too close
if (Vector3.Distance(posA, posB) < minimumDistanceBetweenTeleporters)
{
// find a new position for teleporter B
for (var i = 0; i < coordinates.Count; i++)
{
if (i == indexA) continue;
var newSpotB = coordinates[i];
var newPosB = new Vector3(newSpotB.x - xOffset, yOffset + 0.6f, newSpotB.y - zOffset);
if (Vector3.Distance(posA, newPosB) >= minimumDistanceBetweenTeleporters)
{
posB = newPosB;
break;
}
}
}
var telA = Instantiate(teleporterPrefab, posA, Quaternion.identity, transform);
var telB = Instantiate(teleporterPrefab, posB, Quaternion.identity, transform);

View File

@@ -17,6 +17,12 @@ namespace Infrastructure.Unity
private Vector3 _currentDir;
private float _timer;
private Func<float> _timeDilationProvider;
public void Initialize(Func<float> timeDilationProvider)
{
_timeDilationProvider = timeDilationProvider;
}
private void Awake()
{
@@ -35,9 +41,11 @@ namespace Infrastructure.Unity
private void FixedUpdate()
{
if (IsGrounded()) rb.MovePosition(rb.position + _currentDir * (moveSpeed * Time.fixedDeltaTime));
var dilation = _timeDilationProvider?.Invoke() ?? 1f;
if (Physics.Raycast(transform.position, Vector3.down, out var hit, 1.5f, tileLayer))
if (IsGrounded()) rb.MovePosition(rb.position + _currentDir * (moveSpeed * dilation * Time.fixedDeltaTime));
if (Physics.Raycast(transform.position, Vector3.down, out var hit, 2.0f, tileLayer))
{
if (hit.collider.TryGetComponent<TileViewAdapter>(out var tile))
{
@@ -48,7 +56,7 @@ namespace Infrastructure.Unity
private bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, out var hit, 1.15f, tileLayer);
return Physics.Raycast(transform.position, Vector3.down, out var hit, 2.0f, tileLayer);
}
private void PickNewDirection()