Tile Contents and Interactions
Relevant source files
The following files were used as context for generating this wiki page:The core gameplay of Farm Siege revolves around the state of an 8x8 grid. Each tile on this grid can contain different entities, such as plants, weeds, or moles. The TileContents system defines what can exist on a tile, how it is represented visually, and how it behaves over time. Player interactions, managed through the CharacterContainer, directly modify the state of these tiles.
The state of the entire grid is managed by the GridStore, which orchestrates the spawning, growth, and removal of tile contents. The visual representation of each tile and its content is handled by the GridTile component, which dynamically selects sprites based on the tile's current state. This document outlines the architecture of the tile content system, details each content type, and explains the player interaction logic.
Core Architecture
The system is built around a central enum, a data structure for tile state, and a store to manage the grid's state over time.
TileContents Enum
The TileContents enum is the foundation of the system, defining all possible types of content a tile can hold.
Sources: src/TileContents.ts:1-8
| Enum Value | Description |
|---|---|
Empty | The tile is empty and can be planted on. |
Mole | A mole is currently on this tile. It can be removed by the player. |
Molehill | A mound of dirt left behind by a mole. It can be cleared by the player. |
Weed | A weed is on this tile. It can spread to adjacent empty tiles. |
Plant | A tomato plant is on this tile. It grows over time and can be harvested. |
Lightning | A temporary visual effect indicating a lightning strike has occurred. |
Grid Data Structure
The state of the game grid is stored as a 2D array. Each element in the array is a TileData object, which contains the content type and an associated data object for stateful properties like a plant's age.
The diagram below shows the relationship between the main data types that form the grid.
Tile Rendering
The GridTile component is responsible for rendering a single tile and its contents. It uses a switch statement on the tileData.type to determine which sprite to display.
The following flowchart illustrates the rendering logic within GridTile.
Tile Content Types and Behavior
Each type of tile content has unique behaviors, managed primarily within the GridStore.
Plant (Tomato)
Plants are the primary objective for the player. They are placed by the player and grow over time.
- Growth: A plant's
ageis stored in itsTileData.dataobject. ThegrowPlantsmethod inGridStoreincrements the age of all plants every 100ms. - Visual Stages: The
GridTilecomponent renders different sprites for the tomato plant based on its age relative tovalues.plant.age.fullyGrown. TheTomatocomponent also encapsulates this logic for use in instructions. - Harvesting: Only fully grown plants yield points when removed by the player.
Sources: src/store/GridStore.ts:122-138, src/components/grid/GridTile.ts:84-87, src/components/tileContents/Tomato.ts:14-36, src/components/CharacterContainer.ts:96-99
Mole and Molehill
Moles are obstacles that appear periodically and can destroy plants.
- Spawning: The
updateMolemethod inGridStorehandles mole logic. A mole can spawn on a random tile. If a mole is already active, it moves to a new random tile, leaving aMolehillin its previous location. - Destruction: If a mole spawns on or moves to a tile with a plant, the plant is destroyed.
- Removal: The player can remove a
Molefor points or aMolehillto clear the tile.
The following diagram shows the lifecycle of a mole on the grid.
Sources: src/store/GridStore.ts:140-192
Weed
Weeds appear on empty tiles and can spread, preventing the player from planting.
- Spawning:
updateWeedinGridStorehas a chance to spawn a new weed on a random empty tile. - Spreading: Existing weeds have a chance to spread to an adjacent empty tile. The probability of spreading decreases as the number of weeds increases (
1 / Math.sqrt(foundWeed.length)). - Component: The
Weedcomponent is a simple sprite wrapper forweed.png.
Sources: src/store/GridStore.ts:194-242, src/components/tileContents/Weed.ts
Lightning
Lightning is a random event that clears a 3x3 area of the grid.
- Strike:
updateLightninginGridStoreselects a random tile as the epicenter. It clears all content in a 3x3 area around that tile. - Visuals: The
Lightningcomponent is displayed on the center tile of the strike. - Duration: The lightning visual effect is temporary. A timer removes the
Lightningcontent after 1.4 seconds, leaving an empty tile. The player cannot interact with a tile while the lightning effect is active.
Sources: src/store/GridStore.ts:244-289, src/store/GridStore.ts:81-83, src/components/grid/GridTile.ts:111-121
Player Interactions
The player, controlled via CharacterContainer, can interact with the grid to remove content or place new plants. The primary actions are "use" and "place".
Interaction Logic
The onTick method in CharacterContainer checks for player inputs and triggers actions on the GridStore.
| Action | Key / Button | Functionality - |
|---|---|---|
use | Space, ButtonB | Calls gridStore.removeContent on the player's current tile. The outcome depends on the content removed. - |
place | KeyV, ButtonA | Calls gridStore.placePlant on the player's current tile. This places a new plant if the tile is empty and sets the character's held item to None. |
Sources: src/components/CharacterContainer.ts:31-43, src/components/CharacterContainer.ts:94-136, src/components/CharacterContainer.ts:138-141
The sequence diagram below illustrates the process of a player harvesting a fully grown plant.
Interaction Outcomes
Removing content from a tile has different effects depending on the content type.
| Content Removed | Score Awarded | Character Holds Item |
|---|---|---|
Mole | values.scores.mole | HoldableItems.Hammer |
Plant (fully grown) | values.scores.plant | HoldableItems.Shovel |
Plant (not grown) | 0 | HoldableItems.Shovel |
Weed | values.scores.weed | HoldableItems.Shovel |
Molehill | 0 | HoldableItems.Shovel |
Sources: src/components/CharacterContainer.ts:104-122
Conclusion
The tile content and interaction system is a dynamic and central part of Farm Siege. It defines the game's core entities, their life cycles, and how the player engages with them. The separation of concerns—with TileContents defining the types, GridStore managing state and logic, GridTile handling rendering, and CharacterContainer processing player input—creates a robust and extensible architecture for the game's primary loop. The interplay between automated events like mole spawning and direct player actions like harvesting creates the central challenge of the game.