[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;
import std.algorithm,
std.array,
std.typecons;
import sjscript;
import sjplayer.ElementInterface,
import sjplayer.ContextBuilderInterface,
sjplayer.ElementInterface,
sjplayer.ProgramSet,
sjplayer.ScheduledControllerInterface,
sjplayer.VarStoreInterface;
///
struct Context {
class Context {
public:
@disable this();
@disable this(this);
///
this(ParametersBlock[] params, ProgramSet programs) {
auto builder = new Builder;
auto varstore = new BlackHole!VarStoreInterface;
import sjplayer.CircleElementScheduledController;
@ -29,12 +29,13 @@ struct Context {
),
);
foreach (factory; factories) {
auto result = factory[1].
Create(params.filter!(x => x.name == factory[0]));
elements_ ~= result.elements;
drawers_ ~= result.drawer;
controllers_ ~= result.controllers;
factory[1].
Create(params.filter!(x => x.name == factory[0]), builder);
}
elements_ = builder.elements[];
drawers_ = builder.drawers[];
controllers_ = builder.controllers[];
}
///
~this() {
@ -47,7 +48,6 @@ struct Context {
ElementInterface.DamageCalculationResult CalculateDamage() const {
assert(false); // TODO:
}
///
void DrawElements() {
drawers_.each!(x => x.Draw());
@ -58,6 +58,22 @@ struct Context {
}
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_;
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 sjplayer.ElementInterface,
import sjplayer.ContextBuilderInterface,
sjplayer.ElementInterface,
sjplayer.ProgramSet,
sjplayer.ScheduledControllerInterface,
sjplayer.VarStoreInterface,
sjplayer.util.Period;
///
struct ScheduledControllerCreationResult {
///
ElementInterface[] elements;
///
ScheduledControllerInterface[] controllers;
///
ElementDrawerInterface drawer;
}
///
struct ElementScheduledControllerFactory(ScheduledController, ElementDrawer)
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)) {
auto parallelized = params.ParallelizeParams();
auto elements = appender!(Element[]);
auto controllers = appender!(ScheduledController[]);
auto elements = appender!(Element[]);
foreach (ref serial; parallelized) {
auto element = new Element;
elements ~= element;
controllers ~= new ScheduledController(element, varstore_, serial);
builder.AddElement(element);
builder.AddScheduledController(
new ScheduledController(element, varstore_, serial));
}
return ScheduledControllerCreationResult(
elements[] .map!(x => cast(ElementInterface) x).array,
controllers[].map!(x => cast(ScheduledControllerInterface) x).array,
new ElementDrawer(program_, elements[]));
builder.AddElementDrawer(new ElementDrawer(program_, elements[]));
}
private:

View File

@ -15,5 +15,5 @@ Context CreateContextFromText(string src, ProgramSet programs) {
///
Context CreateContextFromScriptAst(
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();
auto context = script_file.readText.CreateContextFromText(programs);
scope(exit) context.destroy();
while (true) {
sfEvent e;