Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new Boundary class. replace RectBoundBox. change int to double #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/bomberman/animations/Sprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class Sprite

public Image[] spriteImages;
public boolean hasValidSpriteImages;

public Entity entityReference;

public Sprite(Entity e, int actualSize, double playSpeed, int spriteLocationOnSheetX, int spriteLocationOnSheetY, int numberOfFrames, double width, double height,
int scale, boolean leftToRight)
{
Expand All @@ -44,11 +44,11 @@ public Sprite(Entity e, int actualSize, double playSpeed, int spriteLocationOnSh
}

public int getXPosition() {
return entityReference.getPositionX();
return (int) entityReference.getPositionX();
}

public int getYPosition() {
return entityReference.getPositionY();
return (int) entityReference.getPositionY();
}


Expand Down
10 changes: 5 additions & 5 deletions src/bomberman/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package bomberman.entity;

import bomberman.entity.boundedbox.RectBoundedBox;
import bomberman.geometry.Boundary;

/**
*
Expand All @@ -16,8 +16,8 @@ public interface Entity {
boolean isPlayerCollisionFriendly();
void draw();
void removeFromScene();
int getPositionX();
int getPositionY();
RectBoundedBox getBoundingBox();
double getPositionX();
double getPositionY();
Boundary getBoundary();

}
2 changes: 1 addition & 1 deletion src/bomberman/entity/MovingEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
public interface MovingEntity extends Entity {


public void move(int steps, Direction direction);
public void move(double steps, Direction direction);

}
35 changes: 0 additions & 35 deletions src/bomberman/entity/boundedbox/RectBoundedBox.java

This file was deleted.

63 changes: 21 additions & 42 deletions src/bomberman/entity/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@
import bomberman.entity.Entity;
import bomberman.entity.KillableEntity;
import bomberman.entity.MovingEntity;
import bomberman.entity.boundedbox.RectBoundedBox;
import bomberman.geometry.Boundary;
import bomberman.scenes.Sandbox;

public class Player implements MovingEntity, KillableEntity {

private int health;
private boolean isAlive;
RectBoundedBox playerBoundary;
Boundary boundary;

Sprite currentSprite;
PlayerAnimations playerAnimations;

Direction currentDirection;

public int positionX = 0;
public int positionY = 0;

String name;

public Player() {
Expand All @@ -40,14 +36,8 @@ public Player(int posX, int posY) {

private void init(int x, int y) {
name = "Player";

playerAnimations = new PlayerAnimations(this);

positionX = x;
positionY = y;

playerBoundary = new RectBoundedBox(positionX, positionY, GlobalConstants.PLAYER_WIDTH, GlobalConstants.PLAYER_HEIGHT);

boundary = new Boundary(x, y, GlobalConstants.PLAYER_WIDTH, GlobalConstants.PLAYER_HEIGHT);
currentSprite = playerAnimations.getPlayerIdleSprite();
}

Expand Down Expand Up @@ -77,9 +67,7 @@ public String toString() {

@Override
public boolean isColliding(Entity b) {
// playerBoundary.setPosition(positionX, positionY);
RectBoundedBox otherEntityBoundary = (RectBoundedBox) b.getBoundingBox();
return playerBoundary.checkCollision(otherEntityBoundary);
return boundary.intersects(b.getBoundary());
}

@Override
Expand All @@ -94,26 +82,22 @@ public void die() {
setCurrentSprite(playerAnimations.getPlayerDying());
}

private boolean checkCollisions(int x, int y) {
playerBoundary.setPosition(x, y);
private boolean checkCollisions(double x, double y) {
Boundary collision = boundary.getMin(x, y);

for (Entity e : Sandbox.getEntities()) {
if (e != this && isColliding(e) && !e.isPlayerCollisionFriendly()) {
playerBoundary.setPosition(positionX, positionY);
/*
System.out.println("Player x="+getPositionX()+" y="
+getPositionY()+" colliding with x="+e.getPositionX()
+" y="+e.getPositionY());
*/
if (e != this && !e.isPlayerCollisionFriendly() &&
collision.intersects(e.getBoundary())) {
return true;
}
}
playerBoundary.setPosition(positionX, positionY);

boundary.setMin(x, y);
return false;
}

@Override
public void move(int steps, Direction direction) {
public void move(double steps, Direction direction) {

steps *= GameLoop.getDeltaTime();

Expand All @@ -123,29 +107,25 @@ public void move(int steps, Direction direction) {
} else {
switch (direction) {
case UP:
if(!checkCollisions(positionX, positionY - steps)) {
positionY -= steps;
if(!checkCollisions(boundary.getMinX(), boundary.getMinY() - steps)) {
setCurrentSprite(playerAnimations.getMoveUpSprite());
currentDirection = Direction.UP;
}
break;
case DOWN:
if(!checkCollisions(positionX, positionY + steps)) {
positionY += steps;
if(!checkCollisions(boundary.getMinX(), boundary.getMinY() + steps)) {
setCurrentSprite(playerAnimations.getMoveDownSprite());
currentDirection = Direction.DOWN;
}
break;
case LEFT:
if(!checkCollisions(positionX - steps, positionY)) {
positionX -= steps;
if(!checkCollisions(boundary.getMinX() - steps, boundary.getMinY())) {
setCurrentSprite(playerAnimations.getMoveLeftSprite());
currentDirection = Direction.LEFT;
}
break;
case RIGHT:
if(!checkCollisions(positionX + steps, positionY)) {
positionX += steps;
if(!checkCollisions(boundary.getMinX() + steps, boundary.getMinY())) {
setCurrentSprite(playerAnimations.getMoveRightSprite());
currentDirection = Direction.RIGHT;
}
Expand All @@ -171,19 +151,18 @@ public void removeFromScene() {
}

@Override
public int getPositionX() {
return positionX;
public double getPositionX() {
return boundary.getMinX();
}

@Override
public int getPositionY() {
return positionY;
public double getPositionY() {
return boundary.getMinY();
}

@Override
public RectBoundedBox getBoundingBox() {
playerBoundary.setPosition(positionX, positionY);
return playerBoundary;
public Boundary getBoundary() {
return boundary;
}

@Override
Expand Down
48 changes: 20 additions & 28 deletions src/bomberman/entity/staticobjects/BlackBomb.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,37 @@
import bomberman.animations.Sprite;
import bomberman.entity.Entity;
import bomberman.entity.StaticEntity;
import bomberman.entity.boundedbox.RectBoundedBox;
import bomberman.geometry.Boundary;
import java.util.Date;

/**
*
* @author Ashish
*/
public class BlackBomb implements StaticEntity {
public int positionX = 0;
public int positionY = 0;
private int height;
private int width;
private Sprite sprite;
RectBoundedBox entityBoundary;
Boundary entityBoundary;
BombAnimations bomb_animations;
Date addedDate;
int timerDurationInMillis = 2000;
int timerDurationInMillis = 2000;
STATE bombState;

enum STATE
{
INACTIVE, //INACTIVE when bomb's timer hasnt yet started
ACTIVE, //Active when bomb's timer has started and it will explode soon
EXPLODING, //when bomb is exploding
DEAD; //when the bomb has already exploded
}

public BlackBomb(int x, int y) {
positionX = x;
positionY = y;
width = 16;
height = 16;
bomb_animations=new BombAnimations(this);
sprite=bomb_animations.getBlackBomb();
entityBoundary = new RectBoundedBox(positionX, positionY, width, height);
addedDate=new Date();
bombState=STATE.ACTIVE;

public BlackBomb(double x, double y) {
bomb_animations = new BombAnimations(this);
sprite = bomb_animations.getBlackBomb();
entityBoundary = new Boundary(x, y, 16, 16);
addedDate = new Date();
bombState = STATE.ACTIVE;
}

public boolean isAlive(){
STATE s = checkBombState();
if(s==STATE.DEAD){
Expand All @@ -61,15 +53,15 @@ public boolean isAlive(){
return true;
}
}

public STATE checkBombState(){
if(new Date().getTime()>timerDurationInMillis+addedDate.getTime()){
return STATE.DEAD;
}else{
return STATE.ACTIVE;
}
}

@Override
public boolean isColliding(Entity b) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
Expand All @@ -86,23 +78,23 @@ public void removeFromScene() {
}

@Override
public int getPositionX() {
return positionX;
public double getPositionX() {
return entityBoundary.getMinX();
}

@Override
public int getPositionY() {
return positionY;
public double getPositionY() {
return entityBoundary.getMinY();
}

@Override
public RectBoundedBox getBoundingBox() {
public Boundary getBoundary() {
return entityBoundary;
}

@Override
public boolean isPlayerCollisionFriendly() {
return true;
}

}
Loading