[update] Implemented player's moving calculation.

This commit is contained in:
falsycat 2019-10-09 00:00:00 +00:00
parent 1e6ebe97ce
commit 7f2ada6982
3 changed files with 47 additions and 14 deletions

View File

@ -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();

View File

@ -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!(

View File

@ -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();