riversong code showcase
This commit is contained in:
40
Source/Riversong/Game/Vfx/AutoDestroyVfx.cs
Normal file
40
Source/Riversong/Game/Vfx/AutoDestroyVfx.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.VFX;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[RequireComponent(typeof(VisualEffect))]
|
||||
public class AutoDestroyVfx : MonoBehaviour
|
||||
{
|
||||
private const float DestroyDelayWhenNoParticles = 1;
|
||||
|
||||
private VisualEffect _vfx;
|
||||
|
||||
private bool _emittedOnce;
|
||||
|
||||
private float _timer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_vfx = GetComponent<VisualEffect>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_vfx) return;
|
||||
|
||||
_emittedOnce |= _vfx.aliveParticleCount > 0;
|
||||
if (!_emittedOnce) return;
|
||||
|
||||
if (_vfx.aliveParticleCount > 0)
|
||||
{
|
||||
_timer = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
_timer += Time.unscaledDeltaTime;
|
||||
|
||||
if (_timer >= DestroyDelayWhenNoParticles) Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Source/Riversong/Game/Vfx/DustVfxProperties.cs
Normal file
18
Source/Riversong/Game/Vfx/DustVfxProperties.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.VFX;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public static class DustVfxProperties
|
||||
{
|
||||
public static readonly int IntensityID = Shader.PropertyToID("Intensity");
|
||||
|
||||
public static readonly int SizeID = Shader.PropertyToID("Size");
|
||||
|
||||
public static void Setup(VisualEffect vfx, float intensity, Vector2 size)
|
||||
{
|
||||
vfx.SetFloat(IntensityID, intensity);
|
||||
vfx.SetVector2(SizeID, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Source/Riversong/Game/Vfx/IProjectileManager.cs
Normal file
9
Source/Riversong/Game/Vfx/IProjectileManager.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public interface IProjectileManager
|
||||
{
|
||||
float ComputeProjectileTravelTime(Agent source, Agent target);
|
||||
|
||||
void FireProjectile(Agent source, Agent target);
|
||||
}
|
||||
}
|
||||
61
Source/Riversong/Game/Vfx/ProjectileManagerSystem.cs
Normal file
61
Source/Riversong/Game/Vfx/ProjectileManagerSystem.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[Service(typeof(IProjectileManager))]
|
||||
public class ProjectileManagerSystem : GameSystem, IProjectileManager
|
||||
{
|
||||
private const float ProjectileSpeed = 4;
|
||||
|
||||
[InjectService]
|
||||
private IIntentLogicExecutor _intentLogicExecutor;
|
||||
|
||||
public ProjectileManagerSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public float ComputeProjectileTravelTime(Agent source, Agent target)
|
||||
{
|
||||
return math.distance(source.Position, target.Position) / ProjectileSpeed;
|
||||
}
|
||||
|
||||
public void FireProjectile(Agent source, Agent target)
|
||||
{
|
||||
_ = FireProjectileAsync(source, target, ComputeProjectileTravelTime(source, target));
|
||||
}
|
||||
|
||||
private async UniTask FireProjectileAsync(Agent source, Agent target, float travelTime)
|
||||
{
|
||||
var prefabRef = source.Definition.Projectile;
|
||||
var projectile = await prefabRef.InstantiateAsync();
|
||||
|
||||
var p0 = (Vector3)source.Position;
|
||||
var p1 = (Vector3)target.Position;
|
||||
|
||||
// const float peakHeightPerMeter = 0.1f;
|
||||
// var peak = Vector3.Distance(p0, p1) * peakHeightPerMeter;
|
||||
|
||||
var t = 0f;
|
||||
while (t < 1)
|
||||
{
|
||||
t = Mathf.Clamp01(t + Time.deltaTime / travelTime);
|
||||
|
||||
var position = Vector3.Lerp(p0, p1, t);
|
||||
//position.y += 4 * peak * t * (1 - t);
|
||||
|
||||
var rotation = Quaternion.LookRotation((position - projectile.transform.position).normalized);
|
||||
|
||||
projectile.transform.SetPositionAndRotation(position, rotation);
|
||||
|
||||
await UniTask.NextFrame();
|
||||
}
|
||||
|
||||
Addressables.ReleaseInstance(projectile);
|
||||
|
||||
_intentLogicExecutor.CancelRemainingIntents(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user