mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-27 05:37:39 +02:00
add event queue for server
This commit is contained in:
parent
a67dafb4d9
commit
a775a68771
31
game/src/event_queue.rs
Normal file
31
game/src/event_queue.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use std::{collections::VecDeque, sync::Mutex};
|
||||
|
||||
pub enum Event {
|
||||
Skateboard {
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
a: f64,
|
||||
b: f64,
|
||||
c: f64,
|
||||
},
|
||||
}
|
||||
pub struct EventQueue {
|
||||
queue: VecDeque<Event>,
|
||||
}
|
||||
|
||||
impl EventQueue {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
queue: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll(&mut self) -> Option<Event> {
|
||||
self.queue.pop_front()
|
||||
}
|
||||
|
||||
pub fn push(&mut self, event: Event) {
|
||||
self.queue.push_back(event);
|
||||
}
|
||||
}
|
||||
@ -1,22 +1,32 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::time::Duration;
|
||||
use std::{
|
||||
sync::{Arc, Mutex},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use crate::engine::{Color, Renderer, Scene, Shape, V3};
|
||||
use crate::{
|
||||
engine::{Color, Renderer, Scene, Shape, V3},
|
||||
event_queue::EventQueue,
|
||||
vermiparous::Server,
|
||||
};
|
||||
|
||||
mod engine;
|
||||
mod event_queue;
|
||||
mod vermiparous;
|
||||
|
||||
struct Game {
|
||||
objects: Vec<Object>,
|
||||
next_object_id: u32,
|
||||
event_queue: Arc<Mutex<EventQueue>>,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
fn new() -> Self {
|
||||
fn new(event_queue: Arc<Mutex<EventQueue>>) -> Self {
|
||||
Self {
|
||||
objects: Vec::new(),
|
||||
next_object_id: 0,
|
||||
event_queue,
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,7 +91,6 @@ impl Object {
|
||||
*pos += *vel * delta_time.as_secs_f64();
|
||||
}
|
||||
ObjectKind::Ground { pos, vel_z } => {
|
||||
println!("z pos {}", pos.2);
|
||||
pos.2 += *vel_z * delta_time.as_secs_f64();
|
||||
}
|
||||
}
|
||||
@ -92,9 +101,7 @@ impl Object {
|
||||
ObjectKind::Player { pos, .. } => {
|
||||
scene.draw_shape(
|
||||
*pos,
|
||||
&Shape::new_cube(V3(0.2, 0.2, 0.2))
|
||||
.translate(V3(-0.1, -0.1, -0.1))
|
||||
.rotate(V3(pos.0 * 5.0, pos.0 * 5.0, pos.0 * 5.0)),
|
||||
&Shape::new_cube(V3(0.2, 0.2, 0.2)),
|
||||
Color::Green,
|
||||
Color::Black,
|
||||
);
|
||||
@ -125,11 +132,16 @@ impl Object {
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut sdl_io = engine::SdlIo::new()?;
|
||||
let mut game = Game::new();
|
||||
let event_queue = Arc::new(Mutex::new(EventQueue::new()));
|
||||
let mut game = Game::new(event_queue.clone());
|
||||
std::thread::spawn(move || {
|
||||
let server = Server::bind("10.133.51.127:42069");
|
||||
server.start(event_queue);
|
||||
});
|
||||
let mut objects: Vec<ObjectKind> = Vec::new();
|
||||
objects.push(ObjectKind::Player {
|
||||
pos: V3(-1.8, -0.3, 0.0),
|
||||
vel: V3(0.4, 0.0, 0.0),
|
||||
pos: V3(-0.3, -0.25, 0.0),
|
||||
vel: V3(0.0, 0.0, 0.0),
|
||||
});
|
||||
objects.push(ObjectKind::Ground {
|
||||
pos: V3(0.0, -0.3, -0.5),
|
||||
|
||||
@ -1,29 +1,38 @@
|
||||
use std::{
|
||||
io::{BufRead, BufReader},
|
||||
net::TcpStream,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
struct Server(TcpStream);
|
||||
use crate::event_queue::{Event, EventQueue};
|
||||
|
||||
struct Data {
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
a: f64,
|
||||
b: f64,
|
||||
c: f64,
|
||||
}
|
||||
pub struct Server(TcpStream);
|
||||
|
||||
impl Server {
|
||||
pub fn bind(addr: &str) -> Server {
|
||||
pub fn start(mut self, event_queue: Arc<Mutex<EventQueue>>) {
|
||||
loop {
|
||||
let data = match self.read() {
|
||||
Ok(data) => data,
|
||||
Err(err) => {
|
||||
println!("{}", err);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
event_queue.lock().unwrap().push(data);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bind(addr: &str) -> Self {
|
||||
Server(TcpStream::connect(addr).unwrap())
|
||||
}
|
||||
|
||||
fn get_fallible(data: &[f64], idx: usize) -> Result<f64, String> {
|
||||
data.get(idx)
|
||||
.map(|idx| *idx)
|
||||
.ok_or_else(|| format!("protocol error: {idx}"))
|
||||
}
|
||||
pub fn read(&mut self) -> Result<Data, String> {
|
||||
|
||||
pub fn read(&mut self) -> Result<Event, String> {
|
||||
let mut reader = BufReader::new(&mut self.0);
|
||||
let mut data = String::new();
|
||||
reader
|
||||
@ -35,7 +44,7 @@ impl Server {
|
||||
.collect::<Result<Vec<f64>, _>>();
|
||||
let data = data.map_err(|err| format!("protocol error: {}", err.to_string()))?;
|
||||
|
||||
Ok(Data {
|
||||
Ok(Event::Skateboard {
|
||||
x: Self::get_fallible(&data, 0)?,
|
||||
y: Self::get_fallible(&data, 1)?,
|
||||
z: Self::get_fallible(&data, 2)?,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user