/** Creates a new bee widget. */ public HappyMagicalRainbowBee() { shape = new Box(WIDTH, HEIGHT); body = new Body(shape, MASS); loadImages(); icon = new ImageIcon(); icon.setImage(frames.get(0)); setPosition(new Vector2f(0, 0)); }
/** * Draws the widget. * * @param g The object that draws things. */ public void draw(Graphics2D screen) { // Animate! if (System.nanoTime() - timestamp >= CENTISECOND * 50) { timestamp = System.nanoTime(); if (image == frames.get(0)) { image = frames.get(1); } else { image = frames.get(0); } } ROVector2f position = body.getPosition(); float angle = body.getRotation(); boolean hflip = false; boolean vflip = false; if (currentDirection == Direction.WEST) { hflip = false; } else if (currentDirection == Direction.EAST) { hflip = true; } else if (currentDirection == Direction.NORTH) { angle += Math.PI / 2; vflip = false; } else if (currentDirection == Direction.SOUTH) { angle += Math.PI / 2; vflip = true; } drawImage( position.getX(), position.getY(), getWidth(), getHeight(), angle, hflip, vflip, image, screen); }
/** A static method for initialing all the images associated with this widget */ private static void loadImages() { // Already in cache, save us from file IO time. if (frames != null) { return; } // Okay, it's not in cache, load the images. try { frames = new ArrayList<BufferedImage>(); for (int i = 0; i < 2; i++) { frames.add(fixTransparency(ImageIO.read(new File(IMAGE_PREFIX + (i + 1) + ".png")))); } } catch (IOException e) { throw new RuntimeException(e); } }
/** Resets the widget to start state. */ public void resetWidget() { // Reset has been made, so we CAN activate this later. reset = true; // Make inactive active = false; // Not collided. collided = false; bounces = 0; currentDirection = direction; image = frames.get(0); body.setEnabled(false); // Move to initial position. body.set(shape, MASS); body.setPosition(position.x + WIDTH / 2, position.y + HEIGHT / 2); // Make sure the gameplay didn't mess with any initial properties. body.setCanRest(true); body.setDamping(0.0f); body.setFriction(0.01f); body.setGravityEffected(false); body.setIsResting(true); body.setMoveable(true); body.setRestitution(1.5f); body.setRotatable(true); body.setRotation(0.0f); body.setRotDamping(0.0f); body.setMaxVelocity(50f, 50f); body.setForce(-body.getForce().getX(), -body.getForce().getY()); if (currentDirection == Direction.WEST) { body.setForce(-MOVEMENT_SPEED, 0); } else if (currentDirection == Direction.EAST) { body.setForce(MOVEMENT_SPEED, 0); } if (currentDirection == Direction.NORTH) { body.setForce(0, -MOVEMENT_SPEED); } else if (currentDirection == Direction.SOUTH) { body.setForce(0, MOVEMENT_SPEED); } body.adjustVelocity(new Vector2f(-body.getVelocity().getX(), -body.getVelocity().getY())); body.adjustAngularVelocity(-body.getAngularVelocity()); }