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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
//! Game context for ISSUN
//!
//! GameContext holds persistent data that survives scene transitions.
//!
//! # New Architecture (Proposal C)
//!
//! This module provides three specialized contexts:
//! - `ResourceContext`: Global shared state (Resources)
//! - `ServiceContext`: Stateless domain logic (Services)
//! - `SystemContext`: Stateful orchestration (Systems)

use crate::resources::Resources;
use crate::service::Service;
use crate::state::States;
use crate::system::System;
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use tokio::sync::{OwnedRwLockReadGuard, OwnedRwLockWriteGuard, RwLock};

/// Marker trait for game context
///
/// Game context should contain only data that:
/// - Persists across scene transitions
/// - Should be saved/loaded
/// - Is shared between scenes
pub trait GameContext: Send + Sync {
    // Marker trait - no required methods
}

/// Default context implementation
///
/// Provides a simple key-value store for game data, service registry, resources, and states
pub struct Context {
    data: HashMap<String, Box<dyn Any + Send + Sync>>,
    services: HashMap<&'static str, Box<dyn Service>>,
    resources: Resources,
    states: States,
}

impl Default for Context {
    fn default() -> Self {
        Self::new()
    }
}

impl Context {
    /// Create a new empty context
    pub fn new() -> Self {
        Self {
            data: HashMap::new(),
            services: HashMap::new(),
            resources: Resources::new(),
            states: States::new(),
        }
    }

    /// Insert a value into the context
    pub fn insert<T: Any + Send + Sync>(&mut self, key: impl Into<String>, value: T) {
        self.data.insert(key.into(), Box::new(value));
    }

    /// Get a reference to a value from the context
    pub fn get<T: Any + Send + Sync>(&self, key: &str) -> Option<&T> {
        self.data.get(key)?.downcast_ref()
    }

    /// Get a mutable reference to a value from the context
    pub fn get_mut<T: Any + Send + Sync>(&mut self, key: &str) -> Option<&mut T> {
        self.data.get_mut(key)?.downcast_mut()
    }

    /// Remove a value from the context
    pub fn remove(&mut self, key: &str) -> bool {
        self.data.remove(key).is_some()
    }

    /// Check if a key exists in the context
    pub fn contains(&self, key: &str) -> bool {
        self.data.contains_key(key)
    }

    /// Register a service with the context
    ///
    /// Services are accessible by name and provide reusable functionality.
    /// Typically registered during game initialization via GameBuilder.
    pub fn register_service(&mut self, service: Box<dyn Service>) {
        let name = service.name(); // Returns &'static str
        self.services.insert(name, service);
    }

    /// Access a service by name
    ///
    /// Services are typically stateless or have minimal state.
    /// This method provides immutable access to the service.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Get reference to a service
    /// if let Some(combat_service) = ctx.service("combat_service") {
    ///     combat_service.apply_attack(...);
    /// }
    /// ```
    pub fn service(&self, name: &str) -> Option<&dyn Service> {
        self.services.get(name).map(|s| s.as_ref())
    }

    /// Access a service by name with mutable reference
    ///
    /// Use this when the service needs to modify its internal state.
    /// Most services should be stateless and use `service()` instead.
    pub fn service_mut(&mut self, name: &str) -> Option<&mut dyn Service> {
        self.services.get_mut(name).map(|s| s.as_mut())
    }

    /// Get a service with a specific type
    ///
    /// This provides type-safe access via downcasting.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use issun::plugin::CombatService;
    ///
    /// if let Some(combat) = ctx.service_as::<CombatService>("combat_service") {
    ///     combat.apply_attack(...);
    /// }
    /// ```
    pub fn service_as<T: Service + 'static>(&self, name: &str) -> Option<&T> {
        self.service(name)?.as_any().downcast_ref::<T>()
    }

    /// Get a service with a specific type (mutable)
    pub fn service_as_mut<T: Service + 'static>(&mut self, name: &str) -> Option<&mut T> {
        self.service_mut(name)?.as_any_mut().downcast_mut::<T>()
    }

    /// Get the number of registered services
    pub fn service_count(&self) -> usize {
        self.services.len()
    }

    /// Get all registered service names
    pub fn service_names(&self) -> Vec<&'static str> {
        self.services.keys().copied().collect()
    }

    /// Get immutable reference to Resources
    ///
    /// Resources contain read-only data like asset databases and configuration.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Access resource
    /// if let Some(enemy_db) = ctx.resources().get::<EnemyDatabase>() {
    ///     println!("Loaded {} enemies", enemy_db.enemies.len());
    /// }
    /// ```
    pub fn resources(&self) -> &Resources {
        &self.resources
    }

    /// Get mutable reference to Resources
    ///
    /// Use this to register resources during game initialization.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Register resource
    /// ctx.resources_mut().insert(EnemyDatabase::from_file("enemies.ron"));
    /// ```
    pub fn resources_mut(&mut self) -> &mut Resources {
        &mut self.resources
    }

    /// Get immutable reference to States
    ///
    /// States contain mutable runtime game state (save/load targets).
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Access state (read-only)
    /// if let Some(territory_state) = ctx.states().get::<TerritoryState>() {
    ///     println!("Control: {:?}", territory_state.control);
    /// }
    /// ```
    pub fn states(&self) -> &States {
        &self.states
    }

    /// Get mutable reference to States
    ///
    /// Use this to access and modify game state during runtime.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Modify state
    /// if let Some(state) = ctx.states_mut().get_mut::<TerritoryState>() {
    ///     state.control.insert(territory_id, 0.5);
    /// }
    /// ```
    pub fn states_mut(&mut self) -> &mut States {
        &mut self.states
    }
}

impl GameContext for Context {}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;

    // Mock service for testing
    #[derive(Clone)]
    struct MockCombatService {
        damage_multiplier: f32,
    }

    impl MockCombatService {
        fn new() -> Self {
            Self {
                damage_multiplier: 1.0,
            }
        }

        fn calculate(&self, base: i32) -> i32 {
            (base as f32 * self.damage_multiplier) as i32
        }
    }

    #[async_trait]
    impl Service for MockCombatService {
        fn name(&self) -> &'static str {
            "mock_combat"
        }

        fn clone_box(&self) -> Box<dyn Service> {
            Box::new(self.clone())
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
    }

    #[test]
    fn test_service_registration() {
        let mut ctx = Context::new();
        assert_eq!(ctx.service_count(), 0);

        let service = Box::new(MockCombatService::new());
        ctx.register_service(service);

        assert_eq!(ctx.service_count(), 1);
        assert_eq!(ctx.service_names(), vec!["mock_combat"]);
    }

    #[test]
    fn test_service_access() {
        let mut ctx = Context::new();
        ctx.register_service(Box::new(MockCombatService::new()));

        // Access by name
        let service = ctx.service("mock_combat");
        assert!(service.is_some());
        assert_eq!(service.unwrap().name(), "mock_combat");

        // Access missing service
        let missing = ctx.service("nonexistent");
        assert!(missing.is_none());
    }

    #[test]
    fn test_service_type_safe_access() {
        let mut ctx = Context::new();
        ctx.register_service(Box::new(MockCombatService::new()));

        // Type-safe access
        let service = ctx.service_as::<MockCombatService>("mock_combat");
        assert!(service.is_some());

        // Use the service
        let damage = service.unwrap().calculate(100);
        assert_eq!(damage, 100);
    }

    #[test]
    fn test_service_mutable_access() {
        let mut ctx = Context::new();
        ctx.register_service(Box::new(MockCombatService::new()));

        // Mutable access
        let service = ctx.service_as_mut::<MockCombatService>("mock_combat");
        assert!(service.is_some());

        // Modify service state
        let service = service.unwrap();
        service.damage_multiplier = 2.0;

        // Verify change
        let service = ctx.service_as::<MockCombatService>("mock_combat").unwrap();
        let damage = service.calculate(100);
        assert_eq!(damage, 200);
    }

    #[test]
    fn test_multiple_services() {
        let mut ctx = Context::new();

        #[derive(Clone)]
        struct ServiceA;
        #[derive(Clone)]
        struct ServiceB;

        #[async_trait]
        impl Service for ServiceA {
            fn name(&self) -> &'static str {
                "service_a"
            }

            fn clone_box(&self) -> Box<dyn Service> {
                Box::new(self.clone())
            }
            fn as_any(&self) -> &dyn Any {
                self
            }
            fn as_any_mut(&mut self) -> &mut dyn Any {
                self
            }
        }

        #[async_trait]
        impl Service for ServiceB {
            fn name(&self) -> &'static str {
                "service_b"
            }

            fn clone_box(&self) -> Box<dyn Service> {
                Box::new(self.clone())
            }
            fn as_any(&self) -> &dyn Any {
                self
            }
            fn as_any_mut(&mut self) -> &mut dyn Any {
                self
            }
        }

        ctx.register_service(Box::new(ServiceA));
        ctx.register_service(Box::new(ServiceB));

        assert_eq!(ctx.service_count(), 2);
        assert!(ctx.service("service_a").is_some());
        assert!(ctx.service("service_b").is_some());
    }
}

// ==================== New Architecture (Proposal C) ====================

/// Shared resource container type (`Arc<RwLock<...>>`)
type Resource = Arc<RwLock<Box<dyn Any + Send + Sync>>>;

/// Read guard for a resource in ResourceContext
///
/// This guard dereferences to &T and releases the lock on drop.
pub struct ResourceReadGuard<T: 'static> {
    guard: OwnedRwLockReadGuard<Box<dyn Any + Send + Sync>>,
    _marker: PhantomData<T>,
}

impl<T: 'static> Deref for ResourceReadGuard<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.guard
            .downcast_ref::<T>()
            .expect("Resource type mismatch - this is a bug")
    }
}

/// Write guard for a resource in ResourceContext
///
/// This guard dereferences to &mut T and releases the lock on drop.
pub struct ResourceWriteGuard<T: 'static> {
    guard: OwnedRwLockWriteGuard<Box<dyn Any + Send + Sync>>,
    _marker: PhantomData<T>,
}

impl<T: 'static> Deref for ResourceWriteGuard<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.guard
            .downcast_ref::<T>()
            .expect("Resource type mismatch - this is a bug")
    }
}

impl<T: 'static> DerefMut for ResourceWriteGuard<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.guard
            .downcast_mut::<T>()
            .expect("Resource type mismatch - this is a bug")
    }
}

/// Container for global, shared state (Resources)
///
/// Thread-safe with async RwLock for concurrent access:
/// - Multiple readers OR single writer
/// - Immutable during scene rendering, mutable in systems
///
/// # Example
///
/// ```ignore
/// let mut resources = ResourceContext::new();
/// resources.insert(Player::new("Hero"));
/// resources.insert(Score(0));
///
/// // Read access (multiple readers allowed)
/// let player = resources.get::<Player>().await.unwrap();
/// println!("Player HP: {}", player.hp);
///
/// // Write access (exclusive)
/// let mut player = resources.get_mut::<Player>().await.unwrap();
/// player.hp -= 10;
/// ```
pub struct ResourceContext {
    resources: HashMap<TypeId, Resource>,
}

impl ResourceContext {
    /// Create a new empty ResourceContext
    pub fn new() -> Self {
        Self {
            resources: HashMap::new(),
        }
    }

    /// Insert a resource into the context
    ///
    /// # Example
    ///
    /// ```ignore
    /// resources.insert(Player::new("Hero"));
    /// resources.insert(Inventory::default());
    /// ```
    pub fn insert<T: 'static + Send + Sync>(&mut self, resource: T) {
        self.resources
            .insert(TypeId::of::<T>(), Arc::new(RwLock::new(Box::new(resource))));
    }

    /// Insert a pre-boxed resource into the context (internal use)
    ///
    /// Used by GameBuilder to transfer resources from plugin registration.
    pub(crate) fn insert_boxed(
        &mut self,
        type_id: TypeId,
        boxed: Box<dyn std::any::Any + Send + Sync>,
    ) {
        self.resources.insert(type_id, Arc::new(RwLock::new(boxed)));
    }

    /// Get immutable reference to a resource (async read lock)
    ///
    /// Returns a guard that dereferences to &T.
    /// Multiple readers can access simultaneously.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let player = resources.get::<Player>().await?;
    /// println!("HP: {}", player.hp);
    /// // Guard automatically released when dropped
    /// ```
    pub async fn get<T: 'static + Send + Sync>(&self) -> Option<ResourceReadGuard<T>> {
        let resource = self.resources.get(&TypeId::of::<T>())?.clone();
        let guard = resource.read_owned().await;
        Some(ResourceReadGuard {
            guard,
            _marker: PhantomData,
        })
    }

    /// Get mutable reference to a resource (async write lock)
    ///
    /// Returns a guard that dereferences to &mut T.
    /// Exclusive access - blocks all other readers and writers.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let mut player = resources.get_mut::<Player>().await?;
    /// player.hp -= 10;
    /// // Guard automatically released when dropped
    /// ```
    pub async fn get_mut<T: 'static + Send + Sync>(&self) -> Option<ResourceWriteGuard<T>> {
        let resource = self.resources.get(&TypeId::of::<T>())?.clone();
        let guard = resource.write_owned().await;
        Some(ResourceWriteGuard {
            guard,
            _marker: PhantomData,
        })
    }

    /// Try to get immutable reference to a resource without awaiting
    pub fn try_get<T: 'static + Send + Sync>(&self) -> Option<ResourceReadGuard<T>> {
        let resource = self.resources.get(&TypeId::of::<T>())?.clone();
        let guard = resource.try_read_owned().ok()?;
        Some(ResourceReadGuard {
            guard,
            _marker: PhantomData,
        })
    }

    /// Try to get mutable reference to a resource without awaiting
    pub fn try_get_mut<T: 'static + Send + Sync>(&self) -> Option<ResourceWriteGuard<T>> {
        let resource = self.resources.get(&TypeId::of::<T>())?.clone();
        let guard = resource.try_write_owned().ok()?;
        Some(ResourceWriteGuard {
            guard,
            _marker: PhantomData,
        })
    }

    /// Check if a resource exists
    pub fn contains<T: 'static>(&self) -> bool {
        self.resources.contains_key(&TypeId::of::<T>())
    }

    /// Remove a resource from the context
    pub fn remove<T: 'static>(&mut self) -> bool {
        self.resources.remove(&TypeId::of::<T>()).is_some()
    }

    /// Get the number of registered resources
    pub fn len(&self) -> usize {
        self.resources.len()
    }

    /// Check if the context is empty
    pub fn is_empty(&self) -> bool {
        self.resources.is_empty()
    }
}

impl Default for ResourceContext {
    fn default() -> Self {
        Self::new()
    }
}

/// Container for stateless Services (pure domain logic)
///
/// Services provide pure, reusable functionality without state.
///
/// # Example
///
/// ```ignore
/// let mut services = ServiceContext::new();
/// services.register(Box::new(CombatService::new()));
/// services.register(Box::new(LootService::new()));
///
/// // Access by name
/// let combat = services.get_as::<CombatService>("combat_service")?;
/// let damage = combat.calculate_damage(attack, defense);
/// ```
pub struct ServiceContext {
    services: HashMap<&'static str, Box<dyn Service>>,
}

impl ServiceContext {
    /// Create a new empty ServiceContext
    pub fn new() -> Self {
        Self {
            services: HashMap::new(),
        }
    }

    /// Register a service with the context
    ///
    /// Services are identified by their name() method.
    ///
    /// # Example
    ///
    /// ```ignore
    /// services.register(Box::new(CombatService::new()));
    /// ```
    pub fn register(&mut self, service: Box<dyn Service>) {
        let name = service.name();
        self.services.insert(name, service);
    }

    /// Get reference to a service by name
    ///
    /// # Example
    ///
    /// ```ignore
    /// if let Some(service) = services.get("combat_service") {
    ///     println!("Found service: {}", service.name());
    /// }
    /// ```
    pub fn get(&self, name: &str) -> Option<&dyn Service> {
        self.services.get(name).map(|boxed| boxed.as_ref())
    }

    /// Get mutable reference to a service by name
    pub fn get_mut(&mut self, name: &str) -> Option<&mut dyn Service> {
        self.services.get_mut(name).map(|boxed| boxed.as_mut())
    }

    /// Get type-safe reference to a service
    ///
    /// # Example
    ///
    /// ```ignore
    /// let combat = services.get_as::<CombatService>("combat_service")?;
    /// let damage = combat.calculate_damage(100, 20);
    /// ```
    pub fn get_as<T: Service + 'static>(&self, name: &str) -> Option<&T> {
        self.get(name)?.as_any().downcast_ref::<T>()
    }

    /// Get type-safe mutable reference to a service
    pub fn get_as_mut<T: Service + 'static>(&mut self, name: &str) -> Option<&mut T> {
        self.get_mut(name)?.as_any_mut().downcast_mut::<T>()
    }

    /// Get the number of registered services
    pub fn len(&self) -> usize {
        self.services.len()
    }

    /// Check if the context is empty
    pub fn is_empty(&self) -> bool {
        self.services.is_empty()
    }

    /// Get all registered service names
    pub fn service_names(&self) -> Vec<&'static str> {
        self.services.keys().copied().collect()
    }
}

impl Default for ServiceContext {
    fn default() -> Self {
        Self::new()
    }
}

/// Container for stateful Systems (application logic & orchestration)
///
/// Systems are owned by SceneDirector and passed as &mut, so no Arc needed.
/// Systems orchestrate game logic using Services and manage their own state.
///
/// # Example
///
/// ```ignore
/// let mut systems = SystemContext::new();
/// systems.register(CombatSystem::new());
/// systems.register(TurnManager::new());
///
/// // Access with type-safe API
/// let combat = systems.get_mut::<CombatSystem>()?;
/// combat.process_turn(resources).await?;
/// ```
pub struct SystemContext {
    systems: HashMap<TypeId, Box<dyn System>>,
}

impl SystemContext {
    /// Create a new empty SystemContext
    pub fn new() -> Self {
        Self {
            systems: HashMap::new(),
        }
    }

    /// Register a system with the context
    ///
    /// Systems are identified by their TypeId.
    ///
    /// # Example
    ///
    /// ```ignore
    /// systems.register(CombatSystem::new());
    /// systems.register(TurnManager::new());
    /// ```
    pub fn register<T: System + 'static>(&mut self, system: T) {
        self.systems.insert(TypeId::of::<T>(), Box::new(system));
    }

    /// Register an already boxed system (used by GameBuilder)
    pub fn register_boxed(&mut self, system: Box<dyn System>) {
        let type_id = system.as_any().type_id();
        self.systems.insert(type_id, system);
    }

    /// Get immutable reference to a system
    ///
    /// # Example
    ///
    /// ```ignore
    /// let combat = systems.get::<CombatSystem>()?;
    /// let turn_count = combat.turn_count();
    /// ```
    pub fn get<T: System + 'static>(&self) -> Option<&T> {
        self.systems
            .get(&TypeId::of::<T>())?
            .as_any()
            .downcast_ref::<T>()
    }

    /// Get mutable reference to a system
    ///
    /// # Example
    ///
    /// ```ignore
    /// let combat = systems.get_mut::<CombatSystem>()?;
    /// combat.execute_player_attack(resources).await?;
    /// ```
    pub fn get_mut<T: System + 'static>(&mut self) -> Option<&mut T> {
        self.systems
            .get_mut(&TypeId::of::<T>())?
            .as_any_mut()
            .downcast_mut::<T>()
    }

    /// Check if a system exists
    pub fn contains<T: System + 'static>(&self) -> bool {
        self.systems.contains_key(&TypeId::of::<T>())
    }

    /// Remove a system from the context
    pub fn remove<T: System + 'static>(&mut self) -> bool {
        self.systems.remove(&TypeId::of::<T>()).is_some()
    }

    /// Get the number of registered systems
    pub fn len(&self) -> usize {
        self.systems.len()
    }

    /// Check if the context is empty
    pub fn is_empty(&self) -> bool {
        self.systems.is_empty()
    }
}

impl Default for SystemContext {
    fn default() -> Self {
        Self::new()
    }
}

// ==================== Tests for New Architecture ====================

#[cfg(test)]
mod new_context_tests {
    use super::*;
    use async_trait::async_trait;

    // Test Resource
    #[derive(Debug, Clone, PartialEq)]
    struct Player {
        name: String,
        hp: i32,
        max_hp: i32,
    }

    impl Player {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
                hp: 100,
                max_hp: 100,
            }
        }
    }

    #[derive(Debug, Clone, PartialEq)]
    struct Score(i32);

    // Test Service
    #[derive(Clone)]
    struct TestCombatService;

    impl TestCombatService {
        fn calculate_damage(&self, attack: i32, defense: i32) -> i32 {
            (attack - defense).max(1)
        }
    }

    #[async_trait]
    impl Service for TestCombatService {
        fn name(&self) -> &'static str {
            "test_combat"
        }

        fn clone_box(&self) -> Box<dyn Service> {
            Box::new(self.clone())
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
    }

    // Test System
    struct TestCombatSystem {
        turn_count: u32,
    }

    impl TestCombatSystem {
        fn new() -> Self {
            Self { turn_count: 0 }
        }

        fn increment_turn(&mut self) {
            self.turn_count += 1;
        }
    }

    #[async_trait]
    impl System for TestCombatSystem {
        fn name(&self) -> &'static str {
            "test_combat_system"
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
    }

    // ===== ResourceContext Tests =====

    #[tokio::test]
    async fn test_resource_context_insert_and_get() {
        let mut resources = ResourceContext::new();
        resources.insert(Player::new("Hero"));

        let player = resources.get::<Player>().await.unwrap();
        assert_eq!(player.name, "Hero");
        assert_eq!(player.hp, 100);
    }

    #[tokio::test]
    async fn test_resource_context_get_mut() {
        let mut resources = ResourceContext::new();
        resources.insert(Player::new("Hero"));

        {
            let mut player = resources.get_mut::<Player>().await.unwrap();
            player.hp -= 30;
        }

        let player = resources.get::<Player>().await.unwrap();
        assert_eq!(player.hp, 70);
    }

    #[tokio::test]
    async fn test_resource_context_multiple_resources() {
        let mut resources = ResourceContext::new();
        resources.insert(Player::new("Hero"));
        resources.insert(Score(100));

        assert_eq!(resources.len(), 2);

        let player = resources.get::<Player>().await.unwrap();
        let score = resources.get::<Score>().await.unwrap();

        assert_eq!(player.name, "Hero");
        assert_eq!(score.0, 100);
    }

    #[tokio::test]
    async fn test_resource_context_contains() {
        let mut resources = ResourceContext::new();
        resources.insert(Player::new("Hero"));

        assert!(resources.contains::<Player>());
        assert!(!resources.contains::<Score>());
    }

    #[tokio::test]
    async fn test_resource_context_remove() {
        let mut resources = ResourceContext::new();
        resources.insert(Player::new("Hero"));

        assert!(resources.contains::<Player>());
        assert!(resources.remove::<Player>());
        assert!(!resources.contains::<Player>());
    }

    #[tokio::test]
    async fn test_resource_context_concurrent_reads() {
        let mut resources = ResourceContext::new();
        resources.insert(Player::new("Hero"));

        // Multiple concurrent readers should work
        let reader1 = resources.get::<Player>().await.unwrap();
        let reader2 = resources.get::<Player>().await.unwrap();

        assert_eq!(reader1.name, "Hero");
        assert_eq!(reader2.name, "Hero");
    }

    // ===== ServiceContext Tests =====

    #[test]
    fn test_service_context_register_and_get() {
        let mut services = ServiceContext::new();
        services.register(Box::new(TestCombatService));

        let service = services.get("test_combat").unwrap();
        assert_eq!(service.name(), "test_combat");
    }

    #[test]
    fn test_service_context_get_as() {
        let mut services = ServiceContext::new();
        services.register(Box::new(TestCombatService));

        let combat = services.get_as::<TestCombatService>("test_combat").unwrap();
        let damage = combat.calculate_damage(100, 30);
        assert_eq!(damage, 70);
    }

    #[test]
    fn test_service_context_service_names() {
        let mut services = ServiceContext::new();
        services.register(Box::new(TestCombatService));

        let names = services.service_names();
        assert_eq!(names, vec!["test_combat"]);
    }

    #[test]
    fn test_service_context_len_and_is_empty() {
        let mut services = ServiceContext::new();
        assert!(services.is_empty());
        assert_eq!(services.len(), 0);

        services.register(Box::new(TestCombatService));
        assert!(!services.is_empty());
        assert_eq!(services.len(), 1);
    }

    // ===== SystemContext Tests =====

    #[test]
    fn test_system_context_register_and_get() {
        let mut systems = SystemContext::new();
        systems.register(TestCombatSystem::new());

        let system = systems.get::<TestCombatSystem>().unwrap();
        assert_eq!(system.turn_count, 0);
    }

    #[test]
    fn test_system_context_get_mut() {
        let mut systems = SystemContext::new();
        systems.register(TestCombatSystem::new());

        {
            let system = systems.get_mut::<TestCombatSystem>().unwrap();
            system.increment_turn();
            system.increment_turn();
        }

        let system = systems.get::<TestCombatSystem>().unwrap();
        assert_eq!(system.turn_count, 2);
    }

    #[test]
    fn test_system_context_contains() {
        let mut systems = SystemContext::new();
        systems.register(TestCombatSystem::new());

        assert!(systems.contains::<TestCombatSystem>());
        assert_eq!(systems.len(), 1);
    }

    #[test]
    fn test_system_context_remove() {
        let mut systems = SystemContext::new();
        systems.register(TestCombatSystem::new());

        assert!(systems.contains::<TestCombatSystem>());
        assert!(systems.remove::<TestCombatSystem>());
        assert!(!systems.contains::<TestCombatSystem>());
    }

    #[test]
    fn test_system_context_len_and_is_empty() {
        let mut systems = SystemContext::new();
        assert!(systems.is_empty());
        assert_eq!(systems.len(), 0);

        systems.register(TestCombatSystem::new());
        assert!(!systems.is_empty());
        assert_eq!(systems.len(), 1);
    }
}