Skip to content

Instantly share code, notes, and snippets.

@darkwave
Last active August 29, 2015 14:08
Show Gist options
  • Save darkwave/9d8b3497bcb5aedf2b05 to your computer and use it in GitHub Desktop.
Save darkwave/9d8b3497bcb5aedf2b05 to your computer and use it in GitHub Desktop.
IndieVault Jam prototype
import fisica.*;
FWorld world;
FPoly poly;
PShape level;
FPoly target;
PImage splash;
float angle = 0;
boolean go, goLeft, goRight, started;
int food = 0;
float speed = 0;
int lastTime;
LevelManager lm;
void setup() {
size(1024, 768);
Fisica.init(this);
world = new FWorld(-10000, -10000, 10000, 10000);
world.setGravity(0, 0);
noStroke();
lm = new LevelManager();
lm.loadLevel("level_def.svg");
splash = loadImage("splash.png");
}
float life = 10;
void displaySonar(float power) {
pushMatrix();
translate(width / 2, height / 2);
fill(#330033);
float radius = 30 * life;
ellipse(0, 0, radius, radius);
fill(#330033, 128);
ellipse(0, 0, radius + (sin(radians(millis() * .05)) * power), radius + (sin(radians(millis() * .05)) * power));
popMatrix();
}
boolean win = false;
boolean gameover = false;
void draw() {
if (life < 3 && life != 0)
gameover = true;
if (win) {
fill (255);
text("YOU WIN!", width / 2, height / 2 - 200);
return;
}
if (gameover) {
fill (255);
text("YOU ARE A FOSSIL!", width / 2, height / 2 - 200);
return;
}
pushMatrix();
if (!started) {
lastTime = millis();
life = 0;
background(0);
displaySonar(width);
imageMode(CENTER);
float ratio = width / splash.width;
image(splash, width * .5, height * .5, width * .9, (splash.height * ratio) * .9);
} else {
imageMode(CORNER);
background(0);
displaySonar(300);
translate(width / 2 - poly.getX(), height / 2 - poly.getY());
world.step();
world.draw();
poly.setRotation(angle);
if (go && speed < 1000)
speed += 100;
if (speed > 10) {
poly.addImpulse(sin(angle) * speed, -cos(angle) * speed);
}
if (goLeft)
angle -= radians(1);
if (goRight)
angle += radians(1);
speed *= 0.6;
if (poly.isTouchingBody(target)) {
win = true;
//life -= 0.1;
}
if (millis() - lastTime > 1000) {
lastTime = millis();
life -= 0.2;
println(life);
for (FPoly enemy : enemies) {
float randomAngle = random(-PI, PI);
float randomSpeed = random(1000, 5000);
enemy.addImpulse(sin(randomAngle) * randomSpeed, -cos(randomAngle) * randomSpeed);
}
}
}
popMatrix();
//fill(255);
//text(food, 20, 20);
}
void contactStarted(FContact c) {
FBody ball = null;
if (c.getBody1() == poly) {
ball = c.getBody2();
} else if (c.getBody2() == poly) {
ball = c.getBody1();
}
if (ball == null) {
return;
}
if ( ball.getFillColor() == color(0, 128, 0)) {
world.remove(ball);
life++;
food--;
}
if ( ball.getFillColor() == color(1, 1, 1)) {
//world.remove(ball);
life -= 0.5;
}
}
void keyPressed() {
if (!started) {
started = true;
life = 10;
}
if (key == 's') {
save(millis() + ".png");
return;
}
if (keyCode == LEFT)
goLeft = true;
if (keyCode == RIGHT)
goRight = true;
if (keyCode == UP)
go = true;
}
void keyReleased() {
if (keyCode == UP)
go = false;
if (keyCode == LEFT)
goLeft = false;
if (keyCode == RIGHT)
goRight = false;
}
ArrayList<FPoly> enemies;
class LevelManager {
void loadLevel(String level) {
PShape levelLayer = loadShape(level).getChild(1);
enemies = new ArrayList<FPoly>();
for (int l = 0; l < levelLayer.getChildCount (); l++) {
// {
//println(levelLayer.getChild(l).getName());
FPoly obstacle = new FPoly();
obstacle.setStatic(true);
obstacle.setFill(0);
obstacle.setNoStroke();
PShape obstacleShape = levelLayer.getChild(l);
//println(levelLayer.getChild(l).getChildCount());
for (int v = 0; v < obstacleShape.getVertexCount (); v++) {
obstacle.vertex(obstacleShape.getVertex(v).x, obstacleShape.getVertex(v).y);
}
println(levelLayer.getChild(l).getName());
if (levelLayer.getChild(l).getName().indexOf("food") >= 0) {
obstacle.setFill(0, 128, 0);
food++;
}
if (levelLayer.getChild(l).getName().indexOf("dynamic") >= 0) {
obstacle.setStatic(false);
obstacle.setBullet(true);
}
if (levelLayer.getChild(l).getName().indexOf("sensor") >= 0) {
obstacle.setFill(255, 0, 0);
// obstacle.attachImage(loadImage("spugna.png"));
obstacle.setSensor(true);
target = obstacle;
}
if (levelLayer.getChild(l).getName().indexOf("enemy") >= 0) {
obstacle.setFill(1, 1, 1);
obstacle.setStatic(false);
enemies.add(obstacle);
}
if (levelLayer.getChild(l).getName().indexOf("player") >= 0) {
poly = new Player();
for (int v = 0; v < obstacleShape.getVertexCount (); v++) {
poly.vertex(obstacleShape.getVertex(v).x, obstacleShape.getVertex(v).y);
}
poly.setStrokeWeight(3);
poly.setFill(120, 30, 90);
poly.setDensity(10);
poly.setRestitution(0.5);
poly.attachImage(loadImage("kimberella.png"));
world.add(poly);
} else
world.add(obstacle);
//}
}
for (FPoly enemy : enemies) {
println(enemy.getX() + ", " + enemy.getY());
/* FDistanceJoint j = new FDistanceJoint(poly, enemy);
j.setLength(1);
//j.setNoStroke();
j.setStroke(255);
//j.setFill(0);
j.setDrawable(true);
j.setFrequency(0.1);
world.add(j);
*/
}
}
FPoly createGameObject(PShape objectShape) {
FPoly result = new FPoly();
return result;
}
}
class Player extends FPoly {
Player() {
super();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment