mirror of
https://github.com/Noratrieb/game-wip-dontplay.git
synced 2026-01-14 19:55:02 +01:00
Move the player inside World
This commit is contained in:
parent
4fd826c218
commit
390a4d45d2
6 changed files with 54 additions and 46 deletions
58
src/app.rs
58
src/app.rs
|
|
@ -110,21 +110,22 @@ impl App {
|
|||
} else {
|
||||
3.0
|
||||
};
|
||||
self.game.player.hspeed = 0.;
|
||||
self.game.world.player.hspeed = 0.;
|
||||
if self.input.down(Key::A) {
|
||||
self.game.player.hspeed = -spd;
|
||||
self.game.world.player.hspeed = -spd;
|
||||
}
|
||||
if self.input.down(Key::D) {
|
||||
self.game.player.hspeed = spd;
|
||||
self.game.world.player.hspeed = spd;
|
||||
}
|
||||
if self.input.down(Key::W) && self.game.player.can_jump() {
|
||||
self.game.player.vspeed = -10.0;
|
||||
self.game.player.jumps_left = 0;
|
||||
if self.input.down(Key::W) && self.game.world.player.can_jump() {
|
||||
self.game.world.player.vspeed = -10.0;
|
||||
self.game.world.player.jumps_left = 0;
|
||||
}
|
||||
self.game.player.down_intent = self.input.down(Key::S);
|
||||
self.game.world.player.down_intent = self.input.down(Key::S);
|
||||
let terminal_velocity = 60.0;
|
||||
self.game.player.vspeed = self
|
||||
self.game.world.player.vspeed = self
|
||||
.game
|
||||
.world
|
||||
.player
|
||||
.vspeed
|
||||
.clamp(-terminal_velocity, terminal_velocity);
|
||||
|
|
@ -149,37 +150,37 @@ impl App {
|
|||
});
|
||||
});
|
||||
imm_dbg!(on_screen_tile_ents.len());
|
||||
self.game
|
||||
.player
|
||||
.col_en
|
||||
.move_y(self.game.player.vspeed, |player_en, off| {
|
||||
self.game.world.player.col_en.move_y(
|
||||
self.game.world.player.vspeed,
|
||||
|player_en, off| {
|
||||
let mut col = false;
|
||||
for en in &on_screen_tile_ents {
|
||||
if player_en.would_collide(&en.col, off) {
|
||||
if en.platform {
|
||||
if self.game.player.vspeed < 0. {
|
||||
if self.game.world.player.vspeed < 0. {
|
||||
continue;
|
||||
}
|
||||
// If the player's feet are below the top of the platform,
|
||||
// collision shouldn't happen
|
||||
let player_feet = player_en.pos.y + player_en.bb.y;
|
||||
if player_feet > en.col.pos.y || self.game.player.down_intent {
|
||||
if player_feet > en.col.pos.y || self.game.world.player.down_intent
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
col = true;
|
||||
if self.game.player.vspeed > 0. {
|
||||
self.game.player.jumps_left = 1;
|
||||
if self.game.world.player.vspeed > 0. {
|
||||
self.game.world.player.jumps_left = 1;
|
||||
}
|
||||
self.game.player.vspeed = 0.;
|
||||
self.game.world.player.vspeed = 0.;
|
||||
}
|
||||
}
|
||||
col
|
||||
});
|
||||
self.game
|
||||
.player
|
||||
.col_en
|
||||
.move_x(self.game.player.hspeed, |player_en, off| {
|
||||
},
|
||||
);
|
||||
self.game.world.player.col_en.move_x(
|
||||
self.game.world.player.hspeed,
|
||||
|player_en, off| {
|
||||
let mut col = false;
|
||||
for en in &on_screen_tile_ents {
|
||||
if en.platform {
|
||||
|
|
@ -187,13 +188,14 @@ impl App {
|
|||
}
|
||||
if player_en.would_collide(&en.col, off) {
|
||||
col = true;
|
||||
self.game.player.hspeed = 0.;
|
||||
self.game.world.player.hspeed = 0.;
|
||||
}
|
||||
}
|
||||
col
|
||||
});
|
||||
self.game.player.vspeed += self.game.gravity;
|
||||
let (x, y, _w, _h) = self.game.player.col_en.en.xywh();
|
||||
},
|
||||
);
|
||||
self.game.world.player.vspeed += self.game.gravity;
|
||||
let (x, y, _w, _h) = self.game.world.player.col_en.en.xywh();
|
||||
self.game.camera_offset.x = (x - rt_size.x as i32 / 2).try_into().unwrap_or(0);
|
||||
self.game.camera_offset.y = (y - rt_size.y as i32 / 2).try_into().unwrap_or(0);
|
||||
}
|
||||
|
|
@ -219,8 +221,8 @@ impl App {
|
|||
mouse_tpos.y / CHUNK_EXTENT as TilePosScalar
|
||||
);
|
||||
if self.debug.freecam && self.input.pressed(Key::P) {
|
||||
self.game.player.col_en.en.pos.x = wpos.x as i32;
|
||||
self.game.player.col_en.en.pos.y = wpos.y as i32;
|
||||
self.game.world.player.col_en.en.pos.x = wpos.x as i32;
|
||||
self.game.world.player.col_en.en.pos.y = wpos.y as i32;
|
||||
}
|
||||
if self.input.lmb_down {
|
||||
let t = self.game.world.tile_at_mut(mouse_tpos, &self.game.worldgen);
|
||||
|
|
|
|||
12
src/debug.rs
12
src/debug.rs
|
|
@ -53,21 +53,21 @@ fn debug_panel_ui(
|
|||
} else {
|
||||
ui.label(format!(
|
||||
"Player Depth: {}",
|
||||
LengthDisp(game.player.feet_y() as f32 - WorldPos::SURFACE as f32)
|
||||
LengthDisp(game.world.player.feet_y() as f32 - WorldPos::SURFACE as f32)
|
||||
));
|
||||
ui.label(format!(
|
||||
"Player offset from center: {}",
|
||||
LengthDisp(game.player.col_en.en.pos.x as f32 - WorldPos::CENTER as f32)
|
||||
LengthDisp(game.world.player.col_en.en.pos.x as f32 - WorldPos::CENTER as f32)
|
||||
));
|
||||
ui.label(format!(
|
||||
"Hspeed: {} ({} km/h)",
|
||||
game.player.hspeed,
|
||||
px_per_frame_to_km_h(game.player.hspeed)
|
||||
game.world.player.hspeed,
|
||||
px_per_frame_to_km_h(game.world.player.hspeed)
|
||||
));
|
||||
ui.label(format!(
|
||||
"Vspeed: {} ({} km/h)",
|
||||
game.player.vspeed,
|
||||
px_per_frame_to_km_h(game.player.vspeed)
|
||||
game.world.player.vspeed,
|
||||
px_per_frame_to_km_h(game.world.player.vspeed)
|
||||
));
|
||||
}
|
||||
ui.label("Music volume");
|
||||
|
|
|
|||
14
src/game.rs
14
src/game.rs
|
|
@ -1,5 +1,3 @@
|
|||
mod player;
|
||||
|
||||
use derivative::Derivative;
|
||||
use egui_inspect::derive::Inspect;
|
||||
use sfml::{
|
||||
|
|
@ -16,14 +14,11 @@ use crate::{
|
|||
worldgen::Worldgen,
|
||||
};
|
||||
|
||||
use self::player::Player;
|
||||
|
||||
#[derive(Derivative, Inspect)]
|
||||
#[derivative(Debug)]
|
||||
pub struct GameState {
|
||||
pub camera_offset: WorldPos,
|
||||
pub world: World,
|
||||
pub player: Player,
|
||||
pub gravity: f32,
|
||||
pub tile_to_place: TileId,
|
||||
pub current_biome: Biome,
|
||||
|
|
@ -79,7 +74,7 @@ impl GameState {
|
|||
});
|
||||
}
|
||||
pub fn draw_entities(&mut self, rw: &mut RenderTexture) {
|
||||
let (x, y, w, h) = self.player.col_en.en.xywh();
|
||||
let (x, y, w, h) = self.world.player.col_en.en.xywh();
|
||||
let mut rect_sh = RectangleShape::new();
|
||||
rect_sh.set_position((
|
||||
(x - self.camera_offset.x as i32) as f32,
|
||||
|
|
@ -90,8 +85,8 @@ impl GameState {
|
|||
rect_sh.set_size((2., 2.));
|
||||
rect_sh.set_fill_color(Color::RED);
|
||||
rect_sh.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,
|
||||
(self.world.player.col_en.en.pos.x - self.camera_offset.x as i32) as f32,
|
||||
(self.world.player.col_en.en.pos.y - self.camera_offset.y as i32) as f32,
|
||||
));
|
||||
rw.draw(&rect_sh);
|
||||
}
|
||||
|
|
@ -147,8 +142,7 @@ impl Default for GameState {
|
|||
spawn_point.y -= 1104;
|
||||
Self {
|
||||
camera_offset: spawn_point,
|
||||
world: Default::default(),
|
||||
player: Player::new_at(spawn_point),
|
||||
world: World::new(spawn_point),
|
||||
gravity: 0.55,
|
||||
tile_to_place: 1,
|
||||
current_biome: Biome::Surface,
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ mod game;
|
|||
mod graphics;
|
||||
mod input;
|
||||
mod math;
|
||||
mod player;
|
||||
mod res;
|
||||
mod stringfmt;
|
||||
mod tiles;
|
||||
|
|
|
|||
15
src/world.rs
15
src/world.rs
|
|
@ -1,7 +1,7 @@
|
|||
use egui_inspect::derive::Inspect;
|
||||
use fnv::FnvHashMap;
|
||||
|
||||
use crate::worldgen::Worldgen;
|
||||
use crate::{math::WorldPos, player::Player, worldgen::Worldgen};
|
||||
|
||||
pub type ChunkPosScalar = u16;
|
||||
|
||||
|
|
@ -11,13 +11,24 @@ pub struct ChunkPos {
|
|||
pub y: ChunkPosScalar,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Inspect)]
|
||||
#[derive(Debug, Inspect)]
|
||||
pub struct World {
|
||||
/// The currently loaded chunks
|
||||
chunks: FnvHashMap<ChunkPos, Chunk>,
|
||||
/// This is the number of ticks since the world has started.
|
||||
/// In other words, the age of the world.
|
||||
pub ticks: u64,
|
||||
pub player: Player,
|
||||
}
|
||||
|
||||
impl World {
|
||||
pub fn new(spawn_point: WorldPos) -> Self {
|
||||
Self {
|
||||
chunks: Default::default(),
|
||||
ticks: Default::default(),
|
||||
player: Player::new_at(spawn_point),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl World {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue