issun 0.10.0

A mini game engine for logic-focused games - Build games in ISSUN (一寸) of time
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# ISSUN Plugin System

## Overview

The Plugin system allows you to compose game systems in a modular, reusable way following the **80/20 pattern**:
- Framework provides **80%** reusable logic
- Games customize **20%** specific to their needs

## Architecture

Plugins bundle **System** (Application Logic) + **Service** (Domain Service):

```
plugin/
├── mod.rs           # Plugin trait definition
├── combat/
│   ├── plugin.rs    # TurnBasedCombatPlugin (registers System + Service)
│   ├── engine.rs    # CombatSystem (System - orchestration)
│   ├── service.rs   # CombatService (Service - pure logic)
│   └── types.rs     # Domain types (Combatant trait, etc.)
├── inventory/
│   ├── plugin.rs    # InventoryPlugin
│   ├── service.rs   # InventoryService (generic item management)
│   └── types.rs     # Item trait
└── loot/
    ├── plugin.rs    # LootPlugin
    ├── service.rs   # LootService (drop calculations)
    └── types.rs     # Rarity enum, DropConfig
```

**Service vs System**:
- **Service** (`service.rs`): Pure functions, stateless
  - Example: `CombatService::calculate_damage(base, defense) -> i32`
  - Example: `LootService::select_rarity(rng) -> Rarity`
- **System** (`engine.rs`): Stateful orchestration
  - Example: `CombatSystem { turn_count, log, combat_service }`

See [ARCHITECTURE.md](../../../ARCHITECTURE.md) for detailed guide.

## Built-in Plugins

### 1. TurnBasedCombatPlugin ✅

Turn-based combat system with damage calculation and combat logging.

**Components**:
- `CombatService` - Pure damage calculations
- `CombatSystem` - Turn management, combat log, score tracking

**Features**:
- Turn counter
- Damage calculation (Attack - Defense, min 1)
- Combat log
- Score tracking
- Trait-based combatants (`Combatant` trait)

**Status**: ✅ Production ready

**Usage**:
```rust
use issun::prelude::*;

// Define your combatants
#[derive(Debug, Clone)]
pub struct Player {
    pub hp: i32,
    pub attack: i32,
    pub defense: i32,
}

impl Combatant for Player {
    fn attack(&self) -> i32 { self.attack }
    fn defense(&self) -> i32 { self.defense }
    fn hp(&self) -> i32 { self.hp }
    fn is_alive(&self) -> bool { self.hp > 0 }
    fn take_damage(&mut self, damage: i32) { self.hp -= damage; }
}

// Register plugin
let game = GameBuilder::new()
    .with_plugin(TurnBasedCombatPlugin::default())
    .build()
    .await?;
```

**80/20 Split**:
- **80% Framework**: Damage formula, turn counter, combat log, score tracking
- **20% Game**: Combatant types, special attacks, status effects

---

### 2. InventoryPlugin ✅

Generic item management system that works with any item type.

**Components**:
- `InventoryService` - Generic item operations
- `Item` trait - Auto-implemented for `T: Clone + Send + Sync + 'static`

**Features**:
- Transfer items between inventories
- Generic type support (works with any type)
- Stack/unstack items
- Remove/consume items
- Count items

**Status**: ✅ Production ready

**Usage**:
```rust
use issun::prelude::*;

// Your item type automatically implements Item trait
#[derive(Debug, Clone)]
pub struct Weapon {
    pub name: String,
    pub attack: i32,
}

// Use the service
let mut player_inv = vec![weapon1.clone()];
let mut chest_inv = vec![weapon2.clone()];

// Transfer item from chest to player
InventoryService::transfer_item(&mut chest_inv, &mut player_inv, 0);

// Register plugin
let game = GameBuilder::new()
    .with_plugin(InventoryPlugin::new())
    .build()
    .await?;
```

**80/20 Split**:
- **80% Framework**: Item transfer, stack management, generic operations
- **20% Game**: Specific item types, equipment slots, item effects

---

### 3. LootPlugin ✅

Drop generation and rarity system with weighted random selection.

**Components**:
- `LootService` - Drop rate calculations, rarity selection
- `Rarity` enum - 5-tier system (Common → Legendary)
- `DropConfig` - Configurable drop rates

**Features**:
- 5-tier rarity system with drop weights
- Weighted random rarity selection
- Drop rate calculation with multipliers
- Multi-source drop counting
- Configurable DropConfig

**Status**: ✅ Production ready

**Usage**:
```rust
use issun::prelude::*;

// Configure drop rates
let config = DropConfig::new(0.3, 1.5); // 30% base × 1.5 = 45% chance
let mut rng = rand::thread_rng();

// Check if drop should occur
if LootService::should_drop(&config, &mut rng) {
    // Select rarity using weighted random
    let rarity = LootService::select_rarity(&mut rng);

    // Generate item based on rarity (game-specific)
    let item = match rarity {
        Rarity::Common => generate_common_item(),
        Rarity::Legendary => generate_legendary_item(),
        // ...
    };
}

// Calculate drops from multiple sources
let dead_enemies = 5;
let drop_count = LootService::calculate_drop_count(dead_enemies, &config, &mut rng);

// Register plugin
let game = GameBuilder::new()
    .with_plugin(LootPlugin::new())
    .build()
    .await?;
```

**Rarity Weights**:
- `Rarity::Common` - 50.0 (most common)
- `Rarity::Uncommon` - 25.0
- `Rarity::Rare` - 15.0
- `Rarity::Epic` - 7.0
- `Rarity::Legendary` - 3.0 (rarest)

**80/20 Split**:
- **80% Framework**: Rarity system, weighted selection, drop rate formula
- **20% Game**: Loot tables, item generation, rarity display

---

### Future / Additional Plugins 🔮

#### DungeonPlugin ✅

Floor progression and room navigation with mutable dungeon state stored in `ResourceContext`.

**Components**:
- `DungeonService` – Pure logic (available rooms, progression rules)
- `DungeonSystem` – Mutates `DungeonState` runtime resource
- `DungeonConfig` (Resource) + `DungeonState` (Runtime Resource)

**Usage**:
```rust
let game = GameBuilder::new()
    .with_plugin(DungeonPlugin::default())
    .build()
    .await?;

// Access runtime state via ResourceContext
let mut state = game.resources.get_mut::<DungeonState>().await?;
state.current_floor = 2;
```

#### RoomBuffPlugin ✅

Manages active buffs per room with runtime `ActiveBuffs` stored in `ResourceContext`.

**Components**:
- `BuffService` – Pure calculations (attack/defense bonuses, regen, etc.)
- `BuffSystem` – Applies/clears buffs, mutates `ActiveBuffs`
- `RoomBuffDatabase` (Resource) + `ActiveBuffs` (Runtime Resource)

**Usage**:
```rust
builder
    .register_resource(RoomBuffDatabase::default())
    .register_runtime_state(ActiveBuffs::default());

let mut active = game.resources.get_mut::<ActiveBuffs>().await?;
```

---

## Design Principles

### 1. **80/20 Rule**
- 80% reusable across projects
- 20% customization via game-specific logic

### 2. **Composable**
- Plugins can depend on other plugins
- Clean separation of concerns
- No tight coupling

### 3. **Optional**
- All plugins are opt-in
- Zero overhead if not used
- Progressive enhancement

### 4. **Game-agnostic**
- Generic interfaces (Combatant trait, Item trait, etc.)
- Configuration-driven behavior
- Easy to adapt to different game types

---

## Creating a Custom Plugin

A typical plugin bundles System + Service. There are two ways to implement plugins:

### Method 1: Using `#[derive(Plugin)]` (Recommended)

The derive macro automatically generates the Plugin trait implementation:

```rust
use issun::prelude::*;
use std::sync::Arc;

// 1. Define Service (pure logic)
#[derive(Service)]
#[service(name = "my_service")]
pub struct MyService {
    multiplier: f32,
}

impl MyService {
    pub fn calculate(&self, value: i32) -> i32 {
        (value as f32 * self.multiplier) as i32
    }
}

// 2. Define System (orchestration)
#[derive(System)]
#[system(name = "my_system")]
pub struct MySystem {
    counter: u32,
    my_service: MyService,
}

impl MySystem {
    pub fn process(&mut self, value: i32) -> i32 {
        self.counter += 1;
        self.my_service.calculate(value)
    }
}

// 3. Create Plugin using derive macro
#[derive(Plugin)]
#[plugin(name = "my_custom_plugin")]
pub struct MyCustomPlugin {
    #[plugin(service)]
    service: MyService,
    #[plugin(system)]
    system: MySystem,
}

impl MyCustomPlugin {
    pub fn new() -> Self {
        Self {
            service: MyService { multiplier: 1.5 },
            system: MySystem { counter: 0, my_service: MyService { multiplier: 1.5 } },
        }
    }
}
```

**Field Annotations**:
- `#[plugin(skip)]` - Field not registered (hooks, internal state)
- `#[plugin(resource)]` - Register as Resource (read-only config/definitions)
- `#[plugin(runtime_state)]` - Register as runtime state (mutable)
- `#[plugin(service)]` - Register as Service
- `#[plugin(system)]` - Register as System

### Method 2: Manual Implementation (For Special Cases)

Use manual implementation when you need:
- Custom `dependencies()` logic
- Dynamic initialization in `initialize()`
- Complex registration logic

```rust
use issun::prelude::*;
use async_trait::async_trait;

pub struct MyCustomPlugin;

#[async_trait]
impl Plugin for MyCustomPlugin {
    fn name(&self) -> &'static str {
        "my_custom_plugin"
    }

    fn build(&self, builder: &mut dyn PluginBuilder) {
        // Register both System and Service
        builder.register_system(Box::new(MySystem::new()));
        builder.register_service(Box::new(MyService::new()));
    }

    fn dependencies(&self) -> Vec<&'static str> {
        vec!["issun:time"]  // Declare dependencies on other plugins
    }

    async fn initialize(&mut self) {
        // Setup logic (optional)
    }
}
```

---

## Trait Extension Pattern

Games can extend framework types with game-specific functionality:

```rust
// Framework provides Rarity enum
use issun::prelude::Rarity;
use ratatui::style::Color;

// Game extends with UI display methods
pub trait RarityExt {
    fn ui_color(&self) -> Color;
    fn ui_symbol(&self) -> &'static str;
    fn display_name(&self) -> &'static str;
}

impl RarityExt for Rarity {
    fn ui_color(&self) -> Color {
        match self {
            Rarity::Common => Color::Gray,
            Rarity::Legendary => Color::Yellow,
            // ...
        }
    }

    fn ui_symbol(&self) -> &'static str {
        match self {
            Rarity::Common => "○",
            Rarity::Legendary => "❖",
            // ...
        }
    }

    fn display_name(&self) -> &'static str {
        match self {
            Rarity::Common => "Common",
            Rarity::Legendary => "Legendary",
            // ...
        }
    }
}

// Use both framework and game-specific methods
let rarity = Rarity::Legendary;
let weight = rarity.drop_weight();  // Framework method
let color = rarity.ui_color();      // Game-specific method
```

This pattern allows:
- ✅ Using framework's core logic
- ✅ Adding game-specific display/UI logic
- ✅ Keeping framework types clean
- ✅ Maintaining separation of concerns

---

## Plugin Composition

Combine multiple plugins for rich functionality:

```rust
use issun::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let game = GameBuilder::new()
        .with_plugin(TurnBasedCombatPlugin::default())?
        .with_plugin(InventoryPlugin::new())?
        .with_plugin(LootPlugin::new())?
        .build()
        .await?;

    // All three plugins are now available!
    // - Combat system for battles
    // - Inventory for item management
    // - Loot system for drops

    Ok(())
}
```

---

## Example: Junk Bot Game

See `examples/junk-bot-game/` for a complete example using all three plugins:
- Turn-based combat with bots
- Inventory and weapon switching
- Loot drops with rarity
- Room buffs and floor progression

**Run it**:
```bash
cargo run --example junk-bot-game
```

---

## Testing

Each plugin includes comprehensive unit tests:

```bash
# Test all plugins
cargo test -p issun

# Test specific plugin
cargo test -p issun combat
cargo test -p issun inventory
cargo test -p issun loot
```

**Current test coverage**:
- TurnBasedCombatPlugin: ✅ Fully tested
- InventoryPlugin: ✅ 8 tests
- LootPlugin: ✅ 8 tests

---

## Next Steps

1. ✅ Complete TurnBasedCombatPlugin implementation
2. ✅ Extract InventoryPlugin from junk-bot-game
3. ✅ Extract LootPlugin from junk-bot-game
4. 🔄 Design DungeonPlugin
5. 🔄 Design BuffPlugin
6. 🔄 Add more plugin examples
7. 🔄 Publish to crates.io