From 803b4132af2ee984dedba8f4d9a5362a8f87a54e Mon Sep 17 00:00:00 2001 From: falsycat Date: Sun, 6 Oct 2019 00:00:00 +0000 Subject: [PATCH] [add] Added CircleElementScheduledController. --- .../CircleElementScheduledController.d | 48 ++++++++++++++++ .../sjplayer/ScheduledControllerInterface.d | 56 +++++++++++++++++-- sjscript/src/sjscript/package.d | 1 + 3 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 sjplayer/src/sjplayer/CircleElementScheduledController.d diff --git a/sjplayer/src/sjplayer/CircleElementScheduledController.d b/sjplayer/src/sjplayer/CircleElementScheduledController.d new file mode 100644 index 0000000..6c13c63 --- /dev/null +++ b/sjplayer/src/sjplayer/CircleElementScheduledController.d @@ -0,0 +1,48 @@ +/// License: MIT +module sjplayer.CircleElementScheduledController; + +import gl4d; + +import sjscript; + +import sjplayer.CircleElement, + sjplayer.ScheduledControllerInterface, + sjplayer.VarStoreInterface; + +/// +class CircleElementScheduledController : + AbstractScheduledControllerWithOperationImpl { + public: + /// + this( + CircleElement element, + in VarStoreInterface varstore, + in ParametersBlock[] operations) { + super(varstore, operations); + element_ = element; + } + + protected: + override void PrepareOperation(ref in ParametersBlock params) { + element_.alive = true; + element_.damage = 0; + element_.nearness_coe = 0; + element_.matrix = mat3.identity; + element_.weight = 1; + element_.smooth = 0; + element_.color = vec4(1, 1, 1, 1); + } + override void FinalizeOperation(ref in ParametersBlock params) { + element_.alive = false; + } + + override float GetVariable(string name) const { + throw new Exception("not implemented"); // TODO: + } + override void SetParameter(ref in Parameter param) { + throw new Exception("not implemented"); // TODO: + } + + private: + CircleElement element_; +} diff --git a/sjplayer/src/sjplayer/ScheduledControllerInterface.d b/sjplayer/src/sjplayer/ScheduledControllerInterface.d index 404238d..1572c89 100644 --- a/sjplayer/src/sjplayer/ScheduledControllerInterface.d +++ b/sjplayer/src/sjplayer/ScheduledControllerInterface.d @@ -2,11 +2,14 @@ module sjplayer.ScheduledControllerInterface; import std.algorithm, - std.array; + std.array, + std.exception, + std.format; import sjscript; -import sjplayer.util.compare; +import sjplayer.VarStoreInterface, + sjplayer.util.compare; /// interface ScheduledControllerInterface { @@ -34,7 +37,7 @@ abstract class AbstractScheduledController : ScheduledControllerInterface { ProcessOperation(*last_operation); return; } - FinishOperation(*last_operation); + FinalizeOperation(*last_operation); } if (next_operation_index_ >= operations_.length) return; @@ -51,7 +54,7 @@ abstract class AbstractScheduledController : ScheduledControllerInterface { abstract void ProcessOperation(ref in ParametersBlock params); - abstract void FinishOperation(ref in ParametersBlock params); + abstract void FinalizeOperation(ref in ParametersBlock params); private: const ParametersBlock[] operations_; @@ -60,3 +63,48 @@ abstract class AbstractScheduledController : ScheduledControllerInterface { size_t next_operation_index_; } + +/// +abstract class AbstractScheduledControllerWithOperationImpl : + AbstractScheduledController { + public: + /// + this(in VarStoreInterface varstore, in ParametersBlock[] operations) { + super(operations); + varstore_ = varstore; + } + + protected: + static struct VarStore { + public: + float opIndex(string name) { + float result = void; + if (!this_.GetVariable(name).collectException(result)) return result; + if (!this_.varstore_[name] .collectException(result)) return result; + if (!this_.user_vars_[name] .collectException(result)) return result; + throw new Exception("unknown variable %s".format(name)); + } + private: + AbstractScheduledControllerWithOperationImpl this_; + } + + override void ProcessOperation(ref in ParametersBlock params) { + foreach (const ref param; params.parameters) { + if (param.name.length >= 2 && param.name[0..2] == "__") { + user_vars_[param.name[2..$]] = + param.rhs.CalculateExpression(VarStore(this)); + continue; + } + SetParameter(param); + } + } + + abstract float GetVariable(string name) const; + + abstract void SetParameter(ref in Parameter param); + + private: + const VarStoreInterface varstore_; + + float[string] user_vars_; +} diff --git a/sjscript/src/sjscript/package.d b/sjscript/src/sjscript/package.d index 8e696eb..d594ae8 100644 --- a/sjscript/src/sjscript/package.d +++ b/sjscript/src/sjscript/package.d @@ -13,6 +13,7 @@ import sjscript.Token, public { import sjscript.Expression, sjscript.ParametersBlock, + sjscript.calculate, sjscript.exception; }