NaughtScape[07] - Oh Fiddlesticks, Time to Refactor ...

in #programming7 years ago

My original plan was to have an implementation of A* pathfinding in my character's movement code, but as I started to implement it I realized that something was wrong with how I set up my Tiles. I had set up my Tile class and PhysicalWorld class to both extend MonoBehaviour.

<p dir="auto">Normally this wouldn't be an issue, but to implement A* I will need to have a separate data class to represent a Tile. This unfortunately wouldn't be as easy as just stripping out the <code>: MonoBehaviour after my class name, which will remove the inheritance from <strong>MonoBehaviour, I instead would have to refactor a good portion of my code. <p dir="auto">My plan from here was: <ul> <li>Remove extension of <code>MonoBehaviour from Tile class <li>Change any references to a <strong>Tile using <strong>MonoBehaviour functionality. <li>Create a new <strong>WorldData class to act as a data class for the game world. <li>Move <strong>TileMap reading from <strong>PhysicalWorld to <strong>WorldData <li>Squash bugs <h2>First Steps <p dir="auto">To start myself off all I had to do was go to my Tile class and remove the <strong>MonoBehaviour extension from the class name, then wait to see what breaks. I took a bit of the same approach with creating a <strong>WorldData class, I created the file and created a simple <em>ReadMap function. Then I cut all of the code in <strong>PhysicalWorld's <em>GenerateMap relating to reading the map and placed it in the new function I created. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmNVMY2zADpWjGSj7JFTis73K8R6REWDdHSZBh3cuFb2bj/read%20map.PNG" alt="read map.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmNVMY2zADpWjGSj7JFTis73K8R6REWDdHSZBh3cuFb2bj/read%20map.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmNVMY2zADpWjGSj7JFTis73K8R6REWDdHSZBh3cuFb2bj/read%20map.PNG 2x" /> <blockquote> <p dir="auto">I passed in the tilemap so that I wouldn't need to keep a reference to it in the data class. <p dir="auto">This function grabs the object attached to the passed in tile map that holds the tiles, and loops through the tiles found inside. To try and cut down on the overhead of searching for tiles, I create a <code>Dictionary<string, Tile> that I can search through to find tiles by name, this dictionary will be the main data store for my tiles. I store the necessary information from the <strong>TerrainTile into a new <strong>Tile. The new tile is then saved to the dictionary. <p dir="auto">This still left me with the issue that the <strong>PhysicalWorld needs to generate the graphics based on the tile map. Since the reading of the tile map now happened in the <strong>WorldData I had to get the tile information from the data class into the graphics class. Luckily I thought ahead enough to make this fairly easy. Because I already have a dictionary containing all of the tiles I could just return this. <p dir="auto">Instead I chose to go with an accessor function <strong>GetTiles. This function returns an array of the tiles in the dictionary instead of the actual dictionary. This is because I would like the dictionary to only be edited from within the <strong>WorldData class. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmXcbVLeJ4KkfShPHctKvs2ZFdBYPhWW7Q1GF7Hgqo4KAi/get%20tiles.PNG" alt="get tiles.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmXcbVLeJ4KkfShPHctKvs2ZFdBYPhWW7Q1GF7Hgqo4KAi/get%20tiles.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmXcbVLeJ4KkfShPHctKvs2ZFdBYPhWW7Q1GF7Hgqo4KAi/get%20tiles.PNG 2x" /> <blockquote> <p dir="auto">The <strong>WorldData function that returns an array of all of the tiles. <p dir="auto">With this I could get the full list of tiles from the <strong>WorldData into the <strong>PhysicalWorld. This was how I managed to get the necessary information about the <strong>Tiles to <em>BuildGround. <p dir="auto">I ended up looping through the array using a <code>foreach loop. I used each <strong>Tile to generate a new <em>Vector3 world position, set the _<em>currTerrainTile to that of the tiles, and then it would call <em>BuildGround and as long as the _<em>currTerrainTile.models field was not null. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmPNsXxWG8cvoTxW2t8qwmr49Y1idraxcbVFSsu462w8xG/new%20generate%20world.PNG" alt="new generate world.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmPNsXxWG8cvoTxW2t8qwmr49Y1idraxcbVFSsu462w8xG/new%20generate%20world.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmPNsXxWG8cvoTxW2t8qwmr49Y1idraxcbVFSsu462w8xG/new%20generate%20world.PNG 2x" /> <blockquote> <p dir="auto">Refactored <em>GenerateWorld function <h3>Side-quest ! <p dir="auto">While editing the TileMap code I decided to try and tackle the issue of why my tile coordinates weren't using <em>ints, they were using <em>floats which looked ugly. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmT83R3QaMcSwiBnxTwkn9wqkompp3sPwvtnxj3UFCctiA/TileMap_Inspector_old.png" alt="TileMap_Inspector_old.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmT83R3QaMcSwiBnxTwkn9wqkompp3sPwvtnxj3UFCctiA/TileMap_Inspector_old.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmT83R3QaMcSwiBnxTwkn9wqkompp3sPwvtnxj3UFCctiA/TileMap_Inspector_old.png 2x" /> <p dir="auto">After a little bit of debugging, and a lot of frustration, I finally was able to track down the cause of the bug. The <strong>TileMaps <em>pixelToUnit variable was set too high so instead of setting the tiles 1 unit apart, they were getting set a fraction of 1 apart. <p dir="auto">So I made it set the <em>pixelsToUnit property to just be the same as the tile width. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmdKpai3N2iJr7DqbEKFCwKebu5WwFo8EpGqifkf76ZV8w/pixel_to_unit_fix.PNG" alt="pixel_to_unit_fix.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmdKpai3N2iJr7DqbEKFCwKebu5WwFo8EpGqifkf76ZV8w/pixel_to_unit_fix.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmdKpai3N2iJr7DqbEKFCwKebu5WwFo8EpGqifkf76ZV8w/pixel_to_unit_fix.PNG 2x" /> <blockquote> <p dir="auto">Seemed too easy to be true ! <p dir="auto">This made all of the tile coordinates be 1 apart from each other, but because their pivots were on the center point they also started at 0.5. To fix this I switched the pivots on the sprites to be the top left. <h2>It's a Small World After All <p dir="auto">This was able to solve my tile coordinate problem so that my tiles now used _int_s for their positions. This did cause another issue I hadn't thought about though, it ended up changing how the tile texture scaling worked. This meant the tile objects were in the correct location, but the visuals looked like this: <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmVDdNWixwoTAsLSGNxZLfLtZ41AfgDPjNH3wGASNNQ8JR/texture%20size%20issue.PNG" alt="texture size issue.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmVDdNWixwoTAsLSGNxZLfLtZ41AfgDPjNH3wGASNNQ8JR/texture%20size%20issue.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmVDdNWixwoTAsLSGNxZLfLtZ41AfgDPjNH3wGASNNQ8JR/texture%20size%20issue.PNG 2x" /> <blockquote> <p dir="auto">Tiles have to correct spacing, but incorrect texture size. <p dir="auto">This issue actually took me a long time to diagnose and eventually solve. At first I thought it might be that the tile sprites weren't set to the correct Pixels Per Unit amount, so I went through and set them all to 16. That didn't have the desired effect though. I spent a couple of days trying to solve this one issue, searching through classes again and again. I followed the textures from the beginning right up until they were drawn and still couldn't find what was causing the issue. <p dir="auto">Then as I was looking through the object and it's relationships I noticed that there was a reference set to a sprite that either had escaped my first tirade of changing the Pixels To Unit ratio or had somehow gotten reset. Because it was the sprite the default tile got it's dimensions from it altered the size of all of the tiles to be placed. <h2>Back to the Beginning <p dir="auto">With that all that was left was to clean up and comment my code. I did a few sweeps and got rid of errant <em>Debug statements, removed unused code lines, and fixed any minor bugs that popped up while testing the running of the <em>WorldGeneration. After that I ended up back where I started ! Which is exactly where I wanted to be functionality wise. <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmZYAnYCapNHPfiU1cXvfU5iAcXbfpTf7T1HmybwTHukeM/back%20to%20the%20start.PNG" alt="back to the start.PNG" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmZYAnYCapNHPfiU1cXvfU5iAcXbfpTf7T1HmybwTHukeM/back%20to%20the%20start.PNG 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmZYAnYCapNHPfiU1cXvfU5iAcXbfpTf7T1HmybwTHukeM/back%20to%20the%20start.PNG 2x" /> <h3>Until Next Time ! <p dir="auto">If you have any suggestions for improvements, questions, or links to resources that would help me out feel free to leave a comment. I am using <a href="https://unity3d.com/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Unity as a game engine and <a href="https://www.blender.org/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Blender for any 3D modeling I may have to do during this project. Upvote if you liked the post or found it helpful, and follow me for future updates on <strong>NaughtScape, sometimes other programming, and occasionally other stuff. All of the source code can be found on my <a href="https://github.com/MapleThunder/NaughtScape" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Github. <p dir="auto">You could also add me in game on OSRS if you play !<br /> My ign is <strong>MapleThunder.