Add a terrain def and use it
This commit is contained in:
parent
d4175e0710
commit
22ed2261f3
12
src/defs.rs
12
src/defs.rs
@ -1,10 +1,10 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct SurfaceDef {
|
pub struct SurfaceDef<'a> {
|
||||||
label: String,
|
pub label: &'a str,
|
||||||
name: String,
|
pub name: &'a str,
|
||||||
description: String,
|
pub description: &'a str,
|
||||||
texture_index: u32,
|
pub texture_index: u32,
|
||||||
support: f32,
|
pub support: f32,
|
||||||
}
|
}
|
||||||
|
107
src/main.rs
107
src/main.rs
@ -7,10 +7,45 @@ use bevy::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use bevy_ecs_tilemap::prelude::*;
|
use bevy_ecs_tilemap::prelude::*;
|
||||||
use rand::{thread_rng, Rng};
|
use defs::SurfaceDef;
|
||||||
|
|
||||||
mod defs;
|
mod defs;
|
||||||
|
|
||||||
|
const TERRAINS: [SurfaceDef; 3] = [
|
||||||
|
SurfaceDef {
|
||||||
|
label: "Mud",
|
||||||
|
name: "mud",
|
||||||
|
description: "Soil saturated with water.",
|
||||||
|
texture_index: 13,
|
||||||
|
support: 20.0,
|
||||||
|
},
|
||||||
|
SurfaceDef {
|
||||||
|
label: "Grass",
|
||||||
|
name: "grass",
|
||||||
|
description: "Green. Try to touch it.",
|
||||||
|
texture_index: 14,
|
||||||
|
support: 40.0,
|
||||||
|
},
|
||||||
|
SurfaceDef {
|
||||||
|
label: "Sand",
|
||||||
|
name: "sand",
|
||||||
|
description: "Gets everywhere, ruins vanilla sex fantasies.",
|
||||||
|
texture_index: 15,
|
||||||
|
support: 20.0,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
#[derive(Component, Clone)]
|
||||||
|
struct TileTerrain {
|
||||||
|
terrain_id: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TileTerrain {
|
||||||
|
fn def(&self) -> &SurfaceDef {
|
||||||
|
&TERRAINS[self.terrain_id]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn make_ground_layer(
|
fn make_ground_layer(
|
||||||
commands: &mut Commands,
|
commands: &mut Commands,
|
||||||
tilemap_size: TilemapSize,
|
tilemap_size: TilemapSize,
|
||||||
@ -19,18 +54,22 @@ fn make_ground_layer(
|
|||||||
) {
|
) {
|
||||||
let mut tile_storage = TileStorage::empty(tilemap_size);
|
let mut tile_storage = TileStorage::empty(tilemap_size);
|
||||||
let tilemap_entity = commands.spawn_empty().id();
|
let tilemap_entity = commands.spawn_empty().id();
|
||||||
let mut random = thread_rng();
|
|
||||||
|
|
||||||
for x in 0..tilemap_size.x {
|
for x in 0..tilemap_size.x {
|
||||||
for y in 0..tilemap_size.y {
|
for y in 0..tilemap_size.y {
|
||||||
let tile_pos = TilePos { x, y };
|
let tile_pos = TilePos { x, y };
|
||||||
|
|
||||||
|
let tile_terrain = TileTerrain { terrain_id: 1 };
|
||||||
let tile_entity = commands
|
let tile_entity = commands
|
||||||
.spawn(TileBundle {
|
.spawn((
|
||||||
position: tile_pos,
|
tile_terrain.clone(),
|
||||||
tilemap_id: TilemapId(tilemap_entity),
|
TileBundle {
|
||||||
texture_index: TileTextureIndex(random.gen_range(13..=19)),
|
position: tile_pos,
|
||||||
..Default::default()
|
tilemap_id: TilemapId(tilemap_entity),
|
||||||
})
|
texture_index: TileTextureIndex(tile_terrain.def().texture_index),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
))
|
||||||
.id();
|
.id();
|
||||||
tile_storage.set(&tile_pos, tile_entity);
|
tile_storage.set(&tile_pos, tile_entity);
|
||||||
}
|
}
|
||||||
@ -39,18 +78,16 @@ fn make_ground_layer(
|
|||||||
let grid_size = tile_size.into();
|
let grid_size = tile_size.into();
|
||||||
let map_type = TilemapType::default();
|
let map_type = TilemapType::default();
|
||||||
|
|
||||||
commands
|
commands.entity(tilemap_entity).insert(TilemapBundle {
|
||||||
.entity(tilemap_entity)
|
grid_size,
|
||||||
.insert(TilemapBundle {
|
map_type,
|
||||||
grid_size,
|
size: tilemap_size,
|
||||||
map_type,
|
storage: tile_storage.clone(),
|
||||||
size: tilemap_size,
|
texture: TilemapTexture::Single(texture_handle),
|
||||||
storage: tile_storage.clone(),
|
tile_size,
|
||||||
texture: TilemapTexture::Single(texture_handle),
|
transform: get_tilemap_center_transform(&tilemap_size, &grid_size, &map_type, 0.0),
|
||||||
tile_size,
|
..Default::default()
|
||||||
transform: get_tilemap_center_transform(&tilemap_size, &grid_size, &map_type, 0.0),
|
});
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
@ -61,12 +98,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
let tilemap_size = TilemapSize { x: 320, y: 320 };
|
let tilemap_size = TilemapSize { x: 320, y: 320 };
|
||||||
let tile_size = TilemapTileSize { x: 32.0, y: 32.0 };
|
let tile_size = TilemapTileSize { x: 32.0, y: 32.0 };
|
||||||
|
|
||||||
make_ground_layer(
|
make_ground_layer(&mut commands, tilemap_size, texture_handle, tile_size);
|
||||||
&mut commands,
|
|
||||||
tilemap_size,
|
|
||||||
texture_handle,
|
|
||||||
tile_size,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mouse_motion(
|
fn mouse_motion(
|
||||||
@ -86,17 +118,18 @@ fn mouse_motion(
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins
|
.add_plugins(
|
||||||
.set(WindowPlugin {
|
DefaultPlugins
|
||||||
window: WindowDescriptor {
|
.set(WindowPlugin {
|
||||||
width: 1270.0,
|
window: WindowDescriptor {
|
||||||
height:720.0,
|
width: 1270.0,
|
||||||
title: String::from("Monstrous"),
|
height: 720.0,
|
||||||
..Default::default()
|
title: String::from("Monstrous"),
|
||||||
},
|
..Default::default()
|
||||||
..default()
|
},
|
||||||
})
|
..default()
|
||||||
.set(ImagePlugin::default_nearest()),
|
})
|
||||||
|
.set(ImagePlugin::default_nearest()),
|
||||||
)
|
)
|
||||||
.add_plugin(LogDiagnosticsPlugin::default())
|
.add_plugin(LogDiagnosticsPlugin::default())
|
||||||
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user