Some issues in the game that I will have to solve are how to make the player unit interact with the game world. In Such a way that he can stand on blocks, climb ladders etc. The world entities aren’t even connected to each other yet. But that’s not the main issue. On one hand I will need to draw the player to the screen by using coordinates somewhere between (0,0) to the corresponding width and height. On the other hand I want the player to interact with the game world by using a separate set of coordinates. Depending on where the player is within the game he will also have to be able to interact with the world, which means that the array grids that now are used as maps also have to know their own coordinates somehow. This has gotten a bit messy quickly. As always when writing code, there is no right way of doing it, only ‘less worse’.
A possible solution for this is to create a class that stores a map, which is an array grid of numbers, and the corresponding drawable texture for it. Besides this, the class should also have its own starting coordinates representing where this map piece is in the world. In other words, it could have a coordinate for its upper-left corner (if anything here sounds stupid it is because I made this up as I wrote it). I already know that a map will always have a width of 20x20 and height of 15x20. To display these maps on the screen I simply scale up the drawable images that I generate for them. By first loading the grids from text-files and then draw to an image in the code 20x20 blocks representing each number of the grid. So anyway, a map coordinate will go from an unknown value, (x,y), to (x+20x20,y+15x20). Likely I will need to initialize a starting position for a map and then write a function that calculates the other connected maps coordinates which are in the same world. The question that comes from this is how I will determine in what map the player is currently in. I should always easily have the players coordinates available by looking in the “entity class” that I have made for the player, but what I fear is that I might have to loop through all map classes for a world in order to find in which of them the player is in. I need a smart way of organizing the map classes in perhaps an array of some sort so that I can always instantly know where the player is at, instead of doing something that takes a linear time, O(n). The big O notation is used to discuss theoretical speeds of algorithms. For instance, if there were a million map classes and the algorithm to find the player was O(n) there is a possibility that the program needs to run one million times all the time as you’d play the game.
I need to know that if I divide the player coordinates x with the map width (20x20=400) and y with the map height (20x15=300), that I always will get the corresponding number for the map inside of a 2D array, containing all the map pieces. If you didn’t know, you don’t get any decimals when dividing two “integers” programmatically.
After I have solved the problems mentioned above, I also will have to correctly draw the player to the screen, possibly by subtracting its current coordinates with its present map coordinates. But then the problem arises, what if the map coordinate is negative? Do I instead add the players coordinates with the maps coordinates? Most likely not. Also, I will need to draw the map it is currently in on the right location of the screen as well as neighboring maps if certain conditions are met. “The right way” to position a map on the screen may entirely depend on how far to one side the player is. I.e. if he is by the edge of the map I may need to only display half of the map.