forked from karx/catbox
timer lolz
This commit is contained in:
parent
71aa76b7c2
commit
8a33c0f42e
|
@ -12,7 +12,7 @@ exclude = ["src/main.rs"]
|
|||
|
||||
[dependencies.sdl2]
|
||||
version = "0.35.2"
|
||||
features = ["image", "ttf"]
|
||||
features = ["image", "ttf", "bundled"]
|
||||
|
||||
[dependencies]
|
||||
rodio = { version = "0.15.0", optional = true}
|
||||
|
|
Binary file not shown.
Binary file not shown.
10
src/lib.rs
10
src/lib.rs
|
@ -113,6 +113,7 @@ use std::{
|
|||
ops::{Deref, DerefMut},
|
||||
path::Path,
|
||||
slice::IterMut,
|
||||
time::Instant,
|
||||
};
|
||||
use vec2::Vec2Int;
|
||||
|
||||
|
@ -706,6 +707,7 @@ pub struct Game {
|
|||
pub width: u32,
|
||||
/// The height of the opened window
|
||||
pub height: u32,
|
||||
pub time: Cell<Instant>,
|
||||
stopped: Cell<bool>,
|
||||
}
|
||||
|
||||
|
@ -725,9 +727,17 @@ impl Game {
|
|||
title: title.to_string(),
|
||||
width,
|
||||
height,
|
||||
time: Instant::now().into(),
|
||||
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.
|
||||
///
|
||||
|
|
106
src/main.rs
106
src/main.rs
|
@ -22,69 +22,73 @@ fn main() {
|
|||
#[cfg(feature = "audio")]
|
||||
cat_box::play("output.mp3", 120);
|
||||
game.run(|ctx| {
|
||||
i = (i + 1) % 255;
|
||||
ctx.set_background_colour(i as u8, 64, 255);
|
||||
if game.step() >= 1 {
|
||||
i = (i + 1) % 255;
|
||||
ctx.set_background_colour(i as u8, 64, 255);
|
||||
|
||||
draw_text(
|
||||
ctx,
|
||||
format!("i is {}", i),
|
||||
"MesloLGS NF Regular.ttf",
|
||||
72,
|
||||
(300, 300),
|
||||
cat_box::TextMode::Shaded {
|
||||
foreground: (255, 255, 255),
|
||||
background: (0, 0, 0),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
draw_text(
|
||||
ctx,
|
||||
format!("i is {}", i),
|
||||
"MesloLGS NF Regular.ttf",
|
||||
72,
|
||||
(300, 300),
|
||||
cat_box::TextMode::Shaded {
|
||||
foreground: (255, 255, 255),
|
||||
background: (0, 0, 0),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
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 (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));
|
||||
spr.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);
|
||||
s.set_angle(angle.to_degrees());
|
||||
|
||||
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() {
|
||||
println!("Sprites collided! {}", i);
|
||||
}
|
||||
let keys = get_keyboard_state(ctx).keys;
|
||||
|
||||
s2.draw(ctx).unwrap();
|
||||
s.draw(ctx).unwrap();
|
||||
coll.draw(ctx).unwrap();
|
||||
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() {
|
||||
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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue