- Edited
Summary:
Implemented a Cell Manager for data storage of grid squares and introduced a custom raycasting system to detect cell selection via mouse clicks.
What's New:
- Bitwise Data Packing and Unpacking - Breaking away from the traditional route of storing structs or classes in an array, I took the more challenging path of bitwise packing into a byte array. Though this adds an extra layer of intricacy to the system, it significantly optimizes data storage.
- Custom Raycasting System - Bypassing the usual static mesh rigid bodies and mesh colliders, I opted for a custom raycasting mechanism. The system evaluates the position of the ray at each step to determine whether it intersects with the grid. Additionally, it consults the heightmap to accurately identify terrain types based on different Y-axis levels, such as hills and water.
Challenges and Solutions:
- Cell Manager: I ventured into the realm of bitwise operations to achieve a level of data optimization that might make developers cry, but would certainly make computers happy. The Cell Manager has two primary responsibilities: packing and unpacking cell data into a byte array.
- Packing Data - Let's explain the first responsibility. Suppose a cell has the following attributes: "Terrain Type: WATER," "Resource Type: FISH," and "Occupied: True." The process is as follows:
- The terrain type "Water" (binary representation: 000) is shifted 5 bits to the left, resulting in "00000000."
- The resource type "Fish" (binary: 0010) is shifted 1 bit to the left, giving us "00000100."
- The "Occupied" status (binary: 1) remains as "00000001."
- Finally, these are combined using the bitwise OR operation, resulting in a packed value of "00000101."
- Data Unpacking: The second responsibility involves the reverse process, which involves interpreting the packed data back into its original, readable form. For example, if we need to access cell data at row "3" and column "3," the unpacking process is as follows:
- Shift the packed data "00000101" 5 bits to the right, yielding "00000000." Then use an AND operation with 0x7 (00000111) to get "0," representing the "WATER" terrain type.
- Shift the packed data "00000101" 1 bit to the right, giving "00000010." Using an AND operation with 0xF (00001111), we get "2," signifying the "FISH" resource type.
- Use an AND operation on the packed data "00000101" with 0x1 (00000001) to get "1," which corresponds to "True" for the "Occupied" status.
This approach allows for efficient data storage and quick data retrieval, although it does introduce a level of complexity in the codebase. Now, who cried while reading this?
- Packing Data - Let's explain the first responsibility. Suppose a cell has the following attributes: "Terrain Type: WATER," "Resource Type: FISH," and "Occupied: True." The process is as follows:
- Custom Raycaster: I've opted for a unique approach, forgoing the typical use of static rigidbodies with trimesh collision for each triangle. Instead, I've implemented my own raycaster. This custom system launches a ray in the normalized direction from the camera towards the mouse position.
This methodology offers a fine-tuned approach to grid interaction, eliminating the need for additional collision geometry. To make this more concrete, let's explore the code involved:
Here's a breakdown of the procedure:- Step 1: Calculate the directional vector pointing towards the mouse position.
- Step 2: Scale this direction by multiplying it with a step value, effectively determining the length of each ray step.
- Step 3: Compute a new ray position by summing the camera position with the scaled directional vector.
- Step 4: Derive the row and column indices by dividing the ray position by the cell size.
- Step 5: Validate if both row and column values are above 0 and if the ray's Y position is less than a single step.
- Step 6: We loop through Steps 2-5 until one of two conditions is met: either we acquire valid cell row and column, or we end up with null values.
Up Next: Data Import via Google Sheets
My next endeavor steers us towards data importation via Google Sheets. The focus here is to fetch data, such as building, unit or card
data, and store them as game "Resources".
- Find a way to import Google Sheet data into the engine.
- Create/Update/Delete custom "Resource" classes to hold the data for different game elements like buildings, units, and cards.
Wondering how I will create custom edtior button to pull the data. Is there an easy way to do this?