Your browser does not support the canvas tag.

HugoBranco_AsteroidAttractor

ONLY WORKS OFFLINE PLEASE DOWNLOAD .ZIP FILE


https://www.dropbox.com/s/dpbdgzq9vebuue1/asteroidhugobranco.zip?dl=0
Abrir em modo Java, obrigado!
Hugo Branco

Interação:
Clica e arrasta o rato para criares forças de atracção!
250 Asteroides Caidos e o jogo acaba.
Ganha quem aguentar mais tempo!

Descrição:
ASTEROID ATRACTOR
Mini jogo em que o planeta Terra está a sofrer com uma grave chuva de meteoros,
A tecnologia do futuro permitiu construir uma máquina que gera gravidade...
foi a única solução na tentativa desesperada de salvar o Planeta, porém a máquina
apenas funciona alguns segundos... ESTAMOS CONDENADOS!
http://hugobranco.weebly.com 



Notas para executar este trabalho.

  Na sua instalaçao de Processing

  1. instalar a biblioteca Box2D-for-Processing
  https://github.com/shiffman/Box2D-for-Processing

  2. copiar o codigo abaixo para um sketch novo

  3. correr o sketch :)


import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

// A reference to our box2d world
Box2DProcessing box2d;

// A list we'll use to track fixed objects
ArrayList boundaries;
// A list for all of our rectangles
ArrayList boxes;
ArrayList geradores;

//PImage image;
PImage bg;

int numerodecaixascaidas = 0;
float tempodejogosegs = 0.0f;

//gravação em sequencia de imagens
boolean gravar = false;
int gravar_indice = 0;

void setup() {
  size(1280,720);
  bg = loadImage("bg.jpg");
  smooth();
  //image = loadImage("image.png");
  // Initialize box2d physics and create the world
  box2d = new Box2DProcessing(this);
  box2d.createWorld();
  // We are setting a custom gravity
  box2d.setGravity(0, -10);

  // Create ArrayLists
  boxes = new ArrayList();
  boundaries = new ArrayList();
  geradores = new ArrayList();

criarChao(150, 50, 5, 2);
criarChao(300, 80, 5, 2);
criarChao(500, 30, 5, 5);
criarChao( height - 5 - 3, 25, 5, 6);

}

void draw() {
  background(bg);

  // We must always step through time!
  box2d.step();

  // When the mouse is clicked, add a new Box object
  if (random(1) < 0.1) {
    Box p = new Box(random(width),10);
    boxes.add(p);
  }

  if (mousePressed && frameCount%10==0) {
//        Box p = new Box(mouseX, mouseY);
//  boxes.add(p);

  GForce g = new GForce(mouseX, mouseY, random(50,200));
  geradores.add( g );

    for (Box b: boxes) {
     Vec2 wind = new Vec2(10,0);
     b.applyForce(wind);
    }
  }

  // Display all the boundaries
  for (Boundary wall: boundaries) {
    wall.display();
  }

  // Display all the boxes
  for (GForce g: geradores) {
   g.update();

   g.display();
}

   for (int i = geradores.size()-1; i >= 0; i--) {


    if(geradores.get(i).life<0){

     geradores.remove(i);
    }

   }


     // Display all the boxes
  for (Box b: boxes) {
    b.display();
  }

  // Boxes that leave the screen, we delete them
  // (note they have to be deleted from both the box2d world and our list
  for (int i = boxes.size()-1; i >= 0; i--) {
    Box b = boxes.get(i);
    if (b.done()) {
      boxes.remove(i);
      numerodecaixascaidas++;


      if(numerodecaixascaidas>250) { // exit();
   println ("segundos: " +tempodejogosegs);
   exit ();
    }
    }
  }
  fill(255);


  tempodejogosegs = millis( ) * 0.001f;

  String txt = "ASTEROID ATRACTOR\nAsteroids: " + boxes.size( )
  + "\nFallen Asteroids: " + numerodecaixascaidas
  + "\nTime : " + tempodejogosegs
//  + "\nRecording : " + gravar_indice
  ;

  text(txt ,20,20);
}





// A fixed boundary class
class Boundary {

  // A boundary is a simple rectangle with x,y,width,and height
  float x;
  float y;
  float w;
  float h;


  // But we also have to make a body for box2d to know about it
  Body b;

  Boundary(float x_,float y_, float w_, float h_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;


    // Define the polygon
    PolygonShape sd = new PolygonShape();
    // Figure out the box2d coordinates
    float box2dW = box2d.scalarPixelsToWorld(w/2);
    float box2dH = box2d.scalarPixelsToWorld(h/2);
    // We're just a box
    sd.setAsBox(box2dW, box2dH);


    // Create the body
    BodyDef bd = new BodyDef();
    bd.type = BodyType.STATIC;
    bd.position.set(box2d.coordPixelsToWorld(x,y));
    b = box2d.createBody(bd);

    // Attached the shape to the body using a Fixture
    b.createFixture(sd,1);
  }

  // Draw the boundary, if it were at an angle we'd have to do something fancier
  void display() {
    fill(#E3BA79);
    stroke(20);
    rectMode(CENTER);
    rect(x,y,w,h);


  }

}



// A rectangular box
class Box {

  // We need to keep track of a Body and a width and height
  Body body;
  float w;
  float h;

PImage imagem;
  // Constructor
  Box(float x, float y) {
    int modo = (int) random(3);
    if(modo==0) imagem = loadImage("meteoro0.png");
    else if(modo==1) imagem = loadImage("meteoro1.png");
    else if(modo==2) imagem = loadImage("meteoro2.png");

    w = random(25, 60);
    h = w;
    // Add the box to the box2d world
    makeBody(new Vec2(x, y), w, h);
  }

  // This function removes the particle from the box2d world
  void killBody() {
    box2d.destroyBody(body);
  }

  // Is the particle ready for deletion?
  boolean done() {
    // Let's find the screen position of the particle
    Vec2 pos = box2d.getBodyPixelCoord(body);
    // Is it off the bottom of the screen?
    if (pos.y > height+w*h) {
      killBody();
      return true;
    }
    return false;
  }

  void applyForce(Vec2 force) {
    Vec2 pos = body.getWorldCenter();
    body.applyForce(force, pos);
  }

  // Drawing the box
  void display() {
    // We look at each body and get its screen position
    Vec2 pos = box2d.getBodyPixelCoord(body);
    // Get its angle of rotation
    float a = body.getAngle();

    imageMode(CENTER);
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(-a);
    fill(175);
    noStroke();
    //rect(0, 0, w, h);
    image(imagem, 0, 0, w, h);
    popMatrix();
  }

  // This function adds the rectangle to the box2d world
  void makeBody(Vec2 center, float w_, float h_) {

    // Define a polygon (this is what we use for a rectangle)
    PolygonShape sd = new PolygonShape();
    float box2dW = box2d.scalarPixelsToWorld(w_/2);
    float box2dH = box2d.scalarPixelsToWorld(h_/2);
    sd.setAsBox(box2dW, box2dH);

    // Define a fixture
    FixtureDef fd = new FixtureDef();
    fd.shape = sd;
    // Parameters that affect physics
    fd.density = 1;
    fd.friction = 0.3;
    fd.restitution = 0.2;

    // Define the body and make it from the shape
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    bd.position.set(box2d.coordPixelsToWorld(center));
    bd.angle = random(TWO_PI);

    body = box2d.createBody(bd);
    body.createFixture(fd);
  }
}

void criarChao(float y, float w, float h, int numplacas ){
  for (int i = 0; i < numplacas; i++) {
    float x = random( 0, width );
    //float y = height - h;
    boundaries.add(new Boundary(x,y,w,h));
  }
}

// A rectangular box
class GForce {

  // We need to keep track of a Body and a width and height
 // Body body;
  float w;
  float h;
  float x, y;
  float life = 255;
  // Constructor
  GForce(float x, float y, float rad) {
    w = rad;
    h = rad;
    this.x = x;
    this.y = y;
    // Add the box to the box2d world
   // makeBody(new Vec2(x, y), w, h);
  }

void update ( ) {
  life -= 2;
  // distancias
   for (Box b: boxes) {

     Vec2 pos = box2d.getBodyPixelCoord(b.body);

     Vec2 delta = new Vec2();
     delta.x = pos.x - x;
     delta.y = pos.y - y;


 float distancia = sqrt( delta.x*delta.x + delta.y*delta.y);

float soma = b.w + w;
if( distancia < soma) {

  delta.x *=-3.5;
  delta.y *=3;

   b.applyForce(delta);
}

}
}

  void display() {
   fill(0,255,255,100);
    noStroke();
    ellipse(x,y,w,h);

  }
}

Source code: HugoBranco_AsteroidAttractor

Built with Processing and Processing.js