The Haunted Library Part 5: Finishing the Escape Room

Planning
Step 1
Follow the clues!

In this lesson, we will be making the Escape Room happen! We will use the functions we made to spawn entities, show levers and buttons, and fill inventories, and pull that together into the sequence players have to follow in order to escape.

Temp1557341328
Planning
Step 2
Save yourself some time!

To keep from having to solve the Escape Room every time you want to test something, modify these two exports functions.

/js secretPassage() will just open the Secret Passage now, so that you can easily get in and out.

/js testInventory() will just give you the key, so that you can always get out of the library.

Temp1557342829

P.S. Using world.dropItem() is a super easy way to get your custom item if you don't want to make a crafting recipe!

Step 3
Find the opposite corner of the library

To tell if a player is inside the library, we will need the coordinates of the opposite corner from our starting corner. Move into the back right corner of the library, and break a block to get the location of the corner just below the ceiling.

Temp1555533224
Step 4
Save the second corner

Add a property called corner2 to your library.interior object. Yaw and pitch won't matter, since we are only comparing the X, Y, and Z to see if a player is inside.

Temp1555533337
Step 5
Find the player's starting location

We need to choose a point to teleport all the players to when the Escape Room starts. Move to the entrance, and record the position of where you are standing.

You will want to save your Yaw and Pitch this time, to make the players look up at the spooky portrait.

Temp1555533633
Step 6
Save the starting position
Temp1555533739
Step 7
Make a function to determine if something is inside a room

This function will take an entity (like a player, custom Mob, even a block) and tell you true or false on whether not it is between the corner locations you give it for corner1 and corner2.

Temp1555534208
Step 8
Check if a player is in the library

Test the new function by checking if a player is in the library each time someone moves on the server.

Temp1555534626
Check Your Work
Step 9
Are you inside the library?

Save, deploy, reload and move around. You should see "Someone is in the library" if you are inside of it, and you should stop seeing that message if you go outside.

Temp1555534705
Step 10
Add started and finished properties to the library
Temp1555534863
Step 11
Make a method to start the library
Temp1555535793
Step 12
Make the library start automatically if anyone walks in
Temp1555535361
Check Your Work
Step 13
Test out your library start.

Save, deploy, reload and move around inside the library. You should be teleported to the entrance, looking up at the portrait.

If an instructor is available to connect to your server, they should be teleported into the library as well.

Step 14
Clear out items and entities
Temp1555708581

This code will put all of the players in Adventure Mode, show the title of the Haunted Library, and kill all entities.

You may want to keep the inventory clear and gamemode commented, so that you don't get pulled out of Creative and lose all your items every time you reload

Check Your Work
Step 15
Test the start again

Save, deploy, and reload. As soon as you are inside the library, it should start, and kill any entities in the area.

Temp1557433221

You may notice that hanging entities, like paintings and item frames, also get killed by this command! We will add them back in the next step.

Step 16
Place the portrait and filing cabinet drawers

Add the portrait and filing cabinet drawers back in by calling the methods you made after the console command to kill all entities except players.

Temp1555708909
Check Your Work
Step 17
Test the start again

Save, deploy, reload, and make sure your item frames and paintings are appearing

Temp1557433460
Step 18
Cancel hanging entity breaks

Adventure mode will keep our players from being able to break blocks, but for some reason they can still break hanging entities, like item frames and paintings. We will add this event function for the hangingBreakByEntity event to cancel any breaks while the library is going.

Temp1557433774
Step 19
Place the office supplies when a player finds the Restricted Section

The if statement here cancels the event if the player tries to take anything except "The Secrets of the Library" out of the Restricted Shelf.

That means that our else will happen when they do click the correct item. That is when we will fill the filing cabinet with office supplies.

Temp1555715144
Step 20
Add a sign

To make sure players notice that the filing cabinet has changed, we're going to use the drone to add a sign when we fill the filing cabinet.

Temp1557434398
Check Your Work
Step 21
Test out the sign and office supplies

Save, deploy, reload, and go get "The Secrets of the Library" out of the Restricted Shelf. The sign should appear over your filing cabinet, and the bottom one should have paper, ink, and a book and quill in it.

Temp1557434277
Step 22
Add some spooky music and bats

Use a for loop to spawn some bats when the library starts, and play the creepy record11.

Temp1557434854
Step 23
Make the library dusty!

Borrowing some skills we learned in the Custom Particle Trails lesson, make a small smoke trail follow anyone while they are in the library.

Temp1557434971
Step 24
Find the opposite corner of the secret room

This will be the ceiling block directly above the corner where your pressure plate goes.

Temp1555716637
Step 25
Save the second corner and give the room an "entered" property
Temp1555716946
Step 26
Lock the player in

We will add this code. It checks if no one has entered the secret room yet, since we only want it to lock the first time we go in.

Temp1555960609
Check Your Work
Step 27
Test the closing passage

Save, deploy, reload, and make sure the secret passage closes once you enter the secret room.

Temp1555717238
Step 28
Start the library if even if you start in the basement

Right now, a problem with our code is that you can't get out of the hidden basement unless the library has started, but you do get locked in. We'll use an if and else statement to teleport you to the start if you are down there before the library has started.

Temp1556835175
Step 29
Place the hidden cookies

Call your function that fills Melvil's secret stash of cookies in the chest under the desk, but not until a player clicks the enchanting table in the Secret Room.

Temp1555963622
Check Your Work
Step 30
Test out the secret stash

Before you do anything, check the chest under the desk to make sure that it is empty. Save, deploy, reload, and go get Melvil's diary out of the basement. When you get back upstairs, the desk chest should be full of cookies.

Temp1557435252
Planning
Step 31
Duplicate ghosts. . .

Right now, our code will spawn a Melvil ghost every time we step on the pressure plate in the secret room. In the next few steps, we will be fixing this bug.

Temp1557435497
Step 32
Make a variable to tell if the ghost has spawned
Temp1556054913
Step 33
Make the variable true when you spawn the ghost
Temp1556055048
Step 34
Only spawn the ghost if one hasn't spawned yet.
Temp1556055204
Step 35
Find the locations for the doors

Because we have two doors, and each door is two blocks, to keep track of when a player tries to open the door, we need to save 4 locations.

Temp1555970170
Step 36
Save the locations

Give the library object a property called doors and assign it to an array of the four door location blocks.

Temp1555970322
Step 37
Use a loop to check all four door blocks

Because we have four different blocks for the doors, we need to use a loop to check them all inside of our playerInteract event function. If the player is holding the key to the library, they will be able to open the door, and escape the escape room.

Otherwise, the door should slam closed (because the event that should have opened it gets canceled) and make a thudding noise.

Temp1555971000
Check Your Work
Step 38
Test the doors

Save, deploy, reload, and try to open the doors. Make sure you have headphones to test the sound as well.

Then, give yourself the library key with /js testInventory() and try opening the door again. You should be able to get out.

Temp1557435713
Step 39
Rebuild the doors when the library starts

To keep a door from being stuck open when you first walk in, we build new doors in the same place. Here, we use library.doors[0] and library.doors[2], because they were the two bottom blocks.

You can tell by which blocks have the lower Y

Temp1556052205
Step 40
Find a location outside of the library
Temp1557345050
Step 41
Make a method to reset the library
Temp1556051280
Step 42
Make a method to finish the library that calls your library reset method
Temp1557436405
Step 43
Call the library finish method when the library is finished
Temp1557436559
Planning
Step 44
You finished the library!

Congratulations, you have made the entire Haunted Library Escape Room! If you can, grab some friends and have them try to solve the puzzle! Or celebrate by blowing it to smithereens with a Nuclear Bow. We can always use the drone to build it back again.

Temp1557437192

The final lesson of this course is a completely new challenge that will test what you learned in the previous lessons. We will break it down into individual challenges, worth a grand total of 1000 points!

The Haunted Library Part 5: Finishing the Escape Room Info

Account

MVCode Clubs

Created By

Mandala2 slinkous

Course:

The Haunted Library

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