Compare commits

..

2 Commits

3 changed files with 61 additions and 24 deletions

View File

@@ -1,20 +1,22 @@
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
use crate::game::Game;
use sdl2::event::{Event};
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::render::{Canvas, Texture};
use std::time::{Duration, Instant};
#[cfg(target_os = "emscripten")]
pub mod emscripten;
mod animation;
mod constants;
mod direction;
mod entity;
mod game;
mod pacman;
mod entity;
mod animation;
mod modulation;
#[cfg(target_os = "emscripten")]
mod emscripten;
pub fn main() {
let sdl_context = sdl2::init().unwrap();
@@ -59,9 +61,9 @@ pub fn main() {
keycode: Some(Keycode::Escape) | Some(Keycode::Q),
..
} => return false,
Event::KeyDown { keycode , .. } => {
Event::KeyDown { keycode, .. } => {
game.keyboard_event(keycode.unwrap());
},
}
_ => {}
}
}
@@ -72,7 +74,10 @@ pub fn main() {
if start.elapsed() < loop_time {
::std::thread::sleep(loop_time - start.elapsed());
} else {
println!("Game loop behind schedule by: {:?}", start.elapsed() - loop_time);
println!(
"Game loop behind schedule by: {:?}",
start.elapsed() - loop_time
);
}
true

26
src/modulation.rs Normal file
View File

@@ -0,0 +1,26 @@
pub struct SpeedModulator {
tick_count: u32,
ticks_left: u32,
}
impl SpeedModulator {
pub fn new(percent: f32) -> Self {
let ticks_required: u32 = (1f32 / (1f32 - percent)).round() as u32;
SpeedModulator {
tick_count: ticks_required,
ticks_left: ticks_required,
}
}
pub fn next(&mut self) -> bool {
self.ticks_left -= 1;
if self.ticks_left == 0 {
self.ticks_left = self.tick_count;
false
} else {
true
}
}
}

View File

@@ -3,7 +3,10 @@ use sdl2::{
video::Window,
};
use crate::{animation::AnimatedTexture, direction::Direction, entity::Entity, constants::CELL_SIZE};
use crate::{
animation::AnimatedTexture, constants::CELL_SIZE, direction::Direction, entity::Entity,
modulation::SpeedModulator,
};
pub struct Pacman<'a> {
// Absolute position on the board (precise)
@@ -11,6 +14,7 @@ pub struct Pacman<'a> {
pub direction: Direction,
pub next_direction: Option<Direction>,
speed: u32,
modulation: SpeedModulator,
sprite: AnimatedTexture<'a>,
}
@@ -21,6 +25,7 @@ impl Pacman<'_> {
direction: Direction::Right,
next_direction: None,
speed: 2,
modulation: SpeedModulator::new(0.9333),
sprite: AnimatedTexture::new(atlas, 4, 3, 32, 32, Some((-4, -4))),
}
}
@@ -53,27 +58,28 @@ impl Entity for Pacman<'_> {
fn tick(&mut self) {
let can_change = self.internal_position() == (0, 0);
println!("{:?} ({:?})", self.internal_position(), can_change);
if can_change {
if let Some(direction) = self.next_direction {
self.direction = direction;
self.next_direction = None;
}
}
let speed = self.speed as i32;
match self.direction {
Direction::Right => {
self.position.0 += speed;
}
Direction::Left => {
self.position.0 -= speed;
}
Direction::Up => {
self.position.1 -= speed;
}
Direction::Down => {
self.position.1 += speed;
if self.modulation.next() {
let speed = self.speed as i32;
match self.direction {
Direction::Right => {
self.position.0 += speed;
}
Direction::Left => {
self.position.0 -= speed;
}
Direction::Up => {
self.position.1 -= speed;
}
Direction::Down => {
self.position.1 += speed;
}
}
}
}