Add NPC and Power-Up features with associated prefabs and effects
This commit is contained in:
84
Assets/Scripts/Infrastructure/Unity/PowerUpViewAdapter.cs
Normal file
84
Assets/Scripts/Infrastructure/Unity/PowerUpViewAdapter.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using Core.Domain;
|
||||
using Core.Domain.Status.Effects;
|
||||
using KBCore.Refs;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Infrastructure.Unity
|
||||
{
|
||||
public class PowerUpViewAdapter : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private PowerUpType type;
|
||||
[SerializeField] private float duration = 10f;
|
||||
[SerializeField] private ParticleSystem pickupVfx;
|
||||
|
||||
[Self] [SerializeField] private MeshRenderer meshRenderer;
|
||||
|
||||
private MaterialPropertyBlock _propBlock;
|
||||
private static readonly int ColorProperty = Shader.PropertyToID("_BaseColor");
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_propBlock = new MaterialPropertyBlock();
|
||||
ConfigureVisuals();
|
||||
}
|
||||
|
||||
private void ConfigureVisuals()
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case PowerUpType.LightFooted:
|
||||
SetColor(EffectColors.LightFootedColor);
|
||||
break;
|
||||
case PowerUpType.SpeedBoost:
|
||||
SetColor(EffectColors.SpeedBoostColor);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.TryGetComponent<PlayerController>(out var player))
|
||||
{
|
||||
ApplyEffect(player);
|
||||
|
||||
if (pickupVfx)
|
||||
{
|
||||
var vfx = Instantiate(pickupVfx, transform.position, Quaternion.identity);
|
||||
Destroy(vfx.gameObject, 2f);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyEffect(PlayerController player)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case PowerUpType.LightFooted:
|
||||
player.Status.AddEffect(new LightFootedEffect(duration));
|
||||
break;
|
||||
case PowerUpType.SpeedBoost:
|
||||
player.Status.AddEffect(new SpeedBoostEffect(duration, 1.5f));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Configure(PowerUpType newType)
|
||||
{
|
||||
type = newType;
|
||||
}
|
||||
|
||||
private void SetColor(Color color)
|
||||
{
|
||||
meshRenderer.GetPropertyBlock(_propBlock);
|
||||
_propBlock.SetColor(ColorProperty, color);
|
||||
meshRenderer.SetPropertyBlock(_propBlock);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user