Compare commits

..

5 Commits
master ... dev

Author SHA1 Message Date
39ad9eb4b8 Unload assets with asset manager instead of disposing them 2018-11-22 23:06:47 +04:00
88833ae206 Add 15% powerup chance to level_1 and level_2 2018-11-22 23:06:29 +04:00
b1db9c7048 Spawn additional balls at point of existing balls 2018-11-22 22:47:42 +04:00
d32cccebd6 Add randomPowerUpChance to Level type.
Allow defining said chance in level files.
Generate powerups on the fly when a brick is hit and the chance is met
2018-11-22 21:46:49 +04:00
35dea75776 Reset sprite batch color when rendering menu button 2018-11-22 21:21:32 +04:00
12 changed files with 65 additions and 26 deletions

View File

@ -1,4 +1,5 @@
{
randomPowerUpChance: 0.15
bricks: [
{
row: 15

View File

@ -1,4 +1,5 @@
{
randomPowerUpChance: 0.15
bricks: [
{
row: 5

View File

@ -177,4 +177,8 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener {
return isStuck;
}
public void setStuck(boolean isStuck) {
this.isStuck = isStuck;
}
}

View File

@ -165,6 +165,11 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
}
}
float powerUpChance = ((PlayState) state).currentLevel.getRandomPowerUpChance();
if (powerUpType == null && powerUpChance > 0f && MathUtils.randomBoolean(powerUpChance)) {
powerUpType = PowerUpType.getWeightedRandom();
}
if (powerUpType != null) {
((PlayState) state).powerUps.add(powerUpType.createInstance((PlayState) state, this));
}

View File

@ -31,7 +31,7 @@ public abstract class Entity {
}
public void setPos(Vector2 pos) {
this.pos = pos;
this.pos.set(pos);
}
public float getX() {

View File

@ -3,6 +3,7 @@ package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.me.brickbuster.entity.Ball;
import com.me.brickbuster.entity.Paddle;
import com.me.brickbuster.state.PlayState;
@ -10,6 +11,7 @@ import com.me.brickbuster.state.PlayState;
public class MultiBallPowerUp extends PowerUp {
private Vector2 pos;
private static final Array<Ball> ballsToAdd = new Array<Ball>();
public MultiBallPowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, color);
@ -18,10 +20,26 @@ public class MultiBallPowerUp extends PowerUp {
@Override
public void activate() {
for (int x = 0; x < 2; x++) {
Ball ball = new Ball((PlayState) state);
ball.launch();
((PlayState) state).balls.add(ball);
final Array<Ball> existing = ((PlayState) state).balls;
if (existing.size >= 3) {
return;
}
for (Ball b : existing) {
Vector2 velocity = b.getBody().getLinearVelocity();
velocity.setAngleRad(velocity.angleRad() - MathUtils.PI/8);
for (int x = 0; x < 2; x++) {
Ball ball = new Ball((PlayState) state);
ball.setX(b.getX());
ball.setY(b.getY());
ball.setStuck(false);
ball.getBody().setLinearVelocity(velocity);
ballsToAdd.add(ball);
velocity.setAngleRad(velocity.angleRad() + MathUtils.PI/4);
}
}
existing.addAll(ballsToAdd);
ballsToAdd.clear();
}
}

View File

@ -1,30 +1,26 @@
package com.me.brickbuster.layout;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.state.FieldState;
import com.me.brickbuster.state.PlayState;
public class FileLayout implements BrickLayout {
private static final Json JSON = new Json();
private PlayState state;
private FieldState state;
private LevelJsonTemplate template;
private Array<Brick> bricks;
private FileHandle file;
public FileLayout(PlayState state, FileHandle file) {
public FileLayout(FieldState state, LevelJsonTemplate template) {
this.state = state;
this.file = file;
this.template = template;
}
@Override
public void initialize() {
bricks = new Array<Brick>();
LevelJsonTemplate level = JSON.fromJson(LevelJsonTemplate.class, file);
for (LevelJsonTemplate.BrickJsonTemplate brick : level.bricks) {
for (LevelJsonTemplate.BrickJsonTemplate brick : template.bricks) {
final float x = PlayState.EDGE_PADDING + Brick.BRICK_WIDTH/2 + brick.col * Brick.BRICK_WIDTH;
final float y = PlayState.EDGE_PADDING + Brick.BRICK_HEIGHT/2 + brick.row * Brick.BRICK_HEIGHT;

View File

@ -2,14 +2,19 @@ package com.me.brickbuster.layout;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Json;
import com.me.brickbuster.state.FieldState;
import com.me.brickbuster.state.PlayState;
public class FileLevelLoader implements LevelLoader {
private PlayState state;
private static final Json JSON = new Json();
private FieldState state;
private int level = 1;
public FileLevelLoader(PlayState state) {
public FileLevelLoader(FieldState state) {
this.state = state;
}
@ -17,8 +22,15 @@ public class FileLevelLoader implements LevelLoader {
public Level getNextLevel() {
FileHandle file = Gdx.files.internal("levels/level_" + level + ".json");
if (file.exists()) {
LevelJsonTemplate template = JSON.fromJson(LevelJsonTemplate.class, file);
Texture background = null;
if (template.backgroundFile != null && !"".equals(template.backgroundFile)) {
background = new Texture(Gdx.files.internal(template.backgroundFile));
}
level++;
return new Level(new FileLayout(state, file), null, null);
return new Level(new FileLayout(state, template), background, template.randomPowerUpChance);
}
return null;
}

View File

@ -6,16 +6,16 @@ public class Level {
private final BrickLayout layout;
private final Texture background;
private final Texture border;
private final float randomPowerUpChance;
public Level(BrickLayout layout) {
this(layout, null, null);
this(layout, null, 0f);
}
public Level(BrickLayout layout, Texture background, Texture border) {
public Level(BrickLayout layout, Texture background, float randomPowerUpChance) {
this.layout = layout;
this.background = background;
this.border = border;
this.randomPowerUpChance = randomPowerUpChance;
}
public BrickLayout getLayout() {
@ -26,8 +26,8 @@ public class Level {
return background;
}
public Texture getBorder() {
return border;
public float getRandomPowerUpChance() {
return randomPowerUpChance;
}
}

View File

@ -9,7 +9,7 @@ import java.util.List;
public class LevelJsonTemplate {
public String backgroundFile;
public String borderFile;
public float randomPowerUpChance;
public List<BrickJsonTemplate> bricks;
public static class BrickJsonTemplate {

View File

@ -24,7 +24,7 @@ public abstract class FieldState extends State {
@Override
public void dispose() {
super.dispose();
textures.dispose();
game.assets.unload("textures.atlas");
}
}

View File

@ -2,6 +2,7 @@ package com.me.brickbuster.state;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector3;
import com.me.brickbuster.BrickBuster;
@ -23,6 +24,7 @@ public class MenuState extends State {
public void render() {
game.sb.setProjectionMatrix(game.cam.combined);
game.sb.begin();
game.sb.setColor(Color.WHITE);
game.sb.draw(playButton,
BrickBuster.BOARD_WIDTH/2-playButton.getWidth()/2,
BrickBuster.BOARD_HEIGHT/2-playButton.getHeight()/2);