Package ponkOut.logic

Source Code of ponkOut.logic.Block

/* Copyright 2010, 2013 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut.  If not, see <http://www.gnu.org/licenses/>.
*/

package ponkOut.logic;

import java.util.Comparator;

import org.lwjgl.util.vector.Vector2f;

import ponkOut.graphics.BlockGO;
import ponkOut.graphics.GraphicsObjectsManager;

public class Block extends Entity {
  public enum CornerPlace {
    TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
  };

  public enum FacePlace {
    LEFT, RIGHT, TOP, BOTTOM
  };

  protected BlockGO graphicsObject;
  private GraphicsObjectsManager goManager;
  private float width;
  private float height;

  /**
   * left face this blocks belongs to or null if it does not belong to a
   * BlockFace
   */
  private BlockFace leftFace;
  private BlockFace rightFace;
  private BlockFace topFace;
  private BlockFace bottomFace;

  /**
   * orders blocks from left to right
   */
  protected static final Comparator<Block> horizontalComperator = new Comparator<Block>() {
    @Override
    public int compare(Block block1, Block block2) {
      float dx = (block1.getPosition().x - block2.getPosition().x);
      if (dx > 0)
        return 1;
      else if (dx < 0)
        return -1;
      else
        return 0;
    }
  };

  /**
   * orders blocks from top to bottom
   */
  protected static final Comparator<Block> verticalComparator = new Comparator<Block>() {
    @Override
    public int compare(Block block1, Block block2) {
      float dy = (block2.getPosition().y - block1.getPosition().y);
      if (dy > 0)
        return 1;
      else if (dy < 0)
        return -1;
      else
        return 0;
    }
  };

  public Block(Vector2f position, float width, float height, GraphicsObjectsManager goManager) {
    setPosition(position);
    this.width = width;
    this.height = height;
    this.goManager = goManager;
    leftFace = null;
    rightFace = null;
    topFace = null;
    bottomFace = null;

    EntityManager.getInstance().addBlock(this);
    graphicsObject = new BlockGO(this, goManager);
  }

  @Override
  public boolean isMovable() {
    return false;
  }

  public Vector2f getCornerPos(CornerPlace corner) {
    Vector2f p = getPosition();
    switch (corner) {
    case TOP_LEFT:
      return new Vector2f(p.x - width / 2.0f, p.y + height / 2.0f);
    case TOP_RIGHT:
      return new Vector2f(p.x + width / 2.0f, p.y + height / 2.0f);
    case BOTTOM_LEFT:
      return new Vector2f(p.x - width / 2.0f, p.y - height / 2.0f);
    case BOTTOM_RIGHT:
      return new Vector2f(p.x + width / 2.0f, p.y - height / 2.0f);

    default:
      throw new IllegalArgumentException("corner does not have a valid value");
    }
  }

  /** Call this method when a ball hits the block */
  public void notifyHit(Ball ball) {
    Paddle lastPaddle = ball.getLastPaddle();
    if (lastPaddle != null) {
      float itemSpeed = 2.0f;
      Vector2f itemVelocity = null;

      switch (lastPaddle.getPlace()) {
      case LEFT:
        itemVelocity = new Vector2f(-itemSpeed, 0.0f);
        break;

      case RIGHT:
        itemVelocity = new Vector2f(itemSpeed, 0.0f);
        break;

      case TOP:
        itemVelocity = new Vector2f(0.0f, itemSpeed);
        break;

      case BOTTOM:
        itemVelocity = new Vector2f(0.0f, -itemSpeed);
        break;

      default:
        throw new IllegalStateException("Paddle in unspecified place");
      }

      new Item(0, getPosition(), itemVelocity, 1.0f, 1.0f, goManager);
    }

    EntityManager.getInstance().removeBlock(this);
    goManager.removeWorldObject(graphicsObject);
  }

  public BlockFace getLeftFace() {
    return leftFace;
  }

  public void setLeftFace(BlockFace leftFace) {
    this.leftFace = leftFace;
  }

  public BlockFace getRightFace() {
    return rightFace;
  }

  public void setRightFace(BlockFace rightFace) {
    this.rightFace = rightFace;
  }

  public BlockFace getTopFace() {
    return topFace;
  }

  public void setTopFace(BlockFace topFace) {
    this.topFace = topFace;
  }

  public BlockFace getBottomFace() {
    return bottomFace;
  }

  public void setBottomFace(BlockFace bottomFace) {
    this.bottomFace = bottomFace;
  }

  public BlockFace getFace(FacePlace facePlace) {
    switch (facePlace) {
    case LEFT:
      return leftFace;
    case RIGHT:
      return rightFace;
    case TOP:
      return topFace;
    case BOTTOM:
      return bottomFace;

    default:
      throw new IllegalArgumentException("facePlace does not have a valid value");
    }
  }

  public void setFace(BlockFace face) {
    switch (face.getFacePlace()) {
    case LEFT:
      setLeftFace(face);
      break;

    case RIGHT:
      setRightFace(face);
      break;

    case TOP:
      setTopFace(face);
      break;

    case BOTTOM:
      setBottomFace(face);
      break;
    }
  }

  public void removeFace(FacePlace facePlace) {
    switch (facePlace) {
    case LEFT:
      leftFace = null;
      break;

    case RIGHT:
      rightFace = null;
      break;

    case TOP:
      topFace = null;
      break;

    case BOTTOM:
      bottomFace = null;
      break;

    default:
      throw new IllegalArgumentException("facePlace does not have a valid value");
    }
  }
}
TOP

Related Classes of ponkOut.logic.Block

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.