From 30509956d4274b2565052dc045eec3742a159357 Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 3 Feb 2020 17:27:35 +0100 Subject: added forgotten pos.rs --- src/pos.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/pos.rs (limited to 'src/pos.rs') diff --git a/src/pos.rs b/src/pos.rs new file mode 100644 index 0000000..df63570 --- /dev/null +++ b/src/pos.rs @@ -0,0 +1,45 @@ + + + +use std::ops::Add; +use serde_json::{Value, json}; +use specs::{Component, VecStorage}; +use super::util::{clamp, ToJson}; + +#[derive(Component, Debug, Hash, PartialEq, Eq, Clone, Copy)] +#[storage(VecStorage)] +pub struct Pos { + pub x: i32, + pub y: i32 +} + +impl Pos { + + pub fn new(x: i32, y: i32) -> Pos { + Pos {x, y} + } + + pub fn clamp(self, smaller: Pos, larger: Pos) -> Pos { + Pos { + x: clamp(self.x, smaller.x, larger.x), + y: clamp(self.y, smaller.y, larger.y) + } + } +} + +impl Add for Pos { + type Output = Pos; + + fn add(self, other: Pos) -> Pos { + Pos { + x: self.x + other.x, + y: self.y + other.y + } + } +} + +impl ToJson for Pos { + fn to_json(&self) -> Value { + json!([self.x, self.y]) + } +} -- cgit