diff --git a/src/game/pathfinder.rs b/src/game/pathfinder.rs index b524870..77e6e33 100644 --- a/src/game/pathfinder.rs +++ b/src/game/pathfinder.rs @@ -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}; @@ -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 { let delta: (i8, i8) = dir.into(); if (self.x == 0 && delta.0 < 0) @@ -52,6 +51,12 @@ impl Position { } } +impl HasPosition for Position { + fn pos(&self) -> Position { + *self + } +} + impl From for JsValue { fn from(pos: Position) -> JsValue { serde_wasm_bindgen::to_value(&pos).expect("serializable Position") diff --git a/src/lib.rs b/src/lib.rs index 815281a..fd64ee9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::*}; } diff --git a/src/objects/impls/arena/area_effect.rs b/src/objects/impls/arena/area_effect.rs index 85bbbdb..f0d3861 100644 --- a/src/objects/impls/arena/area_effect.rs +++ b/src/objects/impls/arena/area_effect.rs @@ -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::*; @@ -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() diff --git a/src/objects/impls/arena/body_part.rs b/src/objects/impls/arena/body_part.rs index 42ddf37..8bab22a 100644 --- a/src/objects/impls/arena/body_part.rs +++ b/src/objects/impls/arena/body_part.rs @@ -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::*; @@ -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() diff --git a/src/objects/impls/arena/flag.rs b/src/objects/impls/arena/flag.rs index e786043..ee6d289 100644 --- a/src/objects/impls/arena/flag.rs +++ b/src/objects/impls/arena/flag.rs @@ -1,4 +1,4 @@ -use crate::objects::GameObject; +use crate::{objects::GameObject, prelude::*}; use js_sys::Object; use wasm_bindgen::prelude::*; @@ -17,6 +17,14 @@ extern "C" { pub fn my(this: &Flag) -> Option; } +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() diff --git a/src/objects/impls/arena/score_collector.rs b/src/objects/impls/arena/score_collector.rs index d5bf76b..625d2ac 100644 --- a/src/objects/impls/arena/score_collector.rs +++ b/src/objects/impls/arena/score_collector.rs @@ -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::*; @@ -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() diff --git a/src/objects/impls/construction_site.rs b/src/objects/impls/construction_site.rs index a73889a..05bc7a5 100644 --- a/src/objects/impls/construction_site.rs +++ b/src/objects/impls/construction_site.rs @@ -1,6 +1,8 @@ use crate::{ constants::ReturnCode, + game::pathfinder::Position, objects::{GameObject, Structure}, + HasPosition, }; use js_sys::Object; use wasm_bindgen::prelude::*; @@ -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(), + } + } +} diff --git a/src/objects/impls/creep.rs b/src/objects/impls/creep.rs index 184a37b..4b034c6 100644 --- a/src/objects/impls/creep.rs +++ b/src/objects/impls/creep.rs @@ -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::*, }; @@ -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(), + } + } +} diff --git a/src/objects/impls/game_object.rs b/src/objects/impls/game_object.rs index dd82565..7e37d60 100644 --- a/src/objects/impls/game_object.rs +++ b/src/objects/impls/game_object.rs @@ -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(), @@ -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 { GameObject::ticks_to_decay(self.as_ref()) } diff --git a/src/objects/impls/owned_structure.rs b/src/objects/impls/owned_structure.rs index 52553b3..46925e3 100644 --- a/src/objects/impls/owned_structure.rs +++ b/src/objects/impls/owned_structure.rs @@ -1,4 +1,5 @@ use crate::{ + game::pathfinder::Position, objects::{GameObject, Structure}, prelude::*, }; @@ -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(), + } + } +} diff --git a/src/objects/impls/resource.rs b/src/objects/impls/resource.rs index 25efd00..9bdb441 100644 --- a/src/objects/impls/resource.rs +++ b/src/objects/impls/resource.rs @@ -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::*; @@ -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(), + } + } +} diff --git a/src/objects/impls/source.rs b/src/objects/impls/source.rs index 7d10f13..56fb6dc 100644 --- a/src/objects/impls/source.rs +++ b/src/objects/impls/source.rs @@ -1,4 +1,4 @@ -use crate::objects::GameObject; +use crate::{game::pathfinder::Position, objects::GameObject, HasPosition}; use js_sys::Object; use wasm_bindgen::prelude::*; @@ -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(), + } + } +} diff --git a/src/objects/impls/structure.rs b/src/objects/impls/structure.rs index 7c37ac4..724d323 100644 --- a/src/objects/impls/structure.rs +++ b/src/objects/impls/structure.rs @@ -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::*; @@ -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(), + } + } +} diff --git a/src/objects/impls/structure_container.rs b/src/objects/impls/structure_container.rs index c334571..4c7f73a 100644 --- a/src/objects/impls/structure_container.rs +++ b/src/objects/impls/structure_container.rs @@ -1,4 +1,5 @@ use crate::{ + game::pathfinder::Position, objects::{GameObject, Store, Structure}, prelude::*, }; @@ -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(), + } + } +} diff --git a/src/objects/impls/structure_extension.rs b/src/objects/impls/structure_extension.rs index c70dd3d..17d8de1 100644 --- a/src/objects/impls/structure_extension.rs +++ b/src/objects/impls/structure_extension.rs @@ -1,4 +1,5 @@ use crate::{ + game::pathfinder::Position, objects::{GameObject, OwnedStructure, Store, Structure}, prelude::*, }; @@ -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(), + } + } +} diff --git a/src/objects/impls/structure_rampart.rs b/src/objects/impls/structure_rampart.rs index 0a18613..0e96d4c 100644 --- a/src/objects/impls/structure_rampart.rs +++ b/src/objects/impls/structure_rampart.rs @@ -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::*; @@ -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(), + } + } +} diff --git a/src/objects/impls/structure_road.rs b/src/objects/impls/structure_road.rs index 6299fa5..6750a39 100644 --- a/src/objects/impls/structure_road.rs +++ b/src/objects/impls/structure_road.rs @@ -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::*; @@ -15,3 +19,12 @@ extern "C" { pub type StructureRoad; } + +impl HasPosition for StructureRoad { + fn pos(&self) -> Position { + Position { + x: self.x(), + y: self.y(), + } + } +} diff --git a/src/objects/impls/structure_spawn.rs b/src/objects/impls/structure_spawn.rs index 43b68f1..b73f9d9 100644 --- a/src/objects/impls/structure_spawn.rs +++ b/src/objects/impls/structure_spawn.rs @@ -1,5 +1,6 @@ use crate::{ constants::{Part, ReturnCode}, + game::pathfinder::Position, objects::{Creep, GameObject, OwnedStructure, Store, Structure}, prelude::*, }; @@ -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(), + } + } +} diff --git a/src/objects/impls/structure_tower.rs b/src/objects/impls/structure_tower.rs index 9f839f2..08a4abf 100644 --- a/src/objects/impls/structure_tower.rs +++ b/src/objects/impls/structure_tower.rs @@ -1,5 +1,6 @@ use crate::{ constants::ReturnCode, + game::pathfinder::Position, objects::{Creep, GameObject, OwnedStructure, Store, Structure}, prelude::*, }; @@ -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(), + } + } +} diff --git a/src/objects/impls/structure_wall.rs b/src/objects/impls/structure_wall.rs index 18876bd..a450d20 100644 --- a/src/objects/impls/structure_wall.rs +++ b/src/objects/impls/structure_wall.rs @@ -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::*; @@ -13,3 +17,12 @@ extern "C" { #[derive(Clone)] pub type StructureWall; } + +impl HasPosition for StructureWall { + fn pos(&self) -> Position { + Position { + x: self.x(), + y: self.y(), + } + } +} diff --git a/src/traits.rs b/src/traits.rs index f6590d7..f8a1429 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -22,6 +22,18 @@ pub trait HasCooldown { fn cooldown(&self) -> u32; } +pub trait HasPosition { + /// The position of the object + fn pos(&self) -> Position; + fn range_to(&self, has_pos: &impl HasPosition) -> u8 { + let other = has_pos.pos(); + std::cmp::max( + self.pos().x.abs_diff(other.x), + self.pos().y.abs_diff(other.y), + ) + } +} + // pub trait HasNativeId { // fn native_id(&self) -> JsString; // } @@ -152,8 +164,6 @@ pub trait GameObjectProperties { /// The Y coordinate in the room. fn y(&self) -> u8; - fn pos(&self) -> Position; - /// If defined, then this object will disappear after this number of ticks. fn ticks_to_decay(&self) -> Option;