From 7c83fe7eeaac3736d5a59b270bbbbde59780e536 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 16 Jul 2024 16:21:37 +0200 Subject: item rendering and movement --- light-client/makefile | 8 +- light-client/src/game.rs | 155 +++++++++++++++++++++++++++++++----- light-client/src/main.rs | 18 ++++- light-client/src/network.rs | 50 ++++++------ light-client/src/sprite_renderer.rs | 48 +++++++++-- light-client/src/tilemap.rs | 2 +- server/protocol/src/lib.rs | 2 + server/protocol/src/movement.rs | 113 ++++++++++++++++++++++++++ server/src/customer/mod.rs | 6 +- server/src/customer/movement.rs | 108 ------------------------- server/src/customer/pathfinding.rs | 4 +- 11 files changed, 346 insertions(+), 168 deletions(-) create mode 100644 server/protocol/src/movement.rs delete mode 100644 server/src/customer/movement.rs diff --git a/light-client/makefile b/light-client/makefile index cb652999..6a56fc62 100644 --- a/light-client/makefile +++ b/light-client/makefile @@ -17,8 +17,14 @@ clean: rm -fr assets/sprites rm -f assets/atlas.ta assets/atlas.meta.csv + +IMPORT = ../target/release/tex_import +EXPORT = ../target/release/tex_export +PACK = ../target/release/tex_pack +COMPOSE = ../target/release/tex_compose + %.ta: %.import.png - ../target/release/tex_import $< $@ + $(IMPORT) $< $@ %.png: %.ta ../target/release/tex_export $< $@ diff --git a/light-client/src/game.rs b/light-client/src/game.rs index cf265344..b35a15d7 100644 --- a/light-client/src/game.rs +++ b/light-client/src/game.rs @@ -15,31 +15,47 @@ along with this program. If not, see . */ -use crate::{sprite_renderer::SpriteRenderer, tilemap::Tilemap}; +use crate::{ + sprite_renderer::{SpriteRect, SpriteRenderer}, + tilemap::Tilemap, +}; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, - PacketC, PlayerID, TileIndex, + movement::MovementBase, + ClientGamedata, ItemIndex, ItemLocation, PacketC, PacketS, PlayerID, TileIndex, }; use log::{info, warn}; -use sdl2::rect::FRect; -use std::collections::HashMap; +use sdl2::{ + keyboard::{KeyboardState, Scancode}, + rect::{FRect, Rect}, +}; +use std::collections::{HashMap, HashSet, VecDeque}; pub struct Game { + data: ClientGamedata, tiles: HashMap, tilemap: Tilemap, + collision_map: HashSet, players: HashMap, my_id: PlayerID, + + item_sprites: Vec, + movement_send_cooldown: f32, } pub struct Tile { kind: TileIndex, + item: Option, } pub struct Player { + movement: MovementBase, + item: Option, + name: String, character: i32, +} +pub struct Item { position: Vec2, - name: String, - boosting: bool, - rot: f32, + kind: ItemIndex, } impl Game { @@ -49,6 +65,10 @@ impl Game { players: HashMap::new(), tilemap: Tilemap::default(), my_id: PlayerID(0), + data: ClientGamedata::default(), + collision_map: HashSet::new(), + movement_send_cooldown: 0., + item_sprites: Vec::new(), } } @@ -57,6 +77,20 @@ impl Game { PacketC::Init { id } => self.my_id = id, PacketC::Data { data } => { self.tilemap.init(&data.tile_names, renderer.metadata()); + self.item_sprites = data + .item_names + .iter() + .map(|name| { + SpriteRect::new( + renderer + .metadata() + .get(&format!("{name}:a")) + .copied() + .unwrap_or(Rect::new(0, 0, 32, 24)), + ) + }) + .collect(); + self.data = data; } PacketC::UpdateMap { tile, @@ -64,9 +98,15 @@ impl Game { neighbors, } => { if let Some(kind) = kind { - self.tiles.insert(tile, Tile { kind }); + self.tiles.insert(tile, Tile { kind, item: None }); + if self.data.tile_collide[kind.0] { + self.collision_map.remove(&tile); + } else { + self.collision_map.insert(tile); + } } else { self.tiles.remove(&tile); + self.collision_map.remove(&tile); } self.tilemap.set(tile, kind, neighbors); } @@ -81,10 +121,16 @@ impl Game { id, Player { character, - position, name, - boosting: false, - rot: 0., + item: None, + movement: MovementBase { + position, + facing: Vec2::X, + rotation: 0., + velocity: Vec2::ZERO, + boosting: false, + stamina: 0., + }, }, ); } @@ -98,14 +144,21 @@ impl Game { rot, boosting, } => { - if let Some(p) = self.players.get_mut(&player) { - p.position = pos; - p.rot = rot; - p.boosting = boosting; + if player != self.my_id { + if let Some(p) = self.players.get_mut(&player) { + p.movement.position = pos; + p.movement.rotation = rot; + p.movement.boosting = boosting; + } } } - PacketC::MoveItem { from, to } => (), - PacketC::SetItem { location, item } => (), + PacketC::MoveItem { from, to } => *self.get_item(to) = self.get_item(from).take(), + PacketC::SetItem { location, item } => { + *self.get_item(location) = item.map(|kind| Item { + kind, + position: Vec2::ZERO, + }) + } PacketC::SetProgress { item, progress, @@ -132,18 +185,80 @@ impl Game { } } + pub fn get_item(&mut self, location: ItemLocation) -> &mut Option { + match location { + ItemLocation::Tile(pos) => &mut self.tiles.get_mut(&pos).unwrap().item, + ItemLocation::Player(pid) => &mut self.players.get_mut(&pid).unwrap().item, + } + } + + pub fn tick(&mut self, dt: f32, keyboard: &KeyboardState, packet_out: &mut VecDeque) { + let direction = IVec2::new( + keyboard.is_scancode_pressed(Scancode::D) as i32 + - keyboard.is_scancode_pressed(Scancode::A) as i32, + keyboard.is_scancode_pressed(Scancode::S) as i32 + - keyboard.is_scancode_pressed(Scancode::W) as i32, + ) + .as_vec2(); + let boost = keyboard.is_scancode_pressed(Scancode::K); + let interact = keyboard.is_scancode_pressed(Scancode::Space) + | keyboard.is_scancode_pressed(Scancode::J); + + self.movement_send_cooldown -= dt; + let send_movement = self.movement_send_cooldown < 0.; + if send_movement { + self.movement_send_cooldown += 0.04 + } + + for (pid, player) in &mut self.players { + if *pid == self.my_id { + let movement_packet = + player + .movement + .update(&self.collision_map, direction, boost, dt); + + if send_movement { + packet_out.push_back(movement_packet); + } + } + if let Some(item) = &mut player.item { + item.position = player.movement.position + } + } + for (pos, tile) in &mut self.tiles { + if let Some(item) = &mut tile.item { + item.position = pos.as_vec2() + 0.5 + } + } + } + pub fn draw(&self, ctx: &mut SpriteRenderer) { 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., + p.movement.position.x - src.width() as f32 / 32. / 2., + p.movement.position.y + 0.3 - 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); + ctx.draw(dst.y + dst.h + 1., src, dst); + if let Some(item) = &p.item { + item.draw(ctx, &self.item_sprites) + } + } + for tile in self.tiles.values() { + if let Some(item) = &tile.item { + item.draw(ctx, &self.item_sprites) + } } } } + +impl Item { + pub fn draw(&self, ctx: &mut SpriteRenderer, item_sprites: &[SpriteRect]) { + eprintln!("item {} at {}", self.kind.0, self.position); + item_sprites[self.kind.0].draw_at(ctx, self.position) + } +} diff --git a/light-client/src/main.rs b/light-client/src/main.rs index 8be38e4c..b00ba734 100644 --- a/light-client/src/main.rs +++ b/light-client/src/main.rs @@ -17,8 +17,13 @@ */ use game::Game; use network::Network; -use sdl2::{event::Event, keyboard::Keycode, pixels::Color}; +use sdl2::{ + event::Event, + keyboard::{KeyboardState, Keycode}, + pixels::Color, +}; use sprite_renderer::SpriteRenderer; +use std::time::{Duration, Instant}; pub mod game; pub mod network; @@ -55,6 +60,10 @@ fn main() { character: 0, }); + let mut events = sdl_context.event_pump().unwrap(); + + let mut last_tick = Instant::now(); + 'mainloop: loop { net.poll(); @@ -62,6 +71,11 @@ fn main() { game.packet_in(packet, &mut renderer); } + let keyboard = KeyboardState::new(&events); + let dt = last_tick.elapsed().min(Duration::from_secs_f32(1. / 30.)); + game.tick(dt.as_secs_f32(), &keyboard, &mut net.queue_out); + last_tick += dt; + game.draw(&mut renderer); canvas.set_draw_color(Color::BLACK); @@ -69,7 +83,7 @@ fn main() { renderer.submit(&mut canvas); canvas.present(); - for event in sdl_context.event_pump().unwrap().poll_iter() { + for event in events.poll_iter() { match event { Event::Quit { .. } | Event::KeyDown { diff --git a/light-client/src/network.rs b/light-client/src/network.rs index 47eb66ab..ed160773 100644 --- a/light-client/src/network.rs +++ b/light-client/src/network.rs @@ -75,37 +75,41 @@ 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(packet) => { - debug!("<- {packet:?}"); - Some(packet) - } - Err(e) => { - warn!("invalid json packet: {e:?}"); - None - } - }, - Ok(Message::Binary(packet)) => { - match bincode::decode_from_slice(&packet, BINCODE_CONFIG) { - Ok((packet, _)) => { + loop { + self.queue_in.extend(match self.sock.read() { + Ok(Message::Text(packet)) => match serde_json::from_str(&packet) { + Ok(packet) => { debug!("<- {packet:?}"); Some(packet) } Err(e) => { - warn!("invalid bincode packet: {e:?}"); + warn!("invalid json packet: {e:?}"); None } + }, + Ok(Message::Binary(packet)) => { + match bincode::decode_from_slice(&packet, BINCODE_CONFIG) { + Ok((packet, _)) => { + debug!("<- {packet:?}"); + Some(packet) + } + Err(e) => { + warn!("invalid bincode packet: {e:?}"); + None + } + } } - } - Ok(_) => None, - Err(e) => { - if let Some(e) = e.into_non_blocking() { - warn!("{e:?}"); + Ok(_) => None, + Err(e) => { + if let Some(e) = e.into_non_blocking() { + warn!("{e:?}"); + None + } else { + break; + } } - None - } - }); + }); + } for packet in self.queue_out.drain(..) { debug!("-> {packet:?}"); diff --git a/light-client/src/sprite_renderer.rs b/light-client/src/sprite_renderer.rs index 187ccc7b..aadbfbe3 100644 --- a/light-client/src/sprite_renderer.rs +++ b/light-client/src/sprite_renderer.rs @@ -46,6 +46,12 @@ pub struct SpriteDraw { dst: FRect, } +pub struct SpriteRect { + z_offset: f32, + src: Rect, + relative_dst: FRect, +} + impl<'a> SpriteRenderer<'a> { pub fn init(texture_creator: &'a TextureCreator) -> Self { let palette = include_str!("../assets/palette.csv") @@ -114,7 +120,7 @@ impl<'a> SpriteRenderer<'a> { metadata, sprites: vec![], view_offset: Vec2::ZERO, - view_scale: Vec2::new(32., 24.) * 3., + view_scale: Vec2::splat(3.), } } @@ -127,15 +133,15 @@ impl<'a> SpriteRenderer<'a> { &self.misc_textures } - pub fn draw(&mut self, z_order: i32, src: Rect, dst: FRect) { + pub fn draw(&mut self, z_order: f32, src: Rect, dst: FRect) { self.sprites.push(SpriteDraw { - z_order, + z_order: (z_order * 24.) as i32, src, dst: FRect::new( - (dst.x + self.view_offset.x) * self.view_scale.x, - (dst.y + self.view_offset.y) * self.view_scale.y, - dst.w * self.view_scale.x, - dst.h * self.view_scale.y, + ((dst.x + self.view_offset.x) * 32.).round() * self.view_scale.x, + ((dst.y + self.view_offset.y) * 24.).round() * self.view_scale.y, + (dst.w * 32.).round() * self.view_scale.x, + (dst.h * 24.).round() * self.view_scale.y, ), }) } @@ -164,3 +170,31 @@ impl PartialEq for SpriteDraw { self.z_order == other.z_order && self.src == other.src && self.dst == other.dst } } + +impl SpriteRect { + pub fn draw_at(&self, ctx: &mut SpriteRenderer, pos: Vec2) { + ctx.draw( + self.z_offset + pos.y + 5., + self.src, + FRect::new( + self.relative_dst.x + pos.x, + self.relative_dst.y + pos.y, + self.relative_dst.w, + self.relative_dst.h, + ), + ) + } + pub fn new(src: Rect) -> Self { + let relative_dst = FRect::new( + 0.0 - src.width() as f32 / 32. / 2., + 0.3 - src.height() as f32 / 24., + src.width() as f32 / 32., + src.height() as f32 / 24., + ); + Self { + z_offset: -relative_dst.h, + relative_dst, + src, + } + } +} diff --git a/light-client/src/tilemap.rs b/light-client/src/tilemap.rs index 3464248d..e84db8b1 100644 --- a/light-client/src/tilemap.rs +++ b/light-client/src/tilemap.rs @@ -88,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) * 24.) as i32, src, dst); + ctx.draw(dst.y + dst.h, src, dst); } } } diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 895af376..f2a6dd27 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -28,6 +28,8 @@ use std::{ pub use glam; +pub mod movement; + pub const VERSION: (u32, u32) = (1, 0); pub const BINCODE_CONFIG: Configuration> = diff --git a/server/protocol/src/movement.rs b/server/protocol/src/movement.rs new file mode 100644 index 00000000..486da816 --- /dev/null +++ b/server/protocol/src/movement.rs @@ -0,0 +1,113 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + Copyright 2024 tpart + + 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 . + +*/ +use crate::{ + glam::{IVec2, Vec2}, + PacketS, +}; +use std::collections::HashSet; + +const PLAYER_SIZE: f32 = 0.4; +const PLAYER_FRICTION: f32 = 10.0; +const PLAYER_SPEED: f32 = 55.0; +const BOOST_FACTOR: f32 = 2.5; +const BOOST_DURATION: f32 = 0.3; +const BOOST_RESTORE: f32 = 0.5; + +pub struct MovementBase { + pub position: Vec2, + pub facing: Vec2, + pub rotation: f32, + pub velocity: Vec2, + pub boosting: bool, + pub stamina: f32, +} + +impl MovementBase { + pub fn new(position: Vec2) -> Self { + Self { + position, + facing: Vec2::X, + velocity: Vec2::ZERO, + boosting: false, + stamina: 0., + rotation: 0., + } + } + pub fn update( + &mut self, + map: &HashSet, + direction: Vec2, + mut boost: bool, + dt: f32, + ) -> PacketS { + let direction = direction.clamp_length_max(1.); + if direction.length() > 0.1 { + self.facing = direction + (self.facing - direction) * (-dt * 10.).exp(); + } + self.rotation = self.facing.x.atan2(self.facing.y); + boost &= direction.length() > 0.1; + self.boosting = boost && (self.boosting || self.stamina >= 1.) && self.stamina > 0.; + self.stamina += if self.boosting { + -dt / BOOST_DURATION + } else { + dt / BOOST_RESTORE + }; + self.stamina = self.stamina.max(0.).min(1.); + let speed = PLAYER_SPEED * if self.boosting { BOOST_FACTOR } else { 1. }; + self.velocity += direction * dt * speed; + self.position += self.velocity * dt; + self.velocity = self.velocity * (-dt * PLAYER_FRICTION).exp(); + collide_player(self, map); + + PacketS::Position { + pos: self.position, + boosting: self.boosting, + rot: self.rotation, + } + } +} + +pub fn collide_player(p: &mut MovementBase, map: &HashSet) { + for xo in -1..=1 { + for yo in -1..=1 { + let tile = IVec2::new(xo, yo) + p.position.as_ivec2(); + if map.contains(&tile) { + continue; + } + let tile = tile.as_vec2(); + let d = aabb_point_distance(tile, tile + Vec2::ONE, p.position); + if d > PLAYER_SIZE { + continue; + } + let h = 0.01; + let d_sample_x = + aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(h, 0.)); + let d_sample_y = + aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(0., h)); + let grad = (Vec2::new(d_sample_x, d_sample_y) - d) / h; + + p.position += (PLAYER_SIZE - d) * grad; + p.velocity -= grad * grad.dot(p.velocity); + } + } +} + +pub fn aabb_point_distance(min: Vec2, max: Vec2, p: Vec2) -> f32 { + (p - p.clamp(min, max)).length() +} diff --git a/server/src/customer/mod.rs b/server/src/customer/mod.rs index aeaa7ae1..bf385927 100644 --- a/server/src/customer/mod.rs +++ b/server/src/customer/mod.rs @@ -15,15 +15,15 @@ along with this program. If not, see . */ -pub mod movement; mod pathfinding; use crate::{data::Gamedata, game::Tile}; use anyhow::{anyhow, Result}; use fake::{faker, Fake}; -use hurrycurry_protocol::{glam::IVec2, DemandIndex, Message, PacketS, PlayerID}; +use hurrycurry_protocol::{ + glam::IVec2, movement::MovementBase, DemandIndex, Message, PacketS, PlayerID, +}; use log::info; -use movement::MovementBase; use pathfinding::{find_path, Path}; use rand::{random, thread_rng}; use std::{ diff --git a/server/src/customer/movement.rs b/server/src/customer/movement.rs deleted file mode 100644 index 34ed5b16..00000000 --- a/server/src/customer/movement.rs +++ /dev/null @@ -1,108 +0,0 @@ -/* - Hurry Curry! - a game about cooking - Copyright 2024 metamuffin - Copyright 2024 tpart - - 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 . - -*/ -use hurrycurry_protocol::{glam::{IVec2, Vec2}, PacketS}; -use std::collections::HashSet; - -const PLAYER_SIZE: f32 = 0.4; -const PLAYER_FRICTION: f32 = 10.0; -const PLAYER_SPEED: f32 = 55.0; -const BOOST_FACTOR: f32 = 2.5; -const BOOST_DURATION: f32 = 0.3; -const BOOST_RESTORE: f32 = 0.5; - -pub struct MovementBase { - pub position: Vec2, - pub facing: Vec2, - pub vel: Vec2, - pub boosting: bool, - pub stamina: f32, -} - -impl MovementBase { - pub fn new(position: Vec2) -> Self { - Self { - position, - facing: Vec2::X, - vel: Vec2::ZERO, - boosting: false, - stamina: 0., - } - } - pub fn update( - &mut self, - map: &HashSet, - direction: Vec2, - mut boost: bool, - dt: f32, - ) -> PacketS { - let direction = direction.clamp_length_max(1.); - if direction.length() > 0.1 { - self.facing = direction + (self.facing - direction) * (-dt * 10.).exp(); - } - let rot = self.facing.x.atan2(self.facing.y); - boost &= direction.length() > 0.1; - self.boosting = boost && (self.boosting || self.stamina >= 1.) && self.stamina > 0.; - self.stamina += if self.boosting { - -dt / BOOST_DURATION - } else { - dt / BOOST_RESTORE - }; - self.stamina = self.stamina.max(0.).min(1.); - let speed = PLAYER_SPEED * if self.boosting { BOOST_FACTOR } else { 1. }; - self.vel += direction * dt * speed; - self.position += self.vel * dt; - self.vel = self.vel * (-dt * PLAYER_FRICTION).exp(); - collide_player(self, map); - - PacketS::Position { - pos: self.position, - boosting: false, - rot, - } - } -} - -pub fn collide_player(p: &mut MovementBase, map: &HashSet) { - for xo in -1..=1 { - for yo in -1..=1 { - let tile = IVec2::new(xo, yo) + p.position.as_ivec2(); - if map.contains(&tile) { - continue; - } - let tile = tile.as_vec2(); - let d = aabb_point_distance(tile, tile + Vec2::ONE, p.position); - if d > PLAYER_SIZE { - continue; - } - let h = 0.01; - let d_sample_x = - aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(h, 0.)); - let d_sample_y = - aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(0., h)); - let grad = (Vec2::new(d_sample_x, d_sample_y) - d) / h; - - p.position += (PLAYER_SIZE - d) * grad; - p.vel -= grad * grad.dot(p.vel); - } - } -} - -pub fn aabb_point_distance(min: Vec2, max: Vec2, p: Vec2) -> f32 { - (p - p.clamp(min, max)).length() -} diff --git a/server/src/customer/pathfinding.rs b/server/src/customer/pathfinding.rs index 2b9f98f4..d1e1e997 100644 --- a/server/src/customer/pathfinding.rs +++ b/server/src/customer/pathfinding.rs @@ -15,10 +15,8 @@ along with this program. If not, see . */ -use super::movement::MovementBase; use hurrycurry_protocol::{ - glam::{IVec2, Vec2}, - PacketS, + glam::{IVec2, Vec2}, movement::MovementBase, PacketS }; use log::trace; use std::{ -- cgit v1.2.3-70-g09d2