Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/tilesets/tileset_blocked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions crates/assets/src/tileset_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use bevy::{
pub struct TilesetAsset {
pub soil_tileset: Handle<Image>,
pub fog_tileset: Handle<Image>,
pub blocked_tileset: Handle<Image>,
pub water_tileset: Handle<Image>,
pub floor_tileset: Handle<Image>,
}

impl TilesetAsset {
const SOIL_PATH: &'static str = "tilesets/tileset_soil.png";
const FOG_PATH: &'static str = "tilesets/tileset_fog.png";
const BLOCKED_PATH: &'static str = "tilesets/tileset_blocked.png";
const WATER_PATH: &'static str = "tilesets/tileset_water.png";
const FLOOR_PATH: &'static str = "tilesets/tileset_floors.png";
}
Expand All @@ -34,6 +36,12 @@ impl FromWorld for TilesetAsset {
settings.sampler = ImageSampler::nearest();
},
),
blocked_tileset: assets.load_with_settings(
TilesetAsset::BLOCKED_PATH,
|settings: &mut ImageLoaderSettings| {
settings.sampler = ImageSampler::nearest();
},
),
water_tileset: assets.load_with_settings(
TilesetAsset::WATER_PATH,
|settings: &mut ImageLoaderSettings| {
Expand Down
18 changes: 16 additions & 2 deletions crates/camera/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ fn setup(mut commands: Commands) {
commands.spawn((
input_map,
Camera2d,
Projection::Orthographic(OrthographicProjection {
near: -0.5,
..OrthographicProjection::default_2d()
}),
CameraLayer(0),
DespawnOnExit(AppState::MainGame),
));
Expand All @@ -85,8 +89,17 @@ fn zoom(
}
}

fn scroll(query: Single<(&mut CameraLayer, &ActionState<CameraControls>), With<Camera2d>>) {
let (mut layer, action_state) = query.into_inner();
fn scroll(
query: Single<
(
&mut CameraLayer,
&ActionState<CameraControls>,
&mut Transform,
),
With<Camera2d>,
>,
) {
let (mut layer, action_state, mut transform) = query.into_inner();
let mut delta = 0;
if action_state.just_pressed(&CameraControls::ScrollUp) {
delta += 1;
Expand All @@ -95,6 +108,7 @@ fn scroll(query: Single<(&mut CameraLayer, &ActionState<CameraControls>), With<C
delta -= 1;
}
layer.0 += delta;
transform.translation.z = layer.0 as f32;
}

fn pan(
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn apply_world_coordinates(
for (mut transform, coordinates) in query.iter_mut() {
transform.translation.x = coordinates.0.x * TILE_SIZE.x;
transform.translation.y = coordinates.0.y * TILE_SIZE.y;
transform.translation.z = 0.1;
transform.translation.z = coordinates.0.z + 0.1;
transform.set_changed();
}
}
17 changes: 13 additions & 4 deletions crates/map_generation/src/chunk_visualisation/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(crate) fn on_insert(
.insert(ChildOf(world_map.entity));

// Setup hashmaps for tilemaps
let mut tilemaps = Tilemaps::default();
let mut tilemaps = Tilemaps::new(chunk_visualisation_settings.visible_layers);

// we iterate over every x and y coordinate of the current chunk and go from the current camera layer downwards visible_layers + 1 layers
for x in 0..CHUNK_SIZE.x {
Expand All @@ -80,21 +80,30 @@ pub(crate) fn on_insert(
}
}

for (tile_type, map) in &tilemaps.0 {
for ((z, tile_type), map) in &tilemaps.0 {
if !map.is_empty() {
let tileset_image = match *tile_type {
TileType::Full => &tileset.floor_tileset,
TileType::Half => &tileset.soil_tileset,
TileType::Animated => &tileset.water_tileset,
TileType::Fog => &tileset.fog_tileset,
TileType::Blocked => &tileset.blocked_tileset,
};
spawn_tile_map(&mut commands, tile_type, map, target, tileset_image);
spawn_tile_map(
&mut commands,
camera_layer.0 - (*z),
tile_type,
map,
target,
tileset_image,
);
}
}
}

fn spawn_tile_map(
commands: &mut Commands,
z: i32,
tile_type: &TileType,
tile_map: &Tilemap,
parent_chunk: Entity,
Expand Down Expand Up @@ -127,7 +136,7 @@ fn spawn_tile_map(
texture: TilemapTexture::Single(tileset.clone()),
tile_size,
anchor: TilemapAnchor::BottomLeft,
transform: Transform::from_xyz(0.0, 0.0, tile_type.z_offset()),
transform: Transform::from_xyz(0.0, 0.0, z as f32 + tile_type.z_offset()),
..default()
})
.insert(ChildOf(parent_chunk));
Expand Down
60 changes: 45 additions & 15 deletions crates/map_generation/src/chunk_visualisation/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{collections::HashMap, hash::Hash};

use bevy::{color::palettes::css::WHITE, ecs::relationship::RelatedSpawnerCommands, prelude::*};
use bevy::{
color::palettes::css::{BLACK, WHITE},
ecs::relationship::RelatedSpawnerCommands,
prelude::*,
};
use bevy_ecs_tilemap::prelude::*;
use common::{constants::TILE_SIZE, traits::Neighbors, types::IWorldCoordinates};

Expand All @@ -12,6 +16,7 @@ pub(crate) enum TileType {
Half,
Animated,
Fog,
Blocked,
}

impl TileType {
Expand All @@ -38,16 +43,18 @@ impl TileType {
TileType::Half => "Full-Tile Tilemap",
TileType::Animated => "Animated Tilemap",
TileType::Fog => "Fog Tilemap",
TileType::Blocked => "Blocked Tilemap",
}
}

/// Returns the tilemap offset based on its type
pub(crate) const fn z_offset(&self) -> f32 {
match *self {
TileType::Full => -0.5,
TileType::Full => -0.1,
TileType::Half => 0.0,
TileType::Animated => -0.5,
TileType::Fog => 1.0,
TileType::Animated => -0.1,
TileType::Fog => 0.2,
TileType::Blocked => -0.1,
}
}
}
Expand All @@ -62,6 +69,8 @@ pub(crate) enum TileWrapper {
Animated,
/// Wrapper for a fog type, carries the opacity in percent (ranging 0.0 to 1.0)
Fog(f32),
// Wrapper for a tile that is not visible
Blocked,
}

impl TileWrapper {
Expand Down Expand Up @@ -118,6 +127,17 @@ impl TileWrapper {
.id();
tile_storage.set(&position, tile_entity);
}
(TileWrapper::Blocked, TilePosType::Full(position)) => {
let tile_entity = parent
.spawn(TileBundle {
position,
tilemap_id,
color: TileColor(BLACK.into()),
..default()
})
.id();
tile_storage.set(&position, tile_entity);
}
_ => (),
}
}
Expand All @@ -133,15 +153,18 @@ pub(crate) enum TilePosType {
pub(crate) type Tilemap = HashMap<TilePosType, TileWrapper>;

/// Contains all relevant tilemaps
pub(crate) struct Tilemaps(pub(crate) HashMap<TileType, Tilemap>);
pub(crate) struct Tilemaps(pub(crate) HashMap<(i32, TileType), Tilemap>);

impl Default for Tilemaps {
fn default() -> Self {
impl Tilemaps {
pub(crate) fn new(layers: i32) -> Self {
let mut map = HashMap::new();
map.insert(TileType::Full, HashMap::new());
map.insert(TileType::Half, HashMap::new());
map.insert(TileType::Animated, HashMap::new());
map.insert(TileType::Fog, HashMap::new());
for i in 0..=layers {
map.insert((i, TileType::Full), HashMap::new());
map.insert((i, TileType::Half), HashMap::new());
map.insert((i, TileType::Animated), HashMap::new());
map.insert((i, TileType::Fog), HashMap::new());
}
map.insert((0, TileType::Blocked), HashMap::new());
Tilemaps(map)
}
}
Expand Down Expand Up @@ -185,16 +208,23 @@ impl ToTiles for BlockType {
// add its state to the flag
flags |= solid << index;
}
tilemaps.0.entry(TileType::Half).and_modify(|m| {
tilemaps.0.entry((z, TileType::Half)).and_modify(|m| {
m.insert(
TilePosType::Half((pos.x, pos.y)),
TileWrapper::Half((*self, flags)),
);
});
// Blocks the view of the tile
tilemaps.0.entry((z, TileType::Blocked)).and_modify(|m| {
m.insert(
TilePosType::Full(TilePos::new(pos.x, pos.y)),
TileWrapper::Blocked,
);
});
true
}
BlockType::Solid(_) => {
tilemaps.0.entry(TileType::Full).and_modify(|m| {
tilemaps.0.entry((z, TileType::Full)).and_modify(|m| {
m.insert(
TilePosType::Full(TilePos::new(pos.x, pos.y)),
TileWrapper::Full(*self),
Expand All @@ -203,7 +233,7 @@ impl ToTiles for BlockType {
true
}
BlockType::Liquid => {
tilemaps.0.entry(TileType::Animated).and_modify(|m| {
tilemaps.0.entry((z, TileType::Animated)).and_modify(|m| {
m.insert(
TilePosType::Full(TilePos::new(pos.x, pos.y)),
TileWrapper::Animated,
Expand All @@ -212,7 +242,7 @@ impl ToTiles for BlockType {
true
}
BlockType::None if z != 0 => {
tilemaps.0.entry(TileType::Fog).and_modify(|m| {
tilemaps.0.entry((z, TileType::Fog)).and_modify(|m| {
m.insert(
TilePosType::Full(TilePos::new(pos.x, pos.y)),
TileWrapper::Fog(z as f32 / visible_layers),
Expand Down
Loading