[add] Added CircleElementScheduledController.

This commit is contained in:
falsycat 2019-10-06 00:00:00 +00:00
parent ecbed62b80
commit 803b4132af
3 changed files with 101 additions and 4 deletions

View File

@ -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_;
}

View File

@ -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_;
}

View File

@ -13,6 +13,7 @@ import sjscript.Token,
public {
import sjscript.Expression,
sjscript.ParametersBlock,
sjscript.calculate,
sjscript.exception;
}