Advertisement
Collzi

World.java

Oct 4th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. package com.collzi.pong.model;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.Input.Keys;
  7. import com.badlogic.gdx.math.Rectangle;
  8. import com.badlogic.gdx.math.Vector2;
  9.  
  10. import com.collzi.pong.PongDesktop;
  11. import com.collzi.pong.view.WorldRenderer;
  12.  
  13. public class World {
  14.    
  15.     Player player;
  16.     Player bot;
  17.     Ball ball;
  18.     WorldRenderer renderer;
  19.  
  20.     Rectangle topRect = new Rectangle();
  21.     Rectangle bottomRect = new Rectangle();
  22.     ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
  23.    
  24.     private int playerScore = 0;
  25.     private int botScore = 0;
  26.     private float xd, yd;
  27.    
  28.     public Player getPlayer() { return player; }
  29.     public Player getBot() { return bot; }
  30.     public Ball getBall() { return ball; }
  31.     public Rectangle getTopBounds() { return topRect; }
  32.     public Rectangle getBottomBounds() { return bottomRect; }
  33.     public WorldRenderer getWorldRenderer() { return renderer; }
  34.     public ArrayList<Rectangle> getRectangles() { return rectangles; }
  35.    
  36.     public void setWorldRenderer(WorldRenderer renderer) { this.renderer = renderer; }
  37.    
  38.     public World() {
  39.         setWorldBounds();
  40.         player = new Player(new Vector2(8, (PongDesktop.HEIGHT / 2) - (Player.HEIGHT / 2 )));
  41.         bot = new Player(new Vector2(((PongDesktop.WIDTH - 8) - Player.WIDTH) , (PongDesktop.HEIGHT / 2) - (Player.HEIGHT /2)));
  42.         ball = new Ball(new Vector2(PongDesktop.WIDTH / 2, (PongDesktop.HEIGHT / 2 ) + 175));
  43.         rectangles.add(bottomRect);
  44.         rectangles.add(topRect);
  45.         rectangles.add(player.bounds);
  46.         rectangles.add(bot.bounds);
  47.     }
  48.    
  49.     private void setWorldBounds() {
  50.         // top
  51.         topRect.x = 5;
  52.         topRect.y = Gdx.graphics.getHeight() - 18;
  53.         topRect.width = Gdx.graphics.getWidth() - 10;
  54.         topRect.height = 13;
  55.         // bottom
  56.         bottomRect.x = 5;
  57.         bottomRect.y = 5;
  58.         bottomRect.width = Gdx.graphics.getWidth() - 10;
  59.         bottomRect.height = 13;
  60.     }
  61.    
  62.     public boolean checkGameOver() {
  63.         if ((playerScore < 10) && (botScore < 10)) {
  64.             if (ball.getBounds().x > (PongDesktop.WIDTH + 10)) {
  65.                 ball = new Ball(new Vector2(PongDesktop.WIDTH / 2, (PongDesktop.HEIGHT / 2) + 175));
  66.                 playerScore++;
  67.                 System.out.println("Point for player: " + playerScore);
  68.             } else if (ball.getBounds().x < -10) {
  69.                 ball = new Ball(new Vector2(PongDesktop.WIDTH / 2, (PongDesktop.HEIGHT / 2) + 175));
  70.                 botScore++;
  71.                 System.out.println("Point for bot: " + botScore);
  72.             }
  73.         } else {
  74.             player.getVelocity().y = 0;
  75.             ball.getVelocity().x = 0;
  76.             ball.getVelocity().y = 0;
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81.    
  82.     private void checkBallCollisions() {
  83.         for (Rectangle r: rectangles) {
  84.             if (ball.getBounds().overlaps(r)) {
  85.                 xd = Math.abs((ball.getBounds().x + ball.getBounds().width - r.x - r.width) / 2);
  86.                 yd = Math.abs((ball.getBounds().y + ball.getBounds().height - r.y - r.height) / 2);
  87.                
  88.                 if (xd > yd) {
  89.                     ball.getVelocity().y *= -1;
  90.                 } else if (yd > xd) {
  91.                     ball.getVelocity().x *= -1;
  92.                 } else {
  93.                     // corners, don't know what else to put, maybe random choice between x/y
  94.                     ball.getVelocity().y *= -1;
  95.                 }
  96.             }
  97.         }
  98.     }
  99.    
  100.     private void checkForInput() {
  101.         if (Gdx.input.isKeyPressed(Keys.UP)) {
  102.             if (!player.getBounds().overlaps(topRect))
  103.                 player.getVelocity().y = 1;
  104.             else
  105.                 player.getVelocity().y = 0;
  106.         } else if (!Gdx.input.isKeyPressed(Keys.UP) && player.getVelocity().y > 0)
  107.             player.getVelocity().y = 0;
  108.        
  109.         if (Gdx.input.isKeyPressed(Keys.DOWN)) {
  110.             if (!player.getBounds().overlaps(bottomRect))
  111.                 player.getVelocity().y = -1;
  112.             else
  113.                 player.getVelocity().y = 0;
  114.         } else if (!Gdx.input.isKeyPressed(Keys.DOWN) && player.getVelocity().y < 0)
  115.             player.getVelocity().y = 0;
  116.     }
  117.    
  118.     public void update(float delta) {
  119.         if (!checkGameOver()) {
  120.             checkForInput();
  121.             player.update(delta);
  122.             bot.update(delta);
  123.             ball.update(delta);
  124.             checkBallCollisions();
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement