Skip to content

Commit

Permalink
game's sounds are added to the assets, bug related to the refrence co…
Browse files Browse the repository at this point in the history
…unted should be solved
  • Loading branch information
ParsaAminpour committed Jul 23, 2024
1 parent 3c0693d commit 1c12747
Show file tree
Hide file tree
Showing 8 changed files with 825 additions and 21 deletions.
750 changes: 748 additions & 2 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alsa-sys = "0.3.1"
chrono = "0.4.38"
crossterm = "0.27.0"
inline_colorization = "0.1.6"
ndarray = "0.15.6"
rand = "0.8.5"
rodio = "0.19.0"
sed = "0.1.0"
sled = "0.34.7"
Binary file added src/assets/fire-rpg.wav
Binary file not shown.
Binary file added src/assets/laser_ray_zap_singleshot.wav
Binary file not shown.
Binary file added src/assets/new-high-score.wav
Binary file not shown.
Binary file added src/assets/power-down-rpg.wav
Binary file not shown.
37 changes: 34 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ use crossterm::{
cursor::{Hide, MoveTo, Show}, event::{poll, read, Event, KeyCode}, style::{
Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor}, terminal::{enable_raw_mode, size, Clear, ClearType}, ExecutableCommand, QueueableCommand
};

use std::fs::File;
use std::io::BufReader;
use rodio::{Decoder, OutputStream, source::Source};

use ndarray::{Array2, Array};
use inline_colorization::*;
use rand::prelude::*;
use std::time::Duration;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::cell::RefCell;
// use std::thread;
// use std::sync::{Mutex, Arc};

/*
** GAME PHASES
Expand Down Expand Up @@ -44,6 +51,30 @@ pub struct Fuel {
pub logo: String,
}

pub enum Sound {
EnemyKilled(String),
FuelObtained(String),
BoatCrashed(String)
}

pub fn handle_sound(sound_file: String) {
let (_stream, stream_handler) = OutputStream::try_default().unwrap();
let file = BufReader::new(File::open(sound_file).unwrap());
let source = Decoder::new(file).unwrap();

stream_handler.play_raw(source.convert_samples()).unwrap();
}

pub fn handle_sound2(sound_file: String) {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();

let file = std::fs::File::open(sound_file.to_string()).unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());

sink.sleep_until_end();
}

#[derive(Debug)]
pub struct Game2DMatrix {
pub player_i: u16,
Expand Down Expand Up @@ -121,7 +152,7 @@ impl Game2DMatrix {
}


pub fn draw(&mut self, screen: &mut Stdout, show_enemy: bool, show_fuel: bool) -> Result<()> {
pub fn draw(self:&mut Self, screen: &mut Stdout, show_enemy: bool, show_fuel: bool) -> Result<()> {
screen.queue(Clear(ClearType::All))?;

// draw the map as first scence
Expand Down Expand Up @@ -262,7 +293,7 @@ impl Game2DMatrix {
}

// enemies
let mut enemies_to_remove:Vec<usize> = vec![];
let mut enemies_to_remove: Vec<usize> = vec![];

for (idx, enemy) in self.enemies.iter_mut().enumerate() {
// player collision with the enemies in the ground.
Expand Down
57 changes: 41 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::{stdout, Result, Stdout, Write}, thread::sleep, vec};
use std::{borrow::BorrowMut, io::{stdout, Result, Stdout, Write}, rc::Rc, thread::{self, sleep}, vec};
use crossterm::{
cursor::{Hide, MoveTo, Show}, event::{poll, read, Event, KeyCode}, style::{
Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor}, terminal::{enable_raw_mode, size, Clear, ClearType}, ExecutableCommand, QueueableCommand
Expand All @@ -7,8 +7,9 @@ use ndarray::{Array2, Array};
use inline_colorization::*;
use rand::prelude::*;
use std::time::Duration;

use river_raid::*;
use std::sync::{Arc, Mutex};
use std::cell::RefCell;

fn main() -> Result<()> {
let mut screen = stdout();
Expand All @@ -19,6 +20,7 @@ fn main() -> Result<()> {

nd2array.initialize_ground(&mut screen).unwrap();


while nd2array.game_staus == GameStatus::ALIVE {
// implementing the keyboard binding.
if poll(Duration::from_millis(10))? {
Expand All @@ -27,12 +29,17 @@ fn main() -> Result<()> {
while poll(Duration::from_millis(0)).unwrap() {
let _ = read();
}

// Bug related to the Refrence counted while single thread and multi-thread processes are combined with eachother.
let rc_nd2array = Rc::new(RefCell::new(nd2array));

match key {
Event::Key(event) => {

match event.code {
KeyCode::Char('q') => { break; },

KeyCode::Right => if nd2array.player_i + 1 < nd2array.max_screen_i { nd2array.player_i += 2; },
KeyCode::Right => if nd2array.player_i + 1 < nd2array.max_screen_i { cloned_nd2array.player_i += 2; },

KeyCode::Left => if nd2array.player_i - 1 > 0 { nd2array.player_i -= 2; },

Expand All @@ -41,34 +48,52 @@ fn main() -> Result<()> {
KeyCode::Down => if nd2array.player_j + 1 < nd2array.max_screen_j { nd2array.player_j += 1; },

KeyCode::Char(' ') => {
nd2array.bullets.push(Bullet {
location: Location {
element_i: nd2array.player_j,
element_j: nd2array.player_i,
},
active: true,
logo: '🔥'.to_string()
let atomic_nd2array = Arc::new(Mutex::new(nd2array));
let cloned_atomic_nd2array = Arc::clone(&atomic_nd2array);

let handle_bullet_drowing = std::thread::spawn(move || {
let mut locked_atomic_nd2array = cloned_atomic_nd2array.lock().unwrap();

locked_atomic_nd2array.bullets.push(Bullet {
location: Location {
element_i: Arc::clone(&atomic_nd2array).lock().unwrap().player_j,
element_j: Arc::clone(&atomic_nd2array).lock().unwrap().player_i,
},
active: true,
logo: '🔥'.to_string()
});

});

let handle_sound = thread::spawn(move || {
handle_sound2("src/assets/laser_ray_zap_singleshot.wav".to_string());
});

handle_bullet_drowing.join().unwrap();
handle_sound.join().unwrap();
}
_ => {}
}
},
_ => {}
}
}
sleep(Duration::from_millis(100));
nd2array.reactions().unwrap();
// let rc_nd2array = Rc::new(&nd2array);

sleep(Duration::from_millis(66));
nd2array.reactions().unwrap();

nd2array.draw(&mut screen, rand::thread_rng().gen_bool(0.1), rand::thread_rng().gen_bool(0.01)).unwrap();

nd2array.shift_ground_loc(rand::thread_rng().gen_bool(0.5)).unwrap();

if nd2array.game_staus == GameStatus::DEATH { break; }
// if nd2array.game_staus == GameStatus::DEATH { break; }
}

let rc_nd2array2 = Rc::new(nd2array);
screen.flush().unwrap();
screen.execute(Show)?;
screen.queue(MoveTo(nd2array.max_screen_i / 2, 0))?
screen.queue(MoveTo(Rc::clone(&rc_nd2array2).max_screen_i / 2, 0))?
.queue(Print(format!("{color_green}Thanks for playing{color_reset}\n")))?;

Ok(())
Expand Down

0 comments on commit 1c12747

Please sign in to comment.