Skip to content

Instantly share code, notes, and snippets.

@darkwave
Last active January 29, 2016 23:39
Show Gist options
  • Save darkwave/929b6ca3f204fc05938e to your computer and use it in GitHub Desktop.
Save darkwave/929b6ca3f204fc05938e to your computer and use it in GitHub Desktop.
GGJ16
class Bird {
int frameCounter = 0;
int currentAnimation = 0;
float posX, posY;
PImage[][] animations;
int lastFrame = millis();
int fps = 12;
Bird(float posX, float posY, String baseFilename, int numberOfAnimations, int numberOfFrames) {
animations = new PImage[numberOfAnimations][numberOfFrames];
for (int anim = 0; anim < numberOfAnimations; anim++)
for (int frame = 0; frame < numberOfFrames; frame++) {
animations[anim][frame] = loadImage(baseFilename + "-" + anim + "-" + frame + ".png");
}
this.posX = posX;
this.posY = posY;
}
void display() {
pushMatrix();
translate(posX, posY);
image(animations[currentAnimation][frameCounter % animations[currentAnimation].length], 0, 0);
popMatrix();
if (millis() - lastFrame > 1000 / this.fps) {
frameCounter++;
lastFrame = millis();
}
}
void changeAnim(int newAnim) {
currentAnimation = newAnim % animations.length;
}
}
Bird bird;
TimeBar timeline;
void setup() {
size(1300, 400);
bird = new Bird(width * 0.33, height * 0.5, "bird", 2, 3);
timeline = new TimeBar(100, 0.003, 2);
textAlign(CENTER, CENTER);
}
void draw() {
background(0);
bird.display();
timeline.display();
timeline.step();
}
void keyReleased() {
if (key == '1')
bird.changeAnim(1);
}
void mouseReleased() {
bird.changeAnim(0);
}
float barHeight = 0.6f;
float optimum = 0.2f;
float currentOffset = 0.0;
class TimeBar {
Token[] tokens;
float speed;
boolean paused = false;
TimeBar(int numberOfToken, float speed, int numberOfAnimations) {
tokens = new Token[numberOfToken];
this.speed = speed;
for (int i = 0; i < numberOfToken; i++) {
tokens[i] = new Token(random(1.0, 30.0), int(random(numberOfAnimations)));
}
}
void start() {
currentOffset = 0.0;
}
void pause() {
paused = !paused;
}
void step() {
currentOffset += speed;
}
void display() {
float tokensHeight = height * barHeight;
pushStyle();
strokeWeight(4);
stroke(#cccccc);
line(0, tokensHeight, width, tokensHeight);
pushMatrix();
float minDist = 1000;
int tokenAnim = -1;
translate(-width * currentOffset, tokensHeight);
for (int tokenN = 0; tokenN < tokens.length; tokenN++) {
tokens[tokenN].display();
if (dist(optimum, 0, tokens[tokenN].position - currentOffset, 0) < minDist && dist(optimum, 0, tokens[tokenN].position - currentOffset, 0) < 0.1) {
tokenAnim = tokens[tokenN].anim;
minDist = dist(optimum, 0, tokens[tokenN].position - currentOffset, 0);
}
}
println(tokenAnim);
popMatrix();
line(optimum * width, 0, optimum * width, height);
popStyle();
}
}
class Token {
float position;
int anim;
Token(float position, int anim) {
this.position = position;
this.anim = anim;
}
void display() {
pushMatrix();
translate(width * position, 0);
fill(128);
ellipse(0, 0, 30, 30);
fill(255);
textSize(30);
text("" + anim, 0, 0);
textSize(10);
text("" + dist(optimum, 0, position - currentOffset, 0), 0, 20);
popMatrix();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment