summaryrefslogtreecommitdiff
path: root/game_server
diff options
context:
space:
mode:
Diffstat (limited to 'game_server')
-rw-r--r--game_server/src/collide.rs73
-rw-r--r--game_server/src/main.rs3
-rw-r--r--game_server/src/maths.rs87
-rw-r--r--game_server/src/webhogg_game.rs13
-rw-r--r--game_server/src/webhogg_player.rs3
5 files changed, 170 insertions, 9 deletions
diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs
new file mode 100644
index 0000000..4217e7f
--- /dev/null
+++ b/game_server/src/collide.rs
@@ -0,0 +1,73 @@
+use crate::maths::{Vec2, Box, RBox};
+
+pub trait Collide<Rhs> {
+ fn collides(&self, other: &Rhs) -> bool;
+}
+
+impl Collide<Vec2> for Vec2 {
+ fn collides(&self, other: &Self) -> bool {
+ self == other
+ }
+}
+
+impl Collide<Vec2> for Box {
+ fn collides(&self, other: &Vec2) -> bool {
+ self.pos < other && other < self.pos + self.size
+ }
+}
+
+impl Collide<Box> for Box {
+ fn collides(&self, other: &Self) -> bool {
+ self.collides(other.pos)
+ || self.collides(other.pos + Vec2{x: other.x, y: 0})
+ || self.collides(other.pos + Vec2{x: 0, y: other.y})
+ || self.collides(other.pos + other.size)
+
+ || other.collides(self.pos)
+ || other.collides(self.pos + Vec2{x: self.x, y: 0})
+ || other.collides(self.pos + Vec2{x: 0, y: self.y})
+ || other.collides(self.pos + self.size)
+ }
+}
+
+impl Collide<Vec2> for RBox {
+ fn collides(&self, other: &Vec2) -> bool {
+ let dx = self.size.x;
+ let dy = self.size.y;
+ let len = f32::sqrt(dx*dx+dy*dy);
+ dx /= len;
+ dy /= len;
+
+ let dax = other.x - self.p1.x;
+ let day = other.y - self.p1.y;
+
+ let dot = dax * dx + day * dy;
+ let px = self.pos.x + dx * dot;
+ let py = self.pos.y + dy * dot;
+
+ if !(self.pos < px && px < self.pos + self.size) {
+ return false;
+ }
+
+ let ddx = other.x-px;
+ let ddy = other.y-py;
+ let manhattenDistance = ddx + ddy;
+
+ manhattenDistance < self.w
+ }
+}
+
+impl Collide<Box> for RBox {
+ fn collides(&self, other: &Box) -> bool {
+ self.collides(other.pos)
+ || self.collides(other.pos + Vec2{x: other.x, y: 0})
+ || self.collides(other.pos + Vec2{x: 0, y: other.y})
+ || self.collides(other.pos + other.size)
+
+ || other.collides(self.pos)
+ || other.collides(self.pos + Vec2{x: self.x, y: 0})
+ || other.collides(self.pos + Vec2{x: 0, y: self.y})
+ || other.collides(self.pos + self.size)
+
+ }
+}
diff --git a/game_server/src/main.rs b/game_server/src/main.rs
index a6cc608..cfd9787 100644
--- a/game_server/src/main.rs
+++ b/game_server/src/main.rs
@@ -1,6 +1,9 @@
mod group;
+mod maths;
mod scribble_group;
mod webhogg_group;
+mod webhogg_game;
+mod collide;
mod lobby;
mod server;
mod backend_connection;
diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs
index 635eed6..a55c5ce 100644
--- a/game_server/src/maths.rs
+++ b/game_server/src/maths.rs
@@ -1,3 +1,4 @@
+#[derive(Clone, Copy)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
@@ -7,8 +8,8 @@ impl std::ops::Add for Vec2 {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
- x: x + other.x,
- y: y + other.y
+ x: self.x + other.x,
+ y: self.y + other.y
}
}
}
@@ -17,24 +18,35 @@ impl std::ops::Sub for Vec2 {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
- x: x - other.x,
- y: y - other.y
+ x: self.x - other.x,
+ y: self.y - other.y
}
}
}
impl std::ops::Neg for Vec2 {
type Output = Self;
- fn sub(self) -> Self {
+ fn neg(self) -> Self {
Self {
- x: -x,
- y: -y
+ x: -self.x,
+ y: -self.y
}
}
}
+impl std::cmp::PartialOrd for Vec2 {
+ fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+ Some(if self.x < other.x && self.y < other.y {
+ std::cmp::Ordering::Less
+ } else if self.x > other.x && self.y > other.y {
+ std::cmp::Ordering::Greater
+ } else {
+ std::cmp::Ordering::Equal
+ })
+ }
+}
+
impl std::cmp::PartialEq for Vec2 {
- type Output = bool;
fn eq(&self, other: &Self) -> bool {
f32::abs(self.x - other.x) < 1e-8
&& f32::abs(self.y - other.y) < 1e-8
@@ -45,10 +57,67 @@ impl std::cmp::Eq for Vec2 {}
impl Vec2 {
pub fn distance(&self) -> f32 {
- f32::sqrt(self.distance2)
+ f32::sqrt(self.distance2())
}
pub fn distance2(&self) -> f32 {
self.x * self.x + self.y * self.y
}
}
+
+pub struct Box {
+ pub pos: Vec2,
+ /// the size may not be smaller than zero
+ pub size: Vec2,
+}
+
+impl std::ops::Add<Vec2> for Box {
+ type Output = Self;
+ fn add(self, other: Vec2) -> Self {
+ Self {
+ p1: self.p1 + other,
+ p2: self.p2 + other,
+ }
+ }
+}
+
+impl std::ops::Sub<Vec2> for Box {
+ type Output = Self;
+ fn sub(self, other: Vec2) -> Self {
+ Self {
+ pos: self.pos + other,
+ size: self.size + other
+ }
+ }
+}
+
+pub struct RBox {
+ /// Point 1
+ pub pos: Vec2,
+ /// Point 2
+ pub size: Vec2,
+ /// Width Attention manhatten distance!!!
+ pub w: f32,
+}
+
+impl std::ops::Add<Vec2> for RBox {
+ type Output = Self;
+ fn add(self, other: Vec2) -> Self {
+ Self {
+ pos: self.p1 + other,
+ size: self.p2 + other,
+ w: self.w,
+ }
+ }
+}
+
+impl std::ops::Sub<Vec2> for RBox {
+ type Output = Self;
+ fn sub(self, other: Vec2) -> Self {
+ Self {
+ pos: self.p1 + other,
+ size: self.p2 + other,
+ w: self.w,
+ }
+ }
+}
diff --git a/game_server/src/webhogg_game.rs b/game_server/src/webhogg_game.rs
new file mode 100644
index 0000000..7b94fcb
--- /dev/null
+++ b/game_server/src/webhogg_game.rs
@@ -0,0 +1,13 @@
+use crate::maths::Vec2;
+
+pub struct WebhoggPlayer {
+ pos: Vec2,
+}
+
+pub struct WebhoggGame {
+ player1: WebhoggPlayer,
+ player2: WebhoggPlayer,
+}
+
+impl WebhoggGame {
+}
diff --git a/game_server/src/webhogg_player.rs b/game_server/src/webhogg_player.rs
new file mode 100644
index 0000000..38b9596
--- /dev/null
+++ b/game_server/src/webhogg_player.rs
@@ -0,0 +1,3 @@
+pub struct WebhoggPlayer {
+
+}