MCPcopy Create free account
hub / github.com/17cupsofcoffee/nova / update

Method update

examples/bunnymark.rs:77–126  ·  view source on GitHub ↗
(&mut self, app: &mut App)

Source from the content-addressed store, hash-verified

75
76impl EventHandler for GameState {
77 fn update(&mut self, app: &mut App) {
78 if self.spawn_timer > 0 {
79 self.spawn_timer -= 1;
80 }
81
82 if app.input.is_key_pressed(Key::A) {
83 self.auto_spawn = !self.auto_spawn;
84 }
85
86 let button_down = app.input.is_key_down(Key::Space)
87 || app.input.is_mouse_button_down(MouseButton::Left)
88 || app.input.is_gamepad_button_down(0, GamepadButton::A);
89
90 let should_spawn = self.spawn_timer == 0 && (button_down || self.auto_spawn);
91
92 if should_spawn {
93 for _ in 0..INITIAL_BUNNIES {
94 self.bunnies.push(Bunny::new(&mut self.rng));
95 }
96 self.spawn_timer = 10;
97 }
98
99 for bunny in &mut self.bunnies {
100 bunny.position += bunny.velocity;
101 bunny.velocity.y += GRAVITY;
102
103 if bunny.position.x > MAX_X {
104 bunny.velocity.x *= -1.0;
105 bunny.position.x = MAX_X;
106 } else if bunny.position.x < 0.0 {
107 bunny.velocity.x *= -1.0;
108 bunny.position.x = 0.0;
109 }
110
111 if bunny.position.y > MAX_Y {
112 bunny.velocity.y *= -0.8;
113 bunny.position.y = MAX_Y;
114
115 if self.rng.gen::<bool>() {
116 bunny.velocity.y -= 3.0 + (self.rng.gen::<f32>() * 4.0);
117 }
118 } else if bunny.position.y < 0.0 {
119 bunny.velocity.y = 0.0;
120 bunny.position.y = 0.0;
121 }
122 }
123
124 app.window
125 .set_title(&format!("BunnyMark - {} bunnies", self.bunnies.len()));
126 }
127
128 fn draw(&mut self, app: &mut App) {
129 app.gfx.clear(&app.window, Color::rgb(0.392, 0.584, 0.929));

Callers

nothing calls this directly

Calls 5

is_key_pressedMethod · 0.80
is_key_downMethod · 0.80
is_mouse_button_downMethod · 0.80
set_titleMethod · 0.80

Tested by

no test coverage detected