Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.collzi.pong.model;
- import java.util.ArrayList;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.math.Vector2;
- import com.collzi.pong.PongDesktop;
- import com.collzi.pong.view.WorldRenderer;
- public class World {
- Player player;
- Player bot;
- Ball ball;
- WorldRenderer renderer;
- Rectangle topRect = new Rectangle();
- Rectangle bottomRect = new Rectangle();
- ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
- private int playerScore = 0;
- private int botScore = 0;
- private float xd, yd;
- public Player getPlayer() { return player; }
- public Player getBot() { return bot; }
- public Ball getBall() { return ball; }
- public Rectangle getTopBounds() { return topRect; }
- public Rectangle getBottomBounds() { return bottomRect; }
- public WorldRenderer getWorldRenderer() { return renderer; }
- public ArrayList<Rectangle> getRectangles() { return rectangles; }
- public void setWorldRenderer(WorldRenderer renderer) { this.renderer = renderer; }
- public World() {
- setWorldBounds();
- player = new Player(new Vector2(8, (PongDesktop.HEIGHT / 2) - (Player.HEIGHT / 2 )));
- bot = new Player(new Vector2(((PongDesktop.WIDTH - 8) - Player.WIDTH) , (PongDesktop.HEIGHT / 2) - (Player.HEIGHT /2)));
- ball = new Ball(new Vector2(PongDesktop.WIDTH / 2, (PongDesktop.HEIGHT / 2 ) + 175));
- rectangles.add(bottomRect);
- rectangles.add(topRect);
- rectangles.add(player.bounds);
- rectangles.add(bot.bounds);
- }
- private void setWorldBounds() {
- // top
- topRect.x = 5;
- topRect.y = Gdx.graphics.getHeight() - 18;
- topRect.width = Gdx.graphics.getWidth() - 10;
- topRect.height = 13;
- // bottom
- bottomRect.x = 5;
- bottomRect.y = 5;
- bottomRect.width = Gdx.graphics.getWidth() - 10;
- bottomRect.height = 13;
- }
- public boolean checkGameOver() {
- if ((playerScore < 10) && (botScore < 10)) {
- if (ball.getBounds().x > (PongDesktop.WIDTH + 10)) {
- ball = new Ball(new Vector2(PongDesktop.WIDTH / 2, (PongDesktop.HEIGHT / 2) + 175));
- playerScore++;
- System.out.println("Point for player: " + playerScore);
- } else if (ball.getBounds().x < -10) {
- ball = new Ball(new Vector2(PongDesktop.WIDTH / 2, (PongDesktop.HEIGHT / 2) + 175));
- botScore++;
- System.out.println("Point for bot: " + botScore);
- }
- } else {
- player.getVelocity().y = 0;
- ball.getVelocity().x = 0;
- ball.getVelocity().y = 0;
- return true;
- }
- return false;
- }
- private void checkBallCollisions() {
- for (Rectangle r: rectangles) {
- if (ball.getBounds().overlaps(r)) {
- xd = Math.abs((ball.getBounds().x + ball.getBounds().width - r.x - r.width) / 2);
- yd = Math.abs((ball.getBounds().y + ball.getBounds().height - r.y - r.height) / 2);
- if (xd > yd) {
- ball.getVelocity().y *= -1;
- } else if (yd > xd) {
- ball.getVelocity().x *= -1;
- } else {
- // corners, don't know what else to put, maybe random choice between x/y
- ball.getVelocity().y *= -1;
- }
- }
- }
- }
- private void checkForInput() {
- if (Gdx.input.isKeyPressed(Keys.UP)) {
- if (!player.getBounds().overlaps(topRect))
- player.getVelocity().y = 1;
- else
- player.getVelocity().y = 0;
- } else if (!Gdx.input.isKeyPressed(Keys.UP) && player.getVelocity().y > 0)
- player.getVelocity().y = 0;
- if (Gdx.input.isKeyPressed(Keys.DOWN)) {
- if (!player.getBounds().overlaps(bottomRect))
- player.getVelocity().y = -1;
- else
- player.getVelocity().y = 0;
- } else if (!Gdx.input.isKeyPressed(Keys.DOWN) && player.getVelocity().y < 0)
- player.getVelocity().y = 0;
- }
- public void update(float delta) {
- if (!checkGameOver()) {
- checkForInput();
- player.update(delta);
- bot.update(delta);
- ball.update(delta);
- checkBallCollisions();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement