Skip to main content

Overview

Characters are billboard entities with inventory, health, AI behavior, and animation states. They serve as the base for both players and NPCs.

Function Reference

character_new()

Creates and initializes a new character entity.
t_character *character_new(t_game *game, t_ftm_window *window, char identifier);
game
t_game *
required
Pointer to the game instance
window
t_ftm_window *
required
Pointer to the window instance
identifier
char
required
Character identifier for loading configuration and assets from the map
return
t_character *
Pointer to the newly created character, or NULL on allocation failure

Structure Definition

s_character

The character structure extends t_billboard with combat, inventory, and AI properties.
struct s_character
{
    t_billboard      billboard;
    t_sprite         **using_sprite;
    t_sprite         **death_sprite;
    t_sprite         **hit_sprite;
    t_sprite         **walking_sprite;
    t_sprite         **_sprite;
    t_time           last_hit;
    t_time           last_use;
    t_time           last_auto_use;
    t_fta_audio      *hit_sound;
    t_fta_audio      *death_sound;
    t_entity         *target_entity;
    t_character      *last_hit_by_character;
    t_direction      target_entity_direction;
    t_time           last_inventory_scroll;
    bool             cheating;
    t_item           *inventory[INVENTORY_SIZE];
    t_drop           *drop;
    bool             drop_items;
    double           fov;
    int              rays;
    int              inventory_index;
    char             last_used_item_identifier;
    int              ammo;
    int              score;
    t_time           died_at;
    bool             was_already_dead;
    bool             dead;
};
billboard
t_billboard
Base billboard entity data (see Billboards)
using_sprite
t_sprite **
360-degree sprite array for item usage animation
death_sprite
t_sprite **
360-degree sprite array for death animation
hit_sprite
t_sprite **
360-degree sprite array for hit reaction animation
walking_sprite
t_sprite **
360-degree sprite array for walking animation
_sprite
t_sprite **
Default idle sprite (reference to billboard.sprites)
last_hit
t_time
Timestamp of last damage received
last_use
t_time
Timestamp of last item use
last_auto_use
t_time
Timestamp of last automatic item use
hit_sound
t_fta_audio *
Audio played when character takes damage
death_sound
t_fta_audio *
Audio played when character dies
target_entity
t_entity *
Currently targeted entity for AI/interaction
last_hit_by_character
t_character *
Reference to the character that last damaged this character
target_entity_direction
t_direction
Direction to the target entity
last_inventory_scroll
t_time
Timestamp of last inventory scroll action
cheating
bool
Debug flag for cheat mode
inventory
t_item *[INVENTORY_SIZE]
Array of inventory items (size: 9)
drop
t_drop *
Drop entity spawned when items are dropped
drop_items
bool
Whether character drops items on death
fov
double
Field of view for rendering (defaults to PLAYER_FOV if not set)
rays
int
Number of rays for raycasting
inventory_index
int
Currently selected inventory slot (0-8)
last_used_item_identifier
char
Identifier of the last used item
ammo
int
Current ammunition count
score
int
Character’s score value (awarded to killer on death)
died_at
t_time
Timestamp of death
was_already_dead
bool
Flag tracking previous death state
dead
bool
Current death state

Constants

#define CHARACTER_HIT_DELAY 100.0
#define CHARACTER_DEFAULT_MAX_HEALTH 100
#define INVENTORY_SIZE 9
#define INVENTORY_SCROLL_DELAY 0.5
CHARACTER_HIT_DELAY
double
Duration in milliseconds that hit animation plays
CHARACTER_DEFAULT_MAX_HEALTH
int
Default maximum health if not specified in map configuration
INVENTORY_SIZE
int
Number of inventory slots
INVENTORY_SCROLL_DELAY
double
Minimum delay in seconds between inventory scrolls

Inventory System

Characters have a 9-slot inventory system configured via map types:
Z_0_INVENTORY w
Z_1_INVENTORY p
Z_2_INVENTORY h
Each slot is configured with {identifier}_{slot}_INVENTORY where slot is 0-8.

Map Type Configuration

Z_MAX_HEALTH 150
Z_AMMO 50
Z_SCORE 100
Z_FOV 66.0
Z_DROP_ITEMS TRUE
Z_WALKING assets/sprites/zombie/walk
Z_DEATH assets/sprites/zombie/death
Z_HIT assets/sprites/zombie/hit
Z_HIT assets/sounds/zombie_hit.wav
Z_DEATH assets/sounds/zombie_death.wav
Z_DROP d
MAX_HEALTH
int
Maximum health points (default: 100)
AMMO
int
Starting ammunition
SCORE
int
Score awarded to killer on death
FOV
double
Field of view in degrees
DROP_ITEMS
string
"TRUE" to drop inventory on death, "FALSE" otherwise
WALKING
string
Path to walking sprite assets (360-degree)
DEATH
string
Path to death sprite assets (360-degree)
HIT
string
Path to hit sprite assets (360-degree) or hit sound
DROP
char
Drop entity identifier spawned on death

Animation System

Characters automatically switch between animation states:
  1. Death - When health <= 0 and dead == true
  2. Hit - Within CHARACTER_HIT_DELAY ms after taking damage
  3. Using - While using an item
  4. Walking - While controller movement is active
  5. Idle - Default state
All animations support 360-degree viewing angles.

Combat System

When a character is shot:
void character_shot(t_entity *shooted, t_character *shooter)
  • Updates last_hit timestamp
  • Sets last_hit_by_character to the shooter
  • Plays hit sound or death sound
  • Awards score to shooter on death
  • Sets dead = true when health reaches 0
  • Triggers death animation

Helper Functions

void set_using(t_game *game, t_character *character);
bool walking(t_entity *entity);
bool using(t_character *character);
void call_item_frames(t_character *character);
void drop_items(t_game *game, t_character *character);
void free_inventory_items(t_character *character);

Example

// Create a zombie character
t_character *zombie = character_new(game, window, 'Z');

if (!zombie)
    return (error_handler("Failed to create character"));

// Configure character
zombie->ammo = 0;
zombie->score = 50;
zombie->billboard.entity.health = 150;

// Add item to inventory
zombie->inventory[0] = weapon_new(game, window, 'w');
zombie->inventory_index = 0;

// Check character state
if (zombie->dead)
    respawn_character(zombie);

See Also

  • Player - Player-specific character extension
  • Items - Inventory system
  • Billboards - Base billboard entity

Build docs developers (and LLMs) love