add traits

This commit is contained in:
Nilton Constantino 2026-06-12 15:47:20 +01:00
parent 748fdc3cb0
commit 73a23861a4
No known key found for this signature in database
16 changed files with 224 additions and 4 deletions

View File

@ -0,0 +1,7 @@
namespace Intrepid.Core.Grounding
{
public class MovingGroundSurface : GroundSurface
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 488d97fcd10d484b8d9e50393014a05c
timeCreated: 1781271534

View File

@ -1,3 +1,4 @@
using Intrepid.Diagnostics;
using Intrepid.Structures;
using Intrepid.Utilities;
using UnityEngine;
@ -11,6 +12,7 @@ namespace Intrepid.Gameplay.Entities.Player.Sentinel.StateMachine.States
[GroupConfigurations, SerializeField] private float KnockoutTimeMultiplier = 0.1f;
[GroupConfigurations, SerializeField, Range(0, 3)] private float knockdownTime = .1f;
[GroupConfigurations, SerializeField, Range(0, 3)] private float recoverTime = .5f;
[GroupConfigurations, SerializeField] private float maxKnockdown = 15f;
private Vector2 KnockdownForce { get; set; }
private float KnockoutTime { get; set; }
@ -33,7 +35,7 @@ namespace Intrepid.Gameplay.Entities.Player.Sentinel.StateMachine.States
KnockdownForce = ps.Get<Vector2>(SentinelConstants.Parameters.KnockdownForce);
Brain.Entity.ChangeOrientation(-KnockdownForce.normalized);
KnockoutTime = KnockdownForce.sqrMagnitude * KnockoutTimeMultiplier;
Brain.Entity.DesiredVelocity = KnockdownForce;
Brain.Entity.DesiredVelocity = Vector2.ClampMagnitude(KnockdownForce, maxKnockdown);
KnockdownTimer.Start();
KnockoutTimer.Stop();
RecoverTimer.Stop();

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d317b4205069414bab2594af1367a7cd
timeCreated: 1781273885

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 036b53ea50a747568a81067ed062ab63
timeCreated: 1781273890

View File

@ -0,0 +1,8 @@
namespace Intrepid.Utilities.Structures.Traits
{
public interface IMutableTraits<in T> where T : ITrait
{
void Declare<TT>(TT trait) where TT : T;
bool Discard<TT>() where TT : T;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 60e2f3c521b5464c99572ec85a79b49c
timeCreated: 1781274068

View File

@ -0,0 +1,16 @@
using System.Collections.Generic;
using Intrepid.Structures;
namespace Intrepid.Utilities.Structures.Traits
{
public interface IReadOnlyTraits<T> where T : ITrait
{
IEnumerable<SKey> TraitKeys { get; }
IEnumerable<T> AllTraits { get; }
public bool Has<TT>() where TT : T;
bool HasNot<TT>() where TT : T;
int Count<TT>() where TT : T;
int Count();
bool TryGet<TT>(out TT trait) where TT : T;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a6152bfba97d417aa80a378af0f845b6
timeCreated: 1755750583

View File

@ -0,0 +1,9 @@
using Intrepid.Structures;
namespace Intrepid.Utilities.Structures.Traits
{
public interface ITrait
{
public SKey Key { get; }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: df822f6ba3df45d59124ca029e2bfa68
timeCreated: 1755154881

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Intrepid.Structures;
namespace Intrepid.Utilities.Structures.Traits
{
public sealed class Traits<T> : IReadOnlyTraits<T>, IMutableTraits<T> where T : ITrait
{
private Dictionary<string, T> Holder { get; } = new();
private static string KeyFor<P>() => typeof(P).AssemblyQualifiedName;
public IEnumerable<SKey> TraitKeys => Holder.Values.Select(v => v.Key);
public IEnumerable<Type> TraitTypes => AllTraits.Select(t => t.GetType());
public IEnumerable<T> AllTraits => Holder.Values;
public TraitsQuery<T> Querying() => new(this);
public void Declare<TT>(TT trait) where TT : T
{
Holder[KeyFor<TT>()] = trait;
}
public bool Discard<TT>() where TT : T => Holder.Remove(KeyFor<TT>());
public bool Has<TT>() where TT : T => Holder.ContainsKey(KeyFor<TT>());
public bool HasNot<TT>() where TT : T => !Has<TT>();
public int Count<TT>() where TT : T => AllTraits.Count(t => t is TT);
public int Count() => AllTraits.Count();
public bool TryGet<TT>(out TT trait) where TT : T
{
if (Holder.TryGetValue(KeyFor<TT>(), out var t))
{
trait = (TT)t;
return true;
}
trait = default;
return false;
}
public bool TryFindFirst<TT>(out TT tt) where TT : T
{
if (AllTraits.Any(t => t is TT))
{
tt = (TT)AllTraits.First(t => t is TT);
return true;
}
tt = default;
return false;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: da823f94589947afa622262ed259dc94
timeCreated: 1755154734

View File

@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Intrepid.Utilities.Structures.Traits
{
public sealed class TraitsQuery<T> where T : ITrait
{
public IReadOnlyTraits<T> Source { get; }
private List<Func<IEnumerable<T>, IEnumerable<T>>> Operations { get; } = new();
private Func<T, float> OrderKey { get; set; }
private bool OrderDesc { get; set; } = true;
private int? _skip;
private int? _take;
public TraitsQuery(IReadOnlyTraits<T> source)
{
Source = source;
}
public TraitsQuery<T> Has<TT>() where TT : T
{
Operations.Add(seq => seq.Where(_ => Source.Has<TT>()));
return this;
}
public TraitsQuery<T> Where<TT>(Func<TT, bool> filter = null) where TT : T
{
filter ??= _ => true;
Operations.Add(seq => seq.Where(_ => Source.TryGet<TT>(out var tt) && filter(tt)));
return this;
}
public TraitsQuery<T> OrderBy(Func<T, float> keySelector, bool descending = true)
{
OrderKey = keySelector;
OrderDesc = descending;
return this;
}
public TraitsQuery<T> Skip(int count)
{
if (count < 0)
{
return this;
}
_skip = count;
return this;
}
public TraitsQuery<T> Take(int count)
{
if (count < 0)
{
return this;
}
_take = count;
return this;
}
public IReadOnlyList<T> ToList() => Materialize().ToList();
public T FirstOrDefault() => Materialize().FirstOrDefault();
public bool Any() => Materialize().Any();
public bool TryGet<TT>(out TT trait) where TT : T
{
return Source.TryGet(out trait);
}
private IEnumerable<T> Materialize()
{
var query = Source.AllTraits;
foreach (var operation in Operations)
{
query = operation(query);
}
if (OrderKey is not null)
{
query = OrderDesc
? query.OrderByDescending(OrderKey)
: query.OrderBy(OrderKey);
}
if (_skip is > 0)
{
query = query.Skip(_skip.Value);
}
if (_take is > 0)
{
query = query.Take(_take.Value);
}
return query;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e35096794b4c41948c8c03c8009b7322
timeCreated: 1755757808

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 950aecf629064624ba1c60498dddcb8d
timeCreated: 1779168408