diff --git a/sjplayer/src/sjplayer/ActorController.d b/sjplayer/src/sjplayer/ActorController.d index e8d4410..173a944 100644 --- a/sjplayer/src/sjplayer/ActorController.d +++ b/sjplayer/src/sjplayer/ActorController.d @@ -1,13 +1,16 @@ /// License: MIT module sjplayer.ActorController; -import std.algorithm; +import std.algorithm, + std.range.primitives; import gl4d; import sjscript; -import sjplayer.Actor, +import sjplayer.AbstractScheduledController, + sjplayer.Actor, + sjplayer.ContextBuilderInterface, sjplayer.ScheduledController, sjplayer.VarStoreInterface; @@ -29,7 +32,7 @@ class ActorController : ActorScheduledController { } /// - void Manipulate(vec2 accel) { + void Update(vec2 accel) { actor_.accel += accel; actor_.accel.x = actor_.accel.x.clamp(-MaxAccel, MaxAccel); @@ -56,3 +59,33 @@ private alias ActorScheduledController = ScheduledController!( "color_a": "color.a", ] ); + +/// +struct ActorControllerFactory { + public: + /// + this(in VarStoreInterface varstore, Actor actor) { + varstore_ = varstore; + actor_ = actor; + } + + /// + void Create(R)(R params, ContextBuilderInterface builder) + if (isInputRange!R && is(ElementType!R == ParametersBlock)) { + product_ = new ActorController( + actor_, varstore_, SortParametersBlock(params)); + builder.AddScheduledController(product_); + } + + /// + @property ActorController product() { + return product_; + } + + private: + const VarStoreInterface varstore_; + + Actor actor_; + + ActorController product_; +} diff --git a/sjplayer/src/sjplayer/Context.d b/sjplayer/src/sjplayer/Context.d index bc19403..93999c5 100644 --- a/sjplayer/src/sjplayer/Context.d +++ b/sjplayer/src/sjplayer/Context.d @@ -5,9 +5,13 @@ import std.algorithm, std.array, std.typecons; +import gl4d; + import sjscript; -import sjplayer.Background, +import sjplayer.Actor, + sjplayer.ActorController, + sjplayer.Background, sjplayer.ContextBuilderInterface, sjplayer.ElementDrawerInterface, sjplayer.ElementInterface, @@ -23,11 +27,16 @@ class Context { auto builder = new Builder; auto varstore = new BlackHole!VarStoreInterface; + actor_ = new Actor(programs.Get!ActorProgram); background_ = new Background(programs.Get!BackgroundProgram); import sjplayer.BackgroundScheduledController, sjplayer.CircleElementScheduledController; auto factories = tuple( + tuple( + "actor", + ActorControllerFactory(varstore, actor_), + ), tuple( "background", BackgroundScheduledControllerFactory(varstore, background_), @@ -37,7 +46,7 @@ class Context { CircleElementScheduledControllerFactory(programs, varstore), ), ); - foreach (factory; factories) { + foreach (ref factory; factories) { factory[1]. Create(params.filter!(x => x.name == factory[0]), builder); } @@ -45,6 +54,8 @@ class Context { elements_ = builder.elements[]; drawers_ = builder.drawers[]; controllers_ = builder.controllers[]; + + actor_controller_ = factories[0][1].product; } /// ~this() { @@ -59,6 +70,16 @@ class Context { ElementInterface.DamageCalculationResult CalculateDamage() const { assert(false); // TODO: } + + /// + void UpdateActor(vec2 accel) { + actor_controller_.Update(accel); + } + /// + void OperateScheduledControllers(float time) { + controllers_.each!(x => x.Operate(time)); + } + /// void DrawBackground() { background_.Draw(); @@ -68,8 +89,8 @@ class Context { drawers_.each!(x => x.Draw()); } /// - void OperateScheduledControllers(float time) { - controllers_.each!(x => x.Operate(time)); + void DrawActor() { + actor_.Draw(); } private: @@ -89,7 +110,14 @@ class Context { Appender!(ScheduledControllerInterface[]) controllers; } + Actor actor_; + invariant(actor_); + + ActorController actor_controller_; + invariant(actor_controller_); + Background background_; + invariant(background_); ElementInterface[] elements_; diff --git a/sjplayer/standalone/main.d b/sjplayer/standalone/main.d index afae262..551f775 100644 --- a/sjplayer/standalone/main.d +++ b/sjplayer/standalone/main.d @@ -37,10 +37,12 @@ int main(string[] args) { const beat = msecs/60f * bpm; context.OperateScheduledControllers(beat); + context.UpdateActor(vec2(0, 0)); gl.Clear(GL_COLOR_BUFFER_BIT); context.DrawBackground(); context.DrawElements(); + context.DrawActor(); sfWindow_display(win); } return 0;