Revise anchor data structure.

This commit is contained in:
2023-01-14 21:34:13 +08:00
parent 4f93995bbd
commit 5e4c53113a
5 changed files with 60 additions and 53 deletions

View File

@@ -2,14 +2,11 @@
namespace Cryville.Crtr { namespace Cryville.Crtr {
public class Anchor { public class Anchor {
bool _opened; public int Name { get; private set; }
public bool Opened { get { return _opened; } }
public Transform Transform { get; set; } public Transform Transform { get; set; }
public void Open() { public Anchor(int name, Transform transform) {
_opened = true; Name = name;
} Transform = transform;
public void Close() {
_opened = false;
} }
} }
} }

View File

@@ -38,10 +38,6 @@ namespace Cryville.Crtr.Event {
/// </summary> /// </summary>
public Transform gogroup; public Transform gogroup;
public readonly Dictionary<int, Anchor> Anchors = new Dictionary<int, Anchor>();
protected Transform a_cur;
protected Transform a_head;
protected Transform a_tail;
public Vector3 Position { get; protected set; } public Vector3 Position { get; protected set; }
public Quaternion Rotation { get; protected set; } public Quaternion Rotation { get; protected set; }
public bool Alive { get; private set; } public bool Alive { get; private set; }
@@ -63,6 +59,11 @@ namespace Cryville.Crtr.Event {
public abstract string TypeName { public abstract string TypeName {
get; get;
} }
public readonly Dictionary<int, List<Anchor>> Anchors = new Dictionary<int, List<Anchor>>();
public readonly Dictionary<int, Anchor> OpenAnchors = new Dictionary<int, Anchor>();
protected Anchor a_cur;
protected Anchor a_head;
protected Anchor a_tail;
protected readonly static int _a_cur = IdentifierManager.SharedInstance.Request("cur"); protected readonly static int _a_cur = IdentifierManager.SharedInstance.Request("cur");
protected readonly static int _a_head = IdentifierManager.SharedInstance.Request("head"); protected readonly static int _a_head = IdentifierManager.SharedInstance.Request("head");
protected readonly static int _a_tail = IdentifierManager.SharedInstance.Request("tail"); protected readonly static int _a_tail = IdentifierManager.SharedInstance.Request("tail");
@@ -70,15 +71,18 @@ namespace Cryville.Crtr.Event {
gogroup = new GameObject(TypeName + ":" + Container.GetHashCode().ToString(CultureInfo.InvariantCulture)).transform; gogroup = new GameObject(TypeName + ":" + Container.GetHashCode().ToString(CultureInfo.InvariantCulture)).transform;
if (cs.Parent != null) if (cs.Parent != null)
gogroup.SetParent(cs.Parent.Handler.gogroup, false); gogroup.SetParent(cs.Parent.Handler.gogroup, false);
a_cur = RegisterAnchor(_a_cur).Transform; a_cur = RegisterAnchor(_a_cur);
a_head = RegisterAnchor(_a_head).Transform; a_head = RegisterAnchor(_a_head);
a_tail = RegisterAnchor(_a_tail).Transform; a_tail = RegisterAnchor(_a_tail);
} }
protected Anchor RegisterAnchor(int name) { protected Anchor RegisterAnchor(int name) {
var go = new GameObject("." + IdentifierManager.SharedInstance.Retrieve(name)).transform; var go = new GameObject("." + IdentifierManager.SharedInstance.Retrieve(name)).transform;
go.SetParent(gogroup, false); go.SetParent(gogroup, false);
var result = new Anchor() { Transform = go }; var result = new Anchor(name, go);
Anchors.Add(name, result); List<Anchor> list;
if (!Anchors.TryGetValue(name, out list))
Anchors.Add(name, list = new List<Anchor>());
list.Add(result);
return result; return result;
} }
@@ -102,12 +106,14 @@ namespace Cryville.Crtr.Event {
Alive = false; Alive = false;
} }
protected virtual void PreAwake(ContainerState s) { protected virtual void PreAwake(ContainerState s) {
if (gogroup) gogroup.gameObject.SetActive(true); if (gogroup) {
gogroup.gameObject.SetActive(true);
OpenAnchor(a_head);
}
Awoken = true; Alive = true; Awoken = true; Alive = true;
OpenAnchor(_a_head);
} }
protected virtual void Awake(ContainerState s) { protected virtual void Awake(ContainerState s) {
CloseAnchor(_a_head); if (gogroup) CloseAnchor(a_head);
} }
protected virtual void GetPosition(ContainerState s) { } protected virtual void GetPosition(ContainerState s) { }
public virtual void StartUpdate(ContainerState s) { public virtual void StartUpdate(ContainerState s) {
@@ -121,11 +127,11 @@ namespace Cryville.Crtr.Event {
} }
static readonly SimpleObjectPool<StampedEvent.Anchor> anchorEvPool static readonly SimpleObjectPool<StampedEvent.Anchor> anchorEvPool
= new SimpleObjectPool<StampedEvent.Anchor>(1024); = new SimpleObjectPool<StampedEvent.Anchor>(1024);
protected void PushAnchorEvent(int name, double time) { protected void PushAnchorEvent(double time, Anchor anchor) {
var tev = anchorEvPool.Rent(); var tev = anchorEvPool.Rent();
tev.Time = time; tev.Time = time;
tev.Container = Container; tev.Container = Container;
tev.Name = name; tev.Target = anchor;
ts.Bus.PushTempEvent(tev); ts.Bus.PushTempEvent(tev);
} }
public virtual void Update(ContainerState s, StampedEvent ev) { public virtual void Update(ContainerState s, StampedEvent ev) {
@@ -138,10 +144,15 @@ namespace Cryville.Crtr.Event {
if (ev is StampedEvent.Anchor) { if (ev is StampedEvent.Anchor) {
var tev = (StampedEvent.Anchor)ev; var tev = (StampedEvent.Anchor)ev;
if (gogroup) { if (gogroup) {
OpenAnchor(tev.Name); OpenAnchor(tev.Target);
Anchors[tev.Name].Transform.SetPositionAndRotation(Position, Rotation); #if UNITY_5_6_OR_NEWER
tev.Target.Transform.SetPositionAndRotation(Position, Rotation);
#else
tev.Target.Transform.position = GetCurrentWorldPoint();
tev.Target.Transform.rotation = Quaternion.Euler(s.Direction);
#endif
skinContainer.MatchDynamic(s); skinContainer.MatchDynamic(s);
CloseAnchor(tev.Name); CloseAnchor(tev.Target);
} }
anchorEvPool.Return(tev); anchorEvPool.Return(tev);
} }
@@ -154,13 +165,13 @@ namespace Cryville.Crtr.Event {
} }
} }
public virtual void Anchor() { public virtual void Anchor() {
if (cs.Working) PushAnchorEvent(_a_cur, cs.Time); if (cs.Working) PushAnchorEvent(cs.Time, a_cur);
} }
protected void OpenAnchor(int name) { protected void OpenAnchor(Anchor anchor) {
if (Anchors.ContainsKey(name)) Anchors[name].Open(); OpenAnchors.Add(anchor.Name, anchor);
} }
protected void CloseAnchor(int name) { protected void CloseAnchor(Anchor anchor) {
if (Anchors.ContainsKey(name)) Anchors[name].Close(); OpenAnchors.Remove(anchor.Name);
} }
} }
} }

View File

@@ -24,16 +24,16 @@ namespace Cryville.Crtr {
} }
SectionalGameObject[] sgos; SectionalGameObject[] sgos;
readonly Dictionary<Identifier, JudgeState> judges = new Dictionary<Identifier, JudgeState>(); readonly Dictionary<Chart.Judge, JudgeState> judges = new Dictionary<Chart.Judge, JudgeState>();
class JudgeState { class JudgeState {
public int AbsoluteAnchorName { get; set; } public Anchor AbsoluteAnchor { get; private set; }
public int RelativeAnchorName { get; set; } public Anchor RelativeAnchor { get; private set; }
public bool Judged { get; private set; } public bool Judged { get; private set; }
public float AbsoluteTime { get; private set; } public float AbsoluteTime { get; private set; }
public float RelativeTime { get; private set; } public float RelativeTime { get; private set; }
public JudgeState(NoteHandler handler, int name) { public JudgeState(NoteHandler handler, int name) {
handler.RegisterAnchor(AbsoluteAnchorName = handler.judge.jtabsMap[name]); AbsoluteAnchor = handler.RegisterAnchor(handler.judge.jtabsMap[name]);
handler.RegisterAnchor(RelativeAnchorName = handler.judge.jtrelMap[name]); RelativeAnchor = handler.RegisterAnchor(handler.judge.jtrelMap[name]);
} }
public void MarkJudged(float abs, float rel) { public void MarkJudged(float abs, float rel) {
Judged = true; Judged = true;
@@ -45,9 +45,7 @@ namespace Cryville.Crtr {
public override void PreInit() { public override void PreInit() {
base.PreInit(); base.PreInit();
foreach (var j in Event.judges) { foreach (var j in Event.judges) {
if (!judges.ContainsKey(j.Id)) { judges.Add(j, new JudgeState(this, j.Id.Key));
judges.Add(j.Id, new JudgeState(this, j.Id.Key));
}
} }
} }
public override void Init() { public override void Init() {
@@ -57,10 +55,10 @@ namespace Cryville.Crtr {
protected override void PreAwake(ContainerState s) { protected override void PreAwake(ContainerState s) {
base.PreAwake(s); base.PreAwake(s);
#if UNITY_5_6_OR_NEWER #if UNITY_5_6_OR_NEWER
a_head.SetPositionAndRotation(Position = GetFramePoint(s.Parent, s.Track), Rotation = GetFrameRotation(s.Parent, s.Track)); a_head.Transform.SetPositionAndRotation(Position = GetFramePoint(s.Parent, s.Track), Rotation = GetFrameRotation(s.Parent, s.Track));
#else #else
a_head.position = Position = GetFramePoint(s.Parent, s.Track); a_head.Transform.position = Position = GetFramePoint(s.Parent, s.Track);
a_head.rotation = Rotation = GetFrameRotation(s.Parent, s.Track); a_head.Transform.rotation = Rotation = GetFrameRotation(s.Parent, s.Track);
#endif #endif
} }
protected override void Awake(ContainerState s) { protected override void Awake(ContainerState s) {
@@ -119,8 +117,8 @@ namespace Cryville.Crtr {
if (cs.Working) { if (cs.Working) {
foreach (var j in judges.Values) { foreach (var j in judges.Values) {
if (!j.Judged) continue; if (!j.Judged) continue;
PushAnchorEvent(j.AbsoluteAnchorName, j.AbsoluteTime); PushAnchorEvent(j.AbsoluteTime, j.AbsoluteAnchor);
PushAnchorEvent(j.RelativeAnchorName, j.RelativeTime + cs.Time); PushAnchorEvent(j.RelativeTime + cs.Time, j.RelativeAnchor);
} }
} }
} }
@@ -129,10 +127,10 @@ namespace Cryville.Crtr {
if (s.CloneType == 2 && gogroup) { if (s.CloneType == 2 && gogroup) {
foreach (var i in sgos) i.Seal(); foreach (var i in sgos) i.Seal();
#if UNITY_5_6_OR_NEWER #if UNITY_5_6_OR_NEWER
a_tail.SetPositionAndRotation(GetFramePoint(ts.Parent, ts.Track), Quaternion.Euler(ts.Direction)); a_tail.Transform.SetPositionAndRotation(GetFramePoint(ts.Parent, ts.Track), Quaternion.Euler(ts.Direction));
#else #else
a_tail.position = GetFramePoint(ts.Parent, ts.Track); a_tail.Transform.position = GetFramePoint(ts.Parent, ts.Track);
a_tail.rotation = Quaternion.Euler(ts.Direction); a_tail.Transform.rotation = Quaternion.Euler(ts.Direction);
#endif #endif
} }
OpenAnchor(_a_tail); OpenAnchor(_a_tail);
@@ -197,7 +195,7 @@ namespace Cryville.Crtr {
public void ReportJudge(Judge.JudgeEvent ev, float time, Identifier result) { public void ReportJudge(Judge.JudgeEvent ev, float time, Identifier result) {
JudgeState state; JudgeState state;
if (!judges.TryGetValue(ev.JudgeId, out state)) return; if (!judges.TryGetValue(ev.BaseEvent, out state)) return;
state.MarkJudged(time, (float)(time - cs.Time)); state.MarkJudged(time, (float)(time - cs.Time));
} }
} }

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using CAnchor = Cryville.Crtr.Anchor;
namespace Cryville.Crtr { namespace Cryville.Crtr {
public class StampedEvent : IComparable<StampedEvent> { public class StampedEvent : IComparable<StampedEvent> {
@@ -29,7 +30,7 @@ namespace Cryville.Crtr {
} }
public class Anchor : StampedEvent { public class Anchor : StampedEvent {
public int Name; public CAnchor Target;
public override int Priority { public override int Priority {
get { return 0; } get { return 0; }
} }

View File

@@ -32,10 +32,10 @@ namespace Cryville.Crtr {
ptime = s.Time; ptime = s.Time;
if (s.CloneType == 2) { if (s.CloneType == 2) {
#if UNITY_5_6_OR_NEWER #if UNITY_5_6_OR_NEWER
a_head.SetPositionAndRotation(GetCurrentWorldPoint(), Quaternion.Euler(s.Direction)); a_head.Transform.SetPositionAndRotation(GetCurrentWorldPoint(), Quaternion.Euler(s.Direction));
#else #else
a_head.position = GetCurrentWorldPoint(); a_head.Transform.position = GetCurrentWorldPoint();
a_head.rotation = Quaternion.Euler(s.Direction); a_head.Transform.rotation = Quaternion.Euler(s.Direction);
#endif #endif
} }
else if (s.CloneType == 3) { else if (s.CloneType == 3) {
@@ -118,10 +118,10 @@ namespace Cryville.Crtr {
i.Seal(); i.Seal();
} }
#if UNITY_5_6_OR_NEWER #if UNITY_5_6_OR_NEWER
a_tail.SetPositionAndRotation(GetCurrentWorldPoint(), Quaternion.Euler(ts.Direction)); a_tail.Transform.SetPositionAndRotation(GetCurrentWorldPoint(), Quaternion.Euler(ts.Direction));
#else #else
a_tail.position = GetCurrentWorldPoint(); a_tail.Transform.position = GetCurrentWorldPoint();
a_tail.rotation = Quaternion.Euler(ts.Direction); a_tail.Transform.rotation = Quaternion.Euler(ts.Direction);
#endif #endif
} }
else if (s.CloneType == 3) EndUpdatePosition(ns); else if (s.CloneType == 3) EndUpdatePosition(ns);