Viewport Extensions in Kotlin

Tim Buchalka's Learn Programming Academy
A free video tutorial from Tim Buchalka's Learn Programming Academy
Professional Programmers and Teachers - 842K+ students
4.5 instructor rating • 50 courses • 847,605 students

Lecture description

Write extension methods

Learn more from the full course

Learn Kotlin and Create Games Using the LibGdx Library

Become a real games programmer. Create Games Using Kotlin with the LibGDX Game Development Framework.

21:05:55 of on-demand video • Updated February 2020

  • Learn how to create your own games
  • Understand how to write reusable code that can be reused in other games
  • Learn how to create your own tools for game development
  • Have learned the Kotlin language
  • Understand how to use many useful design patterns
English [Auto] In this video we will create and write some extension methods for rendering a class. So let's get started. First let's see inside the shape renderers. You will notice we have a pattern where we need to call shape render or begin and shape render. And that pattern repeats a couple of times in a few letters one of them is brought great in those circles. We again have a brain that will begin with shape type feel again. After that we have the begin and end with shape type field. Later we help with shape type line. So this is a similar pattern. That we have with BEGIN and END using back. So instead of repeating the same code we will again create a higher order function to make our life easier so that we don't forget to begin and end. Now currently I am in GDX central project but let's switch to our obstacle or what. You will notice I have both projects open at the same time so I have two windows in one window. I have obstacle void and in another window GDX set the code that will right will be in graphics. So in our core Esser see you till we have GDX graphics and there we have our function Bache use. We will write a similar function with ship render by default it will use shape type line. So in other cases when you don't need the line you will have to manually write begin and end. But in most cases we will use a line so it makes sense to create that myth again. A similar function inline shape array. Are things we want to add this method to shape and then use name of the method action braces to unit or in other words aerotow with the braces are representing as you already know parameters and it is a return type. Or in other words units can be similar as void in Job. In other words we are not returning anything from this function. So now we can begin with shape type shape type will be shaped like line or shape pipeline in and between BEGIN and END. We will call our action or in other words the lambda expression that we will pass through the. The next extension method that we will create is great. We will extend viewport to cause and create mettled true. This method will just drop a world greed for specific viewport. So in case that you have multiple viewpoints you'll be able to draw grid on any view into a package. Let's create another file class and call it you part. Not without a lot you put ext viewport ext. Or in other words the XT suffix is frequently used for the files that contain extension methods. So all our files DDX array currently doesn't contain any extension methods but later it will contain. We will try to do the X-Files contains extensional GDX graphics has some metals in some extensionally Judith's input has extension method and GDX log doesn't have extension. You can rename all them with the XT suffix or you can leave them as they are. For now I will leave them or leave the names as they are because on one way there is a different concept. In other words naming convention can be viewport and then x is a suffix. Or you can just leave it as the class name. I'm prefixing these with GDX since those classes that we are extending in these files are just expulse. So let's continue with viewport text first is to extend viewport So the bright fun viewport. Any type of viewport that we use in our game will be able to get dropped. Greed is the name of the method for drawing. We need renderers So we will pass it as parameter. Render is Triplette. And for the cell sizes. Or in other words for those small rectangles that we have seen in previous lesson we will use parameter cell size. By default it will be one cell size equals one and we need to declare it in. And now we can write the method since we are using the default parameter and if we want to use this class in Java directly we can mix Java in Scotland. A good idea is to again as JVM loops cannot take them so that the compiler generates overloaded method where we can use just parameter renderer either call the programed to with senseis one and then we will have another overloaded method with the senseis where we can specify the cell size. So the first is to copy all color from renderer and then we will create some value. So veil bold color is that all the CPI is copy. We are copying the code. Then we will create double world we and double white double. Well we still want to draw a four quadrants and that way when we zoom out we will see the objects of the game that are out of the screen. So it makes it easier double what we did is equal to world. We did multiply by two. So where does this world with its variable come from. Since we're extending viewport we can access the variables of viewport not just when they click on well really gets to the viewport CLOS you method get worked with. So we can immediately access methods and properties in viewport when we are extending that cross or writing extension function for that. But double equals world height multiplied by two. So this one minute and the Willhite are coming from viewport and those values are the values that we are facing to construct. When we create our view. And now to drop everything we can use our new extension function around the rules that we use inside the lambda expression or the anonymous function. We will write all the code for drawing vertical and horizontal lines into drawing x and y line for the exits after we are done with drawing. We will reset callback. So when all is equal to all. But now we can continue with the movement here. We will set the course to white in case that we are changing the color before throwing the Great. So it's always good idea to set some color and reset it back to the old color that white. Be careful when you import the color. There are many other costs. First we are drawing a line. This will not be the copy paste code from the GDX simply. This time we use bible verse. X is equal to minus double world. We did. While X is less than double world we live then we will draw a line in increment x x plus equals size. But before we went into the road the light red light X minus double world height x double world. We are throwing all four quadrants. That is why we need double world. Now when we increment X we can move on. Throw in on points of drop all along the lines where white equals minus double world wide. While White is less than double that is a line minus double what we do. Why double world we do. Why is my. Again we need to increment y y y plus equals cell size. Don't forget that life in the first while we have X plus cell size cell size is just in one of the units. How many world units do we want in one rectangle. Now we need to draw those zero zero lines. Or in other words X is light's broke 0 0 are the lines that represent x and y x. Those will be rendered in red color surrender color red below we just need to draw two lines rather line zero and minus double world height and double world. Now that next line is rather lie and mine is double world. We did double world with us. And the list of lines that we will draw will represent the world puzzles. So anything that goes out of the world we will be able to see the bounce and immediately know that some game object is out of the world to draw well for this I believe all red color equals all the green red line zero world wide world. Really the main point is the red line. Well we know what else we did and the world. But now we can use this method to greed. Inside the game screen. So let's open the game screen. Call us inside the screen. Each here we can remove most of the code but we need all the graphic camera and support. So just remove these two part of the show. We can remove page views from the there and we don't need anything in this post. Later we will render the spot. So first let's create Mario and let it be our camera is Hardtalk graphic camera late for you. You bought in it. But that is a great show. We need to initialize those. So camera equals orthographic camera viewport equals. We know size is in the scheme of the game coffee world cup. This is what we created before and the worst parameter escape gamer. So our world really and world height is six. By that we will have six rectangles horizontally and then vertical. The last variable that we need to realize is that it's easy to shape them in these balls many to dispose render the clock polls in size. We need to know to find the viewport about the size viewport but update what we did. I mean the center of the camera. So we need to find truth and then we can continue inside the range to draw our European growth is now pretty easy when we have this instrument. All we have to do is call Duport got the wrong Greek and base our render as parameter render safe and then we can run it. So let's run it and let's see the degree that we have on the screen. You will notice the grid is very small and it is on the top left. So why is the grid in top left corner and what is the problem. When something like this happens it means usually that something is not configured properly. It can be viewport that is not configured properly or render or Kemmer inside the renderer. Remember how we always need to say the projection matrix. Now you can see what happens if we don't set the projection matrix viewport doesn't know anything about projection matrix or in other words if it indeed needs to know about kimona configuration or we need to do is call Shapeways or whatever name is to render. So we're right in the projection matrix is equal to camera. So whenever something like this happens it usually means you didn't said projection. Now it's run it again and you will see how we have a grade of six. What are the rules. Or in other words we have six rectangles horizontally and vertically we have 10 rectangles. What are those two green lines. Well we are not able to see them because we actually don't need them but later we will need if we set the cameras zoom to some value. So camera is so easy to do with. And let's run it again. You will see those two green lights. Now our game world will be inside this rectangle represented by two red lines which represent X's and two green lines represent the border or in other words the world buttons. Anything that is outside of this rectangle is outside of world and the player will be able to see anything that is outside. So this is where this rock green will be very helpful later when we end all circles into play. You will see how player will be able to go outside of screen which is not good and obstacles will also be able to fall off the screen. So I will see you in the next video.