package [Link].
Screens;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link].GL20;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].Vector2;
import [Link].Vector3;
import [Link].box2d.*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class PlayScreen implements Screen {
public final static float GROUND_Y_PIXELS = AngryBirds.V_HEIGHT * 0.137777f;
private int score = 0;
private final float TIME_STEP = 1 / 144f;
private final int VELOCITY_ITERATIONS = 8;
private final int POSITION_ITERATIONS = 3;
private AngryBirds game;
private Level level;
Texture playBG;
private OrthographicCamera gamecam;
private Viewport gamePort;
Hud hud;
private World world;
private Box2DDebugRenderer b2dr;
private Array<Body> worldBodies = new Array<Body>();
private DrawBody drawBody;
private Bird currentBird;
private boolean isBirdOnCatapult = false;
private boolean isDragging = false;
private Vector2 initialTouch = new Vector2();
private Vector2 maxDragDistance = new Vector2(100, 100); // Adjust as needed
private CollisionListener collisionListener;
public PlayScreen(AngryBirds game, Level level) {
[Link] = game;
[Link] = level;
playBG = new Texture("backgrounds/[Link]");
gamecam = new OrthographicCamera(AngryBirds.V_WIDTH / [Link],
AngryBirds.V_HEIGHT / [Link]);
gamePort = new FitViewport(AngryBirds.V_WIDTH / [Link],
AngryBirds.V_HEIGHT / [Link], gamecam);
hud = new Hud([Link], game, this);
world = new World(new Vector2(0, -10f), true);
b2dr = new Box2DDebugRenderer();
drawBody = new DrawBody(world, b2dr);
collisionListener = new CollisionListener(hud);
[Link](collisionListener);
// Create ground
createGround();
// Create game objects
createGameObjects();
}
@Override
public void show() {
[Link]([Link]);
// Set up the first bird immediately
setupNextBird();
}
@Override
public void render(float delta) {
[Link](0, 0, 0, 1);
[Link](GL20.GL_COLOR_BUFFER_BIT);
[Link]([Link]().combined);
[Link]();
[Link](playBG, 0, 0, AngryBirds.V_WIDTH, AngryBirds.V_HEIGHT);
// Draw the catapult
Catapult catapult = [Link]();
Sprite sprite = new Sprite([Link]());
[Link]([Link]().getWidth() * 0.6f,
[Link]().getHeight() * 0.6f);
[Link]([Link]() / 2, [Link]() / 2);
[Link]([Link](), [Link]());
[Link]([Link]);
drawGameObjects();
[Link]();
[Link](delta);
[Link]();
[Link](TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
[Link](world, [Link]);
// Check and remove birds that have stopped moving or gone out of the
screen
if (currentBird != null && [Link]() &&
(hasBirdStopped(currentBird) || isBirdOutOfScreen(currentBird))) {
[Link]([Link]());
currentBird = null;
isBirdOnCatapult = false;
setupNextBird();
}
List<Pig> pigsToRemove = new ArrayList<>();
for (Pig pig : [Link]()) {
if(isPigOutOfScreen(pig)){
[Link]([Link]());
[Link](pig);
}
}
[Link]().removeAll(pigsToRemove);
[Link]();
handleInput();
handleCollisions();
if ([Link]().isEmpty()) {
[Link](new WinScreen(game, [Link]()));
dispose();
}
// Check for lose condition
if ([Link]().isEmpty() && currentBird == null && !
[Link]().isEmpty()) {
[Link](new LoseScreen(game, [Link]()));
dispose();
}
}
@Override
public void resize(int width, int height) {
[Link](width, height, true);
[Link](width, height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
[Link]();
[Link]();
}
private void createGameObjects() {
// Create pigs
for (Pig pig : [Link]()) {
[Link](world, 0.7f, 0.7f);
}
// Create blocks
for (Block block : [Link]()) {
[Link](world, 1f, 1f);
}
}
private void drawGameObjects() {
[Link](worldBodies);
for (Body body : worldBodies) {
if ([Link]() != null){
Sprite sprite = null;
if([Link]() instanceof Bird){
Bird bird = (Bird) [Link]();
sprite = [Link]();
} else if ([Link]() instanceof Block){
Block block = (Block) [Link]();
sprite = [Link]();
} else if ([Link]() instanceof Pig){
Pig pig = (Pig) [Link]();
sprite = [Link]();
}
[Link]([Link]().x * [Link] -
[Link]() / 2,
[Link]().y * [Link] - [Link]() /
2);
[Link]([Link]() * [Link]);
[Link]([Link]);
}
}
// Draw birds that are not on the catapult below the ground
float birdYPosition = GROUND_Y_PIXELS - 50; // Adjust this value to set the
position below the ground
for (Bird bird : [Link]()) {
Sprite sprite = new Sprite([Link]());
[Link]([Link]().getWidth() * 0.7f,
[Link]().getHeight() * 0.7f);
[Link]([Link]() / 2, [Link]() / 2);
[Link](50 + [Link]().indexOf(bird) * 50,
birdYPosition); // Adjust the x position as needed
[Link]([Link]);
}
}
private void handleInput() {
if (currentBird != null && ![Link]()) {
if ([Link]()) {
Vector3 touchPos3D = new Vector3([Link](),
[Link](), 0);
[Link](touchPos3D);
Vector2 touchPos = new Vector2(touchPos3D.x, touchPos3D.y);
if (!isDragging) {
// Check if the touch position is within the bird's body bounds
if
([Link]().getFixtureList().first().testPoint(touchPos)) {
[Link]([Link](), [Link]());
isDragging = true;
}
} else {
Vector2 currentTouch = new Vector2([Link](),
[Link]());
Vector2 dragDistance = [Link](initialTouch);
dragDistance.x = -dragDistance.x; // Invert the x-axis movement
// Scale the drag distance
float dragScaleFactor = 0.025f; // Adjust this value to scale
the speed
[Link](dragScaleFactor);
// Limit the drag distance
float maxDragLength = 1.2f; // Adjust this value to set the
maximum drag length
if ([Link]() > maxDragLength) {
[Link](maxDragLength);
}
[Link]().setTransform(new
Vector2([Link]().getX() / [Link],
[Link]().getTexture().getHeight() /
[Link]).sub(dragDistance), 0);
}
} else if (isDragging) {
isDragging = false;
Vector2 launchDirection = new Vector2([Link]().getX() /
[Link],
[Link]().getTexture().getHeight() /
[Link]).sub([Link]().getPosition());
[Link]().setType([Link]); // Set
to KinematicBody
launchBird(currentBird, launchDirection);
isBirdOnCatapult = false;
}
}
}
private void setupNextBird() {
if (!isBirdOnCatapult && ![Link]().isEmpty()) {
currentBird = [Link]().remove(0);
[Link](world, 0.7f, 0.7f); // Create the bird body
[Link]().setType([Link]); // Set
to KinematicBody
[Link]().setTransform(new
Vector2([Link]().getX() / [Link] + 0.5f,
[Link]().getTexture().getHeight() / [Link]), 0);
isBirdOnCatapult = true;
}
}
private void launchBird(Bird bird, Vector2 launchDirection) {
if (![Link]()) {
float launchForceMagnitude = [Link]() * 20f; // Adjust the
force multiplier as needed
Vector2 launchForce = [Link]().scl(launchForceMagnitude);
[Link]().applyLinearImpulse(launchForce,
[Link]().getWorldCenter(), true);
[Link](true);
}
}
private boolean hasBirdStopped(Bird bird) {
float velocityThreshold = 0.05f; // Adjust this value as needed
return [Link]().getLinearVelocity().len() < velocityThreshold;
}
private boolean isBirdOutOfScreen(Bird bird) {
float birdX = [Link]().getPosition().x * [Link];
float birdY = [Link]().getPosition().y * [Link];
return birdX < -50 || birdX > AngryBirds.V_WIDTH+50 || birdY < -50;
}
private boolean isPigOutOfScreen(Pig pig) {
float pigX = [Link]().getPosition().x * [Link];
float pigY = [Link]().getPosition().y * [Link];
return pigX < -50 || pigX > AngryBirds.V_WIDTH + 50 || pigY < -50;
}
private void handleCollisions() {
Array<GameObject> objectsToDestroy =
[Link]();
for (GameObject gameObject : objectsToDestroy) {
if (gameObject instanceof Bird) {
Bird bird = (Bird) gameObject;
[Link]().remove(bird);
[Link]([Link]());
} else if (gameObject instanceof Block) {
Block block = (Block) gameObject;
[Link]().remove(block);
[Link]([Link]());
} else if (gameObject instanceof Pig) {
Pig pig = (Pig) gameObject;
[Link]().remove(pig);
[Link]([Link]());
}
}
[Link]();
}
private void createGround() {
BodyDef groundBodyDef = new BodyDef();
[Link] = [Link];
[Link](new Vector2(0, GROUND_Y_PIXELS /
[Link]));
ChainShape groundShape = new ChainShape();
[Link](new Vector2[]{
new Vector2(0, 0),
new Vector2(AngryBirds.V_WIDTH / [Link], 0)
});
FixtureDef groundFixture = new FixtureDef();
[Link] = groundShape;
[Link] = 1f;
[Link] = 0.3f;
[Link](groundBodyDef).createFixture(groundFixture);
[Link]();
}
public Level getLevel() {
return new Level([Link]());
}
}