pub struct Context {
pub time: Time,
pub input: InputState,
pub graphics: GraphicsContext,
pub audio: AudioManager,
pub ui_state: UiState,
pub ui_theme: UiTheme,
pub particles: Vec<ParticleEmitter>,
pub tweens: TweenManager,
pub window: WindowConfig,
/* private fields */
}Expand description
The main context object passed to all game state methods.
Contains all engine subsystems: time, input, graphics, audio, etc. This is the single point of access for everything the game needs each frame.
Fields§
§time: TimeTime and delta tracking.
input: InputStateInput state (keyboard, mouse, actions).
graphics: GraphicsContextGraphics context (drawing commands).
audio: AudioManagerAudio manager (feature-gated).
ui_state: UiStateUI state and theme (feature-gated).
ui_theme: UiTheme§particles: Vec<ParticleEmitter>Active particle emitters (feature-gated).
tweens: TweenManagerActive tweens (feature-gated).
window: WindowConfigWindow configuration.
Implementations§
Source§impl Context
impl Context
Sourcepub fn quit(&mut self)
pub fn quit(&mut self)
Request the game to exit (will close after the current frame).
Examples found in repository?
examples/particles.rs (line 68)
49 fn update(&mut self, ctx: &mut Context) {
50 let dt = ctx.time.delta() as f32;
51
52 // Move fire emitter to mouse X
53 self.emitters[0].position.x = ctx.input.mouse.position.x;
54 self.emitters[0].position.y = ctx.screen_height() - 50.0;
55
56 // Burst on click
57 if ctx.input.mouse.is_pressed(MouseButton::Left) {
58 self.emitters[1].position = ctx.input.mouse.position;
59 self.emitters[1].burst_now(50);
60 }
61
62 // Update all emitters
63 for emitter in &mut self.emitters {
64 emitter.update(dt);
65 }
66
67 if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
68 ctx.quit();
69 }
70 }More examples
examples/scene_demo.rs (line 52)
34 fn update(&mut self, ctx: &mut Context) {
35 if ctx.input.keyboard.is_pressed(KeyCode::Up) || ctx.input.keyboard.is_pressed(KeyCode::W) {
36 self.selected = self.selected.saturating_sub(1);
37 }
38 if ctx.input.keyboard.is_pressed(KeyCode::Down) || ctx.input.keyboard.is_pressed(KeyCode::S) {
39 self.selected = (self.selected + 1).min(self.options.len() - 1);
40 }
41 if ctx.input.keyboard.is_pressed(KeyCode::Enter) {
42 match self.selected {
43 0 => {
44 // Transition to game scene
45 let _transition = Transition::Fade {
46 duration: 0.5,
47 color: [0.0, 0.0, 0.0, 1.0],
48 };
49 // ctx.scene.push_with_transition(GameScene::new(), transition);
50 }
51 2 => {
52 ctx.quit();
53 }
54 _ => {}
55 }
56 }
57 }examples/basics.rs (line 103)
44 fn update(&mut self, ctx: &mut Context) {
45 let dt = ctx.time.delta() as f32;
46
47 // --- Ball physics ---
48 self.ball.velocity.y += 600.0 * dt; // Gravity
49 self.ball.position += self.ball.velocity * dt;
50
51 // Bounce off walls
52 let w = ctx.screen_width();
53 let h = ctx.screen_height();
54
55 if self.ball.position.x - self.ball.radius < 0.0 {
56 self.ball.position.x = self.ball.radius;
57 self.ball.velocity.x = self.ball.velocity.x.abs();
58 }
59 if self.ball.position.x + self.ball.radius > w {
60 self.ball.position.x = w - self.ball.radius;
61 self.ball.velocity.x = -self.ball.velocity.x.abs();
62 }
63 if self.ball.position.y - self.ball.radius < 0.0 {
64 self.ball.position.y = self.ball.radius;
65 self.ball.velocity.y = self.ball.velocity.y.abs();
66 }
67 if self.ball.position.y + self.ball.radius > h {
68 self.ball.position.y = h - self.ball.radius;
69 self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; // Damping
70 }
71
72 // Update trail
73 self.ball.trail.push((self.ball.position, 1.0));
74 if self.ball.trail.len() > 60 {
75 self.ball.trail.remove(0);
76 }
77 for (_, alpha) in &mut self.ball.trail {
78 *alpha -= dt * 2.0;
79 }
80 self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82 // --- Input ---
83 if ctx.input.is_action_pressed("shake") {
84 ctx.graphics.camera.shake(10.0, 0.3);
85 }
86
87 if ctx.input.mouse.is_pressed(MouseButton::Left) {
88 self.click_count += 1;
89 // Change ball color
90 self.hue = (self.hue + 30.0) % 360.0;
91 self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92 // Boost ball toward click
93 let dir = ctx.input.mouse.position - self.ball.position;
94 self.ball.velocity += dir.normalize_or_zero() * 200.0;
95 }
96
97 // Zoom with scroll
98 let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99 ctx.graphics.camera.set_zoom(zoom);
100
101 // Escape to quit
102 if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103 ctx.quit();
104 }
105
106 // Update camera follow target
107 ctx.graphics.camera.follow(self.ball.position, 0.05);
108 }Sourcepub fn is_quitting(&self) -> bool
pub fn is_quitting(&self) -> bool
Check if the game is about to exit.
Sourcepub fn toggle_pause(&mut self)
pub fn toggle_pause(&mut self)
Toggle pause.
Sourcepub fn screen_width(&self) -> f32
pub fn screen_width(&self) -> f32
Current screen width.
Examples found in repository?
examples/basics.rs (line 52)
44 fn update(&mut self, ctx: &mut Context) {
45 let dt = ctx.time.delta() as f32;
46
47 // --- Ball physics ---
48 self.ball.velocity.y += 600.0 * dt; // Gravity
49 self.ball.position += self.ball.velocity * dt;
50
51 // Bounce off walls
52 let w = ctx.screen_width();
53 let h = ctx.screen_height();
54
55 if self.ball.position.x - self.ball.radius < 0.0 {
56 self.ball.position.x = self.ball.radius;
57 self.ball.velocity.x = self.ball.velocity.x.abs();
58 }
59 if self.ball.position.x + self.ball.radius > w {
60 self.ball.position.x = w - self.ball.radius;
61 self.ball.velocity.x = -self.ball.velocity.x.abs();
62 }
63 if self.ball.position.y - self.ball.radius < 0.0 {
64 self.ball.position.y = self.ball.radius;
65 self.ball.velocity.y = self.ball.velocity.y.abs();
66 }
67 if self.ball.position.y + self.ball.radius > h {
68 self.ball.position.y = h - self.ball.radius;
69 self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; // Damping
70 }
71
72 // Update trail
73 self.ball.trail.push((self.ball.position, 1.0));
74 if self.ball.trail.len() > 60 {
75 self.ball.trail.remove(0);
76 }
77 for (_, alpha) in &mut self.ball.trail {
78 *alpha -= dt * 2.0;
79 }
80 self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82 // --- Input ---
83 if ctx.input.is_action_pressed("shake") {
84 ctx.graphics.camera.shake(10.0, 0.3);
85 }
86
87 if ctx.input.mouse.is_pressed(MouseButton::Left) {
88 self.click_count += 1;
89 // Change ball color
90 self.hue = (self.hue + 30.0) % 360.0;
91 self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92 // Boost ball toward click
93 let dir = ctx.input.mouse.position - self.ball.position;
94 self.ball.velocity += dir.normalize_or_zero() * 200.0;
95 }
96
97 // Zoom with scroll
98 let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99 ctx.graphics.camera.set_zoom(zoom);
100
101 // Escape to quit
102 if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103 ctx.quit();
104 }
105
106 // Update camera follow target
107 ctx.graphics.camera.follow(self.ball.position, 0.05);
108 }Sourcepub fn screen_height(&self) -> f32
pub fn screen_height(&self) -> f32
Current screen height.
Examples found in repository?
examples/particles.rs (line 54)
49 fn update(&mut self, ctx: &mut Context) {
50 let dt = ctx.time.delta() as f32;
51
52 // Move fire emitter to mouse X
53 self.emitters[0].position.x = ctx.input.mouse.position.x;
54 self.emitters[0].position.y = ctx.screen_height() - 50.0;
55
56 // Burst on click
57 if ctx.input.mouse.is_pressed(MouseButton::Left) {
58 self.emitters[1].position = ctx.input.mouse.position;
59 self.emitters[1].burst_now(50);
60 }
61
62 // Update all emitters
63 for emitter in &mut self.emitters {
64 emitter.update(dt);
65 }
66
67 if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
68 ctx.quit();
69 }
70 }More examples
examples/basics.rs (line 53)
44 fn update(&mut self, ctx: &mut Context) {
45 let dt = ctx.time.delta() as f32;
46
47 // --- Ball physics ---
48 self.ball.velocity.y += 600.0 * dt; // Gravity
49 self.ball.position += self.ball.velocity * dt;
50
51 // Bounce off walls
52 let w = ctx.screen_width();
53 let h = ctx.screen_height();
54
55 if self.ball.position.x - self.ball.radius < 0.0 {
56 self.ball.position.x = self.ball.radius;
57 self.ball.velocity.x = self.ball.velocity.x.abs();
58 }
59 if self.ball.position.x + self.ball.radius > w {
60 self.ball.position.x = w - self.ball.radius;
61 self.ball.velocity.x = -self.ball.velocity.x.abs();
62 }
63 if self.ball.position.y - self.ball.radius < 0.0 {
64 self.ball.position.y = self.ball.radius;
65 self.ball.velocity.y = self.ball.velocity.y.abs();
66 }
67 if self.ball.position.y + self.ball.radius > h {
68 self.ball.position.y = h - self.ball.radius;
69 self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; // Damping
70 }
71
72 // Update trail
73 self.ball.trail.push((self.ball.position, 1.0));
74 if self.ball.trail.len() > 60 {
75 self.ball.trail.remove(0);
76 }
77 for (_, alpha) in &mut self.ball.trail {
78 *alpha -= dt * 2.0;
79 }
80 self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82 // --- Input ---
83 if ctx.input.is_action_pressed("shake") {
84 ctx.graphics.camera.shake(10.0, 0.3);
85 }
86
87 if ctx.input.mouse.is_pressed(MouseButton::Left) {
88 self.click_count += 1;
89 // Change ball color
90 self.hue = (self.hue + 30.0) % 360.0;
91 self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92 // Boost ball toward click
93 let dir = ctx.input.mouse.position - self.ball.position;
94 self.ball.velocity += dir.normalize_or_zero() * 200.0;
95 }
96
97 // Zoom with scroll
98 let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99 ctx.graphics.camera.set_zoom(zoom);
100
101 // Escape to quit
102 if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103 ctx.quit();
104 }
105
106 // Update camera follow target
107 ctx.graphics.camera.follow(self.ball.position, 0.05);
108 }Sourcepub fn screen_size(&self) -> Vec2
pub fn screen_size(&self) -> Vec2
Current screen size as Vec2.
Sourcepub fn handle_resize(&mut self, width: u32, height: u32)
pub fn handle_resize(&mut self, width: u32, height: u32)
Handle window resize.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Context
impl !Send for Context
impl !Sync for Context
impl !UnwindSafe for Context
impl Freeze for Context
impl Unpin for Context
impl UnsafeUnpin for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more