Created
March 31, 2020 14:32
-
-
Save korchoon/2ceaa4cfe70f497d2f2ddf2ad64345aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ShotView : MonoBehaviour { | |
public Transform HoleRoot; | |
public Renderer SubtractRenderer; | |
public AnimationCurve ScaleXY; | |
public AnimationCurve ScaleZ; | |
public float TimeLen = 0.3f; | |
public void Render(CompShotData data) { | |
var linear = data.Progress; | |
var xy = ScaleXY.Evaluate(linear); | |
transform.SetPose(data.Pose); | |
HoleRoot.localScale = new Vector3(xy, xy, ScaleZ.Evaluate(linear)); | |
} | |
} | |
class CompShotData : IEcsAutoReset { | |
public float EndTime; | |
public float Progress; | |
public Pose Pose; | |
public void Reset() { | |
EndTime = default; | |
Progress = default; | |
Pose = default; | |
} | |
} | |
partial class Dependency { | |
public EcsFilter<CompSubtractData, CompWrap<ShotView>> ShotViews; | |
public EcsFilter<CompShotData> ShotData; | |
} | |
class ShotsRender : MonoBehaviour { | |
public ShotView Prefab; | |
[OnValueChanged(nameof(InitRpm))] | |
public int RPM = 30; | |
[OnValueChanged(nameof(InitPool)), DelayedProperty] | |
public int PoolCapacity = 200; | |
[ShowInInspector, ReadOnly,] int _poolUsage; | |
float _lastShot; | |
float _period; | |
void OnEnable() { | |
InitPool(); | |
InitRpm(); | |
} | |
void InitRpm() { | |
_period = 1f / RPM; | |
Asr.IsNum(_period); | |
} | |
void InitPool() { | |
_poolUsage = 0; | |
foreach (var i in Dependencies.ShotViews.Out(out _, out var get2, out _)) | |
Destroy(get2[i].Value.gameObject); | |
Dependencies.ShotViews.AllDestroy(); | |
for (var i = 0; i < PoolCapacity; i++) { | |
var e = World.NewEntity(); | |
var v = Instantiate(Prefab, transform); | |
e.Set<CompWrap<ShotView>>().Value = v; | |
var shaderData = e.Set<CompSubtractData>(); | |
shaderData.Renderer = v.SubtractRenderer; | |
shaderData.Renderer.gameObject.SetActive(false); | |
} | |
} | |
void FixedUpdate() { | |
var pistolData = Dependencies.PistolData.Get1[0]; | |
var time = Time.time; | |
if (_lastShot + _period < Time.time) { | |
_lastShot = time; | |
if (pistolData.Shooting && pistolData.Hits.Count > 0) { | |
NewPoint(pistolData.MarkerPose); | |
} | |
} | |
void NewPoint(Pose pose) { | |
var e = World.NewEntityWith(out CompShotData data); | |
data.EndTime = Time.time + Prefab.TimeLen; | |
data.Pose = pose; | |
foreach (var hit in pistolData.Hits) { | |
var rg = hit.rigidbody; | |
if (!rg) continue; | |
e.Set<CompRigidbodyContact>().Rigidbody = rg; | |
switch (rg.gameObject.layer) { | |
case LAYERS.EnemyHitbox_Number: | |
e.Set<TagHitboxLayer>(); | |
break; | |
case LAYERS.EnemySphere_Number: | |
e.Set<TagSphereLayer>(); | |
break; | |
} | |
} | |
} | |
// calc time multiplier & cleanup | |
{ | |
foreach (var i in Dependencies.ShotData.Out(out var get1, out var entities)) { | |
var c1 = get1[i]; | |
c1.Progress = c1.EndTime.Progress01(Time.time, Prefab.TimeLen); | |
if (c1.Progress == 1f) entities[i].Destroy(); | |
} | |
} | |
// pass to views | |
{ | |
Dependencies.ShotViews.Out(out var views, out var get2, out _); | |
var viewId = 0; | |
foreach (var i in Dependencies.ShotData.Out(out var get1, out _)) { | |
if (viewId >= PoolCapacity) break; | |
Draw(get1[i], get2[viewId].Value); | |
} | |
if (_poolUsage < viewId) | |
_poolUsage = viewId; | |
// disable the rest | |
for (var i = viewId; i < PoolCapacity; i++) { | |
var view = views[i]; | |
if (!view.Active) continue; | |
view.Renderer.gameObject.SetActive(false); | |
view.Active = false; | |
} | |
void Draw(CompShotData cur, ShotView view1) { | |
var view = views[viewId]; | |
viewId += 1; | |
view1.Render(cur); | |
if (view.Active) return; | |
view.Renderer.gameObject.SetActive(true); | |
view.Active = true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment