[add] Added ContextBuilderInterface and Replaced Context from struct to class.

This commit is contained in:
falsycat 2019-10-07 00:00:00 +00:00
parent b23971448b
commit 58621115e0
5 changed files with 53 additions and 33 deletions

View File

@ -2,23 +2,23 @@
module sjplayer.Context; module sjplayer.Context;
import std.algorithm, import std.algorithm,
std.array,
std.typecons; std.typecons;
import sjscript; import sjscript;
import sjplayer.ElementInterface, import sjplayer.ContextBuilderInterface,
sjplayer.ElementInterface,
sjplayer.ProgramSet, sjplayer.ProgramSet,
sjplayer.ScheduledControllerInterface, sjplayer.ScheduledControllerInterface,
sjplayer.VarStoreInterface; sjplayer.VarStoreInterface;
/// ///
struct Context { class Context {
public: public:
@disable this();
@disable this(this);
/// ///
this(ParametersBlock[] params, ProgramSet programs) { this(ParametersBlock[] params, ProgramSet programs) {
auto builder = new Builder;
auto varstore = new BlackHole!VarStoreInterface; auto varstore = new BlackHole!VarStoreInterface;
import sjplayer.CircleElementScheduledController; import sjplayer.CircleElementScheduledController;
@ -29,12 +29,13 @@ struct Context {
), ),
); );
foreach (factory; factories) { foreach (factory; factories) {
auto result = factory[1]. factory[1].
Create(params.filter!(x => x.name == factory[0])); Create(params.filter!(x => x.name == factory[0]), builder);
elements_ ~= result.elements;
drawers_ ~= result.drawer;
controllers_ ~= result.controllers;
} }
elements_ = builder.elements[];
drawers_ = builder.drawers[];
controllers_ = builder.controllers[];
} }
/// ///
~this() { ~this() {
@ -47,7 +48,6 @@ struct Context {
ElementInterface.DamageCalculationResult CalculateDamage() const { ElementInterface.DamageCalculationResult CalculateDamage() const {
assert(false); // TODO: assert(false); // TODO:
} }
/// ///
void DrawElements() { void DrawElements() {
drawers_.each!(x => x.Draw()); drawers_.each!(x => x.Draw());
@ -58,6 +58,22 @@ struct Context {
} }
private: private:
class Builder : ContextBuilderInterface {
public:
override void AddElement(ElementInterface element) {
elements ~= element;
}
override void AddElementDrawer(ElementDrawerInterface drawer) {
drawers ~= drawer;
}
override void AddScheduledController(ScheduledControllerInterface controller) {
controllers ~= controller;
}
Appender!(ElementInterface[]) elements;
Appender!(ElementDrawerInterface[]) drawers;
Appender!(ScheduledControllerInterface[]) controllers;
}
ElementInterface[] elements_; ElementInterface[] elements_;
ElementDrawerInterface[] drawers_; ElementDrawerInterface[] drawers_;

View File

@ -0,0 +1,16 @@
/// License: MIT
module sjplayer.ContextBuilderInterface;
import sjplayer.ElementInterface,
sjplayer.ScheduledControllerInterface;
///
interface ContextBuilderInterface {
public:
///
void AddElement(ElementInterface element);
///
void AddElementDrawer(ElementDrawerInterface drawer);
///
void AddScheduledController(ScheduledControllerInterface controller);
}

View File

@ -10,23 +10,13 @@ import std.algorithm,
import sjscript; import sjscript;
import sjplayer.ElementInterface, import sjplayer.ContextBuilderInterface,
sjplayer.ElementInterface,
sjplayer.ProgramSet, sjplayer.ProgramSet,
sjplayer.ScheduledControllerInterface, sjplayer.ScheduledControllerInterface,
sjplayer.VarStoreInterface, sjplayer.VarStoreInterface,
sjplayer.util.Period; sjplayer.util.Period;
///
struct ScheduledControllerCreationResult {
///
ElementInterface[] elements;
///
ScheduledControllerInterface[] controllers;
///
ElementDrawerInterface drawer;
}
/// ///
struct ElementScheduledControllerFactory(ScheduledController, ElementDrawer) struct ElementScheduledControllerFactory(ScheduledController, ElementDrawer)
if (is(ScheduledController : ScheduledControllerInterface) && if (is(ScheduledController : ScheduledControllerInterface) &&
@ -54,22 +44,19 @@ struct ElementScheduledControllerFactory(ScheduledController, ElementDrawer)
} }
/// ///
ScheduledControllerCreationResult Create(R)(R params) void Create(R)(R params, ContextBuilderInterface builder)
if (isInputRange!R && is(ElementType!R == ParametersBlock)) { if (isInputRange!R && is(ElementType!R == ParametersBlock)) {
auto parallelized = params.ParallelizeParams(); auto parallelized = params.ParallelizeParams();
auto elements = appender!(Element[]);
auto elements = appender!(Element[]);
auto controllers = appender!(ScheduledController[]);
foreach (ref serial; parallelized) { foreach (ref serial; parallelized) {
auto element = new Element; auto element = new Element;
elements ~= element; elements ~= element;
controllers ~= new ScheduledController(element, varstore_, serial); builder.AddElement(element);
builder.AddScheduledController(
new ScheduledController(element, varstore_, serial));
} }
return ScheduledControllerCreationResult( builder.AddElementDrawer(new ElementDrawer(program_, elements[]));
elements[] .map!(x => cast(ElementInterface) x).array,
controllers[].map!(x => cast(ScheduledControllerInterface) x).array,
new ElementDrawer(program_, elements[]));
} }
private: private:

View File

@ -15,5 +15,5 @@ Context CreateContextFromText(string src, ProgramSet programs) {
/// ///
Context CreateContextFromScriptAst( Context CreateContextFromScriptAst(
ParametersBlock[] params, ProgramSet programs) { ParametersBlock[] params, ProgramSet programs) {
return Context(params, programs); return new Context(params, programs);
} }

View File

@ -26,6 +26,7 @@ int main(string[] args) {
scope(exit) programs.destroy(); scope(exit) programs.destroy();
auto context = script_file.readText.CreateContextFromText(programs); auto context = script_file.readText.CreateContextFromText(programs);
scope(exit) context.destroy();
while (true) { while (true) {
sfEvent e; sfEvent e;