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 {
public class Anchor {
bool _opened;
public bool Opened { get { return _opened; } }
public int Name { get; private set; }
public Transform Transform { get; set; }
public void Open() {
_opened = true;
}
public void Close() {
_opened = false;
public Anchor(int name, Transform transform) {
Name = name;
Transform = transform;
}
}
}

View File

@@ -38,10 +38,6 @@ namespace Cryville.Crtr.Event {
/// </summary>
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 Quaternion Rotation { get; protected set; }
public bool Alive { get; private set; }
@@ -63,6 +59,11 @@ namespace Cryville.Crtr.Event {
public abstract string TypeName {
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_head = IdentifierManager.SharedInstance.Request("head");
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;
if (cs.Parent != null)
gogroup.SetParent(cs.Parent.Handler.gogroup, false);
a_cur = RegisterAnchor(_a_cur).Transform;
a_head = RegisterAnchor(_a_head).Transform;
a_tail = RegisterAnchor(_a_tail).Transform;
a_cur = RegisterAnchor(_a_cur);
a_head = RegisterAnchor(_a_head);
a_tail = RegisterAnchor(_a_tail);
}
protected Anchor RegisterAnchor(int name) {
var go = new GameObject("." + IdentifierManager.SharedInstance.Retrieve(name)).transform;
go.SetParent(gogroup, false);
var result = new Anchor() { Transform = go };
Anchors.Add(name, result);
var result = new Anchor(name, go);
List<Anchor> list;
if (!Anchors.TryGetValue(name, out list))
Anchors.Add(name, list = new List<Anchor>());
list.Add(result);
return result;
}
@@ -102,12 +106,14 @@ namespace Cryville.Crtr.Event {
Alive = false;
}
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;
OpenAnchor(_a_head);
}
protected virtual void Awake(ContainerState s) {
CloseAnchor(_a_head);
if (gogroup) CloseAnchor(a_head);
}
protected virtual void GetPosition(ContainerState s) { }
public virtual void StartUpdate(ContainerState s) {
@@ -121,11 +127,11 @@ namespace Cryville.Crtr.Event {
}
static readonly SimpleObjectPool<StampedEvent.Anchor> anchorEvPool
= new SimpleObjectPool<StampedEvent.Anchor>(1024);
protected void PushAnchorEvent(int name, double time) {
protected void PushAnchorEvent(double time, Anchor anchor) {
var tev = anchorEvPool.Rent();
tev.Time = time;
tev.Container = Container;
tev.Name = name;
tev.Target = anchor;
ts.Bus.PushTempEvent(tev);
}
public virtual void Update(ContainerState s, StampedEvent ev) {
@@ -138,10 +144,15 @@ namespace Cryville.Crtr.Event {
if (ev is StampedEvent.Anchor) {
var tev = (StampedEvent.Anchor)ev;
if (gogroup) {
OpenAnchor(tev.Name);
Anchors[tev.Name].Transform.SetPositionAndRotation(Position, Rotation);
OpenAnchor(tev.Target);
#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);
CloseAnchor(tev.Name);
CloseAnchor(tev.Target);
}
anchorEvPool.Return(tev);
}
@@ -154,13 +165,13 @@ namespace Cryville.Crtr.Event {
}
}
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) {
if (Anchors.ContainsKey(name)) Anchors[name].Open();
protected void OpenAnchor(Anchor anchor) {
OpenAnchors.Add(anchor.Name, anchor);
}
protected void CloseAnchor(int name) {
if (Anchors.ContainsKey(name)) Anchors[name].Close();
protected void CloseAnchor(Anchor anchor) {
OpenAnchors.Remove(anchor.Name);
}
}
}

View File

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

View File

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

View File

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