Skip to content

Commit

Permalink
pushable boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
HeathLoganCampbell committed Oct 25, 2019
1 parent 8f42c54 commit 0bb71ba
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/main/java/com/heathlogancampbell/labgoat/LabGoatGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.heathlogancampbell.engine.graphics.Bitmap;
import com.heathlogancampbell.engine.graphics.Sprite;
import com.heathlogancampbell.engine.inputs.InputListener;
import com.heathlogancampbell.labgoat.commons.Location;
import com.heathlogancampbell.labgoat.entity.Box;
import com.heathlogancampbell.labgoat.entity.Player;
import com.heathlogancampbell.labgoat.level.Level;
Expand Down Expand Up @@ -37,20 +38,29 @@ public LabGoatGame(int width, int height)

TileBase.initTiles(this.tiles);
this.level = new Level("Basic", new TileBase[][]{
{ TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL},
{ TileBase.WALL, TileBase.FLOOR, TileBase.WALL, TileBase.FLOOR, TileBase.FLOOR, TileBase.WALL },
{ TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL},
{ TileBase.WALL, TileBase.FLOOR, TileBase.FLOOR, TileBase.FLOOR, TileBase.FLOOR, TileBase.WALL },
{ TileBase.WALL, TileBase.FLOOR, TileBase.FLOOR, TileBase.FLOOR, TileBase.FLOOR, TileBase.WALL },
{ TileBase.WALL, TileBase.FLOOR, TileBase.FLOOR, TileBase.FLOOR, TileBase.FLOOR, TileBase.WALL },
{ TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL, TileBase.WALL},
},
new int[][]{
{ 0b0010, 0, 0b0010, 0, 0, 0, 0b0010},
{ 0b0010, 0, 0, 0, 0, 0, 0b0010},
{ 0b0010, 0, 0, 0, 0, 0, 0b0010},
{ 0b0010, 0, 0, 0, 0, 0, 0b0010},
{ 0b0010, 0, 0, 0, 0, 0, 0b0010},
{ 0, 0, 0, 0, 0, 0, 0},
});

Box box = new Box(this, tiles);
box.setLocation(new Location(3, 2));

Box box2 = new Box(this, tiles);
box2.setLocation(new Location(2, 2));

this.level.addEntity(new Player(this, tiles));
// this.level.addEntity(new Box(this, tiles));
this.level.addEntity(box);
this.level.addEntity(box2);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Box(LabGoatGame game, Bitmap bitmap)
this.sprite = new Sprite(bitmap, 32, 32, 32 * 2,0);
this.setLocation(new Location(1, 1));
this.setVelocity(new Velocity(0, 0));
this.setSolid(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class EntityBase
@Getter
@Setter
private Velocity velocity;
@Getter
@Setter
private boolean solid = false;

public void draw(Bitmap screen)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void tick(InputListener inputListener)
Location newLoc = this.getLocation().clone();
newLoc.addVelocity(this.getVelocity());

game.getLevel().pushOn(newLoc, this.getVelocity(), this);
if(game.getLevel().isMovable(newLoc))
{
this.getLocation().addVelocity(this.getVelocity());
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/heathlogancampbell/labgoat/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.heathlogancampbell.engine.graphics.Bitmap;
import com.heathlogancampbell.engine.inputs.InputListener;
import com.heathlogancampbell.labgoat.commons.Location;
import com.heathlogancampbell.labgoat.commons.Velocity;
import com.heathlogancampbell.labgoat.entity.EntityBase;
import com.heathlogancampbell.labgoat.tiles.TileBase;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -59,10 +60,34 @@ public void draw(Bitmap screen)
}
}

public void pushOn(Location location, Velocity velocity, EntityBase pusherEntity)
{
for (EntityBase entity : this.entities) {
if( ((int) location.getY()) == ((int) entity.getLocation().getY()) &&
((int) location.getX()) == ((int) entity.getLocation().getX()) )
{
//found our entity
Location newLoc = entity.getLocation().clone();
newLoc.addVelocity(velocity);
if(isMovable(newLoc))
{
entity.getLocation().addVelocity(velocity);
}
}
}
}

public boolean isMovable(Location location)
{
if(location.getY() >= this.tiles.length) return false;
if(location.getX() >= this.tiles[ (int) location.getY()].length) return false;
for (EntityBase entity : this.entities) {
if( ((int) location.getY()) == ((int) entity.getLocation().getY()) &&
((int) location.getX()) == ((int) entity.getLocation().getX()) )
{
return false;
}
}
return !this.tiles[(int) location.getY()][ (int) location.getX()].isSolid();
}

Expand Down

0 comments on commit 0bb71ba

Please sign in to comment.