Skip to content

Commit 2400730

Browse files
authored
fix: remove layers and pick uniform coordinate system (#56)
1 parent ee62e3e commit 2400730

File tree

8 files changed

+74
-133
lines changed

8 files changed

+74
-133
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:cf813c21d758de7bb0572d50c2023150e92d00a021e3c0e957f13cb640590d42
2+
oid sha256:9915c212d99d875deda657ca774e3206d96f1b7b5b638244643ddbac805e6d74
33
size 3341

src/gameplay/logistics/path.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ use crate::gameplay::{
88
world::{
99
construction::{Constructions, StructureConstructed},
1010
demolition::{Demolishable, Demolished},
11-
tilemap::{
12-
TILE_OFFSET,
13-
coord::{Coord, CoordOffset},
14-
map::TileClicked,
15-
},
11+
tilemap::{TILE_OFFSET, coord::Coord, map::TileClicked},
1612
},
1713
};
1814

@@ -65,13 +61,12 @@ fn spawn_path(
6561
.spawn((
6662
Name::new("Path"),
6763
Pathable::walkable(),
64+
Coord::new(coord.x, coord.y),
6865
Sprite::sized(TILE_OFFSET),
6966
AseSlice {
7067
aseprite: asset_server.load("sprites/logistics/path_segments.aseprite"),
7168
name: "C".into(),
7269
},
73-
Coord::new(coord.x, coord.y),
74-
CoordOffset(Vec2::new(0.0, -32.0)),
7570
YSortSprite,
7671
ZIndexSprite(9),
7772
Demolishable,

src/gameplay/logistics/porter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bevy::prelude::*;
1+
use bevy::{prelude::*, sprite::Anchor};
22
use bevy_aseprite_ultra::prelude::{Animation, AseAnimation};
33

44
use crate::gameplay::{
@@ -90,6 +90,7 @@ fn spawn_porter(
9090
Name::new("Porter"),
9191
*transform,
9292
Sprite::default(),
93+
Anchor::BOTTOM_CENTER,
9394
AseAnimation {
9495
aseprite: asset_server.load("sprites/logistics/porter.aseprite"),
9596
animation: Animation::tag("idle"),

src/gameplay/world/construction.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use bevy::prelude::*;
3+
use bevy::{prelude::*, sprite::Anchor};
44
use bevy_aseprite_ultra::prelude::*;
55
use bevy_ecs_tilemap::tiles::TilePos;
66

@@ -175,6 +175,7 @@ fn construct(
175175
.spawn((
176176
Name::new(structure.name.clone()),
177177
Coord::new(tile_click.0.x, tile_click.0.y),
178+
Anchor(Vec2::new(0.0, -0.25)),
178179
Sprite::default(),
179180
AseAnimation {
180181
aseprite: asset_server

src/gameplay/world/deposit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bevy::{asset::LoadedFolder, prelude::*};
1+
use bevy::{asset::LoadedFolder, prelude::*, sprite::Anchor};
22
use bevy_ecs_tilemap::tiles::TilePos;
33
use rand::Rng;
44
use serde::Deserialize;
@@ -76,6 +76,7 @@ fn spawn_deposits(
7676
.spawn((
7777
Name::new(deposit.name.clone()),
7878
Coord::new(tile_pos.x, tile_pos.y),
79+
Anchor(Vec2::new(0.0, -0.25)),
7980
YSortSprite,
8081
ZIndexSprite(10),
8182
Sprite {

src/gameplay/world/tilemap/chunk.rs

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ use bevy::prelude::*;
22
use bevy_ecs_tilemap::prelude::*;
33

44
use crate::{
5-
gameplay::{
6-
sprite_sort::ZIndexSprite,
7-
world::{
8-
WorldSpawnSystems,
9-
tilemap::{CHUNK_SIZE, TILE_OFFSET, TILE_SIZE},
10-
},
5+
gameplay::world::{
6+
WorldSpawnSystems,
7+
tilemap::{CHUNK_SIZE, TILE_OFFSET, TILE_SIZE},
118
},
129
screens::Screen,
1310
};
@@ -25,73 +22,45 @@ pub fn plugin(app: &mut App) {
2522
#[reflect(Component)]
2623
pub struct Chunk;
2724

28-
#[derive(Component, Reflect, Debug)]
29-
#[reflect(Component)]
30-
#[relationship_target(relationship = LayerOf)]
31-
pub struct Layers(Vec<Entity>);
32-
33-
#[derive(Component, Reflect, Debug)]
34-
#[reflect(Component)]
35-
#[relationship(relationship_target = Layers)]
36-
pub struct LayerOf(pub Entity);
37-
3825
fn spawn_chunk(mut commands: Commands, asset_server: Res<AssetServer>) {
39-
let chunk_id = commands
40-
.spawn((
41-
Name::new("Chunk"),
42-
Chunk,
43-
Transform::default(),
44-
Visibility::default(),
45-
))
46-
.id();
26+
let map_size = TilemapSize::from(CHUNK_SIZE);
27+
let tile_size = TilemapTileSize::from(TILE_SIZE);
28+
let grid_size = TilemapGridSize::from(TILE_OFFSET);
29+
let storage = TileStorage::empty(map_size);
4730

48-
for i in 0..5 {
49-
let map_size = TilemapSize::from(CHUNK_SIZE);
50-
let tile_size = TilemapTileSize::from(TILE_SIZE);
51-
let grid_size = TilemapGridSize::from(TILE_OFFSET);
52-
let storage = TileStorage::empty(map_size);
53-
54-
commands.spawn((
55-
Name::new(format!("Layer {i}")),
56-
LayerOf(chunk_id),
57-
ChildOf(chunk_id),
58-
ZIndexSprite(i),
59-
TilemapBundle {
60-
transform: Transform::from_xyz(0.0, grid_size.y / 2.0 * i as f32, 0.0),
61-
grid_size,
62-
size: map_size,
63-
storage,
64-
texture: TilemapTexture::Single(asset_server.load("tiles/grass.png")),
65-
tile_size,
66-
map_type: TilemapType::Isometric(IsoCoordSystem::Diamond),
67-
anchor: TilemapAnchor::Center,
68-
render_settings: TilemapRenderSettings {
69-
render_chunk_size: UVec2::new(map_size.x, 1),
70-
y_sort: true,
71-
},
72-
..default()
31+
commands.spawn((
32+
Name::new("Chunk"),
33+
Chunk,
34+
TilemapBundle {
35+
transform: Transform::from_xyz(0.0, 0.0, 0.0),
36+
grid_size,
37+
size: map_size,
38+
storage,
39+
texture: TilemapTexture::Single(asset_server.load("tiles/grass.png")),
40+
tile_size,
41+
map_type: TilemapType::Isometric(IsoCoordSystem::Diamond),
42+
anchor: TilemapAnchor::Center,
43+
render_settings: TilemapRenderSettings {
44+
render_chunk_size: UVec2::new(map_size.x, 1),
45+
y_sort: true,
7346
},
74-
));
75-
}
47+
..default()
48+
},
49+
));
7650
}
7751

7852
fn spawn_flat_ground(
7953
mut commands: Commands,
80-
chunk_layers: Single<&Layers, With<Chunk>>,
81-
mut tile_storage_query: Query<&mut TileStorage>,
54+
chunk_query: Query<(Entity, &mut TileStorage), With<Chunk>>,
8255
) {
83-
for (i, layer) in chunk_layers.iter().enumerate().take(4) {
84-
let Ok(mut tile_storage) = tile_storage_query.get_mut(layer) else {
85-
continue;
86-
};
87-
56+
for (chunk_entity, mut chunk_tile_storage) in chunk_query {
8857
fill_tilemap_rect(
8958
TileTextureIndex(0),
9059
TilePos::new(0, 0),
91-
TilemapSize::from(CHUNK_SIZE / (i + 1) as u32),
92-
TilemapId(layer),
60+
TilemapSize::from(CHUNK_SIZE),
61+
TilemapId(chunk_entity),
9362
&mut commands,
94-
&mut tile_storage,
63+
&mut chunk_tile_storage,
9564
);
9665
}
9766
}
Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use bevy::prelude::*;
22
use bevy_ecs_tilemap::prelude::*;
33

4-
use crate::gameplay::{
5-
FactorySystems,
6-
world::tilemap::chunk::{Chunk, Layers},
7-
};
4+
use crate::gameplay::{FactorySystems, world::tilemap::chunk::Chunk};
85

96
pub fn plugin(app: &mut App) {
107
app.add_systems(
@@ -17,10 +14,6 @@ pub fn plugin(app: &mut App) {
1714
#[reflect(Component)]
1815
pub struct Coord(pub UVec2);
1916

20-
#[derive(Component, Reflect, Debug, Default, Deref, DerefMut)]
21-
#[reflect(Component)]
22-
pub struct CoordOffset(pub Vec2);
23-
2417
impl Coord {
2518
pub fn new(x: u32, y: u32) -> Self {
2619
Self(UVec2 { x, y })
@@ -29,9 +22,8 @@ impl Coord {
2922

3023
#[allow(clippy::type_complexity)]
3124
fn translate_coord_to_transform(
32-
coord_query: Query<(&mut Transform, &Coord, Option<&CoordOffset>), Changed<Coord>>,
33-
chunk_query: Single<&Layers, With<Chunk>>,
34-
tile_storage_query: Query<&TileStorage>,
25+
coord_query: Query<(&mut Transform, &Coord), Changed<Coord>>,
26+
chunk_entity: Single<Entity, With<Chunk>>,
3527
tilemap_query: Query<
3628
(
3729
&Transform,
@@ -44,19 +36,11 @@ fn translate_coord_to_transform(
4436
Without<Coord>,
4537
>,
4638
) {
47-
for (mut transform, coord, coord_offset) in coord_query {
39+
for (mut transform, coord) in coord_query {
4840
let tile_pos = TilePos::new(coord.x, coord.y);
4941

50-
let Some(layer) = chunk_query.iter().find(|layer| {
51-
tile_storage_query
52-
.get(*layer)
53-
.is_ok_and(|storage| storage.get(&tile_pos).is_none())
54-
}) else {
55-
continue;
56-
};
57-
5842
let Ok((tilemap_transform, map_size, grid_size, tile_size, map_type, anchor)) =
59-
tilemap_query.get(layer)
43+
tilemap_query.get(*chunk_entity)
6044
else {
6145
continue;
6246
};
@@ -65,10 +49,7 @@ fn translate_coord_to_transform(
6549
.center_in_world(map_size, grid_size, tile_size, map_type, anchor)
6650
.extend(0.0);
6751

68-
let z = transform.translation.z;
69-
transform.translation = tile_translation
70-
+ tilemap_transform.translation
71-
+ coord_offset.map(|co| co.0).unwrap_or_default().extend(0.0);
72-
transform.translation.z = z;
52+
transform.translation =
53+
tile_translation.with_z(0.0) + tilemap_transform.translation.with_z(0.0);
7354
}
7455
}

src/gameplay/world/tilemap/map.rs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use crate::{
66
FactorySystems,
77
world::{
88
construction::ValidPlacement,
9-
tilemap::{
10-
chunk::{Chunk, Layers},
11-
coord::Coord,
12-
},
9+
tilemap::{chunk::Chunk, coord::Coord},
1310
},
1411
},
1512
input::cursor::CursorPosition,
@@ -48,7 +45,7 @@ impl Default for HoveredTile {
4845
fn mark_highlighted_tile(
4946
cursor_position: Res<CursorPosition>,
5047
mut hovered_tile: ResMut<HoveredTile>,
51-
chunk_query: Single<&Layers, With<Chunk>>,
48+
chunk_entity: Single<Entity, With<Chunk>>,
5249
tilemap_query: Query<(
5350
&TilemapSize,
5451
&TilemapGridSize,
@@ -59,37 +56,33 @@ fn mark_highlighted_tile(
5956
&TilemapAnchor,
6057
)>,
6158
) {
62-
for layer in chunk_query.iter().rev() {
63-
let Ok((map_size, grid_size, tile_size, map_type, tile_storage, map_transform, anchor)) =
64-
tilemap_query.get(layer)
65-
else {
66-
continue;
67-
};
68-
69-
let cursor_in_map_pos = {
70-
let cursor_pos = Vec4::from((cursor_position.0, 0.0, 1.0));
71-
let cursor_in_map_pos = map_transform.to_matrix().inverse() * cursor_pos;
72-
cursor_in_map_pos.xy()
73-
};
74-
75-
let Some(tile_pos) = TilePos::from_world_pos(
76-
&cursor_in_map_pos,
77-
map_size,
78-
grid_size,
79-
tile_size,
80-
map_type,
81-
anchor,
82-
) else {
83-
continue;
84-
};
85-
86-
if let Some(tile_entity) = tile_storage.get(&tile_pos) {
87-
hovered_tile.0 = tile_entity;
88-
return;
89-
}
90-
}
59+
let Ok((map_size, grid_size, tile_size, map_type, tile_storage, map_transform, anchor)) =
60+
tilemap_query.get(*chunk_entity)
61+
else {
62+
return;
63+
};
64+
65+
let cursor_in_map_pos = {
66+
let cursor_pos = Vec4::from((cursor_position.0, 0.0, 1.0));
67+
let cursor_in_map_pos = map_transform.to_matrix().inverse() * cursor_pos;
68+
cursor_in_map_pos.xy()
69+
};
9170

92-
hovered_tile.0 = Entity::PLACEHOLDER;
71+
let Some(tile_pos) = TilePos::from_world_pos(
72+
&cursor_in_map_pos,
73+
map_size,
74+
grid_size,
75+
tile_size,
76+
map_type,
77+
anchor,
78+
) else {
79+
hovered_tile.0 = Entity::PLACEHOLDER;
80+
return;
81+
};
82+
83+
if let Some(tile_entity) = tile_storage.get(&tile_pos) {
84+
hovered_tile.0 = tile_entity;
85+
}
9386
}
9487

9588
fn highlight_hovered_tile(

0 commit comments

Comments
 (0)