Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created March 20, 2020 13:25
Show Gist options
  • Select an option

  • Save jeffvella/7fde48dd2582db7820354cc3e02f73bb to your computer and use it in GitHub Desktop.

Select an option

Save jeffvella/7fde48dd2582db7820354cc3e02f73bb to your computer and use it in GitHub Desktop.
public class ScoreSystem : SystemBase
{
public const int BaseScoreMultiplier = 10;
private EventQueue<ScoreAddedEvent> _addScoreQueue;
protected override void OnCreate()
{
_addScoreQueue = World.GetOrCreateSystem<EntityEventSystem>().GetQueue<ScoreAddedEvent>();
}
protected override void OnUpdate()
{
var events = _addScoreQueue;
Entities.ForEach((Entity entity, MatchedEvent e, DynamicBuffer<SelectionBufferData> selectionData) =>
{
events.Enqueue(new ScoreAddedEvent
{
Source = ScoreSource.Match,
Value = (int)CalculateScore(selectionData),
Origin = selectionData.Last().Position,
ChainSize = selectionData.Length
});
}).Schedule();
Entities.ForEach((Entity entity, ExplosionEvent e, DynamicBuffer<ExplosionHitBufferData> explosionData) =>
{
events.Enqueue(new ScoreAddedEvent
{
Source = ScoreSource.Explosion,
Value = (int)CalculateScore(explosionData),
Origin = e.Origin,
ChainSize = explosionData.Length,
});
}).Schedule();
}
private static float CalculateScore<T>(DynamicBuffer<T> matches) where T : struct, IBlockDefinition
{
var score = 0f;
for (int i = 0; i < matches.Length; i++)
{
var m = i * matches[i].BlockDefinition.ValueMultiplier;
score += m * (1 + BaseScoreMultiplier) * m;
}
return score;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment