Skip to content

Instantly share code, notes, and snippets.

@aliyome
Last active July 22, 2018 06:20
Show Gist options
  • Save aliyome/3eeb9a702a1ae329bb70 to your computer and use it in GitHub Desktop.
Save aliyome/3eeb9a702a1ae329bb70 to your computer and use it in GitHub Desktop.
tweenっぽい
import std.stdio;
import std.container : DList;
import std.range;
class E {
typeof(this) moveBy(int x, int y, int duration, int parallel = 0) {
timelines[parallel].insertBack(timelineImpl(duration, {
float vx = cast(float)x / duration;
float vy = cast(float)y / duration;
this.x += vx;
this.y += vy;
}));
return this;
}
typeof(this) moveTo(int x, int y, int duration, int parallel = 0) {
int count;
float vx, vy;
timelines[parallel].insertBack(timelineImpl(duration, {
if (count == 0) {
vx = cast(float)(x - this.x) / duration;
vy = cast(float)(y - this.y) / duration;
}
count++;
this.x += vx;
this.y += vy;
}));
return this;
}
typeof(this) pause(int duration, int parallel = 0) {
timelines[parallel].insertBack(timelineImpl(duration));
return this;
}
typeof(this) fire(string type, int parallel = 0) {
timelines[parallel].insertBack(timelineImpl(1, {writeln(type);}));
return this;
}
void update() {
foreach (ref tl; timelines) {
if (!tl.empty)
if (tl.front()() == false)
tl.removeFront();
}
}
private:
bool delegate() timelineImpl(int duration, void delegate() action = null) {
int i;
return {
i++;
if (i > duration)
return false;
else {
if (action !is null)
action();
if (i >= duration)
return false;
else
return true;
}
};
}
DList!(bool delegate())[10] timelines;
float x=0.0f, y=0.0f;
}
bool hoge() { return true; }
unittest {
auto e = new E();
//// serial
//e.moveBy(0, 100, 5)
// .fire("multi way")
// .pause(5)
// .fire("one way")
// .moveTo(0, 0, 5);
//foreach (i; 0 .. 20) {
// write(i, " : ");
// e.update();
//}
// parallel
// move x and y
e.moveBy( 0, 100, 5, 0)
.moveBy(100, 0, 5, 1)
.moveTo( 0, 0, 10, 0);
e.pause(5, 2)
.fire("parallel fire", 2);
foreach (i; 0 .. 20) {
e.update();
writefln("%d : %s, %s", i, e.x, e.y);
}
}
//unittest {
// void delegate()[] timeline;
// int value, prev_value;
// timeline ~= { value++; };
// timeline ~= { value *= 2; };
// timeline ~= { if (prev_value == 2) writeln("exec"); };
// writeln(value);
// foreach (i; 0 .. 10) {
// prev_value = value;
// foreach (tl; timeline) {
// tl();
// }
// writeln(value);
// }
//}
////import gl3n.linalg;
////alias Vector!(float, 3) Vec3f;
////unittest {
//// Vec3f point = Vec3f(0.0f, 0.0f, 1.0f);
//// Vec3f rot = Vec3f(0.0f, 0.0f, 0.0f);
////}
////struct World {
//// Vec3f world2Cam(Vec3f v) {
//// return v - cam.position;
//// }
////private:
//// Camera cam;
////}
////struct Camera {
//// Vec3f position = Vec3f(0.0f, 0.0f, 1.0f);
//// Vec3f rotation = Vec3f(0.0f, 0.0f, 0.0f);
//// Camera rotate(Vec3f rot) {
//// rotation += rot;
//// return this;
//// }
////}
////import std.stdio;
////unittest {
//// Camera cam;
//// World world;
//// world.cam = cam;
//// Vec3f v = Vec3f(0.0f, 0.0f, 1.0f);
//// world.cam.position += Vec3f(10.0f, 10.0f, 10.0f);
//// writeln(world.world2Cam(v));
////}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment