Skip to content

Commit

Permalink
added foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Sep 15, 2016
1 parent 29f1a75 commit 3328a5f
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Bomberman/pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>com.almasb</groupId>
<artifactId>Bomberman</artifactId>
<version>0.1</version>
<version>0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -16,7 +16,7 @@
<maven.compiler.version>3.3</maven.compiler.version>
<maven.shade.version>2.4.2</maven.shade.version>

<fxgl.version>0.2.0</fxgl.version>
<fxgl.version>0.2.7</fxgl.version>
</properties>

<repositories>
Expand Down
96 changes: 89 additions & 7 deletions Bomberman/src/main/java/com/almasb/bomberman/BombermanApp.java
Expand Up @@ -26,47 +26,129 @@

package com.almasb.bomberman;

import com.almasb.bomberman.control.PlayerControl;
import com.almasb.ents.Entity;
import com.almasb.fxgl.app.ApplicationMode;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.entity.Entities;
import com.almasb.fxgl.entity.GameEntity;
import com.almasb.fxgl.gameplay.Level;
import com.almasb.fxgl.input.UserAction;
import com.almasb.fxgl.parser.TextLevelParser;
import com.almasb.fxgl.physics.CollisionHandler;
import com.almasb.fxgl.settings.GameSettings;
import com.almasb.gameutils.math.GameMath;
import javafx.scene.input.KeyCode;

/**
* @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
*/
public class BombermanApp extends GameApplication {

public static final int TILE_SIZE = 80;

private GameEntity player;
private PlayerControl playerControl;

@Override
protected void initSettings(GameSettings settings) {
settings.setTitle("BombermanApp");
settings.setVersion("0.1");
settings.setWidth(10 * TILE_SIZE);
settings.setHeight(10 * TILE_SIZE);
settings.setIntroEnabled(false);
settings.setMenuEnabled(false);
settings.setShowFPS(false);
settings.setApplicationMode(ApplicationMode.DEVELOPER);
}

@Override
protected void initInput() {

getInput().addAction(new UserAction("Move Up") {
@Override
protected void onActionBegin() {
playerControl.moveUp();
}
}, KeyCode.W);

getInput().addAction(new UserAction("Move Left") {
@Override
protected void onActionBegin() {
playerControl.moveLeft();
}
}, KeyCode.A);

getInput().addAction(new UserAction("Move Down") {
@Override
protected void onActionBegin() {
playerControl.moveDown();
}
}, KeyCode.S);

getInput().addAction(new UserAction("Move Right") {
@Override
protected void onActionBegin() {
playerControl.moveRight();
}
}, KeyCode.D);

getInput().addAction(new UserAction("Place Bomb") {
@Override
protected void onActionBegin() {
playerControl.placeBomb();
}
}, KeyCode.F);
}

@Override
protected void initAssets() {
protected void initAssets() {}

private BombermanFactory entityFactory;

public BombermanFactory getEntityFactory() {
return entityFactory;
}

@Override
protected void initGame() {
entityFactory = new BombermanFactory();

TextLevelParser levelParser = new TextLevelParser(entityFactory);

Level level = levelParser.parse("levels/0.txt");

player = entityFactory.newPlayer(0, 0);
playerControl = player.getControlUnsafe(PlayerControl.class);
level.getEntities().add(player);

getGameWorld().setLevel(level);
}

@Override
protected void initPhysics() {

getPhysicsWorld().addCollisionHandler(new CollisionHandler(EntityType.PLAYER, EntityType.POWERUP) {
@Override
protected void onCollisionBegin(Entity pl, Entity powerup) {
powerup.removeFromWorld();
playerControl.increaseMaxBombs();
}
});
}

@Override
protected void initUI() {

}
protected void initUI() {}

@Override
protected void onUpdate() {
protected void onUpdate(double tpf) {}

public void onWallDestroyed(Entity wall) {
if (GameMath.randomBoolean()) {
int x = Entities.getPosition(wall).getGridX(BombermanApp.TILE_SIZE);
int y = Entities.getPosition(wall).getGridY(BombermanApp.TILE_SIZE);

GameEntity powerup = entityFactory.newPowerup(x, y);
getGameWorld().addEntity(powerup);
}
}

public static void main(String[] args) {
Expand Down
58 changes: 58 additions & 0 deletions Bomberman/src/main/java/com/almasb/bomberman/BombermanFactory.java
@@ -0,0 +1,58 @@
package com.almasb.bomberman;

import com.almasb.bomberman.control.BombControl;
import com.almasb.bomberman.control.PlayerControl;
import com.almasb.fxgl.entity.Entities;
import com.almasb.fxgl.entity.GameEntity;
import com.almasb.fxgl.entity.component.CollidableComponent;
import com.almasb.fxgl.parser.EntityFactory;
import com.almasb.fxgl.parser.EntityProducer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
* @author Almas Baimagambetov (almaslvl@gmail.com)
*/
public class BombermanFactory extends EntityFactory {

public BombermanFactory() {
super('0');
}

@EntityProducer('w')
public GameEntity newWall(int x, int y) {
return Entities.builder()
.type(EntityType.WALL)
.at(x * BombermanApp.TILE_SIZE, y * BombermanApp.TILE_SIZE)
.viewFromNodeWithBBox(new Rectangle(BombermanApp.TILE_SIZE, BombermanApp.TILE_SIZE))
.build();
}

public GameEntity newPlayer(int x, int y) {
return Entities.builder()
.type(EntityType.PLAYER)
.at(x * BombermanApp.TILE_SIZE, y * BombermanApp.TILE_SIZE)
.viewFromNodeWithBBox(new Rectangle(BombermanApp.TILE_SIZE, BombermanApp.TILE_SIZE, Color.BLUE))
.with(new CollidableComponent(true))
.with(new PlayerControl())
.build();
}

public GameEntity newBomb(int x, int y, double radius) {
return Entities.builder()
.type(EntityType.BOMB)
.at(x * BombermanApp.TILE_SIZE, y * BombermanApp.TILE_SIZE)
.viewFromNodeWithBBox(new Rectangle(BombermanApp.TILE_SIZE, BombermanApp.TILE_SIZE, Color.RED))
.with(new BombControl(radius))
.build();
}

public GameEntity newPowerup(int x, int y) {
return Entities.builder()
.type(EntityType.POWERUP)
.at(x * BombermanApp.TILE_SIZE, y * BombermanApp.TILE_SIZE)
.viewFromNodeWithBBox(new Rectangle(BombermanApp.TILE_SIZE, BombermanApp.TILE_SIZE, Color.YELLOW))
.with(new CollidableComponent(true))
.build();
}
}
8 changes: 8 additions & 0 deletions Bomberman/src/main/java/com/almasb/bomberman/EntityType.java
@@ -0,0 +1,8 @@
package com.almasb.bomberman;

/**
* @author Almas Baimagambetov (almaslvl@gmail.com)
*/
public enum EntityType {
PLAYER, WALL, BOMB, POWERUP
}
@@ -0,0 +1,42 @@
package com.almasb.bomberman.control;

import com.almasb.bomberman.BombermanApp;
import com.almasb.bomberman.EntityType;
import com.almasb.ents.AbstractControl;
import com.almasb.ents.Entity;
import com.almasb.fxgl.app.FXGL;
import com.almasb.fxgl.entity.Entities;
import com.almasb.fxgl.entity.component.BoundingBoxComponent;

/**
* @author Almas Baimagambetov (almaslvl@gmail.com)
*/
public class BombControl extends AbstractControl {

private double radius;

public BombControl(double radius) {
this.radius = radius;
}

@Override
public void onUpdate(Entity entity, double tpf) {

}

public void explode() {
BoundingBoxComponent bbox = Entities.getBBox(getEntity());

FXGL.getApp()
.getGameWorld()
.getEntitiesInRange(bbox.range(radius, radius))
.stream()
.filter(e -> Entities.getType(e).isType(EntityType.WALL))
.forEach(e -> {
FXGL.<BombermanApp>getAppCast().onWallDestroyed(e);
e.removeFromWorld();
});

getEntity().removeFromWorld();
}
}
@@ -0,0 +1,72 @@
package com.almasb.bomberman.control;

import com.almasb.bomberman.BombermanApp;
import com.almasb.ents.AbstractControl;
import com.almasb.ents.Entity;
import com.almasb.fxgl.app.FXGL;
import com.almasb.fxgl.entity.Entities;
import com.almasb.fxgl.entity.GameEntity;
import com.almasb.fxgl.entity.component.PositionComponent;
import javafx.util.Duration;

/**
* @author Almas Baimagambetov (almaslvl@gmail.com)
*/
public class PlayerControl extends AbstractControl {

private PositionComponent position;
private int maxBombs = 1;
private int bombsPlaced = 0;

@Override
public void onAdded(Entity entity) {
position = Entities.getPosition(entity);
}

@Override
public void onUpdate(Entity entity, double tpf) {

}

public void increaseMaxBombs() {
maxBombs++;
}

public void placeBomb() {
if (bombsPlaced == maxBombs) {
return;
}

bombsPlaced++;

int x = position.getGridX(BombermanApp.TILE_SIZE);
int y = position.getGridY(BombermanApp.TILE_SIZE);

GameEntity bomb = FXGL.<BombermanApp>getAppCast()
.getEntityFactory()
.newBomb(x, y, BombermanApp.TILE_SIZE / 2);

getEntity().getWorld().addEntity(bomb);

FXGL.getMasterTimer().runOnceAfter(() -> {
bomb.getControlUnsafe(BombControl.class).explode();
bombsPlaced--;
}, Duration.seconds(2));
}

public void moveRight() {
position.translateX(BombermanApp.TILE_SIZE);
}

public void moveLeft() {
position.translateX(-BombermanApp.TILE_SIZE);
}

public void moveUp() {
position.translateY(-BombermanApp.TILE_SIZE);
}

public void moveDown() {
position.translateY(BombermanApp.TILE_SIZE);
}
}
10 changes: 10 additions & 0 deletions Bomberman/src/main/resources/assets/text/levels/0.txt
@@ -0,0 +1,10 @@
0000000000
0w000000w0
00ww000000
000w000w00
000w00w000
000w000000
000w00w000
000w000000
0000000000
0000000000

0 comments on commit 3328a5f

Please sign in to comment.