Tiles Gone Wild

This branch of the class architecture starts with TpAnimZoneBase, which subclasses TpFlexAnimatedTile. The other tiles in this branch inherit the ability to have multiple animations selectable via the inspector or from code. Since all of these tiles implement a trigger zone, let's call them zone-based tiles.

First though, what problem are these tiles trying to solve? Certainly, the use of animated tiles is pretty easy to understand, but what are trigger zones?

The idea of a trigger zone is easy enough to understand too: when your Player or some NPC enters an area you want something to happen. TpAnimZoneBase provides the basic framework for establishing a BoundsInt which your code can use to determine zone entry, exit, etc.

By using the messaging and events systems in TpLib you can message a position to a zone-based tile and if it finds a match within its zone it can post an event. That's the basic functionality of TpAnimZoneBase.

TpAnimZoneBase

This is a base-class TPT tile that can be used to defining a trigger area. Its purpose is to define an area on the Tilemap, specifically, a BoundsInt. Since it’s a subclass of TpFlexAnimatedTile, its sprite can be animated. If you don’t desire animation, just don’t add a SpriteAnimationClipSet.

As you change the area positioning and size in editor (with the Selection Inspector), the tile’s sprite is transformed to encompass the trigger area; useful for visualizing the size of the trigger area. Since the tile controls the transform, transform modification in the TilePlusBase section of the inspector is unavailable.

At runtime, this tile is completely passive aside from animation. But with TpLib you can, for example, get a reference to every tile of this Type and use the trigger zones to create an action when a playable entity enters or leaves a trigger zone. It’s very general-purpose, so how you use it is naturally bespoke to your own project. However, one common case might be to load more tiles to open a new area. That leads us to TilePlusAnimZoneLoader, a subclass that embeds tileset information.

TpAnimZoneLoader

This class is a subclass of TpAnimatedZoneBase, and inherits its fields and editor appearance. It’s used to to provide information to make it easy to dynamically load archived Tilemaps from TpTileFab assets, which are created by the Tools/TilePlus/Prefabs/Make Tilefab or Prefab command. Public fields allow you to offset the loaded files from the tile position. You can also preview the loaded tiles.

The tile does not automatically load tilemaps at runtime. Rather, you send a message to it via the the TpLib SendMessage methods. As configured, it expects the message to contain a Vector3Int describing a position. If the position is within the Zone bounds, the tile uses TpLib’s PostTileEvent method to post a trigger event.

See the TopDownDemo’s TdDemoGameController monobehaviour component for an example. TpLib’s LoadImportedTileFab method handles all the details of the loading for you, so its easier than it sounds!

TpAnimZoneSpawner

This can be used to spawn prefabs and TPT tiles, using assets with lists of prefabs or tiles. This tile uses a built-in static library class called SpawningUtil which in turn depends on an interface called ITpSpawnUtilClient.

Prefabs can be unparented or parented to a Scene Object by using the GameObject name or tag. SpawningUtil has built-in but optional prefab pooling (using Unity built-in pool classes).

Tiles can be painted on the same tilemap or on a different tilemap which you specify using a GameObject name or tag, or a reference. This tile can respond to a SendMessage containing a Vector3Int describing a position. If the position is within the Zone bounds, it will spawn prefabs or paint tiles as you’ve configured it. Alternatively, you can spawn/paint via code using the instance methods SpawnPrefab or PaintTile if this approach doesn’t suit you.

You can use either a TpTileList or a TpPrefabList asset (or both) to specify which tiles or prefabs (or both) to use with this tile. Public fields include those from TpAnimZoneBase and its superclasses, and some additional fields.

As mentioned in an earlier post, the provided demo TopDownDemo uses all of the tiles discussed in this post.