Skip to content

Commit

Permalink
entity interaction component works + relevant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
myarichuk committed Sep 25, 2021
1 parent 9e37ecb commit 129a5e6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 46 deletions.
50 changes: 25 additions & 25 deletions RoguelikeToolkit.Entities.Tests/EntityFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,39 @@ public void Can_build_with_scipt()
Assert.NotEqual(0, entity.Get<ActionChanceComponent>().Result);
}

//[Fact]
//public async Task Can_build_with_component_scipt()
//{
// Assert.True(_entityFactory.TryCreateEntity("ActorWithScripts", out var entity));
[Fact]
public void Can_build_with_component_scipt()
{
Assert.True(_entityFactory.TryCreateEntity("ActorWithScripts", out var entity));

// await entity.RunScriptAsync<ActionChanceComponent>(c => c.ActionScript);
entity.ExecuteScriptFrom<ActionChanceComponent>(c => c.ActionScript);

// Assert.NotEqual(0, entity.Get<ActionChanceComponent>().Result);
//}
Assert.NotEqual(0, entity.Get<ActionChanceComponent>().Result);
}

//[Fact]
//public async Task Can_build_with_interaction_scipt()
//{
// Assert.True(_entityFactory.TryCreateEntity("actorWithAbility", out var player));
// Assert.True(_entityFactory.TryCreateEntity("object2", out var enemy));
[Fact]
public void Can_build_with_interaction_scipt()
{
Assert.True(_entityFactory.TryCreateEntity("actorWithAbility", out var player));
Assert.True(_entityFactory.TryCreateEntity("object2", out var enemy));

// Assert.Equal(100.0, enemy.Get<HealthComponent>().Value);
// await player.RunScriptAsync<KickAbility>(enemy, c => c.Effect);
// Assert.NotEqual(100.0, enemy.Get<HealthComponent>().Value);
Assert.Equal(100.0, enemy.Get<HealthComponent>().Value);
player.ExecuteScriptFrom<KickAbility>(ref enemy, c => c.Effect);
Assert.NotEqual(100.0, enemy.Get<HealthComponent>().Value);

//}
}

//[Fact]
//public async Task Can_build_with_interaction_scipt_from_file()
//{
// Assert.True(_entityFactory.TryCreateEntity("actorWithAbility2", out var player));
// Assert.True(_entityFactory.TryCreateEntity("object2", out var enemy));
[Fact]
public void Can_build_with_interaction_scipt_from_file()
{
Assert.True(_entityFactory.TryCreateEntity("actorWithAbility2", out var player));
Assert.True(_entityFactory.TryCreateEntity("object2", out var enemy));

// Assert.Equal(100.0, enemy.Get<HealthComponent>().Value);
// await player.RunScriptAsync<KickAbility>(enemy, c => c.Effect);
// Assert.NotEqual(100.0, enemy.Get<HealthComponent>().Value);
Assert.Equal(100.0, enemy.Get<HealthComponent>().Value);
player.ExecuteScriptFrom<KickAbility>(ref enemy, c => c.Effect);
Assert.NotEqual(100.0, enemy.Get<HealthComponent>().Value);

//}
}

[Fact]
public void Can_build_from_template_with_converting_strings_to_numbers()
Expand Down
22 changes: 11 additions & 11 deletions RoguelikeToolkit.Entities.Tests/KickAbility.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//using RoguelikeToolkit.DiceExpression;
//using RoguelikeToolkit.Scripts;
using RoguelikeToolkit.DiceExpression;
using RoguelikeToolkit.Scripts;

//namespace RoguelikeToolkit.Entities.Tests
//{
// [Component(Name = "KickAbility")]
// public struct KickAbility
// {
// public Dice Strength { get; set; }
namespace RoguelikeToolkit.Entities.Tests
{
[Component(Name = "KickAbility")]
public struct KickAbility
{
public Dice Strength { get; set; }

// public EntityInteractionScript Effect { get; set; }
// }
//}
public EntityInteractionScript Effect { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var health = target.Get<HealthComponent>();
var kickStrength = source.Get<KickAbility>().Strength;
var health = target.GetComponent(HealthComponent);
var kickStrength = source.GetComponent(KickAbility).Strength;

health.Value -= kickStrength.Roll();
target.Set(health);
target.SetComponent(HealthComponent, health);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Components": {
"KickAbility": {
"Strength": "2d6+3",
"Effect": "var targetHealth = target.Get<HealthComponent>(); var kickStrength = source.Get<KickAbility>().Strength; targetHealth.Value -= kickStrength.Roll(); target.Set(targetHealth);"
"Effect": "var targetHealth = target.GetComponent(HealthComponent); var kickStrength = source.GetComponent(KickAbility).Strength; targetHealth.Value -= kickStrength.Roll(); target.SetComponent(HealthComponent, targetHealth);"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace RoguelikeToolkit.Entities.Components.PropertyMappers
public class StringToComponentScript : IPropertyMapper
{
private readonly static ConcurrentDictionary<string, EntityComponentScript> ScriptCache = new();
private static Assembly[] ComponentAssemblyCache;

public int Priority => 5;

Expand Down
20 changes: 20 additions & 0 deletions RoguelikeToolkit.Entities/Extensions/EntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ public static void ExecuteScriptFrom<TComponent>(this ref Entity entity, Func<TC
script.ExecuteOn(ref entity, @params);
}

public static void ExecuteScriptFrom<TComponent>(this ref Entity entity, Func<TComponent, EntityComponentScript> scriptSelector, params (string instanceName, dynamic value)[] @params)
{
if (!entity.Has<TComponent>())
return;

var script = scriptSelector(entity.Get<TComponent>());

script.TryExecuteOn<TComponent>(ref entity, @params);
}

public static void ExecuteScriptFrom<TComponent>(this ref Entity source, ref Entity target, Func<TComponent, EntityInteractionScript> scriptSelector, params (string instanceName, dynamic value)[] @params)
{
if (!source.Has<TComponent>())
return;

var script = scriptSelector(source.Get<TComponent>());

script.ExecuteOn(ref source, ref target, @params);
}

public static string Id(this Entity entity) =>
entity.TryGet<IdComponent>(out var id) ? id.Value : null;

Expand Down
8 changes: 4 additions & 4 deletions RoguelikeToolkit.Scripts.Tests/ScriptBasics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void EntityComponentScript_should_work()

var changeScript = new EntityComponentScript(@"component.RollResult = component.Dice1.Roll();");

Assert.True(changeScript.TryExecuteOn<TestComponent>(entity));
Assert.True(changeScript.TryExecuteOn<TestComponent>(ref entity));

Assert.NotEqual(0, c.RollResult);
}
Expand All @@ -121,7 +121,7 @@ public void EntityComponentScript_without_specific_component_should_not_continue

var changeScript = new EntityComponentScript(@"throw 'this is an error!';");

Assert.False(changeScript.TryExecuteOn<TestComponent>(entity)); //this shouldn't throw since there is no such component...
Assert.False(changeScript.TryExecuteOn<TestComponent>(ref entity)); //this shouldn't throw since there is no such component...
}

[Fact]
Expand All @@ -132,7 +132,7 @@ public void EntityComponentScript_should_propagate_exceptions()
var changeScript = new EntityComponentScript(@"throw 'this is an error!';");
entity.Set(new TestComponent { Dice1 = Dice.Parse("2d+5") });

Assert.Throws<JavaScriptException>(() => changeScript.TryExecuteOn<TestComponent>(entity));
Assert.Throws<JavaScriptException>(() => changeScript.TryExecuteOn<TestComponent>(ref entity));
}


Expand All @@ -148,7 +148,7 @@ public void EntityComponentScript_with_struct_should_work()
Assert.Equal(123, c.Health);

var changeScript = new EntityComponentScript(@"component.Health = 456;");
Assert.True(changeScript.TryExecuteOn<HealthComponent2>(entity));
Assert.True(changeScript.TryExecuteOn<HealthComponent2>(ref entity));

c = entity.Get<HealthComponent2>();
Assert.Equal(456, c.Health);
Expand Down
2 changes: 1 addition & 1 deletion RoguelikeToolkit.Scripts/EntityComponentScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class EntityComponentScript
public EntityComponentScript(string script, params Type[] referenceTypes) =>
_script = new Script(script, referenceTypes);

public bool TryExecuteOn<TComponent>(in Entity entity, params (string instanceName, dynamic value)[] @params)
public bool TryExecuteOn<TComponent>(ref Entity entity, params (string instanceName, dynamic value)[] @params)
{
if(!entity.Has<TComponent>())
return false;
Expand Down

0 comments on commit 129a5e6

Please sign in to comment.