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
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
//! Application configuration and immutable registry construction.
use std::{error::Error, fmt, sync::Arc};
use bevy_ecs::{component::Component, entity_disabling::Disabled, system::SystemParamFunction};
use crate::{
assets::{ImageAssetError, ImageAssetId, ImageAssetLimits, ImageAssetRegistry},
collision::{CircleCollider2d, RectangleCollider2d},
component::{ComponentApprovalError, ComponentRegistry, ComponentTuple},
events::EventRegistry,
headless::{HeadlessRunner, RunnerBuildError},
identity::{ApplicationId, IdentityExhausted, WorldFactoryId},
input::{
Action, ActionBindings, DigitalAxis2d, DuplicateKeyBinding, DuplicateMouseBinding,
MouseButton, PhysicalKeyCode,
},
motion::{
CameraFollowTarget2d, DigitalMovement2d, LinearAcceleration2d, LinearVelocity2d,
follow_camera_target2d, integrate_digital_movement2d, integrate_linear_acceleration2d,
integrate_linear_velocity2d,
},
render::RenderLimits,
resources::{ApplicationResourceError, ApplicationResourceRegistry},
screen::{ScreenImageVisual, ScreenRectangleVisual},
system::{
Stage, StageFactories, SupportedSystemParamTuple, register_system_application_resources,
register_system_events,
},
three_d::{CuboidVisual3d, ThreeDRenderLimits},
time::TimeConfig,
transition::replace_world_on_press,
visual::{ActiveCamera2d, CircleVisual, LineVisual, RectangleVisual, Transform2d},
world::{WorldBuildError, WorldBuilder},
};
/// Default maximum number of managed entities in one active World.
pub const DEFAULT_ENTITY_LIMIT: usize = 100_000;
/// Default maximum structural, transition, and application-control commands
/// in one stage.
pub const DEFAULT_COMMAND_LIMIT: usize = 4_096;
/// Default maximum number of events of one type in one stage invocation.
pub const DEFAULT_EVENT_LIMIT: usize = 1_024;
/// Default maximum retained WorldEnter/WorldExit diagnostic records.
pub const DEFAULT_LIFECYCLE_TRACE_LIMIT: usize = 1_024;
/// Default maximum number of distinct Application Resource types.
pub const DEFAULT_APPLICATION_RESOURCE_LIMIT: usize = 256;
/// Complete frozen configuration for one Sim;Logic application.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AppConfig {
time: TimeConfig,
entity_limit: usize,
command_limit: usize,
event_limit: usize,
input_event_limit: usize,
lifecycle_trace_limit: usize,
application_resource_limit: usize,
render: RenderLimits,
images: ImageAssetLimits,
#[cfg(feature = "headless-text")]
text: crate::text::TextLimits,
}
impl Default for AppConfig {
fn default() -> Self {
Self {
time: TimeConfig::default(),
entity_limit: DEFAULT_ENTITY_LIMIT,
command_limit: DEFAULT_COMMAND_LIMIT,
event_limit: DEFAULT_EVENT_LIMIT,
input_event_limit: crate::input::DEFAULT_INPUT_EVENT_LIMIT,
lifecycle_trace_limit: DEFAULT_LIFECYCLE_TRACE_LIMIT,
application_resource_limit: DEFAULT_APPLICATION_RESOURCE_LIMIT,
render: RenderLimits::default(),
images: ImageAssetLimits::default(),
#[cfg(feature = "headless-text")]
text: crate::text::TextLimits::default(),
}
}
}
impl AppConfig {
/// Replaces the fixed-step time configuration.
pub fn set_time(&mut self, time: TimeConfig) -> &mut Self {
self.time = time;
self
}
/// Replaces the maximum managed entity count.
pub fn set_entity_limit(&mut self, limit: usize) -> Result<&mut Self, AppConfigError> {
if limit == 0 {
return Err(AppConfigError::ZeroEntityLimit);
}
self.entity_limit = limit;
Ok(self)
}
/// Replaces the maximum combined command count for one stage.
pub fn set_command_limit(&mut self, limit: usize) -> Result<&mut Self, AppConfigError> {
if limit == 0 {
return Err(AppConfigError::ZeroCommandLimit);
}
self.command_limit = limit;
Ok(self)
}
/// Replaces the maximum event count for each type in one stage invocation.
pub fn set_event_limit(&mut self, limit: usize) -> Result<&mut Self, AppConfigError> {
if limit == 0 {
return Err(AppConfigError::ZeroEventLimit);
}
self.event_limit = limit;
Ok(self)
}
/// Replaces the maximum physical events per frame, generated logical edges
/// per frame, and retained logical edges for a later FixedUpdate delivery.
///
/// A frame exceeding any bound is rejected before input state changes.
/// Generated edges include synthetic mouse releases on pointer leave;
/// their frame bound still applies while fixed updates are paused.
pub fn set_input_event_limit(&mut self, limit: usize) -> Result<&mut Self, AppConfigError> {
if limit == 0 {
return Err(AppConfigError::ZeroInputEventLimit);
}
self.input_event_limit = limit;
Ok(self)
}
/// Replaces the maximum retained WorldEnter/WorldExit diagnostic records.
///
/// Once full, the trace evicts its oldest record before retaining a new
/// one. This affects diagnostics only, never World lifecycle execution.
pub fn set_lifecycle_trace_limit(&mut self, limit: usize) -> Result<&mut Self, AppConfigError> {
if limit == 0 {
return Err(AppConfigError::ZeroLifecycleTraceLimit);
}
self.lifecycle_trace_limit = limit;
Ok(self)
}
/// Replaces the maximum number of distinct Application Resource types.
///
/// This bounds registry entries, not heap memory retained inside arbitrary
/// user values.
pub fn set_application_resource_limit(
&mut self,
limit: usize,
) -> Result<&mut Self, AppConfigError> {
if limit == 0 {
return Err(AppConfigError::ZeroApplicationResourceLimit);
}
self.application_resource_limit = limit;
Ok(self)
}
/// Replaces renderer-independent extraction and presentation limits.
pub fn set_render_limits(&mut self, limits: RenderLimits) -> &mut Self {
self.render = limits;
self
}
/// Replaces opt-in cuboid, triangle, and physical depth-target limits.
///
/// All other rendering limits are preserved. Final composition still
/// requires a target pass and color texture space in FrameLimits.
pub fn set_three_d_render_limits(&mut self, limits: ThreeDRenderLimits) -> &mut Self {
self.render = self.render.with_three_d_render_limits(limits);
self
}
/// Replaces setup-time image count, dimension, and retained-pixel limits.
///
/// Zero limits are valid and disable the corresponding allowance. This
/// does not enable screen-image extraction or raise presentation budgets.
pub fn set_image_asset_limits(&mut self, limits: ImageAssetLimits) -> &mut Self {
self.images = limits;
self
}
/// Replaces setup-time font count and retained font-byte limits.
///
/// Does not raise render limits or initialize a window/device. Use
/// `headless-text` without defaults to exclude GPU/window dependencies;
/// `text` additionally compiles Engine's retained GPU text bridge.
#[cfg(feature = "headless-text")]
pub fn set_text_limits(&mut self, limits: crate::text::TextLimits) -> &mut Self {
self.text = limits;
self
}
pub(crate) const fn time(self) -> TimeConfig {
self.time
}
pub(crate) const fn entity_limit(self) -> usize {
self.entity_limit
}
pub(crate) const fn command_limit(self) -> usize {
self.command_limit
}
pub(crate) const fn event_limit(self) -> usize {
self.event_limit
}
pub(crate) const fn input_event_limit(self) -> usize {
self.input_event_limit
}
pub(crate) const fn lifecycle_trace_limit(self) -> usize {
self.lifecycle_trace_limit
}
pub(crate) const fn application_resource_limit(self) -> usize {
self.application_resource_limit
}
pub(crate) const fn render(self) -> RenderLimits {
self.render
}
}
/// Invalid finite application limit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppConfigError {
/// An active World must permit at least one entity.
ZeroEntityLimit,
/// A stage must permit at least one command.
ZeroCommandLimit,
/// A stage must permit at least one event of each registered type.
ZeroEventLimit,
/// A frame must permit at least one physical input event.
ZeroInputEventLimit,
/// A lifecycle trace must retain at least its newest record.
ZeroLifecycleTraceLimit,
/// An Application must permit at least one persistent resource type.
ZeroApplicationResourceLimit,
}
impl fmt::Display for AppConfigError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroEntityLimit => formatter.write_str("entity limit must be positive"),
Self::ZeroCommandLimit => formatter.write_str("command limit must be positive"),
Self::ZeroEventLimit => formatter.write_str("event limit must be positive"),
Self::ZeroInputEventLimit => formatter.write_str("input event limit must be positive"),
Self::ZeroLifecycleTraceLimit => {
formatter.write_str("lifecycle trace limit must be positive")
}
Self::ZeroApplicationResourceLimit => {
formatter.write_str("Application Resource limit must be positive")
}
}
}
}
impl Error for AppConfigError {}
/// Failure to create an application runtime identity or its standard registry.
#[derive(Debug)]
pub enum ApplicationCreationError {
/// The process-wide application identity counter is exhausted.
Identity(IdentityExhausted),
/// A built-in component unexpectedly violates the managed component rules.
StandardComponent(ComponentApprovalError),
}
impl fmt::Display for ApplicationCreationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Identity(error) => error.fmt(formatter),
Self::StandardComponent(error) => {
write!(formatter, "standard component approval failed: {error}")
}
}
}
}
impl Error for ApplicationCreationError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Identity(error) => Some(error),
Self::StandardComponent(error) => Some(error),
}
}
}
/// Failure to register a no-payload World factory.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WorldRegistrationError {
/// Diagnostic route names must contain a non-whitespace character.
EmptyName,
/// Diagnostic route names are bounded to 128 UTF-8 bytes.
NameTooLong,
/// Another factory already uses this exact diagnostic route name.
DuplicateName {
/// Rejected duplicate name.
name: String,
},
/// The runtime registry cannot represent another factory slot.
RegistryExhausted,
}
impl fmt::Display for WorldRegistrationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyName => formatter.write_str("World factory name must not be empty"),
Self::NameTooLong => formatter.write_str("World factory name exceeds 128 UTF-8 bytes"),
Self::DuplicateName { name } => {
write!(
formatter,
"World factory name `{name}` is already registered"
)
}
Self::RegistryExhausted => formatter.write_str("World factory registry is exhausted"),
}
}
}
impl Error for WorldRegistrationError {}
pub(crate) type WorldFactory =
dyn Fn(&mut WorldBuilder) -> Result<(), WorldBuildError> + Send + Sync + 'static;
pub(crate) struct RegisteredWorldFactory {
pub(crate) id: WorldFactoryId,
pub(crate) diagnostic_name: String,
pub(crate) factory: Arc<WorldFactory>,
}
/// Mutable pre-startup builder for one typed Sim;Logic application.
pub struct Application<A: Action> {
pub(crate) application: ApplicationId,
pub(crate) config: AppConfig,
pub(crate) components: ComponentRegistry,
pub(crate) events: EventRegistry,
pub(crate) application_resources: ApplicationResourceRegistry,
pub(crate) images: ImageAssetRegistry,
#[cfg(feature = "headless-text")]
pub(crate) texts: crate::text::TextRegistry,
#[cfg(feature = "easter-eggs")]
pub(crate) crab_image: Option<ImageAssetId>,
pub(crate) bindings: ActionBindings<A>,
pub(crate) factories: Vec<RegisteredWorldFactory>,
pub(crate) startup: StageFactories,
pub(crate) fixed: StageFactories,
pub(crate) frame: StageFactories,
}
impl<A: Action> Application<A> {
/// Creates an application builder and approves Sim;Logic's standard
/// world and screen visual, collision, motion, and disabled components,
/// including their complete required-component closure.
pub fn new(config: AppConfig) -> Result<Self, ApplicationCreationError> {
let application = ApplicationId::issue().map_err(ApplicationCreationError::Identity)?;
let mut components = ComponentRegistry::default();
components
.approve::<Transform2d>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<Disabled>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<CircleVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<RectangleVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<LineVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<ScreenRectangleVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<crate::screen::ScreenLineVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<crate::screen::ScreenCircleVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<crate::screen::ScreenPolylineVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<ScreenImageVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
#[cfg(feature = "headless-text")]
components
.approve::<crate::text::ScreenTextVisual>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<CuboidVisual3d>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<crate::three_d::MeshVisual3d>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<ActiveCamera2d>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<CircleCollider2d>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<RectangleCollider2d>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<LinearVelocity2d>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<LinearAcceleration2d>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<DigitalMovement2d<A>>()
.map_err(ApplicationCreationError::StandardComponent)?;
components
.approve::<CameraFollowTarget2d>()
.map_err(ApplicationCreationError::StandardComponent)?;
Ok(Self {
application,
config,
components,
events: EventRegistry::default(),
application_resources: ApplicationResourceRegistry::default(),
images: ImageAssetRegistry::new(application, config.images),
#[cfg(feature = "headless-text")]
texts: crate::text::TextRegistry::new(application, config.text),
#[cfg(feature = "easter-eggs")]
crab_image: None,
bindings: ActionBindings::new(),
factories: Vec::new(),
startup: StageFactories::default(),
fixed: StageFactories::default(),
frame: StageFactories::default(),
})
}
/// Approves one hook-free component type for candidate and command bundles.
pub fn approve_component<T: Component>(&mut self) -> Result<&mut Self, ComponentApprovalError> {
self.components.approve::<T>()?;
Ok(self)
}
/// Registers immutable straight-alpha sRGB RGBA8 pixels before startup.
///
/// Copies exactly one row-major image after validating dimensions, byte
/// length, and configured asset limits. A failed registration publishes no
/// handle or pixel entry. Handles belong only to this Application, survive
/// World replacement, and are not persistent file identifiers. Registering
/// identical pixels twice creates two distinct resources. No GPU or file
/// decoder is involved; normal frames never copy these pixels again on the
/// CPU side. GPU preparation retains its own recovery copy when needed.
pub fn register_image_rgba8(
&mut self,
width: u32,
height: u32,
pixels: &[u8],
) -> Result<ImageAssetId, ImageAssetError> {
self.images.register(width, height, pixels)
}
/// Registers an owned trusted TTF/OTF asset and fixed logical text settings.
///
/// Parsing and limits are checked before publishing an opaque font handle;
/// equal registrations remain distinct. Clones share font storage and the
/// handle survives World replacement. No window, GPU call, font discovery,
/// or download occurs. The caller supplies licensed, trusted font bytes.
#[cfg(feature = "headless-text")]
pub fn register_font(
&mut self,
bytes: Vec<u8>,
settings: crate::text::TextSettings,
) -> Result<crate::text::TextFont, crate::text::TextError> {
self.texts.register(bytes, settings)
}
/// Registers another size/direction/layout configuration sharing an existing
/// font's parsed face and source bytes. No file copy, reparse, or GPU work occurs.
///
/// The source must belong to this application. The new style has its own
/// registration/atlas identity, consumes one font-count slot, and charges zero
/// additional source bytes. Per-style atlas/run budgets remain independent.
/// Failure preserves the registry. A source style can also share its face onward.
#[cfg(feature = "headless-text")]
pub fn register_font_style(
&mut self,
source: &crate::text::TextFont,
settings: crate::text::TextSettings,
) -> Result<crate::text::TextFont, crate::text::TextError> {
self.texts.register_style(source, settings)
}
/// Approves a flat tuple of one through fifteen hook-free component types.
///
/// The registry update is atomic: if one tuple member is rejected, types
/// newly added by this call are removed again. Components that were
/// already approved before the call remain approved. Required components
/// must still be listed explicitly, in any order, and are checked when the
/// application creates a World candidate.
pub fn approve_components<T: ComponentTuple>(
&mut self,
) -> Result<&mut Self, ComponentApprovalError> {
self.components.approve_tuple::<T>()?;
Ok(self)
}
/// Binds one portable physical key to a typed action.
pub fn bind_key(
&mut self,
key: PhysicalKeyCode,
action: A,
) -> Result<&mut Self, DuplicateKeyBinding> {
self.bindings.bind(key, action)?;
Ok(self)
}
/// Binds one portable mouse button to a typed action without replacing an
/// existing binding. Bindings freeze when the application is started.
///
/// Mouse buttons and keys may share an action: it remains held until all
/// bound physical controls release. Their individual edges remain distinct.
pub fn bind_mouse_button(
&mut self,
button: MouseButton,
action: A,
) -> Result<&mut Self, DuplicateMouseBinding> {
self.bindings.bind_mouse_button(button, action)?;
Ok(self)
}
/// Atomically binds W/A/S/D to one typed two-dimensional action axis.
///
/// W maps to positive y, A to negative x, S to negative y, and D to
/// positive x. All four physical keys are checked first in W/A/S/D order.
/// If any is already bound, that binding is reported and the complete
/// table remains unchanged. Repeating one logical action in several axis
/// slots is allowed, matching [`DigitalAxis2d`] sampling semantics.
pub fn bind_wasd(&mut self, axis: DigitalAxis2d<A>) -> Result<&mut Self, DuplicateKeyBinding> {
self.bindings.bind_wasd(axis)?;
Ok(self)
}
/// Atomically binds the physical arrow keys to one typed action axis.
///
/// Left maps to negative x, Right to positive x, Down to negative y, and
/// Up to positive y. All four keys are checked first in that order. If any
/// is already bound, that binding is reported and the complete table
/// remains unchanged. The same axis may be bound to both arrows and W/A/S/D.
pub fn bind_arrows(
&mut self,
axis: DigitalAxis2d<A>,
) -> Result<&mut Self, DuplicateKeyBinding> {
self.bindings.bind_arrows(axis)?;
Ok(self)
}
/// Atomically binds W/A/S/D and the arrow keys to one typed action axis.
///
/// The mappings match [`Application::bind_wasd`] and
/// [`Application::bind_arrows`]. All eight keys are checked before any
/// binding is written, in W, A, S, D, Left, Right, Down, Up order. If any
/// key is already bound, that binding is reported and the complete table
/// remains unchanged. Calling this method again reports W as the first
/// conflict. Use the separate presets when only one key family should be
/// accepted.
pub fn bind_wasd_and_arrows(
&mut self,
axis: DigitalAxis2d<A>,
) -> Result<&mut Self, DuplicateKeyBinding> {
self.bindings.bind_wasd_and_arrows(axis)?;
Ok(self)
}
/// Registers one typed value owned by the Application rather than a World.
///
/// The value becomes available through [`crate::resources::AppRes`] and
/// [`crate::resources::AppResMut`] in FixedUpdate and FrameUpdate, and the
/// same value survives successful World replacement without cloning.
/// Candidate factories and Startup systems cannot access it.
///
/// Registration is explicit, accepts at most one value of each concrete
/// type, and is bounded by [`AppConfig::set_application_resource_limit`].
/// A duplicate returns [`ApplicationResourceError::Duplicate`] and keeps
/// the first value; registration never means replacement. `T` needs no
/// Bevy `Resource` derive.
///
/// ```no_run
/// use sim_logic::prelude::*;
///
/// #[derive(Clone, Copy, PartialEq, Eq, Hash)]
/// enum Action {}
///
/// #[derive(Default)]
/// struct SessionScore(u32);
///
/// fn award(mut score: AppResMut<SessionScore>) {
/// score.0 = score.0.saturating_add(1);
/// }
///
/// # fn main() -> LogicResult {
/// let mut app = Application::<Action>::new(AppConfig::default())?;
/// app.register_app_resource(SessionScore::default())?;
/// app.add_system(Stage::FixedUpdate, award);
/// # Ok(())
/// # }
/// ```
pub fn register_app_resource<T: Send + Sync + 'static>(
&mut self,
resource: T,
) -> Result<&mut Self, ApplicationResourceError> {
self.application_resources
.insert(resource, self.config.application_resource_limit())?;
Ok(self)
}
/// Registers one repeatable synchronous no-payload World factory.
///
/// A factory is a restricted construction recipe, not an effect callback.
/// It must derive the same candidate from its immutable captures, mutate
/// only the supplied [`WorldBuilder`], and finish before returning. It must
/// not perform I/O, start background work, or mutate shared external state.
/// The runtime can discard an isolated candidate, but it cannot roll back
/// effects performed outside that candidate.
pub fn register_world(
&mut self,
diagnostic_name: impl Into<String>,
factory: impl Fn(&mut WorldBuilder) -> Result<(), WorldBuildError> + Send + Sync + 'static,
) -> Result<WorldFactoryId, WorldRegistrationError> {
let diagnostic_name = diagnostic_name.into();
if diagnostic_name.trim().is_empty() {
return Err(WorldRegistrationError::EmptyName);
}
if diagnostic_name.len() > 128 {
return Err(WorldRegistrationError::NameTooLong);
}
if self
.factories
.iter()
.any(|registered| registered.diagnostic_name == diagnostic_name)
{
return Err(WorldRegistrationError::DuplicateName {
name: diagnostic_name,
});
}
let slot = u32::try_from(self.factories.len())
.map_err(|_| WorldRegistrationError::RegistryExhausted)?;
let id = WorldFactoryId::new(self.application, slot, 1);
self.factories.push(RegisteredWorldFactory {
id,
diagnostic_name,
factory: Arc::new(factory),
});
Ok(id)
}
/// Registers an infallible System in the FixedUpdate stage.
///
/// This is the short form of `add_system(Stage::FixedUpdate, system)`.
/// Both forms append to the same ordered stage list.
pub fn add_fixed_system<FunctionMarker: 'static, S>(&mut self, system: S) -> &mut Self
where
S: SystemParamFunction<FunctionMarker, In = (), Out = ()> + Clone + Send + Sync + 'static,
S::Param: SupportedSystemParamTuple,
{
self.add_system(Stage::FixedUpdate, system)
}
/// Registers a fallible System in the FixedUpdate stage.
///
/// This is the short form of
/// `add_fallible_system(Stage::FixedUpdate, system)`. Both forms append to
/// the same ordered stage list and use the same failure semantics.
pub fn add_fallible_fixed_system<FunctionMarker: 'static, S, E>(
&mut self,
system: S,
) -> &mut Self
where
S: SystemParamFunction<FunctionMarker, In = (), Out = Result<(), E>>
+ Clone
+ Send
+ Sync
+ 'static,
S::Param: SupportedSystemParamTuple,
E: fmt::Display + 'static,
{
self.add_fallible_system(Stage::FixedUpdate, system)
}
/// Appends the standard fixed-press World-replacement System.
///
/// In a World containing
/// [`WorldReplacementOnPress<A>`](crate::transition::WorldReplacementOnPress), every matching
/// press occurrence queues one ordinary deferred replacement request with
/// that edge's original causal token. A World without the Resource, or a
/// fixed tick without a matching press, is a successful no-op. Physical
/// key bindings remain explicit and the Resource is inert until this
/// method is called. Presses received while fixed simulation is paused are
/// frame-only input and are not replayed through this adapter on unpause.
///
/// The adapter runs at this exact point in FixedUpdate registration order.
/// Later Systems in the same tick still run before the stage barrier. An
/// accepted replacement then follows the existing arbitration, candidate
/// preparation, lifecycle, catch-up stopping, input consumption, and
/// same-frame extraction rules. Every occurrence consumes one command
/// slot. Each duplicate registration runs at its own position and consumes
/// another slot. If no intervening System changes the route, the repeated
/// token and target coalesce idempotently; otherwise ordinary arbitration
/// decides the differently routed requests.
///
/// This helper provides one unconditional route per World. It does not bind
/// a key, observe held or release state, request application exit, build a
/// route table, attach a payload, or add a FrameUpdate counterpart.
pub fn add_world_replacement_on_press_system(&mut self) -> &mut Self {
self.add_fallible_fixed_system(replace_world_on_press::<A>)
}
/// Appends the standard fixed-step linear-acceleration System.
///
/// Each enabled managed entity with [`LinearAcceleration2d`] changes its
/// [`LinearVelocity2d`] by `acceleration * fixed_delta` at this exact point
/// in registration order. The component is inert until this method is
/// called. Exact zero acceleration is a no-op before arithmetic. Calling
/// this method twice applies acceleration twice per fixed tick.
///
/// Register this before [`Self::add_linear_velocity2d_system`] for
/// semi-implicit Euler motion. Reversing the calls deliberately moves with
/// the old velocity before updating it for the next step. Direct component
/// writes by earlier Systems are visible; deferred spawn or insert Commands
/// first accelerate on a later fixed tick. Arithmetic overflow leaves the
/// failing entity's velocity unchanged; entities processed earlier by the
/// same System may already have changed under ordinary non-transactional
/// fallible-System semantics.
///
/// This adapter does not represent force or mass, move a Transform itself,
/// or apply gravity, drag, collision response, rotation, or world bounds.
pub fn add_linear_acceleration2d_system(&mut self) -> &mut Self {
self.add_fallible_fixed_system(integrate_linear_acceleration2d)
}
/// Appends the standard fixed-step linear-velocity System.
///
/// Each enabled managed entity with [`LinearVelocity2d`] moves by
/// `velocity * fixed_delta` at this exact point in registration order.
/// The component itself is inert until this method is called. Calling the
/// method twice appends two Systems and therefore integrates twice per
/// fixed tick, just like registering an ordinary System twice.
///
/// Direct component writes by earlier Systems are visible to this System,
/// and later Systems observe the integrated translation. Deferred spawn or
/// insert Commands become visible only after the stage barrier, so newly
/// moving entities first integrate on a later fixed tick. Arithmetic
/// overflow follows normal fallible-System semantics and may leave earlier
/// entities in the same query already moved.
///
/// This adapter does not apply acceleration, forces, collision response,
/// rotation, drag, or world bounds.
pub fn add_linear_velocity2d_system(&mut self) -> &mut Self {
self.add_fallible_fixed_system(integrate_linear_velocity2d)
}
/// Appends the standard fixed-step digital-movement System.
///
/// Each enabled managed entity with [`DigitalMovement2d<A>`] samples its
/// typed axis, normalizes the direction, and moves by `speed * fixed_delta`
/// at this exact point in registration order. The component is inert until
/// this method is called, and physical keys must be bound separately.
/// Calling the method twice appends two Systems and moves twice per tick.
///
/// Direct component writes by earlier Systems are visible here; later
/// Systems observe the integrated translation. Deferred spawn or insert
/// Commands become visible only after the stage barrier. If this and the
/// linear-velocity adapter are both registered, their Transform writes
/// compose in explicit registration order. Idle or exactly opposed input
/// is a no-op before speed/time multiplication; active arithmetic overflow
/// follows ordinary fallible-System semantics.
///
/// This adapter does not apply collision response, acceleration, facing,
/// analog input, smoothing, or world bounds.
pub fn add_digital_movement2d_system(&mut self) -> &mut Self {
self.add_fallible_fixed_system(integrate_digital_movement2d::<A>)
}
/// Appends the standard fixed-step camera-follow System.
///
/// With no enabled [`CameraFollowTarget2d`], this System is a successful
/// no-op so other Worlds in the application need no follow target. With
/// exactly one target and one enabled [`ActiveCamera2d`], it replaces the
/// canonical camera center with `target translation + camera offset` at
/// this exact point in registration order. More than one target fails the
/// stage before changing a camera. Missing or duplicate active cameras are
/// left unchanged for the extraction validator and its repair path.
///
/// Register this after target movement for same-tick following, or before
/// movement to deliberately use the previous target position. Deferred
/// spawn or insert Commands first affect following on a later fixed tick;
/// Disabled targets are ignored. Arithmetic overflow leaves the camera
/// unchanged. Duplicate registration performs another ordinary assignment
/// and can observe writes placed between the two adapters.
///
/// The adapter does not run during World preparation, create or initially
/// align a camera, infer teleports, smooth motion, clamp bounds, change zoom
/// or rotation, or choose among multiple targets.
pub fn add_camera_follow2d_system(&mut self) -> &mut Self {
self.add_fallible_fixed_system(follow_camera_target2d)
}
/// Registers an infallible System in the FrameUpdate stage.
///
/// This is the short form of `add_system(Stage::FrameUpdate, system)`.
/// Both forms append to the same ordered stage list.
pub fn add_frame_system<FunctionMarker: 'static, S>(&mut self, system: S) -> &mut Self
where
S: SystemParamFunction<FunctionMarker, In = (), Out = ()> + Clone + Send + Sync + 'static,
S::Param: SupportedSystemParamTuple,
{
self.add_system(Stage::FrameUpdate, system)
}
/// Registers a fallible System in the FrameUpdate stage.
///
/// This is the short form of
/// `add_fallible_system(Stage::FrameUpdate, system)`. Both forms append to
/// the same ordered stage list and use the same failure semantics.
pub fn add_fallible_frame_system<FunctionMarker: 'static, S, E>(
&mut self,
system: S,
) -> &mut Self
where
S: SystemParamFunction<FunctionMarker, In = (), Out = Result<(), E>>
+ Clone
+ Send
+ Sync
+ 'static,
S::Param: SupportedSystemParamTuple,
E: fmt::Display + 'static,
{
self.add_fallible_system(Stage::FrameUpdate, system)
}
/// Registers a recreatable System in explicit execution order.
///
/// The supplied function or closure must be cloneable because every World
/// generation receives a fresh Bevy System instance.
/// Captured values shared through interior mutability remain external to a
/// candidate and therefore are not rolled back when preparation fails.
///
/// Only the managed parameter set documented by
/// [`SupportedSystemParamTuple`] is accepted. In particular, raw Bevy
/// `Query` or `Single`, raw `Entity`, Bevy `Commands`, raw World access,
/// `Local<T>`, and arbitrary custom SystemParams are rejected at compile
/// time instead of running their initialization code against a candidate.
pub fn add_system<FunctionMarker: 'static, S>(&mut self, stage: Stage, system: S) -> &mut Self
where
S: SystemParamFunction<FunctionMarker, In = (), Out = ()> + Clone + Send + Sync + 'static,
S::Param: SupportedSystemParamTuple,
{
register_system_events::<S::Param>(&mut self.events);
if stage != Stage::Startup {
register_system_application_resources::<S::Param>(
&mut self.application_resources,
stage,
std::any::type_name::<S>(),
);
}
match stage {
Stage::Startup => self.startup.add(system),
Stage::FixedUpdate => self.fixed.add(system),
Stage::FrameUpdate => self.frame.add(system),
}
self
}
/// Registers a recreatable System that can stop its stage with an error.
///
/// Returning `Err` skips the remaining Systems in the current stage and
/// discards every [`crate::commands::LogicCommands`] operation queued by
/// that stage. It is not a transaction: direct component and Resource
/// mutations performed before the error remain in an active World. On a
/// failed fixed tick, the runtime restores only the previous interpolation
/// endpoints; current [`Transform2d`] and [`ActiveCamera2d`] values remain
/// mutated.
///
/// A Startup error instead discards the complete isolated World candidate.
/// The error is formatted only on the failing path and is exposed through
/// [`crate::system::SystemRunFailure`]. Panics are outside this typed error
/// contract.
///
/// The same parameter and recreation restrictions as [`Self::add_system`]
/// apply.
pub fn add_fallible_system<FunctionMarker: 'static, S, E>(
&mut self,
stage: Stage,
system: S,
) -> &mut Self
where
S: SystemParamFunction<FunctionMarker, In = (), Out = Result<(), E>>
+ Clone
+ Send
+ Sync
+ 'static,
S::Param: SupportedSystemParamTuple,
E: fmt::Display + 'static,
{
register_system_events::<S::Param>(&mut self.events);
if stage != Stage::Startup {
register_system_application_resources::<S::Param>(
&mut self.application_resources,
stage,
std::any::type_name::<S>(),
);
}
match stage {
Stage::Startup => self.startup.add_fallible(system),
Stage::FixedUpdate => self.fixed.add_fallible(system),
Stage::FrameUpdate => self.frame.add_fallible(system),
}
self
}
/// Freezes the registries, prepares `initial`, and creates the shared
/// headless runtime core.
pub fn build_headless(
self,
initial: WorldFactoryId,
) -> Result<HeadlessRunner<A>, RunnerBuildError> {
HeadlessRunner::from_application(self, initial)
}
/// Runs the one-window desktop adapter until the window closes or a System
/// commits [`crate::commands::LogicCommands::request_exit`].
///
/// Call this on the process main thread. Platform event-loop rules may
/// permit creating only one event loop for the lifetime of the process.
#[cfg(feature = "desktop")]
pub fn run_desktop(
self,
initial: WorldFactoryId,
config: crate::desktop::DesktopConfig,
) -> Result<crate::desktop::DesktopRunReport, crate::desktop::DesktopRunError> {
crate::desktop::run(self, initial, config)
}
/// Runs one desktop window with the default title, size, and VSync mode
/// until the window closes or a System requests exit.
///
/// Call this on the process main thread. Platform event-loop rules may
/// permit creating only one event loop for the lifetime of the process.
#[cfg(feature = "desktop")]
pub fn run(
self,
initial: WorldFactoryId,
) -> Result<crate::desktop::DesktopRunReport, crate::desktop::DesktopRunError> {
self.run_desktop(initial, crate::desktop::DesktopConfig::default())
}
}
#[cfg(test)]
mod tests {
use super::{
AppConfig, AppConfigError, Application, DEFAULT_APPLICATION_RESOURCE_LIMIT,
DEFAULT_EVENT_LIMIT,
};
use crate::input::{DigitalAxis2d, PhysicalKeyCode};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum BindingAction {
Left,
Right,
Down,
Up,
Other,
}
#[test]
fn event_limit_is_positive_and_rejected_updates_are_atomic() {
let mut config = AppConfig::default();
assert_eq!(config.event_limit(), DEFAULT_EVENT_LIMIT);
assert!(matches!(
config.set_event_limit(0),
Err(AppConfigError::ZeroEventLimit)
));
assert_eq!(config.event_limit(), DEFAULT_EVENT_LIMIT);
assert!(config.set_event_limit(7).is_ok());
assert_eq!(config.event_limit(), 7);
}
#[test]
fn application_resource_limit_is_positive_and_rejected_updates_are_atomic() {
let mut config = AppConfig::default();
assert_eq!(
config.application_resource_limit(),
DEFAULT_APPLICATION_RESOURCE_LIMIT
);
assert!(matches!(
config.set_application_resource_limit(0),
Err(AppConfigError::ZeroApplicationResourceLimit)
));
assert_eq!(
config.application_resource_limit(),
DEFAULT_APPLICATION_RESOURCE_LIMIT
);
assert!(config.set_application_resource_limit(7).is_ok());
assert_eq!(config.application_resource_limit(), 7);
}
#[test]
fn public_combined_binding_does_not_install_wasd_before_an_arrow_conflict() {
let axis = DigitalAxis2d::new(
BindingAction::Left,
BindingAction::Right,
BindingAction::Down,
BindingAction::Up,
);
let mut application = Application::new(AppConfig::default()).unwrap();
assert!(
application
.bind_key(PhysicalKeyCode::ArrowLeft, BindingAction::Other)
.is_ok()
);
assert!(
application
.bind_key(PhysicalKeyCode::Escape, BindingAction::Other)
.is_ok()
);
let combined_error = match application.bind_wasd_and_arrows(axis) {
Ok(_) => panic!("occupied arrow should reject the combined preset"),
Err(error) => error,
};
assert_eq!(combined_error.key(), PhysicalKeyCode::ArrowLeft);
assert!(
application
.bind_key(PhysicalKeyCode::KeyW, BindingAction::Up)
.is_ok()
);
let escape_error = match application.bind_key(PhysicalKeyCode::Escape, BindingAction::Up) {
Ok(_) => panic!("the unrelated Escape binding should remain installed"),
Err(error) => error,
};
assert_eq!(escape_error.key(), PhysicalKeyCode::Escape);
}
}