The Haunted Library Part 2: Droning the Interior

Planning
Step 1
Extreme library makeover!

In this lesson, we will learn how to extend the drone to give it new methods (like castle() and dancefloor()!), so that we can build all the desks, bookshelves, and lighting that we need for the inside of our library.

Temp1556647951
Step 2
Make a bookshelf function

We are going to start with an export function, that takes 2 inputs, width and height and builds a bookshelf to those dimensions.

Temp1553717558
Check Your Work
Step 3
Build some bookshelves

Find a space away from your library to test the bookshelf. Save, deploy, reload, and call /js bookshelf(10, 5) to build a large one.

Try out lots of different widths and heights to see how it looks. What happens if you make a very narrow bookshelf?

Temp1553717728
Step 4
Fix the narrow bookshelf

Right now, if we make a bookshelf that is only 2 blocks wide, it will just be oak planks. Use an if statement to only build bookshelf blocks if the width is less than 3.

Temp1553718046
Check Your Work
Step 5
Test out the narrow bookshelf

Save, deploy, reload, and call /js bookshelf(2, 2) to make sure we see books.

Temp1553718225
Step 6
Add a torch

Move the drone up one block below the top. We'll add a fence block to be a sconce for the torch, then move up to put a torch on top of it.

Temp1553718861
Check Your Work
Step 7
Test out the torch
Temp1553718762

Save, deploy, reload, and build a new bookshelf with a torch on it.

Challenge
Step 8
Add a torch to the starting side

The drone automatically creates a checkpoint called "start" when the drone is created. Move the drone there and repeat the steps you did for the first torch to put a torch on the other side of the bookshelf.

Temp1553719141

(The answer is in the code below if you get stuck)

Step 9
Add a ladder to the bookshelf
Temp1553719459

Every good library bookshelf needs a ladder!

Use an if statement to build a ladder on one side if the bookshelf is more than 3 blocks tall and 3 blocks wide.

Temp1553719731
Check Your Work
Step 10
Test the ladder

Save, deploy, reload, and call /js bookshelf(10, 5), to see your new ladder.

Temp1553719622
Step 11
Convert to an extend function

To change this function to be reusable by the drone, we need to get rid of the new drone that is created inside of it. Instead we'll use the keyword this to mean, whatever drone happens to be doing this method.

Temp1553720394
Step 12
Go back to the start

Every drone method needs to return to where it started, so that when people use it, they can keep track of where the drone is.

To do this, we create a checkpoint at the beginning, and have the drone move back to it at the end.

The return() shows that the function is over. It will also print that location if we call this function in the console.

Temp1553801298
Check Your Work
Step 13
Test your bookshelf function again

Save, deploy, reload, and call /js bookshelf(4, 4) to test. Nothing should have changed about the bookshelf, but you should still be able to call it even though it isn't an exports function.

Step 14
Rename the outerCorner
Temp1553720930

Because we have different functions to build the interior and exterior of the library, we need to reorganize the library object to match. Change the code you have so far to reflect that it only builds the exterior of the library.

Step 15
Find the location for the interior corner
Temp1553721792

Use for debug info with F3 to find the location for the front left corner of the inside of the library. We would need to break the wall to be facing the correct direction, so instead we will just make sure that it has the same yaw as the exterior corner.

Temp1553721712
Step 16
Create a method to build the interior

Create a new method called library.interior.build to create the floor of the library. Then, change your buildLibrary exports function to call library.interior.build().

Temp1553722014
Check Your Work
Step 17
Check out the floor

Save, deploy, reload and call /js buildLibrary().

The .boxa() drone method switches between the materials as it builds, which creates this awesome checkerboard effect.

Temp1553797071
Planning
Step 18
Adding in the bookshelves

To build the bookshelves along the walls of the library, we will need to move and turn the drone for each of them. Because the drone always turns clockwise, if we want it to face the wall on our left, we need to turn it 3 times.

Temp1553800413
Step 19
Build the three outside shelves

Now that you have extended the drone to include your bookshelf, you can use d.bookshelf(...) in your code to build the bookshelves. Add this code to your library.interior.build method.

Temp1556222008
Check Your Work
Step 20
Check out your bookshelves

Save, deploy, reload, and call your /js buildLibrary() function. Check to make sure that all of your bookshelves meet at the corners and that none of them are sticking out of the library building.

Temp1553802714
Step 21
Make a testing location for the chandelier

Find a space away from your library, and build up a 6-block-tall tower to find a good starting point for the chandelier. This will be your testLoc in the exports.chandelier function.

Temp1553803705 Temp1553804020
Step 22
Make variables for the materials

Because we will be switching between the materials a lot in this build, define short variables for the 3 different materials we use for the chandelier.

Then, add a parameter called color, so that we can choose different colors of glass.

Temp1553805216
Planning
Step 23
Building the chandelier
Chandelierdiagram
Step 24
Build the chandelier

Add this cone to build the chandelier. The part in green is the optional glass panes around it.

Temp1556226436
Check Your Work
Step 25
Test out the chandelier

Save, deploy, reload, and call /js chandelier("blue"). Try out lots of different colors!

Temp1553805943 Temp1553806230
Step 26
Convert the function to become a drone extension

Just like with the bookshelf, we are going to convert the function so that we can extend the drone to include it. Here are the steps:

  1. Rename the function so that it is no longer an exports. function.
  2. Create a checkpoint for the start
  3. Remove the code where you create a new drone.
  4. Use the this keyword any place you were using your drone (d) variable.
  5. Move the drone back to your start checkpoint inside of a return() statement
  6. Call Drone.extend() with the name of your function as the input.
Temp1553807218
Check Your Work
Step 27
Test out the chandelier drone function

Now that we have extended the drone to include this function, you should be able to call it to build anywhere. Save, deploy, reload, and test /js chandelier("red") in a new spot.

Temp1553807252
Step 28
Find a location for a chandelier inside the library

Find a location for the chandelier in the center of the ceiling in the library. To keep it from overlapping the ceiling blocks, go one block down from the very top.

Since the chandelier looks the same from all sides, the yaw here will just be 0.

Temp1553807382
Step 29
Use the location to build a chandelier

Add your location to the library object inside of the interior object.

Then, inside of the library.interior.build function, move the drone to this location, and have it use the new chandelier method. Choose the color you liked best when you were testing.

Temp1553807714
Check Your Work
Step 30
Check out the new chandelier

Save, deploy, reload, and call /js buildLibrary().

Temp1553878072
Planning
Step 31
Arranging the bookshelves

Use wool blocks to plan out where the bookshelves are going to go. You do not need to follow this diagram, but make sure that there is enough space to walk between them, and enough space for the librarian's desk.

Temp1553893044 Temp1553892826
Step 32
Save locations and widths for all the bookshelves

Create an array of all the bookshelves' data in your library object. You should be looking at the bottom left corner of each shelf when you record its location.

Yaw is extremely important here!

Check Facing in your debug menu:

  • north: 180
  • east: -90
  • south: 0
  • west: 90
Temp1553894343
Step 33
Make a method to build all the bookshelves

Make a method called library.bookshelves.build that uses a for loop to go through all the bookshelves in your library.bookshelves.data array, and builds them with the location and width specified.

Then, call this method in your library.interior.build method.

Temp1556229249
Check Your Work
Step 34
Check out your new shelves

Save, deploy, reload, call /js buildLibrary(). You know the drill.

Temp1553894785
Planning
Step 35
But where will we read?

Next, we are going to build little desks with tables for our library patrons, called "study carrels", and extend the drone to include them.

Temp1554235193
Step 36
Build a study carrel

Create an export function that builds a tiny desk using a stair block, fence post, and wooden pressure plate.

Temp1556230439
Step 37
Extend the drone to include studyCarrel
  1. Make a checkpoint for the start called "study carrel start"
  2. Delete the line that makes a new Drone
  3. Change all the d. to this.
  4. Move the drone back to the study carrel start at the end inside a return() statement
  5. Change the name of the function from export. to just a regular named function
  6. Extend the drone to include this function
Temp1556230597
Step 38
Add the study carrels to the library

Use a for loop to build 2 sets of 4 desks on either side of the library doors, using your new .studyCarrel() drone method.

Temp1554482814
Check Your Work
Step 39
Check out the study carrels

Save, deploy, reload, and call /js buildLibrary.

Temp1554482360
Step 40
Make a test function for the librarian's desk

Upside-down stairs are tricky. We use the drone stairs method to make sure that stairs are facing us, but it doesn't have a way to do the up-side-down ones.

This complicated line:

blocks.stars.oak + ":" + (Drone.PLAYER_STAIRS_FACING[d.dir] +4).toString()

Actually just gives us the block ID's 53:5, 53:6, 53:7, or 53:8, depending on which way the drone is facing.

Temp1553897139
Check Your Work
Step 41
Test out the desk

Find an empty place away from the library, place a block, and call /js buildDesk().

Try facing different directions when you call it to make sure the stairs still work.

Temp1553896976
Step 42
Add a desk location

Use your debugger to find the start location for the desk. You should be facing it the same way as in the picture, and record that yaw.

Temp1553895901 Temp1554140343
Step 43
Change the desk function

Rename your desk building function so that it is not an export function, but a library object method, and make the drone start at the location you just saved.

Temp1554141031
Step 44
Add the desk to your library build

Add your newly renamed library.desk.build() method to the library.interior.build() method.

Then, comment out everything else in the build, so we can focus just on the desk for now.

Temp1554141232
Check Your Work
Step 45
Build the librarian's desk

Save, deploy, reload, call /js buildLibrary().

Temp1554141441
Challenge
Step 46
Optional Challenge: Change the desk material

Using the same method we used to add a color parameter to the chandelier, add a parameter called wood to the library.desk.build function.

Hint: It will be easiest if you make a variable for the slab material and the stair material

Temp1554142592

Note: In 1.12, "acacia" and "dark_oak" do not work. You can always test if a material will work by finding a test space and calling /js box(blocks.stairs.acacia) in game.

This code should work for any slab/stair block. See if you can make a desk out of nether rack!

Planning
Step 47
Moving the drone for the chair

We are going to use a door to look like the back of the chair. This means we need to move the drone one block behind the block we placed for the chair (the purple wool block in this picture) and it needs to face away from the desk.

Temp1554143422
Step 48
Add a chair to the build

Move the drone into place to build a chair with a door and upper half slab. We'll also add a torch in the empty space between the chair and the desk to give it a creepy glow.

Temp1554144214
Check Your Work
Step 49
Test out the chair
Temp1554144142
Step 50
Temp1554144885

Next, we are going to use the drone's .wallsign method to put a sign on the front of the desk.

Just like with custom item lore, walsign takes an array of strings in quotes as the input. So, wallsign(["Hello", "", "World"]) would look like:

---------------------------------
|              Hello             |
|                                |
|              World             |
|                                |
---------------------------------
Check Your Work
Step 51
Test the first sign

Save, deploy, reload, rebuild. You should now see a sign in front of the desk.

Temp1554145011
Challenge
Step 52
Add two more signs
Temp1554145320

Hint: Use an empty string ("") to create a blank line
Hint: You can use the .red() method on a string for signs, just like you do for custom item lore.

Step 53
Add a hidden chest
Temp1554145704

We're going to add a chest underneath the desk in the ground. Move the drone in position and add it with blocks.chest

Check Your Work
Step 54
Test the chest

Save, deploy, reload, rebuild. Check for the chest under the desk, and that it can't be seen from any other angle, and that it can still be opened.

Temp1554145853
Step 55
Pick a location for the Restricted Section

Your Restricted Section will actually be one bookshelf block with a sign above it. You can put this in any bookshelf you like, but it will be most challenging if it's one you can't see when you first walk in.

Temp1554146332
Step 56
Save the location

Add a restrictedSection object as a property of the library, and a location as a loc property of restrictedSection.

Temp1554146561
Step 57
Create a method to build the Restricted Section

We're going to create a method to show which bookshelf the Restricted Section is, by adding a sign on top with torches on either side of it. Once you have made the library.restrictedSection.build method, call it in your library.interior.build method.

Temp1554146947 Temp1554147004
Check Your Work
Step 58
Check out the Restricted Section!
Temp1554146886

Save, deploy, reload, and rebuild. You should now see the sign and torches above your Restricted Section block.

Planning
Step 59
You finished the interior!

Congratulations, you have finished building the actual library! In the next lesson, we will add a spooky basement, and a secret passage you can open with a hidden lever to get there.

The Haunted Library Part 2: Droning the Interior 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