Character system for adventure game protagonists and NPCs.
The Characters module provides a complete character system with:
- Base character functionality for all game entities
- Specialized player character with inventory integration
- NPC system with AI behaviors and scheduling
- Advanced animation with 8-directional movement
- Dialog and interaction systems
- Mood and personality traits
## Character Hierarchy
```
Character (abstract base)
├── Player (protagonist)
├── NPC (non-player characters)
│ ├── SimpleNPC (basic NPCs)
│ └── ScriptableCharacter (Lua-driven)
└── Character (8-dir animations)
```
## Animation System
```crystal
# Characters support multiple animation states
character.play_animation("walk_left")
character.set_animation_state(AnimationState::Talking)
# 8-directional movement for modern games
direction = Direction8.from_velocity(velocity)
character.play_animation(state, direction)
```
## AI Behaviors
```crystal
# NPCs can have complex behaviors
guard = NPC.new("Guard", pos, size)
guard.behavior = PatrolBehavior.new([point1, point2, point3])
guard.mood = CharacterMood::Suspicious
```
## Dialog Integration
```crystal
# Characters can engage in conversations
npc.on_talk do |player|
dialog_tree = load_dialog("guard_conversation")
dialog_tree.start(player, npc)
end
```
## Common Patterns
### Creating Interactive NPCs
```crystal
shopkeeper = NPC.new("Shopkeeper", Vector2.new(400, 300), Vector2.new(64, 96))
shopkeeper.load_spritesheet("shopkeeper.png", 64, 96)
shopkeeper.add_idle_animation
shopkeeper.mood = CharacterMood::Friendly
shopkeeper.on_interact = ->(player : Character) {
if player.has_item?("money")
open_shop_interface
else
shopkeeper.say("Come back when you have money!")
end
}
```
### Player Character Setup
```crystal
player = Player.new("Hero", start_position, Vector2.new(32, 48))
player.load_enhanced_spritesheet("hero_8dir.png", 32, 48, 8, 4)
player.movement_enabled = true
player.inventory_access = true
```
## See Also
- AI::NPCBehavior - NPC behavior patterns
- Dialogue::DialogTree - Conversation system
- AnimationController - Animation management
- Inventory::InventorySystem - Player inventory