commit a188dd691242f4b0a19819e04ba7aaa2ff5c470d
parent 9482b3c5ec978eece61d94d6cf7d1ed581e89679
Author: MTRNord <mtrnord1@gmail.com>
Date: Thu, 1 Apr 2021 00:19:47 +0200
Make random level seeds and scale mao correct
Took 40 minutes
Diffstat:
4 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -3917,6 +3917,7 @@ dependencies = [
"bevy_ldtk",
"bevy_prototype_inline_assets",
"noise",
+ "rand 0.8.3",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
@@ -12,6 +12,7 @@ bevy_discovery = "0.1.0"
#kurinji = "1.0.5"
bevy_ldtk = {version = "0.4.2", features = ["bevy-unstable"]}
noise = "0.7.0"
+rand = "0.8.3"
[dependencies.bevy]
git = "https://github.com/bevyengine/bevy"
diff --git a/src/plugins/player.rs b/src/plugins/player.rs
@@ -1,7 +1,7 @@
use crate::entities::common::Health;
use crate::entities::markers::{Movable, Player, Wall};
use crate::entities::player::{PlayerBundle, PlayerName, PlayerXp};
-use crate::plugins::world::{GridLocation, SPRITE_HEIGHT, SPRITE_WIDTH};
+use crate::plugins::world::{GridLocation, MainCamera, SPRITE_HEIGHT, SPRITE_WIDTH};
use crate::GameState;
use bevy::prelude::*;
use std::collections::HashMap;
@@ -80,6 +80,7 @@ fn player_movement(
Query<(Entity, &Movable, &mut GridLocation)>,
Query<(Entity, &PlayerBundle, &GridLocation)>,
)>,
+ mut camera: Query<&mut GridLocation, With<MainCamera>>,
) {
let _shift = keyboard_input.pressed(KeyCode::LShift) || keyboard_input.pressed(KeyCode::RShift);
let _ctrl =
@@ -152,8 +153,10 @@ fn player_movement(
to_move.append(&mut tmp_to_move);
}
+ let mut camera_grid_location = camera.iter_mut().next().unwrap();
for loc in to_move {
let mut grid_location: Mut<GridLocation> = set.q0_mut().get_component_mut(loc).unwrap();
*grid_location = *grid_location + delta;
+ *camera_grid_location = *camera_grid_location + delta;
}
}
diff --git a/src/plugins/world.rs b/src/plugins/world.rs
@@ -1,7 +1,8 @@
use crate::entities::markers::Wall;
use crate::GameState;
use bevy::prelude::*;
-use noise::{Abs, NoiseFn, OpenSimplex};
+use noise::{NoiseFn, OpenSimplex, Seedable};
+use rand::Rng;
use std::ops;
pub struct MainCamera;
@@ -15,6 +16,7 @@ pub struct WorldState {
pub world: Option<Entity>,
pub collisions: Vec<Vec2>,
pub world_noise: OpenSimplex,
+ pub seed: Option<u32>,
}
pub struct WorldPlugin;
@@ -113,8 +115,14 @@ fn draw(
return;
}
- game_state.world_state.world_noise = OpenSimplex::new();
- let noise = Abs::new(&game_state.world_state.world_noise);
+ if game_state.world_state.seed.is_none() {
+ let mut rng = rand::thread_rng();
+ game_state.world_state.seed = Some(rng.gen());
+ }
+
+ game_state.world_state.world_noise =
+ OpenSimplex::new().set_seed(game_state.world_state.seed.unwrap());
+ let noise = &game_state.world_state.world_noise;
for chunk_y in -2..2 {
for chunk_x in -2..2 {
@@ -123,15 +131,21 @@ fn draw(
let full_x = (chunk_x * 16) + x;
let full_y = (chunk_y * 16) + y;
let coord = GridLocation(full_x, full_y);
+ /*
+ // Get noise value:
+ f = noise1d (x/width);
+ h = f * height + (height/2); // scale and center at screen-center
+ */
if full_x == 0 && full_y == 0 {
continue;
}
- let noise_value = noise.get(Into::<[f64; 2]>::into(coord));
- //println!("X: {}, Y: {}, Noise: {}", x, y, noise_value);
+ let f =
+ noise.get([(full_x as f32 / 16.0) as f64, (full_y as f32 / 16.0) as f64]);
+ let noise_value = f * 16.0 + (16.0 / 2.0);
// scale and center at screen-center
// TODO see https://stackoverflow.com/a/10225718
- if noise_value < 0.2 {
+ if noise_value > 3.0 {
setup_wall(coord, commands, &mut materials);
}
}