summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Kobert <d-kobert@web.de>2019-05-31 14:53:37 +0200
committerDennis Kobert <d-kobert@web.de>2019-05-31 14:53:37 +0200
commit79db730d4e42ce592af753059422410979ff26b0 (patch)
tree93a49e0200051ef6a6ee3131c2dbc8d052941589
parenta8908cac9524fabdcf4232c1394a51848f0335af (diff)
tart implementing game logic
-rw-r--r--game_server/src/collide.rs36
-rw-r--r--game_server/src/main.rs7
-rw-r--r--game_server/src/maths.rs123
-rw-r--r--game_server/src/webhogg_game.rs13
-rw-r--r--game_server/src/webhogg_group.rs71
-rw-r--r--game_server/src/webhogg_player.rs3
6 files changed, 253 insertions, 0 deletions
diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs
new file mode 100644
index 0000000..4169787
--- /dev/null
+++ b/game_server/src/collide.rs
@@ -0,0 +1,36 @@
+pub trait Collide<Rhs> {
+ fn collides(&self, other: &Rhs) -> bool;
+}
+
+impl Collide<Vec2> for Vec2 {
+ fn collides(self, other: Self) {
+ self == other
+ }
+}
+
+impl Collide<Vec2> for Box {
+ fn collides(self, other: Vec2) {
+ self.pos < other < self.pos + self.size
+ }
+}
+
+impl Collide<Box> for Box {
+ fn collides(self, other: Self) {
+ self.collides(other.pos)
+ || other.collides(self.pos)
+ }
+}
+
+impl Collide<Vec2> for RBox {
+ fn collides(self, other: Vec2) {
+
+ || other.pos < self.pos < other.pos + other.size
+ }
+}
+
+impl Collide<Box> for Box {
+ fn collides(self, other: Self) {
+ self.pos < other.pos < self.pos + self.size
+ || other.pos < self.pos < other.pos + other.size
+ }
+}
diff --git a/game_server/src/main.rs b/game_server/src/main.rs
index ab73a97..e10fe3c 100644
--- a/game_server/src/main.rs
+++ b/game_server/src/main.rs
@@ -1,5 +1,12 @@
mod group;
+mod maths;
mod scribble_group;
+<<<<<<< Updated upstream
+=======
+mod webhogg_group;
+mod webhogg_game;
+mod collide;
+>>>>>>> Stashed changes
mod lobby;
mod server;
mod backend_connection;
diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs
new file mode 100644
index 0000000..8844c6e
--- /dev/null
+++ b/game_server/src/maths.rs
@@ -0,0 +1,123 @@
+#[derive(Clone, Copy)]
+pub struct Vec2 {
+ pub x: f32,
+ pub y: f32,
+}
+
+impl std::ops::Add for Vec2 {
+ type Output = Self;
+ fn add(self, other: Self) -> Self {
+ Self {
+ x: self.x + other.x,
+ y: self.y + other.y
+ }
+ }
+}
+
+impl std::ops::Sub for Vec2 {
+ type Output = Self;
+ fn sub(self, other: Self) -> Self {
+ Self {
+ x: self.x - other.x,
+ y: self.y - other.y
+ }
+ }
+}
+
+impl std::ops::Neg for Vec2 {
+ type Output = Self;
+ fn neg(self) -> Self {
+ Self {
+ 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 {
+ fn eq(&self, other: &Self) -> bool {
+ f32::abs(self.x - other.x) < 1e-8
+ && f32::abs(self.y - other.y) < 1e-8
+ }
+}
+
+impl std::cmp::Eq for Vec2 {}
+
+impl Vec2 {
+ pub fn distance(&self) -> f32 {
+ 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 p1: Vec2,
+ /// Point 2
+ pub p2: Vec2,
+ /// Width
+ pub w: f32,
+}
+
+impl std::ops::Add<Vec2> for RBox {
+ type Output = Self;
+ fn add(self, other: Vec2) -> Self {
+ Self {
+ p1: self.p1 + other,
+ p2: self.p2 + other,
+ w: self.w,
+ }
+ }
+}
+
+impl std::ops::Sub<Vec2> for RBox {
+ type Output = Self;
+ fn sub(self, other: Vec2) -> Self {
+ Self {
+ p1: self.p1 + other,
+ p2: 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_group.rs b/game_server/src/webhogg_group.rs
new file mode 100644
index 0000000..5a326d8
--- /dev/null
+++ b/game_server/src/webhogg_group.rs
@@ -0,0 +1,71 @@
+use crate::group::{Group, GroupId};
+use crate::server::{UserId, GameClient,
+ ClientSender, ClientReceiver,
+ GameServerError};
+use std::collections::HashMap;
+use std::sync::{Arc, Mutex};
+
+pub struct WebhoggGroup {
+ id: GroupId,
+ name: String,
+ senders: Arc<Mutex<HashMap<UserId, ClientSender>>>
+}
+
+impl Group for WebhoggGroup {
+ fn id(&self) -> GroupId {
+ self.id
+ }
+
+ fn group_type(&self) -> String {
+ "webhogg".to_string()
+ }
+
+ fn name(&self) -> String {
+ self.name.clone()
+ }
+
+ fn run(&mut self) {
+ info!("a new group {}:'{}' runs now", self.id, self.name);
+ }
+
+ fn add_client(&mut self, id: UserId, client: GameClient) -> Result<(), GameServerError> {
+ if self.senders.lock().unwrap().len() > 1 {
+ return Err(GameServerError::GroupError(
+ format!("user {} was not able to join the {} group, {}",
+ "because the client limit has been exceeded",
+ id, self.name)));
+ }
+ debug!("user {} joined the group {}:'{}'", id, self.id, self.name);
+ let (sen, rec) = client.split();
+ self.senders.lock().unwrap().insert(id, sen);
+ let senders_mutex = self.senders.clone();
+ let self_uid = id;
+ std::thread::spawn(move || Self::launch_game(self_uid, rec, senders_mutex));
+ Ok(())
+ }
+}
+
+impl WebhoggGroup {
+ pub fn new(id: GroupId, name: String) -> Self {
+ Self { id, name, senders: Arc::new(Mutex::new(HashMap::new())) }
+ }
+
+ fn launch_game(self_uid: UserId, mut rec: ClientReceiver, senders_mutex: Arc<Mutex<HashMap<UserId, ClientSender>>>) {
+ loop {
+ let message = match rec.recv_message() {
+ Ok(x) => x,
+ _ => break
+ };
+ //trace!("got message: '{:?}'", message);
+ let mut senders = senders_mutex.lock().unwrap();
+ for (uid, sender) in senders.iter_mut() {
+ if self_uid != *uid {
+ sender.send_message(&message)
+ .unwrap_or_else(|_| debug!("tried to send message to {}, but failed", *uid));
+ }
+ }
+ }
+ senders_mutex.lock().unwrap().remove(&self_uid);
+ info!("client {} has left", self_uid);
+ }
+}
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 {
+
+}