Initiative: Red Dawn

June 2, 2016

Recursively hook user tiles with pre-existing ones

In our game, the player creates floor tiles at will. There are a few pre existing tiles scattered around with mana and stamina potions. But the rest is up to the player. So we need to make sure that works nicely. We need to:
  • When the player spawns a tile right into a pre existing one, that the next one starts after the pre existing one
  • Recursively check if that tile we found has another tile hooked to it and use that as our next tile start point (recursively)
  • Only allow the user to hook to an existing tile from the correct side (i.e.: not from a wrong angle even if the connectors touch)
  • When the user removes tiles, the pre existing tiles should remain there (this is a decision, we could make them disappear like any other if we wanted to… Who knows, we may do in the future)
In order to achieve ths, we’ve added a few invisible elements to our tiles prefabs:
  • A forward connector
  • A back connector
Screen Shot 2016-06-02 at 10.20.48 PM

The back and front tile connectors

Those two elements will allow us to achieve all the requirements above (except the one about tile deletion, but we’ll handle that one separately). First we will check if, once the tile is spawned, its front connector collides with the back connector of another tile. To achieve this, we’ll use a SphereCast, which is basically a “fat” ray cast.
        hit = Physics.SphereCastAll(ray, sphereRadius, maxDistance);         // If we hit anything...         if (hit.Length > 0) {             // Loop through all we hit             for (int x=0; x<hit.Length; x++) {                 if (hit[x].collider.tag == BackTileConnector) {                     // We hit a backConnector                 }             }         }
Then, we need to check if both connectors are aligned correctly. We do that by simply checking both their rotations and make sure they match.
                float angleDif = m_hook.eulerAngles.y  hit[x].transform.eulerAngles.y;
If they match, we can assess we are hooked to another tile correctly. So know we need to update our next tile spawn position to the tile we hooked to, to use that as the current position for the next tile. And then, on that existing tile, run the same check to see if it is also hooked to another tile. We do this recursively until there’s no more hooks. So at the end, our next tile spawn position will be on the last tile of the chain. But also, if when we spawn the tile our front connector does not collide with a back connector, we still need to check if the tile is not overlapping an existing tile in some other way. This one is easy, we can simply do a sphere cast to check if we collide to any existing tiles. If at any point of the process we find an issue, we cancel the tile creation and provide visual feedback to the player. We do that by showing a flash message and showing a ghosted version of the tile the player is trying to place for a brief moment. Using recursive functions is extremely powerful, and as such, extremely dangerous. It’s very easy to fall into an endless loop. Because of that, I highly recommend setting some sort of watchdog to limit the amount of loops we will allow so we don’t freeze our player’s device and crash. Use recursion it with care and you’ll love it! Don’t forget to follow us on twitter for news regarding articles and game development [twitter-follow screen_name=’indelvestudios’]

One Comment

  1. Spell Run – The backstage of a game | indelve
    June 14, 2016 @ 9:53 am

    […] Hooking user tiles with existing ones […]


Leave a Reply