Player Character and Actions
Relevant source files
The following files were used as context for generating this wiki page:
- src/components/CharacterContainer.ts
- src/components/character/Character.ts
- src/components/character/CharacterBase.ts
- src/Game.ts
- src/screens/GameScreen.ts
- src/overlays/Bullets.ts
- src/components/instructions/Instructions7.ts
- src/components/instructions/Instructions3.ts
- src/components/instructions/Instructions2.ts
The player character is the central entity through which the player interacts with the game world of Farm Siege. It is responsible for movement, interacting with the grid to plant and harvest crops, and defending the farm from threats like moles and rabbits. The character's logic, state, and rendering are separated into distinct components to maintain a clean architecture.
The core of the character system is composed of three main parts: CharacterContainer for handling input and action logic, CharacterStore for state management, and the Character component for visual representation. This design allows for a clear flow of data from player input to game state changes and finally to what is rendered on screen.
Architecture Overview
The character system is orchestrated by the CharacterContainer component, which is rendered within the GameScreen. It listens for player inputs, processes game logic on each tick, and updates the CharacterStore. The Character component, in turn, subscribes to the CharacterStore to render the character's current state, including its position, direction, and held item.
This diagram illustrates the high-level data flow and component relationships.
State Management (CharacterStore)
While the CharacterStore.ts file is not directly provided, its structure and purpose are evident from its usage throughout the application, particularly in CharacterContainer.ts. The CharacterStore acts as the single source of truth for all state related to the player character.
Key properties managed by CharacterStore include:
posX,posY: The character's pixel coordinates on the canvas.fieldX,fieldY: The character's grid coordinates.direction: The direction the character is facing (Directions.LeftorDirections.Right).heldItem: The item the character is currently holding, represented by theHoldableItemsenum.bullets: An array ofBulletDataobjects when the gun is fired.hammerPosition: A number representing the animation state of the hammer.
The store exposes methods to manipulate this state, such as move(), fireGun(), and reset(), which are called from the CharacterContainer in response to player actions.
Sources: src/components/CharacterContainer.ts:80-86, src/components/CharacterContainer.ts:110-129, src/components/CharacterContainer.ts:153
Holdable Items
The heldItem state is crucial for determining the character's appearance and available actions. It is defined by the HoldableItems enum.
| Item | Description |
|---|---|
None | The character is holding nothing. Can plant seeds. |
Hammer | Used to smash moles. |
Shovel | Used to harvest plants, remove weeds, and clear molehills. |
Gun | Used to shoot rabbits. |
Sources: src/components/character/Character.ts:20, src/components/CharacterContainer.ts:116-128
Character Rendering
The visual representation of the player is handled by the Character component and its sub-components. It is a composite component that assembles the character's body, clothing, and held item based on props passed down from the CharacterContainer.
Sources: src/components/character/Character.ts
Component Composition
The Character component is built from several smaller, specialized components. This allows for easy customization and conditional rendering of parts like held items.
This diagram shows the compositional hierarchy of the Character component.
Base and Cosmetics
The CharacterBase component renders the main body and eyes. The direction of the eyes (eyes_left.png or eyes_right.png) is determined by the direction prop. The skin color of the base body is sourced from the CosmeticsStore. On top of this base, Pants, Shirt, and Hat components are rendered as cosmetic items.
Sources: src/components/character/CharacterBase.ts, src/components/character/Character.ts:37-45
Player Actions and Input
All player actions are initiated by input events, which are captured and processed within the CharacterContainer's onTick method. An InputMap instance is configured to bind keyboard keys and gamepad buttons to specific game actions.
Sources: [src/components/CharacterContainer.ts:21-58]
Input Mapping
The following table details the default input mappings for player actions.
| Action | Keyboard Key | Gamepad Button | Description |
|---|---|---|---|
up | W, ArrowUp | Left Stick Up | Move character up. |
left | A, ArrowLeft | Left Stick Left | Move character left. |
down | S, ArrowDown | Left Stick Down | Move character down. |
right | D, ArrowRight | Left Stick Right | Move character right. |
use | Space | Button B | Interact with the tile (harvest, smash mole, remove weed). |
place | V | Button A | Plant a crop on the current tile. |
fire | C | Button X | Fire the gun to shoot rabbits. |
Sources: src/components/CharacterContainer.ts:22-45
Action Logic
Movement
On each tick, CharacterContainer checks for pressed movement inputs. It calculates the distance to move based on values.character.movementSpeed and the timeDifference since the last frame, then calls characterStore.move(moveX, moveY) to update the state.
Sources: [src/components/CharacterContainer.ts:87-100]
"Use" Action
The "Use" action is the primary way the character interacts with objects on the grid. When the use input is detected, the logic checks the content of the tile the player is on and performs a corresponding action.
This flowchart shows the logic for the "Use" action.
"Place" Action
When the place input is detected, the character attempts to plant a crop. This action sets the heldItem to None and calls gridStore.placePlant() on the character's current tile coordinates.
Sources: src/components/CharacterContainer.ts:139-142, src/components/instructions/Instructions2.ts
"Fire" Action
The "Fire" action is used to defend against rabbits. It has a cooldown of 1200ms to prevent rapid firing.
The sequence diagram below shows the interactions between different stores and services when the player fires the gun.
Summary
The player character system in Farm Siege is a well-structured module that effectively separates concerns. CharacterContainer serves as the brain, translating player input into actions and state changes. CharacterStore acts as the memory, holding the character's state. Finally, the Character component and its children act as the body, visually representing that state. This architecture allows for clear data flow, easy maintenance, and the ability to extend character functionality by adding new actions or held items.