Skip to content

Instantly share code, notes, and snippets.

@aliyome
Created June 15, 2013 09:53
Show Gist options
  • Save aliyome/e78cf68bbb8349c77ef4 to your computer and use it in GitHub Desktop.
Save aliyome/e78cf68bbb8349c77ef4 to your computer and use it in GitHub Desktop.
import vector;
class PlayerConstants {
Vector2f walkBack;
Vector2f walkFront;
Vector2f dashBack;
Vector2f dashFront;
Vector2f airBack;
Vector2f airFront;
Vector2f jumpUp;
Vector2f jumpBack;
Vector2f jumpFront;
float verticalAccel;
//float frictionStand;
//float frictionCrouch;
}
class CharacterVariables {
}
//updatePhisycs()
enum Facing { Right, Left }
class Rigid {
this (Vector2f pos) {
position = pos;
vPosition = Vector2f(0, 0);
}
void update() {
position += vPosition;
}
Vector2f nextPosition() { return position+vPosition; }
Vector2f position;
Vector2f vPosition;
}
class Gravity {
this() {
g = Vector2f(0, 9.8f);;
}
void addRigid(Rigid r) {
rigids ~= r;
}
void update() {
foreach (r; rigids)
r.vPosition += g;
}
Rigid[] rigids;
Vector2f g;
}
class RigidFloor {
this(float y) { position = Vector2f(0, y); }
Vector2f position;
}
//class Collision {
bool intersect(Rigid r, RigidFloor f) {
if (r.position.y <= f.position.y &&
r.nextPosition.y >= f.position.y)
return true;
else
return false;
}
//}
unittest {
import std.stdio;
auto g = new Gravity();
auto r = new Rigid(Vector2f(0, 0));
auto floor = new RigidFloor(400);
//auto collision = new Collision();
g.addRigid(r);
foreach (i; 0 .. 10) {
g.update();
if (r.intersect(floor)) {
r.position.y = floor.position.y;
r.vPosition.y = 0;
}
else {
r.update();
}
writeln(r.position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment