aboutsummaryrefslogtreecommitdiff
path: root/light-client/src
diff options
context:
space:
mode:
Diffstat (limited to 'light-client/src')
-rw-r--r--light-client/src/game.rs79
-rw-r--r--light-client/src/main.rs9
-rw-r--r--light-client/src/network.rs20
-rw-r--r--light-client/src/sprite_renderer.rs16
-rw-r--r--light-client/src/tilemap.rs19
5 files changed, 134 insertions, 9 deletions
diff --git a/light-client/src/game.rs b/light-client/src/game.rs
index ed1cb7fe..cf265344 100644
--- a/light-client/src/game.rs
+++ b/light-client/src/game.rs
@@ -20,12 +20,15 @@ use hurrycurry_protocol::{
glam::{IVec2, Vec2},
PacketC, PlayerID, TileIndex,
};
+use log::{info, warn};
+use sdl2::rect::FRect;
use std::collections::HashMap;
pub struct Game {
tiles: HashMap<IVec2, Tile>,
tilemap: Tilemap,
players: HashMap<PlayerID, Player>,
+ my_id: PlayerID,
}
pub struct Tile {
@@ -34,6 +37,9 @@ pub struct Tile {
pub struct Player {
character: i32,
position: Vec2,
+ name: String,
+ boosting: bool,
+ rot: f32,
}
impl Game {
@@ -42,11 +48,13 @@ impl Game {
tiles: HashMap::new(),
players: HashMap::new(),
tilemap: Tilemap::default(),
+ my_id: PlayerID(0),
}
}
pub fn packet_in(&mut self, packet: PacketC, renderer: &mut SpriteRenderer) {
match packet {
+ PacketC::Init { id } => self.my_id = id,
PacketC::Data { data } => {
self.tilemap.init(&data.tile_names, renderer.metadata());
}
@@ -62,11 +70,80 @@ impl Game {
}
self.tilemap.set(tile, kind, neighbors);
}
+ PacketC::AddPlayer {
+ id,
+ position,
+ character,
+ name,
+ } => {
+ info!("add player {} {name:?}", id.0);
+ self.players.insert(
+ id,
+ Player {
+ character,
+ position,
+ name,
+ boosting: false,
+ rot: 0.,
+ },
+ );
+ }
+ PacketC::RemovePlayer { id } => {
+ info!("remove player {}", id.0);
+ self.players.remove(&id);
+ }
+ PacketC::Position {
+ player,
+ pos,
+ rot,
+ boosting,
+ } => {
+ if let Some(p) = self.players.get_mut(&player) {
+ p.position = pos;
+ p.rot = rot;
+ p.boosting = boosting;
+ }
+ }
+ PacketC::MoveItem { from, to } => (),
+ PacketC::SetItem { location, item } => (),
+ PacketC::SetProgress {
+ item,
+ progress,
+ warn,
+ } => (),
+ PacketC::Collide { player, force } => (),
+ PacketC::Communicate {
+ player,
+ message,
+ persist,
+ } => (),
+ PacketC::ServerMessage { text } => (),
+ PacketC::Score {
+ points,
+ demands_failed,
+ demands_completed,
+ time_remaining,
+ } => (),
+ PacketC::SetIngame { state, lobby } => (),
+ PacketC::Error { message } => {
+ warn!("server error: {message:?}")
+ }
_ => (),
}
}
pub fn draw(&self, ctx: &mut SpriteRenderer) {
- self.tilemap.draw(ctx)
+ self.tilemap.draw(ctx);
+
+ for p in self.players.values() {
+ let src = ctx.misc_textures().player;
+ let dst = FRect::new(
+ p.position.x - src.width() as f32 / 32. / 2.,
+ p.position.y + 1. - src.height() as f32 / 24.,
+ src.width() as f32 / 32.,
+ src.height() as f32 / 24.,
+ );
+ ctx.draw(((dst.y + dst.h + 1.) * 24.) as i32, src, dst);
+ }
}
}
diff --git a/light-client/src/main.rs b/light-client/src/main.rs
index 5e79aa85..8be38e4c 100644
--- a/light-client/src/main.rs
+++ b/light-client/src/main.rs
@@ -1,5 +1,3 @@
-use std::time::Instant;
-
/*
Hurry Curry! - a game about cooking
Copyright 2024 metamuffin
@@ -28,6 +26,8 @@ pub mod sprite_renderer;
pub mod tilemap;
fn main() {
+ env_logger::init_from_env("LOG");
+
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem
@@ -50,6 +50,11 @@ fn main() {
let mut game = Game::new();
let mut renderer = SpriteRenderer::init(&texture_creator);
+ net.queue_out.push_back(hurrycurry_protocol::PacketS::Join {
+ name: "light".to_string(),
+ character: 0,
+ });
+
'mainloop: loop {
net.poll();
diff --git a/light-client/src/network.rs b/light-client/src/network.rs
index 997a3344..47eb66ab 100644
--- a/light-client/src/network.rs
+++ b/light-client/src/network.rs
@@ -17,13 +17,14 @@
*/
use anyhow::Result;
use hurrycurry_protocol::{PacketC, PacketS, BINCODE_CONFIG};
-use log::warn;
+use log::{debug, warn};
use std::{collections::VecDeque, net::TcpStream};
use tungstenite::{
client::{uri_mode, IntoClientRequest},
client_tls_with_config,
handshake::client::Request,
stream::{MaybeTlsStream, Mode},
+ util::NonBlockingError,
Message, WebSocket,
};
@@ -76,7 +77,10 @@ impl Network {
pub fn poll(&mut self) {
self.queue_in.extend(match self.sock.read() {
Ok(Message::Text(packet)) => match serde_json::from_str(&packet) {
- Ok(p) => Some(p),
+ Ok(packet) => {
+ debug!("<- {packet:?}");
+ Some(packet)
+ }
Err(e) => {
warn!("invalid json packet: {e:?}");
None
@@ -84,7 +88,10 @@ impl Network {
},
Ok(Message::Binary(packet)) => {
match bincode::decode_from_slice(&packet, BINCODE_CONFIG) {
- Ok((p, _)) => Some(p),
+ Ok((packet, _)) => {
+ debug!("<- {packet:?}");
+ Some(packet)
+ }
Err(e) => {
warn!("invalid bincode packet: {e:?}");
None
@@ -93,15 +100,20 @@ impl Network {
}
Ok(_) => None,
Err(e) => {
- warn!("{e:?}");
+ if let Some(e) = e.into_non_blocking() {
+ warn!("{e:?}");
+ }
None
}
});
for packet in self.queue_out.drain(..) {
+ debug!("-> {packet:?}");
self.sock
.write(Message::Text(serde_json::to_string(&packet).unwrap()))
.unwrap();
}
+
+ self.sock.flush().unwrap();
}
}
diff --git a/light-client/src/sprite_renderer.rs b/light-client/src/sprite_renderer.rs
index 64eef8f6..187ccc7b 100644
--- a/light-client/src/sprite_renderer.rs
+++ b/light-client/src/sprite_renderer.rs
@@ -26,6 +26,8 @@ use std::collections::HashMap;
pub struct SpriteRenderer<'a> {
metadata: HashMap<String, Rect>,
+ misc_textures: MiscTextures,
+
texture: Texture<'a>,
view_scale: Vec2,
@@ -34,6 +36,10 @@ pub struct SpriteRenderer<'a> {
sprites: Vec<SpriteDraw>,
}
+pub struct MiscTextures {
+ pub player: Rect,
+}
+
pub struct SpriteDraw {
z_order: i32,
src: Rect,
@@ -101,17 +107,25 @@ impl<'a> SpriteRenderer<'a> {
.collect::<HashMap<_, _>>();
Self {
+ misc_textures: MiscTextures {
+ player: *metadata.get("player+a").unwrap(),
+ },
texture,
metadata,
sprites: vec![],
view_offset: Vec2::ZERO,
- view_scale: Vec2::splat(64.),
+ view_scale: Vec2::new(32., 24.) * 3.,
}
}
+ #[inline]
pub fn metadata(&self) -> &HashMap<String, Rect> {
&self.metadata
}
+ #[inline]
+ pub fn misc_textures(&self) -> &MiscTextures {
+ &self.misc_textures
+ }
pub fn draw(&mut self, z_order: i32, src: Rect, dst: FRect) {
self.sprites.push(SpriteDraw {
diff --git a/light-client/src/tilemap.rs b/light-client/src/tilemap.rs
index cd0f13ac..3464248d 100644
--- a/light-client/src/tilemap.rs
+++ b/light-client/src/tilemap.rs
@@ -1,3 +1,20 @@
+/*
+ Hurry Curry! - a game about cooking
+ Copyright 2024 metamuffin
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, version 3 of the License only.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
use crate::sprite_renderer::SpriteRenderer;
use hurrycurry_protocol::{glam::IVec2, TileIndex};
use sdl2::rect::{FRect, Rect};
@@ -71,7 +88,7 @@ impl Tilemap {
pub fn draw(&self, ctx: &mut SpriteRenderer) {
for &(src, dst) in self.tiles.values() {
- ctx.draw(((dst.y + dst.h) * 32.) as i32, src, dst);
+ ctx.draw(((dst.y + dst.h) * 24.) as i32, src, dst);
}
}
}