​​​​Enclosed | Core Gameplay| GAME DEVLOG #2


Enclosed | Core mechanics | GAME DEVLOG #2

Devlog #2 Clips

Hey!

It's Ken.


In this devlog the core mechanics of the game are implemented, but... let's start first with a visual update:

# Path Visualizing

It is about time that the player will be able to see a visual representation of the path that the paddle follows. The obvious solution is to use the Line Renderer component.

The Line Renderer component is made up of a list of Vector3 positions & that is great since my Path object is also, made up of a list of points.
I added a game object with a line renderer as a child of the path object which the Path scripts now as a reference to.

The way my path generation works is I have a different function to handle each path generation, for example CirclePath(). Every path generation function starts with StartPathGenerating() & ends with FinishPaGenerating()



Since I already have the path regenerating in editor mode whenever I change any of its parameters, all I had to do is slap a function in there to update the line renderer whenever the path regenerates. All this function does is set the points count & then manually set each point:


Now I have a line renderer that dynamically follows the path, but its.... PINK! No worries, to fix that all I need to do is assign a material with my desired texture for the line renderer. I drew a single white "dash" on a 100x150 pixels canvas & made a material out for it:

After Applying the texture & tweaking some properties of the line renderer, here is the result:


# GameManager Object

Before we move on, I'll note that at this point I implemented a GameManager script that sits on a GameManager object. For now this GameManager keeps track of the balls count, player score, game start & game over states & fires some basic GameManager events:

 

# Ball Movement & Collision

Finally it's time to bounce a ball!

I made this ball script which, well, handles the ball :)

Basically,  the ball is a physics based object (has a rigidbody) & is moving by setting its velocity direction & speed. The ball has a temporary reference to the Path object in the scene to get it's center so the ball is initialized to the center of the path. Later I am planning to have the ball spawning be handled by the GameManager object, because the ball script shouldn't have access to the path, but its fine for now.

Anyways, I implemented the ball movement to wait until the player first moves the paddle. If the level loaded, the ball will start moving only after the player first moved. This logic is handled by subscribing to the GameManager GameStart event which is triggered on player movement.

After having the ball moving across the screen, all I added was an OnCollisionEnter2D function to detect whenever the ball collides with an object marked with the "WhatCanBounceOf" layer mask - the paddle. If the ball collides with the paddle I get the normal of the collision point & add a small random angle value to it so the ball bounce won't be too obvious:

With that I have basically implemented the core mechanic of the infamous pong game :)
Let's tackle some UI now for score counting.

# Score UI

The game UI for now is as basic as it gets - a text field that holds the score value. The score of the player goes up by 1 whenever the ball hits the paddle.


# Path Boundary & Game Over

The final topic I'll tackle this devlog is the game over state.

The game should be over when the ball is lost... but how do I know when the ball is lost?  The simple solution is to know when the ball "collides" with the path. I could go & check for a collision with the path, should be easy since I had the path system also update an edge collider for visual purposes. The down side is that it could lead to a trigger even though the ball isn't really lost, for example a situation like this might trigger a game over:

If the ball moves right then it will hit the path before the paddle & will trigger a game over even though the ball will hit the paddle.

To make sure the ball is really lost I need to have it cross a boundary that is larger then the path itself. Since the path is generated dynamically I need to have the boundary also update dynamically. Fortunately for me, It was a really easy setup! 

All I ended up doing is making a new child object for the Path, and giving it its own script called "PathBoundary". All this script does is update an edge collider to match exactly the path object, the catch was making the scale of the path boundary slightly larger then the path scale itself which will make sure the ball is really lost by the player:

That way, I have a boundary the matches the path but is slightly larger. For now when the ball collides with the boundary, a GameOver event is fired by the GameManager.



That's it for this progress update.

For the next time I want to start working on a power-up system to make the game play more engaging.

Ken.

Leave a comment

Log in with itch.io to leave a comment.