Skip to content

Commit

Permalink
one more test and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
myarichuk committed Sep 22, 2021
1 parent 0f7e3f0 commit e5e21b8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
5 changes: 0 additions & 5 deletions RoguelikeToolkit.Scripts.Tests/HealthComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
public class HealthComponent
{
public double Health;

public HealthComponent(double health)
{
Health = health;
}
}

public struct HealthComponent2
Expand Down
39 changes: 18 additions & 21 deletions RoguelikeToolkit.Scripts.Tests/ScriptBasics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,31 +104,28 @@ public void EntityComponentScript_with_struct_should_work()
Assert.Equal(456, c.Health);
}

//[Fact]
//public async Task EntityInteractionScript_should_work()
//{
// var caster = _world.CreateEntity();
// caster.Set(new HealthComponent(50.0));

// var target = _world.CreateEntity();
// target.Set(new HealthComponent(100.0));
[Fact]
public void EntityInteractionScript_should_work()
{
var caster = _world.CreateEntity();
caster.Set(new HealthComponent { Health = 50.0 });

// var healthStealSpell = new EntityInteractionScript(
// @"
// var sourceHealth = source.Get<HealthComponent>();
// var targetHealth = target.Get<HealthComponent>();
var target = _world.CreateEntity();
target.Set(new HealthComponent2 { Health = 100.0 });

// sourceHealth.Health += 50;
// targetHealth.Health -= 50;
// ", Assembly.GetExecutingAssembly());
var healthStealSpell = new EntityInteractionScript(
@"
source.Health += 50;
target.Health -= 50;
");

// await healthStealSpell.RunAsyncOn(caster, target);
healthStealSpell.ExecuteOn<HealthComponent, HealthComponent2>(caster, target);

// var sourceHealth = caster.Get<HealthComponent>();
// var targetHealth = target.Get<HealthComponent>();
var sourceHealth = caster.Get<HealthComponent>();
var targetHealth = target.Get<HealthComponent2>();

// Assert.Equal(100.0, sourceHealth.Health);
// Assert.Equal(50.0, targetHealth.Health);
//}
Assert.Equal(100.0, sourceHealth.Health);
Assert.Equal(50.0, targetHealth.Health);
}
}
}
24 changes: 24 additions & 0 deletions RoguelikeToolkit.Scripts/EntityInteractionScript.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DefaultEcs;

namespace RoguelikeToolkit.Scripts
{
public class EntityInteractionScript
{
private readonly InteractionScript _script;

public EntityInteractionScript(string script, string sourceInstanceName = null, string targetInstanceName = null) =>
_script = new InteractionScript(script, sourceInstanceName, targetInstanceName);

public void ExecuteOn<TSourceComponent, TTargetComponent>(in Entity sourceEntity, in Entity targetEntity)
{
if (!sourceEntity.Has<TSourceComponent>() ||
!targetEntity.Has<TTargetComponent>())
return;

ref var source = ref sourceEntity.Get<TSourceComponent>();
ref var target = ref targetEntity.Get<TTargetComponent>();

_script.ExecuteOn(ref source, ref target);
}
}
}

0 comments on commit e5e21b8

Please sign in to comment.