Last active
April 23, 2023 16:40
-
-
Save Mroik/83cc3be9e9dc023b690f728157682c2c to your computer and use it in GitHub Desktop.
2D stuff on terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::time::Instant; | |
struct Engine { | |
timer: Instant, | |
tick_rate: u128, | |
fps: u128, | |
last_update: u128, | |
last_draw: u128, | |
acc: u128, | |
width: i32, | |
height: i32, | |
entities: Vec<Square>, | |
current_char: usize, | |
} | |
const ENTITY_CHARS: &str = "+OCVFDSARE"; | |
impl Engine { | |
fn new(tick_rate: u128, width: i32, height: i32, fps: u128) -> Self { | |
Engine { | |
timer: Instant::now(), | |
tick_rate: 1000 / tick_rate, | |
fps, | |
last_update: 0, | |
last_draw: 0, | |
acc: 0, | |
width, | |
height, | |
entities: vec![], | |
current_char: 0, | |
} | |
} | |
fn update(&mut self) { | |
for x in self.entities.as_mut_slice() { | |
let mut x_side = false; | |
let mut y_side =false; | |
if x.vector.0 + x.x >= self.width || x.vector.0 + x.x <= 0 { | |
x_side = true; | |
} else { | |
x.x += x.vector.0; | |
} | |
if x.vector.1 + x.y >= self.height || x.vector.1 + x.y <= 0 { | |
y_side = true; | |
} else { | |
x.y += x.vector.1; | |
} | |
x.on_bounce((x_side, y_side)); | |
} | |
} | |
fn draw(&mut self) { | |
// Clear screen | |
for _ in 0..self.height { | |
println!(); | |
} | |
for y in 0..self.height { | |
for x in 0..self.width { | |
let should_draw = self.entities | |
.iter() | |
.any(|sq| sq.in_position((x, y))); | |
if should_draw { | |
print!("{}", ENTITY_CHARS.chars().nth(self.current_char).unwrap()); | |
self.current_char += 1; | |
self.current_char %= ENTITY_CHARS.chars().count(); | |
} else { | |
print!("."); | |
} | |
} | |
println!(); | |
} | |
} | |
fn add_entity(&mut self, square: Square) { | |
self.entities.push(square); | |
} | |
} | |
struct Square { | |
size: i32, | |
x: i32, | |
y: i32, | |
vector: (i32, i32), | |
} | |
impl Square { | |
fn in_position(&self, (x, y): (i32, i32)) -> bool { | |
let ss = self.size - 1; | |
let is_x = x >= self.x - ss / 2 && x <= self.x + ss - ss / 2; | |
let is_y = y >= self.y - ss/ 2 && y <= self.y + ss - ss / 2; | |
return is_x && is_y; | |
} | |
fn on_bounce(&mut self, (x_side, y_side): (bool, bool)) { | |
if x_side { | |
self.vector.0 = -self.vector.0; | |
} | |
if y_side { | |
self.vector.1 = -self.vector.1; | |
} | |
} | |
} | |
fn main() { | |
let mut engine = Engine::new(64, 100, 50, 120); | |
engine.add_entity(Square { | |
size: 5, | |
x: engine.width / 2, | |
y: engine.height / 2, | |
vector: (1, 0), | |
}); | |
engine.add_entity(Square { | |
size: 5, | |
x: engine.width / 2, | |
y: engine.height / 2 + 7, | |
vector: (-2, -1), | |
}); | |
engine.add_entity(Square { | |
size: 5, | |
x: 7, | |
y: 44, | |
vector: (1, 1), | |
}); | |
engine.add_entity(Square { | |
size: 3, | |
x: 44, | |
y: 7, | |
vector: (-1, 1), | |
}); | |
engine.add_entity(Square { | |
size: 4, | |
x: 33, | |
y: 22, | |
vector: (-1, 1), | |
}); | |
// Event loop | |
loop { | |
let mut temp = engine.last_update; | |
engine.last_update = engine.timer.elapsed().as_millis(); | |
let d_t = engine.last_update - temp; | |
engine.acc += d_t; | |
while engine.acc > engine.tick_rate { | |
engine.update(); | |
engine.acc -= engine.tick_rate; | |
} | |
temp = engine.timer.elapsed().as_millis(); | |
if temp - engine.last_draw > 1000 / engine.fps { | |
engine.draw(); | |
engine.last_draw = temp; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment