From 7f2ada6982c6313fffc4ad5889ff5b635cd27163 Mon Sep 17 00:00:00 2001 From: falsycat Date: Wed, 9 Oct 2019 00:00:00 +0000 Subject: [PATCH] [update] Implemented player's moving calculation. --- sjplayer/src/sjplayer/Actor.d | 18 +++++++++--------- sjplayer/src/sjplayer/ActorController.d | 24 +++++++++++++++++++----- sjplayer/standalone/main.d | 19 +++++++++++++++++++ 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/sjplayer/src/sjplayer/Actor.d b/sjplayer/src/sjplayer/Actor.d index 2bdf5dd..299e023 100644 --- a/sjplayer/src/sjplayer/Actor.d +++ b/sjplayer/src/sjplayer/Actor.d @@ -9,7 +9,7 @@ class Actor { /// this(ActorProgram program) { pos = vec2(0, 0); - accel = vec2(0, 0); + speed = vec2(0, 0); color = vec4(0, 0, 0, 0); program_ = program; @@ -17,11 +17,11 @@ class Actor { /// void Draw() { - program_.Draw(pos, accel, color); + program_.Draw(pos, speed, color); } vec2 pos; - vec2 accel; + vec2 speed; vec4 color; private: @@ -49,22 +49,22 @@ class ActorProgram { /// enum FragmentShaderSrc = ShaderHeader ~ q{ layout(location = 0) uniform vec2 pos; - layout(location = 1) uniform vec2 accel; + layout(location = 1) uniform vec2 speed; layout(location = 2) uniform vec4 color; in vec2 uv_; out vec4 pixel_; - float line(float u, float p, float a) { + float line(float u, float p, float s) { return (1-smoothstep(0, 0.003, abs(u-p))); } void main() { float alpha = - line(uv_.x, pos.x, accel.x) + - line(uv_.y, pos.y, accel.y); + line(uv_.x, pos.x, speed.x) + + line(uv_.y, pos.y, speed.y); pixel_ = color; pixel_.a *= clamp(alpha, 0, 1); } @@ -102,10 +102,10 @@ class ActorProgram { } /// - void Draw(vec2 pos, vec2 accel, vec4 color) { + void Draw(vec2 pos, vec2 speed, vec4 color) { program_.Use(); program_.uniform!0 = pos; - program_.uniform!1 = accel; + program_.uniform!1 = speed; program_.uniform!2 = color; vao_.Bind(); diff --git a/sjplayer/src/sjplayer/ActorController.d b/sjplayer/src/sjplayer/ActorController.d index c62f59b..427076a 100644 --- a/sjplayer/src/sjplayer/ActorController.d +++ b/sjplayer/src/sjplayer/ActorController.d @@ -19,7 +19,11 @@ import sjplayer.AbstractScheduledController, class ActorController : ActorScheduledController, ActorControllerInterface { public: /// - enum MaxAccel = 1e-1; + enum AccelAdjustment = 0.005; + /// + enum MaxSpeed = 0.03; + /// + enum SpeedAttenuation = 0.05; /// this( @@ -30,17 +34,25 @@ class ActorController : ActorScheduledController, ActorControllerInterface { actor_ = actor; varstore_ = varstore; operations_ = operations; + + accel_ = vec2(0, 0); } override void Accelarate(vec2 accel) { - actor_.accel += accel; + accel_ = accel * AccelAdjustment; } override void Update() { - actor_.accel.x = actor_.accel.x.clamp(-MaxAccel, MaxAccel); - actor_.accel.y = actor_.accel.y.clamp(-MaxAccel, MaxAccel); + actor_.speed += accel_; - actor_.pos += actor_.accel; + const speed_length = actor_.speed.length; + if (speed_length > MaxSpeed) { + actor_.speed = actor_.speed / speed_length * MaxSpeed; + } + + actor_.pos += actor_.speed; // TODO: clamping the actor position + + actor_.speed *= 1-SpeedAttenuation; } private: @@ -49,6 +61,8 @@ class ActorController : ActorScheduledController, ActorControllerInterface { const VarStoreInterface varstore_; const ParametersBlock[] operations_; + + vec2 accel_; } private alias ActorScheduledController = ScheduledController!( diff --git a/sjplayer/standalone/main.d b/sjplayer/standalone/main.d index ac124f2..c95ebea 100644 --- a/sjplayer/standalone/main.d +++ b/sjplayer/standalone/main.d @@ -37,6 +37,8 @@ int main(string[] args) { const msecs = sfMusic_getPlayingOffset(music).microseconds * 1e-6f; const beat = msecs/60f * bpm; + context.actor.Accelarate(GetAccelarationInput()); + context.OperateScheduledControllers(beat); context.actor.Update(); context.posteffect.Update(); @@ -53,6 +55,23 @@ int main(string[] args) { return 0; } +vec2 GetAccelarationInput() { + auto result = vec2(0, 0); + if (sfKeyboard_isKeyPressed(sfKeyLeft)) { + result.x -= 1; + } + if (sfKeyboard_isKeyPressed(sfKeyRight)) { + result.x += 1; + } + if (sfKeyboard_isKeyPressed(sfKeyUp)) { + result.y += 1; + } + if (sfKeyboard_isKeyPressed(sfKeyDown)) { + result.y -= 1; + } + return result; +} + sfWindow* Initialize() { DerelictSFML2System.load(); DerelictSFML2Window.load();