Redesign the scaling of the game for depth to feel more meaningful

This commit is contained in:
crumblingstatue 2023-04-04 23:29:32 +02:00
parent d6f3eb07f2
commit 6c99beb47d
5 changed files with 42 additions and 17 deletions

View file

@ -13,7 +13,7 @@ use crate::{
game::{for_each_tile_on_screen, Biome, GameState},
graphics::{self, NATIVE_RESOLUTION},
input::Input,
math::{px_per_frame_to_km_h, wp_to_tp, WorldPos, TILE_SIZE},
math::{px_per_frame_to_km_h, wp_to_tp, WorldPos, M_PER_PX, TILE_SIZE},
res::Res,
world::Tile,
};
@ -239,15 +239,14 @@ fn debug_panel_ui(
ui.add(egui::DragValue::new(&mut game.camera_offset.x));
ui.label("Cam y");
ui.add(egui::DragValue::new(&mut game.camera_offset.y));
let tp = game.camera_offset.tile_pos();
imm_dbg!(tp);
let co = game.camera_offset;
ui.label(format!(
"Cam Depth: {}",
LengthDisp(tp.y as i64 - wp_to_tp(WorldPos::SURFACE) as i64)
LengthDisp(co.y as f32 - WorldPos::SURFACE as f32)
));
ui.label(format!(
"Cam offset from center: {}",
LengthDisp(tp.x as i64 - wp_to_tp(WorldPos::CENTER) as i64)
LengthDisp(co.x as f32 - WorldPos::CENTER as f32)
));
} else {
ui.label("Player x");
@ -258,11 +257,11 @@ fn debug_panel_ui(
imm_dbg!(tp);
ui.label(format!(
"Player Depth: {}",
LengthDisp(tp.y as i64 - wp_to_tp(WorldPos::SURFACE) as i64)
LengthDisp(game.player.feet_y() as f32 - WorldPos::SURFACE as f32)
));
ui.label(format!(
"Player offset from center: {}",
LengthDisp(tp.x as i64 - wp_to_tp(WorldPos::CENTER) as i64)
LengthDisp(game.player.col_en.en.pos.x as f32 - WorldPos::CENTER as f32)
));
ui.label(format!(
"Hspeed: {} ({} km/h)",
@ -296,12 +295,17 @@ fn debug_panel_ui(
});
}
struct LengthDisp(i64);
struct LengthDisp(f32);
impl fmt::Display for LengthDisp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let km = self.0 / 1000;
let m = self.0 % 1000;
write!(f, "{km} km, {m} m")
let meters = self.0 * M_PER_PX;
if meters.abs() > 1000. {
let km = (meters / 1000.).floor();
let m = meters % 1000.;
write!(f, "{km} km, {m} m")
} else {
write!(f, "{meters} m")
}
}
}

View file

@ -1,6 +1,8 @@
mod player;
use sfml::graphics::{Rect, RectangleShape, RenderTarget, RenderWindow, Sprite, Transformable};
use sfml::graphics::{
Color, Rect, RectangleShape, RenderTarget, RenderWindow, Shape, Sprite, Transformable,
};
use crate::{
graphics::{ScreenPos, ScreenPosScalar, NATIVE_RESOLUTION},
@ -56,6 +58,13 @@ impl GameState {
));
rs.set_size((w as f32, h as f32));
rw.draw(&rs);
rs.set_size((2., 2.));
rs.set_fill_color(Color::RED);
rs.set_position((
(self.player.col_en.en.pos.x - self.camera_offset.x as i32) as f32,
(self.player.col_en.en.pos.y - self.camera_offset.y as i32) as f32,
));
rw.draw(&rs);
}
}
@ -79,7 +88,7 @@ pub fn for_each_tile_on_screen(camera_offset: WorldPos, mut f: impl FnMut(TilePo
impl Default for GameState {
fn default() -> Self {
let mut spawn_point = WorldPos::SURFACE_CENTER;
spawn_point.y -= 300;
spawn_point.y -= 304;
Self {
camera_offset: spawn_point,
world: Default::default(),

View file

@ -15,7 +15,7 @@ pub struct Player {
impl Player {
pub fn new_at(pos: WorldPos) -> Self {
Self {
col_en: MobileEntity::from_pos_and_bb(vec2(pos.x as i32, pos.y as i32), vec2(15, 24)),
col_en: MobileEntity::from_pos_and_bb(vec2(pos.x as i32, pos.y as i32), vec2(20, 46)),
vspeed: 0.0,
hspeed: 0.0,
jumps_left: 0,
@ -30,4 +30,7 @@ impl Player {
pub fn can_jump(&self) -> bool {
self.jumps_left > 0
}
pub fn feet_y(&self) -> i32 {
self.col_en.en.pos.y + self.col_en.en.bb.y
}
}

View file

@ -8,13 +8,16 @@ pub struct WorldPos {
pub y: WorldPosScalar,
}
/// Tile size in pixels
pub const TILE_SIZE: u8 = 32;
/// Pixels per meter. One meter = one tile, so this is the same as `TILE_SIZE`.
pub const PX_PER_M: u8 = TILE_SIZE;
/// Pixels per meter.
pub const PX_PER_M: f32 = TILE_SIZE as f32 * 2.;
/// Meters per pixel
pub const M_PER_PX: f32 = 1. / PX_PER_M;
pub const FPS_TARGET: u8 = 60;
pub fn px_per_frame_to_m_per_s(px_per_frame: f32) -> f32 {
let m_per_frame = px_per_frame / PX_PER_M as f32;
let m_per_frame = px_per_frame / PX_PER_M;
m_per_frame * FPS_TARGET as f32
}