COMPONENT: 1. The Cannon - lperala/The-Castle-Engineer GitHub Wiki

What is The Cannon?

The Cannon is the main enemy in the current build of the game. It creates an instance of certain gameobjects and adds them force towards certain target. The Cannon uses the CannonScript1 script to perform.

What inputs are given to The Cannon on each level?

On each level The Cannon gets these varying parameters.

-public int MaxShots, how many cannonballs are getting shot in total

-public int CannonBallBigAmount, how many bigger cannonballs are getting shot

-public GameObject CannonBall, properties of the CannonBall gameobject

-public GameObject CannonBallBig, porperties of the CannonBallBig gameobject

-public float CoordMin, CoordMax, Multiplier, bunch of coordinates and multipliers

How does The Cannon know when to shoot and when to stop shooting?

The shooting control is done with a simple if statement:

if (TotalShots <= MaxShots)

If the TotalShots (Shots that have been shot so far) has not yet exceeded the Maximum amount of shots allowed for the level, we create a new shooting vector and a cannon ball instance based on that.

How does The Cannon know Where to shoot?

If there are still cannonballs left to shoot, we run the function CreateTargetVector();. This function creates a vector with slight randomness. The vector always has a stable x-coordinate of 20. The y-coordinate is created by picking a random float between the CoordMin and the CoordMax.

Why is there just one random coordinate?

By making just one of the coordinates random, we can make every single shot hit close to the certain spot with different angles. Lets observe the image below: Vectors.png

In the image we can see two vectors:
-Vector1 with y-coordinate=20 & x-coordinate=20
-Vector2 with y-coordinate=30 & x-coordinate=20

When the x-coordinate stays on the stable value of 20, the higher y-value does not just make the angle of the shot higher; it also makes the vector longer. As the shooting power is based on the length of the vector, we are left with higher and lower angled shots that both have approximately the same landing spot. The bottom part of the image represents the paths of the shots for both vector1 and vector2.

How does the cannon actually shoot the cannonballs?

After we have created a target shooting angle we will call the CreateCannonBallInstance();) function. The function creates a copy of the cannonball gameobject and places it underneath the cannon sprite. When the copy has been created we give the copy gameobject's RigidBody2D component some force towards the target vector. This is done with CannonRB.AddForce(_Target * Multiplier);

CannonRB is the copy object

.Addfore is the RigidBody2D function

_Target is the target vector

Multiplier is a multiplier that can be adjusted for each level. If we need the cannon to shoot further we can just increase the multiplier so that the shot goes to the right places.

When is the level completed then?

When the King goes to the victory pose, the player knows that all the cannonballs have been shot. The King is still vulnerable in this pose. When the King has been in the victory pose for 3 seconds, the level is completed.

⚠️ **GitHub.com Fallback** ⚠️