Ball (Brick Breaker Minigame) - UQcsse3200/2023-studio-2 GitHub Wiki
Description
The Ball is the core entity of this game as it bounces of the surface and the slider to destroy the bricks and one all the bricks are destroyed using the ball the game ends.
Once you press ⎵
, the game will start and the ball will be off the slider.
Design of the ball
BallFactory
Entity Creation
This function, createMinigameBall
, is designed to create and return a ball
entity for the mini-game. Here's a short breakdown of its functionality:
- Retrieve an
InputComponent
specific to a ball from the input service. - Create a new
Entity
object calledball
. - Add three components to the
ball
entity:- A
TextureRenderComponent
to render the ball using an image found at "images/minigame/Ball.png". - The
inputComponent
retrieved earlier to handle ball-specific input. - A
PhysicsComponent
to handle the ball's physics behavior.
- A
- Set the entity type of the ball to "ball".
- Return the configured
ball
entity.
In essence, the function creates the ball entity with input and physics behavior for brick breaker mini-game.
public static Entity createMinigameBall() {
InputComponent inputComponent =
ServiceLocator.getInputService().getInputFactory().createForBall();
Entity ball =
new Entity()
.addComponent(new TextureRenderComponent("images/minigame/Ball.png"))
.addComponent(inputComponent)
.addComponent(new PhysicsComponent());
ball.setEntityType("ball");
return ball;
}
Key components defining its behavior
TextureRenderComponent
: Handles the rendering of the ball's visual representation.PhysicsComponent
: Provides the necessary physics properties to handle collision and placement.inputComponent
: Specifies the keys or the input required for the movement of the ball.
Spawning Ball
This spawnBall
function carries out the following tasks:
-
Create a New Ball Entity: It utilizes the
BallFactory.createMinigameBall()
method to create a new ball entity, which is then assigned to thenewBall
variable. -
Positioning the Ball: The function then positions or spawns this
newBall
entity at a specified location or configuration given byBALL_SPAWN
. ThespawnEntityAt
method takes in four arguments:- The entity to spawn (
newBall
in this case). - The location/configuration (
BALL_SPAWN
). - Two boolean values (in this case,
true
for both), which probably control some attributes or behaviors of the spawning process. Without additional context, it's hard to say what these two booleans exactly represent.
- The entity to spawn (
-
Add to Targetables: The
newBall
entity is added to a list (or similar collection) namedtargetables
. This suggests that the ball might be a target for some gameplay mechanics, but without more context, this is just an assumption. -
Assign to Instance Variable: The
newBall
entity is then assigned to the instance variableball
. This implies that this class likely keeps track of a singular active or current ball, which now becomes thenewBall
. -
Return the New Ball: Finally, the
newBall
entity is returned by the function, potentially allowing other parts of the program to interact with or reference it immediately after it's spawned.
In summary, spawnBall
creates a new ball entity, places it in the mini-game, adds it to a list of targetable entities, and updates the class's current ball reference to this new ball.
private Entity spawnBall()
{
Entity newBall = BallFactory.createMinigameBall();
spawnEntityAt(newBall, BALL_SPAWN, true, true);
targetables.add(newBall);
ball = newBall;
return newBall;
}