import org.jbox2d.common.Vec2; import org.jbox2d.dynamics.Body; import pulpcore.Input; import pulpcore.Stage; import pulpcore.sprite.Button; import pulpcore.sprite.ImageSprite; import pulpcore.sprite.Sprite; import pulpfizz.physics.BodyUtils; import pulpfizz.physics.NewWorld; import pulpfizz.pulp.CameraControlledScene; import pulpfizz.pulp.body.Java2DBodySprite; import pulpfizz.pulp.body.PhysicsLayer; public class GameScene extends CameraControlledScene { private PhysicsLayer physics; private Hand hand; private Button practice; public static final String IMG_LOC = "img/"; Menus.MainMenu menu; private ImageSprite titleSprite; ToyBox box; private Button home; private Button next; private Java2DBodySprite borderSprite; @Override public void load() { super.load(); Stage.setFrameRate(60); getCamera().setRespondToInput(false); ImageSprite is = new ImageSprite(IMG_LOC + "bg.jpg", 0, -20); add(is); physics = new PhysicsLayer(40.0); physics.setAnchor(Sprite.SOUTH); add(physics); physics.setSize(Stage.getWidth(), Stage.getHeight() * 10f); physics.setLocation(Stage.getWidth() / 2f, Stage.getHeight()); resetWorld(); hand = new BlokksHand(physics, this); addToHUD(hand); Blokk.BodyShapes[] values = Blokk.BodyShapes.values(); for (int i = 0; i < values.length; i++) { box.getContents().add(values[i]); } setCursor(Input.CURSOR_OFF); titleSprite = new ImageSprite(IMG_LOC + "drawing.png", 0, -170); add(titleSprite); menu = new Menus.MainMenu(); addToHUD(menu); menu.setLocation(Stage.getWidth() / 2, Stage.getHeight() / 2); home = Button.createLabeledButton("Main Menu", 30, Stage.getHeight() - 5); home.setAnchor(Sprite.SOUTH_WEST); addToHUD(home); next = Button.createLabeledButton("Skip", Stage.getWidth() - 30, Stage.getHeight() - 5); next.setAnchor(Sprite.SOUTH_EAST); addToHUD(next); loadHomeButtons(); } private void createWorld() { physics.setGravity(new Vec2(0, -20.0f)); NewWorld world = physics.getWorld(); world.setContinuousPhysics(true); world.setWorldIterations(20); // world.setSpeedMultiplier(world.getSpeedMultiplier()*2f); /* Add a physical border to the world so our objects can't escape. */ float h = physics.getPhysicsHeight(); float w = physics.getPhysicsWidth(); Body l = BodyUtils.createBox(world, -w/2f-2, 0, 5, h); Body r = BodyUtils.createBox(world, w/2f+2, 0, 5, h); Body b = BodyUtils.createBox(world, 0, -h/2f, w+3, 2.5f); physics.add(new Java2DBodySprite(l,physics,0.25f)); physics.add(new Java2DBodySprite(r,physics,0.25f)); physics.add(new Java2DBodySprite(b,physics,0.25f)); /* * Expand the world a bit, so mouse movements near the edge don't do anything nasty. */ physics.getWorld().getAABB().lowerBound.subLocal(new Vec2(100, 100)); physics.getWorld().getAABB().upperBound.addLocal(new Vec2(100, 100)); } int challengeCount = 1; @Override public void update(int elapsedTime) { super.update(elapsedTime); if (menu.practice.isClicked()) { resetWorld(); box.setInfinite(true); loadPracticeButtons(); } else if (menu.challenge.isClicked()) { challengeCount = 1; resetWorld(); Challenges.challenge(physics, box, challengeCount++); loadChallengeButtons(); } else if (home.isClicked()) { resetWorld(); loadHomeButtons(); } else if (next.isClicked()) { if (challengeCount == 5) challengeCount = 1; resetWorld(); Challenges.challenge(physics, box, challengeCount++); } } synchronized void resetWorld() { physics.removeAll(); physics.getWorld().destroy(); physics.getWorld().unregisterPostStep(box); createWorld(); Vec2 boxPos = new Vec2(-physics.getPhysicsWidth() / 2 + 1, -physics.getPhysicsHeight() / 2 + 2); box = new ToyBox(physics, boxPos.x, boxPos.y); } boolean swipeClean; void loadPracticeButtons() { home.visible.set(true); titleSprite.alpha.animateTo(0, 0); menu.alpha.animateTo(0, 0); } void loadChallengeButtons() { home.visible.set(true); titleSprite.alpha.animateTo(0, 0); menu.alpha.animateTo(0, 0); next.visible.set(true); } void loadHomeButtons() { home.visible.set(false); next.visible.set(false); titleSprite.alpha.animateTo(255, 200); menu.alpha.animateTo(255, 200); } }