Spawn additional balls at point of existing balls

This commit is contained in:
Matt Low 2018-11-22 22:47:00 +04:00
parent d32cccebd6
commit b1db9c7048
3 changed files with 27 additions and 5 deletions

View File

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

View File

@ -31,7 +31,7 @@ public abstract class Entity {
} }
public void setPos(Vector2 pos) { public void setPos(Vector2 pos) {
this.pos = pos; this.pos.set(pos);
} }
public float getX() { 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.graphics.Color;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.me.brickbuster.entity.Ball; import com.me.brickbuster.entity.Ball;
import com.me.brickbuster.entity.Paddle; import com.me.brickbuster.entity.Paddle;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
@ -10,6 +11,7 @@ import com.me.brickbuster.state.PlayState;
public class MultiBallPowerUp extends PowerUp { public class MultiBallPowerUp extends PowerUp {
private Vector2 pos; private Vector2 pos;
private static final Array<Ball> ballsToAdd = new Array<Ball>();
public MultiBallPowerUp(PlayState state, Vector2 brick, Color color) { public MultiBallPowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, color); super(state, brick, color);
@ -18,10 +20,26 @@ public class MultiBallPowerUp extends PowerUp {
@Override @Override
public void activate() { public void activate() {
for (int x = 0; x < 2; x++) { final Array<Ball> existing = ((PlayState) state).balls;
Ball ball = new Ball((PlayState) state); if (existing.size >= 3) {
ball.launch(); return;
((PlayState) state).balls.add(ball);
} }
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();
} }
} }