Skip to content

Commit

Permalink
feat: HasPosition trait (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfrank authored Aug 3, 2024
1 parent 6ad8ca2 commit c9cca0d
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 24 deletions.
15 changes: 10 additions & 5 deletions src/game/pathfinder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::constants::{Direction, ROOM_HEIGHT, ROOM_WIDTH};
use crate::{
constants::{Direction, ROOM_HEIGHT, ROOM_WIDTH},
HasPosition,
};
use js_sys::{Array, Object};
use serde::{Deserialize, Serialize};
use std::{fmt, ops::Add};
Expand All @@ -17,10 +20,6 @@ impl fmt::Display for Position {
}

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)
Expand Down Expand Up @@ -52,6 +51,12 @@ impl Position {
}
}

impl HasPosition for Position {
fn pos(&self) -> Position {
*self
}
}

impl From<Position> for JsValue {
fn from(pos: Position) -> JsValue {
serde_wasm_bindgen::to_value(&pos).expect("serializable Position")
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ pub use crate::{constants::*, enums::*, objects::*, traits::*};
// ///
// /// This module contains all base functionality traits, and no structures.
pub mod prelude {
pub use crate::traits::*;
pub use crate::{game::pathfinder::Position, traits::*};
}
11 changes: 10 additions & 1 deletion src/objects/impls/arena/area_effect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{constants::EffectType, objects::GameObject};
use crate::{constants::EffectType, objects::GameObject, prelude::*};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand All @@ -16,6 +16,15 @@ extern "C" {
pub fn effect(this: &AreaEffect) -> EffectType;
}

impl HasPosition for AreaEffect {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}

// impl JsContainerFromValue for AreaEffect {
// fn from_value(val: JsValue) -> Self {
// val.unchecked_into()
Expand Down
10 changes: 9 additions & 1 deletion src/objects/impls/arena/body_part.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{constants::Part, objects::GameObject};
use crate::{constants::Part, objects::GameObject, prelude::*};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand All @@ -16,6 +16,14 @@ extern "C" {
pub fn part_type(this: &BodyPart) -> Part;
}

impl HasPosition for BodyPart {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
// impl JsContainerFromValue for BodyPart {
// fn from_value(val: JsValue) -> Self {
// val.unchecked_into()
Expand Down
10 changes: 9 additions & 1 deletion src/objects/impls/arena/flag.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::objects::GameObject;
use crate::{objects::GameObject, prelude::*};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand All @@ -17,6 +17,14 @@ extern "C" {
pub fn my(this: &Flag) -> Option<bool>;
}

impl HasPosition for Flag {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
// impl JsContainerFromValue for Flag {
// fn from_value(val: JsValue) -> Self {
// val.unchecked_into()
Expand Down
10 changes: 9 additions & 1 deletion src/objects/impls/arena/score_collector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{constants::ResourceType, objects::GameObject};
use crate::{constants::ResourceType, objects::GameObject, prelude::*};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -28,6 +28,14 @@ extern "C" {
pub fn score_total(this: &ScoreCollector) -> u32;
}

impl HasPosition for ScoreCollector {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
// impl JsContainerFromValue for ScoreCollector {
// fn from_value(val: JsValue) -> Self {
// val.unchecked_into()
Expand Down
11 changes: 11 additions & 0 deletions src/objects/impls/construction_site.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{
constants::ReturnCode,
game::pathfinder::Position,
objects::{GameObject, Structure},
HasPosition,
};
use js_sys::Object;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -36,3 +38,12 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn remove(this: &ConstructionSite) -> ReturnCode;
}

impl HasPosition for ConstructionSite {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
11 changes: 10 additions & 1 deletion src/objects/impls/creep.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
constants::{Direction, Part, ResourceType, ReturnCode},
game::pathfinder::FindPathOptions,
game::pathfinder::{FindPathOptions, Position},
objects::{ConstructionSite, GameObject, Resource, Source, Store},
prelude::*,
};
Expand Down Expand Up @@ -194,3 +194,12 @@ impl HasStore for Creep {
Self::store(self)
}
}

impl HasPosition for Creep {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
8 changes: 3 additions & 5 deletions src/objects/impls/game_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ impl GameObject {
}
}
}
}

pub fn pos(&self) -> Position {
impl HasPosition for GameObject {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
Expand Down Expand Up @@ -108,10 +110,6 @@ where
GameObject::y(self.as_ref())
}

fn pos(&self) -> Position {
GameObject::pos(self.as_ref())
}

fn ticks_to_decay(&self) -> Option<u32> {
GameObject::ticks_to_decay(self.as_ref())
}
Expand Down
10 changes: 10 additions & 0 deletions src/objects/impls/owned_structure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
game::pathfinder::Position,
objects::{GameObject, Structure},
prelude::*,
};
Expand Down Expand Up @@ -30,3 +31,12 @@ where
OwnedStructure::my(self.as_ref())
}
}

impl HasPosition for OwnedStructure {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
13 changes: 12 additions & 1 deletion src/objects/impls/resource.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{constants::ResourceType, objects::GameObject};
use crate::{
constants::ResourceType, game::pathfinder::Position, objects::GameObject, HasPosition,
};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand All @@ -21,3 +23,12 @@ extern "C" {
#[wasm_bindgen(method, getter = resourceType)]
pub fn resource_type(this: &Resource) -> ResourceType;
}

impl HasPosition for Resource {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
11 changes: 10 additions & 1 deletion src/objects/impls/source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::objects::GameObject;
use crate::{game::pathfinder::Position, objects::GameObject, HasPosition};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand All @@ -21,3 +21,12 @@ extern "C" {
pub fn energy_capacity(this: &Source) -> u32;

}

impl HasPosition for Source {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
11 changes: 10 additions & 1 deletion src/objects/impls/structure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{objects::GameObject, prelude::*};
use crate::{game::pathfinder::Position, objects::GameObject, prelude::*};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -32,3 +32,12 @@ where
Structure::hits_max(self.as_ref())
}
}

impl HasPosition for Structure {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
10 changes: 10 additions & 0 deletions src/objects/impls/structure_container.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
game::pathfinder::Position,
objects::{GameObject, Store, Structure},
prelude::*,
};
Expand Down Expand Up @@ -28,3 +29,12 @@ impl HasStore for StructureContainer {
Self::store(self)
}
}

impl HasPosition for StructureContainer {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
10 changes: 10 additions & 0 deletions src/objects/impls/structure_extension.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
game::pathfinder::Position,
objects::{GameObject, OwnedStructure, Store, Structure},
prelude::*,
};
Expand Down Expand Up @@ -31,3 +32,12 @@ impl HasStore for StructureExtension {
Self::store(self)
}
}

impl HasPosition for StructureExtension {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
15 changes: 14 additions & 1 deletion src/objects/impls/structure_rampart.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::objects::{GameObject, OwnedStructure, Structure};
use crate::{
game::pathfinder::Position,
objects::{GameObject, OwnedStructure, Structure},
HasPosition,
};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand All @@ -13,3 +17,12 @@ extern "C" {
#[derive(Clone)]
pub type StructureRampart;
}

impl HasPosition for StructureRampart {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
15 changes: 14 additions & 1 deletion src/objects/impls/structure_road.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::objects::{GameObject, Structure};
use crate::{
game::pathfinder::Position,
objects::{GameObject, Structure},
HasPosition,
};
use js_sys::Object;
use wasm_bindgen::prelude::*;

Expand All @@ -15,3 +19,12 @@ extern "C" {
pub type StructureRoad;

}

impl HasPosition for StructureRoad {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
10 changes: 10 additions & 0 deletions src/objects/impls/structure_spawn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
constants::{Part, ReturnCode},
game::pathfinder::Position,
objects::{Creep, GameObject, OwnedStructure, Store, Structure},
prelude::*,
};
Expand Down Expand Up @@ -66,3 +67,12 @@ impl HasStore for StructureSpawn {
Self::store(self)
}
}

impl HasPosition for StructureSpawn {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
10 changes: 10 additions & 0 deletions src/objects/impls/structure_tower.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
constants::ReturnCode,
game::pathfinder::Position,
objects::{Creep, GameObject, OwnedStructure, Store, Structure},
prelude::*,
};
Expand Down Expand Up @@ -56,3 +57,12 @@ impl HasStore for StructureTower {
Self::store(self)
}
}

impl HasPosition for StructureTower {
fn pos(&self) -> Position {
Position {
x: self.x(),
y: self.y(),
}
}
}
Loading

0 comments on commit c9cca0d

Please sign in to comment.