[JavaScript] Minecraft Minigames 4: CTF Arena - Flags

Planning
Step 1
In this lesson we will add flags that spawn in the base and can be stolen, carried, and captured by the enemy team!
Temp1465603061
Step 2
Before we can begin programming the behavior of the flags, we need to define their locations

redFlagLoc and blueFlagLoc will be the starting position of the flags in the red and blue base respectively.


redBaseCorner1 and redBaseCorner2 will be two opposite corners of a rectangle that spans the entrance to the red base. We know the red team has scored if a red player is holding the blue flag in that rectangle. The same is true for the blue base.


REMEMBER: Your values will be different for these locations!

Temp1465590349
Information
Step 3
How to get the flag locations

Place a banner of the appropriate color in the base where you want it to spawn, then look at it in debug mode and find the "Looking at:" values to see the location of the block you are targeting.

Temp1465590538
Information
Step 4
How to get the base corner locations

redBaseCorner1 will be the front left block of the doorway one above the ground,
-210 84 296 in this example.


redBaseCorner2 will be one block behind the back right block of the doorway one above the ground to that the border surrounds a row of air blocks in back of the doorway,
-212 84 303 in this example.

Step 5
Add custom items for the red and blue flags

This gives us two new items called redFlag and blueFlag that are colored banners with the corresponding display name "Red Flag" or "Blue Flag" printed in the appropriate color text.

Temp1465591174
Step 6
We will use a function called placeFlagAt() to place a flag of a given color at a given location

This is useful because placing a banner in Minecraft is not as simple as other blocks, due to the fact that the direction they are facing matters. This helper function will allow us to place a flag properly using one line of code in our other functions.

Temp1465591497
Step 7
Place flags in both bases in the startCTF() function

Add these lines to the startCTF() function to make sure we always start the game with flags in the right locations.

Temp1465592037
Step 8
Remove both flags at the end of the game

Add these two lines of code to the stopCTF() function to remove the flags from the bases when the game is over.

Temp1465592131
Step 9
Save, deploy, and reload

This should not be the first time you save in this lesson, make sure you are saving constantly!

Temp1465592168
Check Your Work
Step 10
Start and stop the game to make sure both flags appear and disappear at the right time
Step 11
Handling player interaction with the flags

The onPlayerInteract function will be called whenever a PlayerInteractEvent occurs. This function will allow players to pick up the opposing team's flag and return their own flag to their base.

Temp1465592988 Temp1465593059
Information
Step 12
This function handles four main scenarios, let's take a closer look

The four scenarios:
1. Red player clicks blue flag
2. Red player clicks red flag
3. Blue player clicks red flag
4. Blue player clicks blue flag


Read through the explanations below to understand how this function works

Temp1465593927
Check Your Work
Step 13
See if you can pick up the enemy team's flag

You may notice that if a player gets hit while holding a flag, they don't drop it! We need to add that to our onEntityDamageByEntity function before we can test returning the flag to base.

Step 14
If a player is hit while holding a flag, have them drop the flag at their current location before going to jail

Add this code to the onEntityDamageByEntity function to cause players to drop the flag properly when hit

Temp1465595035
Check Your Work
Step 15
Make sure players drop the flag they are carrying if they get hit and players can return their own flag to the base

Remember to test this for BOTH teams. You may have a typo in one if-statement but not the other. Always test every possible scenario when checking your work!

Step 16
Before we can add the ability to score flag captures we need a way to determine if a player is inside a certain area

This function isInRect() will return true if the player is inside the area bounded by corner1 and corner2


It works by sorting the X and Z values of the locations from low to high. Then, if the player's X position is greater than that of the higher X corner, we know the player is outside the rectangle and we return false. The same is true if the player's X position is lower than that of the lower X corner. We then do the same thing for the Z positions. If we make it past both of those checks without returning false, we know the player must be inside the rectangle and we return true.

Temp1465600388
Step 17
We will use the PlayerMoveEvent to check for flag captures

The PlayerMoveEvent occurs whenever a player moves, so we can use this to detect a flag capture the moment a player enters their base with the opposing team's flag.

Temp1465601982
Information
Step 18
Let's examine how this function checks for flag captures

Take a closer look at the onPlayerMove function and make sure you understand how it determines whether or not a player has scored a flag capture.

Temp1465601900
Check Your Work
Step 19
Test your ability to score flag captures and win the game!

Make sure to test flag capturing on BOTH teams. Capture the flag 3 times as each team to make sure both teams can win the game!


Planning
Step 20
In the next lesson will add some improvements to our capture the flag game like colored armor for each team!

We will also prevent cheating by stopping players from breaking blocks, using the inventory, and more!

Temp1465603436
[JavaScript] Minecraft Minigames 4: CTF Arena - Flags Info

Account

MVCode Clubs

Created By

Mvcc logo new aaron

Course:

Minecraft Minigames in JavaScript

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach