diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-16 12:10:12 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-16 12:10:12 +0200 |
commit | 5cad8fad7c8e8b4358c9b0290c228ac9fac164f5 (patch) | |
tree | f70efa6135f2ae2bca1b228b85fdea6af6cd2812 /light-client | |
parent | 92ebc438268851cd9abb7af7d4938c10bd6cf80a (diff) | |
download | hurrycurry-5cad8fad7c8e8b4358c9b0290c228ac9fac164f5.tar hurrycurry-5cad8fad7c8e8b4358c9b0290c228ac9fac164f5.tar.bz2 hurrycurry-5cad8fad7c8e8b4358c9b0290c228ac9fac164f5.tar.zst |
render players
Diffstat (limited to 'light-client')
-rw-r--r-- | light-client/Cargo.toml | 1 | ||||
-rw-r--r-- | light-client/assets/misc.ini | 2 | ||||
-rw-r--r-- | light-client/makefile | 14 | ||||
-rw-r--r-- | light-client/src/game.rs | 79 | ||||
-rw-r--r-- | light-client/src/main.rs | 9 | ||||
-rw-r--r-- | light-client/src/network.rs | 20 | ||||
-rw-r--r-- | light-client/src/sprite_renderer.rs | 16 | ||||
-rw-r--r-- | light-client/src/tilemap.rs | 19 | ||||
-rw-r--r-- | light-client/tools/src/bin/tex_pack.rs | 2 |
9 files changed, 144 insertions, 18 deletions
diff --git a/light-client/Cargo.toml b/light-client/Cargo.toml index ec8b75ec..d7cb336c 100644 --- a/light-client/Cargo.toml +++ b/light-client/Cargo.toml @@ -12,3 +12,4 @@ bincode = "2.0.0-rc.3" log = "0.4.22" env_logger = "0.11.3" anyhow = "1.0.86" +clap = { version = "4.5.9", features = ["derive"] } diff --git a/light-client/assets/misc.ini b/light-client/assets/misc.ini new file mode 100644 index 00000000..07052b90 --- /dev/null +++ b/light-client/assets/misc.ini @@ -0,0 +1,2 @@ + +player=player diff --git a/light-client/makefile b/light-client/makefile index 4f900412..cb652999 100644 --- a/light-client/makefile +++ b/light-client/makefile @@ -14,7 +14,7 @@ tex_import: $(TEXTURES_IMPORT_PNG) tex_export: $(TEXTURES_PNG) $(SPRITES_PNG) assets/atlas.png clean: rm -f $(PNG) - rm -f $(SPRITES) + rm -fr assets/sprites rm -f assets/atlas.ta assets/atlas.meta.csv %.ta: %.import.png @@ -22,12 +22,10 @@ clean: %.png: %.ta ../target/release/tex_export $< $@ -assets/sprites/items/all: assets/items.ini $(TEXTURES) - @mkdir -p assets/sprites/items - ../target/release/tex_compose $< assets/textures assets/sprites/items -assets/sprites/tiles/all: assets/tiles.ini $(TEXTURES) - @mkdir -p assets/sprites/tiles - ../target/release/tex_compose $< assets/textures assets/sprites/tiles +assets/sprites/%/all: assets/%.ini $(TEXTURES) + @mkdir -p $(basename $@) + ../target/release/tex_compose $< assets/textures $(basename $@) + @touch $@ -assets/atlas.ta assets/atlas.meta.csv: assets/sprites/items/all assets/sprites/tiles/all +assets/atlas.ta assets/atlas.meta.csv: assets/sprites/items/all assets/sprites/tiles/all assets/sprites/misc/all ../target/release/tex_pack assets/atlas.ta assets/atlas.meta.csv $(SPRITES) 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); } } } diff --git a/light-client/tools/src/bin/tex_pack.rs b/light-client/tools/src/bin/tex_pack.rs index e6d73ab3..1258b5bf 100644 --- a/light-client/tools/src/bin/tex_pack.rs +++ b/light-client/tools/src/bin/tex_pack.rs @@ -60,7 +60,7 @@ fn main() { cursor_x = 0; } if cursor_y + height > atlas_size { - panic!("texture too big or atlas full") + panic!("texture too big or atlas full"); } row_height = row_height.max(atlas_size); let texcoord = [cursor_x, cursor_y, width, height]; |