Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visuals, Position #12

Merged
merged 10 commits into from
May 28, 2024
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ arrayvec = "0.5"
enum_dispatch = "0.3"
enum-iterator = "0.6"
js-sys = "0.3"
num-derive = "0.3"
num-derive = "0.4"
num-traits = "0.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod types;
//pub mod look; // TODO: most/all of this is World specific
//pub mod find; // TODO: most/all of this is World specific

pub use self::{numbers::*, small_enums::*, types::*};
pub use self::{extra::*, numbers::*, small_enums::*, types::*};
//pub use self::{extra::*, look::*, recipes::FactoryRecipe}; // TODO: most/all
// of this is World specific

Expand Down
4 changes: 4 additions & 0 deletions src/constants/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ pub const RANGED_MASS_ATTACK_POWER_RANGE_2: u32 = 4;
///
/// [`Creep::ranged_mass_attack`]: crate::objects::Creep::ranged_mass_attack
pub const RANGED_MASS_ATTACK_POWER_RANGE_3: u32 = 1;

pub const ROOM_WIDTH: u8 = 100;

pub const ROOM_HEIGHT: u8 = 100;
17 changes: 17 additions & 0 deletions src/constants/small_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ pub enum Direction {
TopLeft = 8,
}

impl From<Direction> for (i8, i8) {
/// Returns the change in (x, y) when moving in each direction.
#[inline]
fn from(direction: Direction) -> (i8, i8) {
match direction {
Direction::Top => (0, -1),
Direction::TopRight => (1, -1),
Direction::Right => (1, 0),
Direction::BottomRight => (1, 1),
Direction::Bottom => (0, 1),
Direction::BottomLeft => (-1, 1),
Direction::Left => (-1, 0),
Direction::TopLeft => (-1, -1),
}
}
}

impl ::std::ops::Neg for Direction {
type Output = Direction;

Expand Down
1 change: 1 addition & 0 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use wasm_bindgen::prelude::*;

pub mod pathfinder;
pub mod utils;
pub mod visual;

#[wasm_bindgen(module = "game")]
extern "C" {
Expand Down
59 changes: 58 additions & 1 deletion src/game/pathfinder.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,70 @@
use crate::constants::{Direction, ROOM_HEIGHT, ROOM_WIDTH};
use js_sys::{Array, Object};
use serde::{Deserialize, Serialize};
use std::{fmt, ops::Add};
use wasm_bindgen::prelude::*;

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Position {
pub x: u8,
pub y: u8,
}

impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[pos {},{}]", self.x, self.y)
}
}

impl Position {
pub fn range_to(&self, pos: &Position) -> u8 {
std::cmp::max(self.x.abs_diff(pos.x), self.y.abs_diff(pos.y))
}

pub fn checked_add_direction(&self, dir: Direction) -> Option<Position> {
let delta: (i8, i8) = dir.into();
if (self.x == 0 && delta.0 < 0)
|| (self.x == ROOM_WIDTH - 1 && delta.0 > 0)
|| (self.y == 0 && delta.1 < 0)
|| (self.y == ROOM_HEIGHT - 1 && delta.1 > 0)
{
None
} else {
Some(Position {
x: self.x.wrapping_add_signed(delta.0),
y: self.y.wrapping_add_signed(delta.1),
})
}
}

pub fn saturating_add_direction(&self, dir: Direction) -> Position {
let mut delta: (i8, i8) = dir.into();
if self.x >= ROOM_WIDTH - 1 && delta.0 > 0 {
delta.0 = 0;
}
if self.y >= ROOM_HEIGHT - 1 && delta.1 > 0 {
delta.1 = 0;
}
Position {
x: self.x.saturating_add_signed(delta.0),
y: self.y.saturating_add_signed(delta.1),
}
}
}

impl From<Position> for JsValue {
fn from(pos: Position) -> JsValue {
serde_wasm_bindgen::to_value(&pos).expect("serializable Position")
}
}

impl Add<Direction> for Position {
type Output = Position;
fn add(self, other: Direction) -> Self::Output {
self.checked_add_direction(other).expect("room boundaries")
}
}

#[wasm_bindgen(module = "game/path-finder")]
extern "C" {
/// Find an optimal path between origin and goal. Note that searchPath
Expand Down
Loading
Loading