/* * Nothing important here... */ package org.hiddenbox.test; import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.BasicGame; import org.newdawn.slick.Color; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.geom.Polygon; import net.phys2d.math.Vector2f; import net.phys2d.raw.Body; import net.phys2d.raw.BodyList; import net.phys2d.raw.StaticBody; import net.phys2d.raw.shapes.Box; import net.phys2d.raw.World; import net.phys2d.raw.strategies.QuadSpaceStrategy; /** * Simple phys2d box test. * @author Thomas Hourdel */ public class BoxHell extends BasicGame { private GameContainer container; private World world; private Body floor; private int box_nbr = 0; private int remainder = 0; private int step = 5; /** * Constructor. */ public BoxHell() { super("Physic Test - Box Hell"); } /** * Initialize in-game elements. * @param container * @throws org.newdawn.slick.SlickException */ @Override public void init(GameContainer c) throws SlickException { this.container = c; world = new World(new Vector2f(0.0f, 10.0f), 8, new QuadSpaceStrategy(20, 20)); reset(); } /** * Update entities & physics. * @param container * @param delta * @throws org.newdawn.slick.SlickException */ @Override public void update(GameContainer container, int delta) throws SlickException { Input input = container.getInput(); delta += remainder; remainder = delta % step; for(int i = 0; i < delta / step; i++) { // Spawn boxes on SPACE down if(input.isKeyDown(Input.KEY_SPACE) && box_nbr < 3) { Body body = new Body("box", new Box(20.0f, 20.0f), 50.0f); body.setPosition(-50, (float) (((Math.random() * 60) + 50))); body.setFriction(0.7f); body.setDamping(0.0f); world.add(body); body.adjustAngularVelocity(1); body.adjustVelocity(new Vector2f(40,-(float)(Math.random() * 20 + 20))); box_nbr++; } // Physic step world.step(step / 60.0f); } } /** * Reset the game physics. */ public void reset() { world.clear(); box_nbr = 0; floor = new StaticBody("floor", new Box(900f, 30f)); floor.setPosition(450f, 435f); floor.setFriction(0.9f); world.add(floor); } /** * Where the magic happens... Render to screen. * @param container * @param g * @throws org.newdawn.slick.SlickException */ @Override public void render(GameContainer container, Graphics g) throws SlickException { // Background g.setColor(new Color(75,75,75)); g.fillRect(0, 0, 900, 450); // Bodies g.setColor(new Color(0,0,0)); BodyList bodies = world.getBodies(); Polygon p; Vector2f bounds[]; for(int i = 0; i < bodies.size(); i++) { Body body = bodies.get(i); bounds = ((Box)(body.getShape())).getPoints(body.getPosition(), body.getRotation()); p = new Polygon(); p.addPoint(bounds[0].x, bounds[0].y); p.addPoint(bounds[1].x, bounds[1].y); p.addPoint(bounds[2].x, bounds[2].y); p.addPoint(bounds[3].x, bounds[3].y); g.fill(p); } } /** * Handle key pressed events. * @param key * @param c */ @Override public void keyPressed(int key, char c) { if(key == Input.KEY_ESCAPE) { container.exit(); } if(key == Input.KEY_R) { reset(); } } /** * Entry point. * @param args the command line arguments */ public static void main(String args[]) { try { AppGameContainer app = new AppGameContainer(new BoxHell(), 900, 450, false); app.start(); } catch(SlickException e) { e.printStackTrace(); } } }