From 79db730d4e42ce592af753059422410979ff26b0 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 14:53:37 +0200 Subject: tart implementing game logic --- game_server/src/collide.rs | 36 +++++++++++ game_server/src/main.rs | 7 +++ game_server/src/maths.rs | 123 ++++++++++++++++++++++++++++++++++++++ game_server/src/webhogg_game.rs | 13 ++++ game_server/src/webhogg_group.rs | 71 ++++++++++++++++++++++ game_server/src/webhogg_player.rs | 3 + 6 files changed, 253 insertions(+) create mode 100644 game_server/src/collide.rs create mode 100644 game_server/src/maths.rs create mode 100644 game_server/src/webhogg_game.rs create mode 100644 game_server/src/webhogg_group.rs create mode 100644 game_server/src/webhogg_player.rs (limited to 'game_server/src') 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 { + fn collides(&self, other: &Rhs) -> bool; +} + +impl Collide for Vec2 { + fn collides(self, other: Self) { + self == other + } +} + +impl Collide for Box { + fn collides(self, other: Vec2) { + self.pos < other < self.pos + self.size + } +} + +impl Collide for Box { + fn collides(self, other: Self) { + self.collides(other.pos) + || other.collides(self.pos) + } +} + +impl Collide for RBox { + fn collides(self, other: Vec2) { + + || other.pos < self.pos < other.pos + other.size + } +} + +impl Collide 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 { + 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 for Box { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + p1: self.p1 + other, + p2: self.p2 + other, + } + } +} + +impl std::ops::Sub 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 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 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>> +} + +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>>) { + 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 { + +} -- cgit v1.2.3-70-g09d2