Posted September 25, 2022 by FernKing
*******************
*******************
In this week, I took a long hard look at the walls of code I wrote in the week prior and came to the educated conclusion that they were rather trash. The logic was clunky and could be implemented way better, so I decided to go back and make it way better.
For a unit move to be classified as valid, there can be no units in its path between the units current and target square.
The old method of analysing this was super brute force and not efficient at all. It involved manually Finding all squares between the current and target square, finding all units on those squares and then processing logic for those units. This method overused the find function and would be to hard to alter for different unit movement types. It hit me one morning that there was an infinitely better way of doing this….
The new, sexy method involved using a straight line raycast between the current and target square, with a layerMask to target only units. This would return all units hit between the current and target. Furthermore, this method works for both the diagonal and straight movement unit types.
The basic move logic got the same makeover…
Similar to above, the old logic was brute force. It involved performing many lines of arithmetic on the name of the current square to see if the target square was in the correct path. This was clunky and didn’t spark joy.
When doing the raycast I had a moment of big brain enlightenment specifically to do with the line made between the current and target square.
A horizontal move always produced a line with angle 45, 135 degrees to the horizontal. Similarly, straight movement either produced lines of 0, 180, 270 degrees to the horizontal. Therefore, a move could be analysed and determined valid based on the angle of the line.
All in all, these two adjustments managed to cut the move logic code down by about 200 lines.
Following this little tangent, I decided to implement the book. The book provides info on a given unit to the player. As discussed before, the info tab appears in the action UI and the button can be selected to bring up important information on the selected unit.
As seen when button is pressed a canvas is instantiated corresponding with the name of the selected unit. If another canvas is present prior to this it is closed/destroyed. Future implementation will involve adding tabs to each canvas which provide: Flavour Text, ability info, movement info. This will be a straight forward yet time consuming task.
Here’s a lil sneak peak of the abilities I been working on…..
I’ll talk about it next week
Next Week Goals
Thanks for reading x