From b1db9c7048ac89bc48447fa7032c7a14f1210980 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Thu, 22 Nov 2018 22:47:00 +0400 Subject: [PATCH] Spawn additional balls at point of existing balls --- core/src/com/me/brickbuster/entity/Ball.java | 4 +++ .../src/com/me/brickbuster/entity/Entity.java | 2 +- .../entity/powerup/MultiBallPowerUp.java | 26 ++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 359edd7..9160aa6 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -177,4 +177,8 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { return isStuck; } + public void setStuck(boolean isStuck) { + this.isStuck = isStuck; + } + } diff --git a/core/src/com/me/brickbuster/entity/Entity.java b/core/src/com/me/brickbuster/entity/Entity.java index 635157b..cfdfb6c 100644 --- a/core/src/com/me/brickbuster/entity/Entity.java +++ b/core/src/com/me/brickbuster/entity/Entity.java @@ -31,7 +31,7 @@ public abstract class Entity { } public void setPos(Vector2 pos) { - this.pos = pos; + this.pos.set(pos); } public float getX() { diff --git a/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java b/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java index 9e6bbb9..acfd20a 100644 --- a/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java @@ -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 ballsToAdd = new Array(); 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 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(); } }