Net.Like.Xue.Tokyo/Assets/BITKit/Unity/Scripts/VFX/VFXService.cs

64 lines
1.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using BITKit.Entities;
using Net.BITKit.Impact;
using UnityEngine;
namespace Net.BITKit.VFX
{
public class VFXService
{
private readonly IEntitiesService _entitiesService;
private readonly IReadOnlyCollection<ScriptableImpact> _scriptableImpacts;
public VFXService(IEntitiesService entitiesService)
{
_entitiesService = entitiesService;
_scriptableImpacts = _entitiesService.QueryComponents<ScriptableImpact>().ToArray();
}
public AudioClip GetAudioClip(IReadOnlyCollection<string> tags)
{
var bestMatches = Search(tags);
return bestMatches.AudioClip;
}
public Transform GetPrefab(IReadOnlyCollection<string> tags)
{
var bestMatches = Search(tags);
return bestMatches.Prefab;
}
private ScriptableImpact Search(IReadOnlyCollection<string> tags)
{
var maxPoint = 0;
var max = new HashSet<ScriptableImpact>();
foreach (var impact in _scriptableImpacts)
{
var point = impact.Tags.Intersect(tags).Count();
if (point > maxPoint)
{
maxPoint = point;
max.Clear();
max.Add(impact);
}else if (point == maxPoint)
{
max.Add(impact);
}
}
if (max.Count is 1)
{
return max.First();
}
return max.OrderByDescending(x => x.Priority).First();
}
}
}