timer lolz

This commit is contained in:
gallant 2023-03-09 20:54:28 -08:00
parent 71aa76b7c2
commit 8a33c0f42e
6 changed files with 66 additions and 52 deletions

View file

@ -12,7 +12,7 @@ exclude = ["src/main.rs"]
[dependencies.sdl2] [dependencies.sdl2]
version = "0.35.2" version = "0.35.2"
features = ["image", "ttf"] features = ["image", "ttf", "bundled"]
[dependencies] [dependencies]
rodio = { version = "0.15.0", optional = true} rodio = { version = "0.15.0", optional = true}

BIN
SDL2.dll Normal file

Binary file not shown.

BIN
SDL2_image.dll Normal file

Binary file not shown.

BIN
SDL2_ttf.dll Normal file

Binary file not shown.

View file

@ -113,6 +113,7 @@ use std::{
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
path::Path, path::Path,
slice::IterMut, slice::IterMut,
time::Instant,
}; };
use vec2::Vec2Int; use vec2::Vec2Int;
@ -706,6 +707,7 @@ pub struct Game {
pub width: u32, pub width: u32,
/// The height of the opened window /// The height of the opened window
pub height: u32, pub height: u32,
pub time: Cell<Instant>,
stopped: Cell<bool>, stopped: Cell<bool>,
} }
@ -725,9 +727,17 @@ impl Game {
title: title.to_string(), title: title.to_string(),
width, width,
height, height,
time: Instant::now().into(),
stopped: Cell::new(false), stopped: Cell::new(false),
} }
} }
pub fn step(&self) -> u128 {
let a = self.time.get().elapsed().as_millis().clone();
a
}
pub fn t_reset(&self) {
self.time.set(Instant::now());
}
/// Runs the game. Note: this method blocks, as it uses an infinite loop. /// Runs the game. Note: this method blocks, as it uses an infinite loop.
/// ///

View file

@ -22,69 +22,73 @@ fn main() {
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
cat_box::play("output.mp3", 120); cat_box::play("output.mp3", 120);
game.run(|ctx| { game.run(|ctx| {
i = (i + 1) % 255; if game.step() >= 1 {
ctx.set_background_colour(i as u8, 64, 255); i = (i + 1) % 255;
ctx.set_background_colour(i as u8, 64, 255);
draw_text( draw_text(
ctx, ctx,
format!("i is {}", i), format!("i is {}", i),
"MesloLGS NF Regular.ttf", "MesloLGS NF Regular.ttf",
72, 72,
(300, 300), (300, 300),
cat_box::TextMode::Shaded { cat_box::TextMode::Shaded {
foreground: (255, 255, 255), foreground: (255, 255, 255),
background: (0, 0, 0), background: (0, 0, 0),
}, },
) )
.unwrap(); .unwrap();
let (start_x, start_y) = s.position().into(); let (start_x, start_y) = s.position().into();
let m = get_mouse_state(ctx);
let x_diff = m.x - start_x;
let y_diff = m.y - start_y;
let angle = f64::from(y_diff).atan2(f64::from(x_diff));
s.set_angle(angle.to_degrees());
for spr in coll.iter() {
let (start_x, start_y) = spr.position().into();
let m = get_mouse_state(ctx); let m = get_mouse_state(ctx);
let x_diff = m.x - start_x; let x_diff = m.x - start_x;
let y_diff = m.y - start_y; let y_diff = m.y - start_y;
let angle = f64::from(y_diff).atan2(f64::from(x_diff)); let angle = f64::from(y_diff).atan2(f64::from(x_diff));
spr.set_angle(angle.to_degrees()); s.set_angle(angle.to_degrees());
}
let keys = get_keyboard_state(ctx).keys;
for key in keys {
let offset = match key {
Scancode::Escape => {
game.terminate();
(0, 0)
}
Scancode::W | Scancode::Up => (0, 5),
Scancode::S | Scancode::Down => (0, -5),
Scancode::A | Scancode::Left => (-5, 0),
Scancode::D | Scancode::Right => (5, 0),
_ => (0, 0),
};
s.translate(offset);
for spr in coll.iter() { for spr in coll.iter() {
spr.translate(offset); let (start_x, start_y) = spr.position().into();
let m = get_mouse_state(ctx);
let x_diff = m.x - start_x;
let y_diff = m.y - start_y;
let angle = f64::from(y_diff).atan2(f64::from(x_diff));
spr.set_angle(angle.to_degrees());
} }
}
if !cat_box::physics::check_for_collision_with_collection(&s2, &coll).is_empty() { let keys = get_keyboard_state(ctx).keys;
println!("Sprites collided! {}", i);
}
s2.draw(ctx).unwrap(); for key in keys {
s.draw(ctx).unwrap(); let offset = match key {
coll.draw(ctx).unwrap(); Scancode::Escape => {
game.terminate();
(0, 0)
}
Scancode::W | Scancode::Up => (0, 5),
Scancode::S | Scancode::Down => (0, -5),
Scancode::A | Scancode::Left => (-5, 0),
Scancode::D | Scancode::Right => (5, 0),
_ => (0, 0),
};
s.translate(offset);
for spr in coll.iter() {
spr.translate(offset);
}
}
if !cat_box::physics::check_for_collision_with_collection(&s2, &coll).is_empty() {
println!("Sprites collided! {}", i);
}
game.t_reset();
}
s2.draw(ctx).unwrap();
s.draw(ctx).unwrap();
coll.draw(ctx).unwrap();
}) })
.unwrap(); .unwrap();
} }