/** * What happens with the widget when another widget touches it. * * @param other_body The body that touched it. * @param f The x,y coordinate that is the contact point. */ public void reactToTouchingBody(CollisionEvent e) { float speed = MOVEMENT_SPEED + (MOVEMENT_SPEED * bounces * 0.05f); if (currentDirection == Direction.WEST) { currentDirection = direction.EAST; body.setForce(2 * speed, 0); } else if (currentDirection == Direction.EAST) { currentDirection = Direction.WEST; body.setForce(-2 * speed, 0); } else if (currentDirection == Direction.NORTH) { currentDirection = Direction.SOUTH; body.setForce(0, 2 * speed); } else if (currentDirection == Direction.SOUTH) { currentDirection = direction.NORTH; body.setForce(0, -2 * speed); } bounces++; }
/** Activates the widget. */ public void activateWidget() { // Enforce that reset is called at least once before activation. if (!reset) { return; } // Make active. active = true; body.setEnabled(true); }
/** * Gets the boundary of the widget, should be used as a way to detect overlapping widgets. * * @return The four corners x,y coordinates. */ public Vector2f[] getBoundary() { Box box = (Box) body.getShape(); Vector2f[] bounds = new Vector2f[4]; bounds[0] = new Vector2f(position.x, position.y); bounds[1] = new Vector2f(position.x + WIDTH, position.y); bounds[2] = new Vector2f(position.x + WIDTH, position.y + HEIGHT); bounds[3] = new Vector2f(position.x, position.y + HEIGHT); return bounds; }
/** * 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); }
/** * Retrieves the height of the widget based on the specified boundary. * * @return The height of the widget (based on the boundary). */ private float getHeight() { return ((Box) body.getShape()).getSize().getY(); }
/** * Retrieves the width of the widget based on the specified boundary. * * @return The width of the widget (based on the boundary). */ private float getWidth() { return ((Box) body.getShape()).getSize().getX(); }
/** 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()); }
/** * Get the position of the widget. * * @return The x,y coordinates within the container. */ public Vector2f getPosition() { return new Vector2f(body.getPosition()); }
/** * Set the position of the widget. * * @param f The x,y coordinates within the container. */ public void setPosition(Vector2f f) { body.setPosition(f.x + WIDTH / 2, f.y + HEIGHT / 2); position = f; }