-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ee45e7
commit 7c8daed
Showing
15 changed files
with
44 additions
and
947 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ wasm-pack.log | |
yarn.lock | ||
package-lock.json | ||
node_modules | ||
web/styles/dist.css | ||
web/styles/dist.css | ||
/examples |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"rust-analyzer.linkedProjects": [ | ||
"./Cargo.toml", | ||
"./Cargo.toml", | ||
"./Cargo.toml" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +0,0 @@ | ||
use wasm_bindgen::prelude::*; | ||
|
||
use super::{Coord, generate_random_points}; | ||
|
||
#[wasm_bindgen] | ||
#[derive(Debug, Clone)] | ||
pub struct Food { | ||
pub x: u32, | ||
pub y: u32, | ||
} | ||
|
||
#[wasm_bindgen] | ||
#[derive(Debug, Clone)] | ||
pub struct FoodSpawner { | ||
x_bounds: u32, | ||
y_bounds: u32, | ||
food_vec: Vec<Food>, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl FoodSpawner { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(x_bounds: u32, y_bounds: u32) -> Self { | ||
Self { | ||
x_bounds, | ||
y_bounds, | ||
food_vec: Vec::with_capacity(10) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn spawn(&mut self) { | ||
let Coord{x, y} = generate_random_points(self.x_bounds, self.y_bounds); | ||
self.food_vec.push(Food { | ||
x, | ||
y | ||
}); | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn eat_food(&mut self, x: u32, y: u32) { | ||
let idx: usize = self.food_vec.iter().position(|food| food.x == x && food.y == y).expect("Invalid food coordinates"); | ||
self.food_vec.swap_remove(idx); | ||
} | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
use wasm_bindgen::prelude::*; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,8 @@ | ||
mod utils; | ||
mod snake; | ||
mod food; | ||
pub mod utils; | ||
pub mod snake; | ||
pub mod game; | ||
pub mod food; | ||
|
||
use wasm_bindgen::prelude::*; | ||
use getrandom::getrandom; | ||
pub static ROW_BOUND: u32 = 33; | ||
pub static COL_BOUND: u32 = 56; | ||
|
||
#[wasm_bindgen] | ||
pub struct Coord { | ||
pub x: u32, | ||
pub y: u32 | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl Coord { | ||
#[wasm_bindgen(getter)] | ||
pub fn get_x(&self) -> u32 { | ||
self.x | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn get_y(&self) -> u32 { | ||
self.y | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn generate_random_points(x_bound: u32, y_bound: u32) -> Coord { | ||
Coord { | ||
x: { | ||
let mut buffer = [0u8; 4]; | ||
getrandom(&mut buffer).expect("Failed to generate random number"); | ||
let random_number = u32::from_ne_bytes(buffer) % (x_bound + 1); | ||
random_number | ||
}, | ||
y: { | ||
let mut buffer = [0u8; 4]; | ||
getrandom(&mut buffer).expect("Failed to generate random number"); | ||
let random_number = u32::from_ne_bytes(buffer) % (y_bound + 1); | ||
random_number | ||
} | ||
} | ||
} |
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
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function updateGithubStarCount() { | ||
const starCountCard = document.querySelector("#github-stars"); | ||
fetch(`https://api.github.com/repos/vilayat-ali/wasmslither`) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
starCountCard.innerHTML = data.stargazers_count; | ||
}) | ||
.catch((error) => console.error(error)); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.