Message
Message
Screens;
import com.badlogic.angrybirds.*;
import com.badlogic.angrybirds.Scenes.Hud;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import java.util.ArrayList;
import java.util.List;
@Override
public void show() {
Gdx.input.setInputProcessor(hud.stage);
// Set up the first bird immediately
setupNextBird();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
game.batch.begin();
game.batch.draw(playBG, 0, 0, AngryBirds.V_WIDTH, AngryBirds.V_HEIGHT);
drawGameObjects();
game.batch.end();
hud.stage.act(delta);
hud.stage.draw();
b2dr.render(world, gamecam.combined);
// Check and remove birds that have stopped moving or gone out of the
screen
if (currentBird != null && currentBird.isLaunched() &&
(hasBirdStopped(currentBird) || isBirdOutOfScreen(currentBird))) {
world.destroyBody(currentBird.getBody());
currentBird = null;
isBirdOnCatapult = false;
setupNextBird();
}
handleInput();
handleCollisions();
if (level.getPigs().isEmpty()) {
game.setScreen(new WinScreen(game, hud.getScore()));
dispose();
}
@Override
public void resize(int width, int height) {
gamePort.update(width, height, true);
hud.resize(width, height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
world.dispose();
b2dr.dispose();
}
// Create blocks
for (Block block : level.getBlocks()) {
block.createBody(world, 1f, 1f);
}
}
private void drawGameObjects() {
world.getBodies(worldBodies);
// 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 : level.getBirds()) {
Sprite sprite = new Sprite(bird.getTexture());
sprite.setSize(bird.getTexture().getWidth() * 0.7f,
bird.getTexture().getHeight() * 0.7f);
sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
sprite.setPosition(50 + level.getBirds().indexOf(bird) * 50,
birdYPosition); // Adjust the x position as needed
sprite.draw(game.batch);
}
}
if (!isDragging) {
// Check if the touch position is within the bird's body bounds
if
(currentBird.getBody().getFixtureList().first().testPoint(touchPos)) {
initialTouch.set(Gdx.input.getX(), Gdx.input.getY());
isDragging = true;
}
} else {
Vector2 currentTouch = new Vector2(Gdx.input.getX(),
Gdx.input.getY());
Vector2 dragDistance = currentTouch.sub(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
dragDistance.scl(dragScaleFactor);
currentBird.getBody().setTransform(new
Vector2(level.getCatapult().getX() / AngryBirds.PPM,
level.getCatapult().getTexture().getHeight() /
AngryBirds.PPM).sub(dragDistance), 0);
}
} else if (isDragging) {
isDragging = false;
Vector2 launchDirection = new Vector2(level.getCatapult().getX() /
AngryBirds.PPM,
level.getCatapult().getTexture().getHeight() /
AngryBirds.PPM).sub(currentBird.getBody().getPosition());
currentBird.getBody().setType(BodyDef.BodyType.DynamicBody); // Set
to KinematicBody
launchBird(currentBird, launchDirection);
isBirdOnCatapult = false;
}
}
}
world.createBody(groundBodyDef).createFixture(groundFixture);
groundShape.dispose();
}