Just a really bad terrain generator

This commit is contained in:
Gender Shrapnel 2022-08-11 05:11:22 +02:00
parent d31c73b005
commit d38e41de29
Signed by: modzero
GPG Key ID: 4E11A06C6D1E5213
5 changed files with 144 additions and 42 deletions

43
Cargo.lock generated
View File

@ -2004,6 +2004,7 @@ version = "0.1.0"
dependencies = [
"bevy",
"bevy_ecs_tilemap",
"rand",
]
[[package]]
@ -2418,6 +2419,12 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-crate"
version = "1.1.3"
@ -2458,6 +2465,36 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "range-alloc"
version = "0.1.2"
@ -3009,7 +3046,7 @@ dependencies = [
"js-sys",
"log",
"naga",
"parking_lot 0.11.2",
"parking_lot 0.12.1",
"raw-window-handle",
"smallvec",
"wasm-bindgen",
@ -3035,7 +3072,7 @@ dependencies = [
"fxhash",
"log",
"naga",
"parking_lot 0.11.2",
"parking_lot 0.12.1",
"profiling",
"raw-window-handle",
"smallvec",
@ -3072,7 +3109,7 @@ dependencies = [
"metal",
"naga",
"objc",
"parking_lot 0.11.2",
"parking_lot 0.12.1",
"profiling",
"range-alloc",
"raw-window-handle",

View File

@ -9,3 +9,4 @@ license = "AGPL-3.0-or-later"
[dependencies]
bevy = "0.8.0"
bevy_ecs_tilemap = "0.7.0"
rand = "0.8.5"

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -2,29 +2,33 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
input::mouse::MouseMotion,
prelude::*,
render::texture::ImageSettings, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
render::texture::ImageSettings,
};
use bevy_ecs_tilemap::prelude::*;
use rand::{thread_rng, Rng};
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());
let texture_handle = asset_server.load("tileset.png");
let tilemap_size = TilemapSize { x: 320, y: 320 };
fn make_ground_layer(
commands: &mut Commands,
tilemap_size: TilemapSize,
texture_handle: Handle<Image>,
tile_size: TilemapTileSize,
) {
let mut tile_storage = TileStorage::empty(tilemap_size);
let tilemap_entity = commands.spawn().id();
let mut random = thread_rng();
for x in 0..320u32 {
for y in 0..320u32 {
for x in 0..tilemap_size.x {
for y in 0..tilemap_size.y {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn()
.insert_bundle(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture: TileTexture(random.gen_range(13..=19)),
..Default::default()
})
.id();
@ -32,21 +36,90 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
}
}
let tile_size = TilemapTileSize { x: 32.0, y: 32.0 };
commands
.entity(tilemap_entity)
.insert_bundle(TilemapBundle {
grid_size: TilemapGridSize { x: 32.0, y: 32.0 },
grid_size: tile_size.into(),
size: tilemap_size,
storage: tile_storage,
texture: TilemapTexture(texture_handle),
tile_size,
transform: bevy_ecs_tilemap::helpers::get_centered_transform_2d(&tilemap_size, &tile_size, 0.0),
transform: bevy_ecs_tilemap::helpers::get_centered_transform_2d(
&tilemap_size,
&tile_size,
0.0,
),
..Default::default()
});
}
fn make_wall_layer(
commands: &mut Commands,
tilemap_size: TilemapSize,
texture_handle: Handle<Image>,
tile_size: TilemapTileSize,
) {
let mut tile_storage = TileStorage::empty(tilemap_size);
let tilemap_entity = commands.spawn().id();
let mut random = thread_rng();
for x in 0..tilemap_size.x {
for y in 0..tilemap_size.y {
let tile_pos = TilePos { x, y };
if random.gen_bool(0.5) {
let tile_entity = commands
.spawn()
.insert_bundle(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture: TileTexture(random.gen_range(0..=12)),
..Default::default()
})
.id();
tile_storage.set(&tile_pos, Some(tile_entity));
}
}
}
commands
.entity(tilemap_entity)
.insert_bundle(TilemapBundle {
grid_size: tile_size.into(),
size: tilemap_size,
storage: tile_storage,
texture: TilemapTexture(texture_handle),
tile_size,
transform: bevy_ecs_tilemap::helpers::get_centered_transform_2d(
&tilemap_size,
&tile_size,
1.0,
),
..Default::default()
});
}
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());
let texture_handle = asset_server.load("tileset.png");
let tilemap_size = TilemapSize { x: 320, y: 320 };
let tile_size = TilemapTileSize { x: 32.0, y: 32.0 };
make_ground_layer(
&mut commands,
tilemap_size,
texture_handle.clone(),
tile_size,
);
make_wall_layer(
&mut commands,
tilemap_size,
texture_handle,
tile_size,
);
}
fn mouse_motion(
mut motion_evr: EventReader<MouseMotion>,
buttons: Res<Input<MouseButton>>,
@ -79,12 +152,3 @@ fn main() {
.add_system(mouse_motion)
.run();
}
#[cfg(test)]
mod tests {
#[test]
fn test_true()
{
assert!(false);
}
}