Finalizing the map

The last step to complete the map behaviour is having undiscovered tiles, that will be discovered when a starship travels across them. Let's create a bunch of functions to handle this.

The first thing I did was creating a new grey-colored material for the undiscovered tiles, which is assigned by defualt to the hex asset.

Next, I created an helper array of UPrimitiveComponent* in MainMap.h, to be filled when I spawn the tiles. This way, I always have a quick reference to what object is inside every cell of the map:



Then I added the highlighted lines to my MainMap constructor:


All the tiles are flagged as "undiscovered" at the beginning, and I store the asset pointers in the tilesArray variable. 

After this, I moved to the MyPlayerController class, and added the following lines after the creation of the p1 variable:


What I'm doing here, is passing a reference of the map to the player's ship, and then calling a new method of MainMap that discovers the tiles around the ship position. (Don't worry, we haven't defined that method yet, we'll do that in a moment).

Okay, this is the initial state of the level: a completely undiscovered map, with only the tiles around our ship already undiscovered.

Next, I want a way to discover other cells after I move my ship. So I opened the file PlayerShipClass.cpp and added the following line to the move() method:

Now you can understand why I passed a reference of the map to the player's ship class: I want to call the same method from here too, every time I complete the movement of my starship.

Now, all we have to do is implementing the method DiscoverTiles() in the MainMap class:

The method accepts 3 parameters: X and Y coordinates in World Space, and a radius of action expressed in number of cells. What we have to do is transforming the X and Y coordinates in J, K indexes of our tiles array, and then perform a distance check with the other cells to see if they are close enough to be undiscovered or not. Here's the complete method:


First, I find the indexes of the tile that has X and Y coordinates, with the following helper method:


Next, I set that tile as discovered, and I change its material to the standardMaterial.

After this, I check all the tiles of my map using the function FVector2D::Distance(); if they're closer than our range multiplied by the tileDimension constant (in my case it's 180.0f), then we discover them too.

This is the final result, now we have an interactive map that changes when we travel around!




Commenti