Skip to content
Merged
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
8 changes: 4 additions & 4 deletions crates/common/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use bevy::prelude::*;

use crate::{constants::TILE_SIZE, types::BlockCoordinates};
use crate::{constants::TILE_SIZE, types::IWorldCoordinates};

pub fn world_position_to_world_coordinates(world_position: Vec3) -> BlockCoordinates {
pub fn world_position_to_world_coordinates(world_position: Vec3) -> IWorldCoordinates {
let x = world_position.x / TILE_SIZE.x;
let y = world_position.y / TILE_SIZE.y;
let z = world_position.z;
BlockCoordinates(IVec3::new(
IWorldCoordinates(IVec3::new(
x.round() as i32,
y.round() as i32,
z.round() as i32,
))
}

pub fn world_coordinates_to_world_position(world_coordinates: BlockCoordinates) -> Vec3 {
pub fn world_coordinates_to_world_position(world_coordinates: IWorldCoordinates) -> Vec3 {
let x = world_coordinates.0.x as f32 * TILE_SIZE.x;
let y = world_coordinates.0.y as f32 * TILE_SIZE.y;
let z = world_coordinates.0.z as f32;
Expand Down
24 changes: 13 additions & 11 deletions crates/common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ use bevy::{

use crate::traits::Neighbors;

/// These are essentially rounded world coordinates.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Reflect)]
pub struct BlockCoordinates(pub IVec3);
pub struct IWorldCoordinates(pub IVec3);

impl BlockCoordinates {
impl IWorldCoordinates {
/// Manually overrides the z value of the coordinates.
pub fn with_z_offset(mut self, z_offset: i32) -> BlockCoordinates {
pub fn with_z_offset(mut self, z_offset: i32) -> IWorldCoordinates {
self.0.z = z_offset;
self
}
}

impl Neighbors<BlockCoordinates> for BlockCoordinates {
fn same_layer_neighbors(&self) -> Vec<(BlockCoordinates, u32)> {
impl Neighbors<IWorldCoordinates> for IWorldCoordinates {
fn same_layer_neighbors(&self) -> Vec<(IWorldCoordinates, u32)> {
self.0
.same_layer_neighbors()
.iter()
.map(|(vec, cost)| (BlockCoordinates(*vec), *cost))
.map(|(vec, cost)| (IWorldCoordinates(*vec), *cost))
.collect()
}

fn all_neighbors(&self) -> Vec<(BlockCoordinates, u32)> {
fn all_neighbors(&self) -> Vec<(IWorldCoordinates, u32)> {
todo!("not implemented")
}
}
Expand All @@ -43,17 +44,18 @@ impl From<(u32, u32, u32)> for ChunkBlockCoordinates {
}
}

/// World coordinates that can be directly taken from a translation.
#[derive(Clone, Copy, Debug, PartialEq, Default, Reflect, Component)]
pub struct WorldCoordinates(pub Vec3);

impl WorldCoordinates {
pub fn block(&self) -> BlockCoordinates {
BlockCoordinates(self.0.round().as_ivec3())
pub fn block(&self) -> IWorldCoordinates {
IWorldCoordinates(self.0.round().as_ivec3())
}
}

impl From<&BlockCoordinates> for WorldCoordinates {
fn from(wc: &BlockCoordinates) -> WorldCoordinates {
impl From<&IWorldCoordinates> for WorldCoordinates {
fn from(wc: &IWorldCoordinates) -> WorldCoordinates {
WorldCoordinates(wc.0.as_vec3())
}
}
6 changes: 3 additions & 3 deletions crates/designations/src/designations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use bevy::{prelude::*, window::PrimaryWindow};
use bevy_inspector_egui::bevy_egui::{EguiContexts, EguiPrimaryContextPass};
use camera::CameraLayer;
use common::{
functions::world_position_to_world_coordinates, states::AppState, types::BlockCoordinates,
functions::world_position_to_world_coordinates, states::AppState, types::IWorldCoordinates,
};
use leafwing_input_manager::{
Actionlike,
plugin::InputManagerPlugin,
prelude::{ActionState, InputMap, MouseMove},
};
use map_generation::map_generation::WorldMap;
use map_generation::world_map::WorldMap;
use work::{WorkOrder, work_order_queue::WorkOrderQueue};

use crate::ui;
Expand All @@ -23,7 +23,7 @@ pub(crate) enum MouseActions {

#[derive(Message)]
enum BrushInputEvent {
Designated(BlockCoordinates),
Designated(IWorldCoordinates),
}

#[derive(Default, Reflect, Resource)]
Expand Down
5 changes: 5 additions & 0 deletions crates/map_generation/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

This crate is responsible for generating and maintaining the maps.

## Interaction
When possible, the world map shouldn't be changed from outside the crate. Use
`UpdateMap` to send messages to the map, like `UpdateMap::Damage(...)`.
The map in turn will then send `MapUpdate` events to let others know of updates.

## Visualisation and rendering
* Every chunk should be rendered as a single Sprite/Mesh
* Starting at the current layer every tile is checked downards (negative z direction). If a solid tile is hit, that tile will be rendered
Expand Down
10 changes: 5 additions & 5 deletions crates/map_generation/src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use common::types::{BlockCoordinates, ChunkBlockCoordinates, ChunkCoordinates};
use common::types::{ChunkBlockCoordinates, ChunkCoordinates, IWorldCoordinates};
use noise::{NoiseFn, OpenSimplex};

use crate::block_type::{BlockType, SolidMaterial};
Expand Down Expand Up @@ -55,7 +55,7 @@ pub(crate) trait ToChunkAndBlock {
fn to_chunk_and_block(&self) -> (ChunkCoordinates, ChunkBlockCoordinates);
}

impl ToChunkAndBlock for BlockCoordinates {
impl ToChunkAndBlock for IWorldCoordinates {
fn to_chunk_and_block(&self) -> (ChunkCoordinates, ChunkBlockCoordinates) {
(
ChunkCoordinates(self.0.div_euclid(CHUNK_SIZE.as_ivec3())),
Expand All @@ -75,12 +75,12 @@ pub(crate) fn to_index(coordinates: impl Into<ChunkBlockCoordinates>) -> usize {
pub(crate) fn to_world_coordinates(
chunk_coordinates: ChunkCoordinates,
block_coordinates: impl Into<ChunkBlockCoordinates>,
) -> BlockCoordinates {
) -> IWorldCoordinates {
let block_coordinates: ChunkBlockCoordinates = block_coordinates.into();
let x = chunk_coordinates.0.x * CHUNK_SIZE.x as i32 + block_coordinates.0.x as i32;
let y = chunk_coordinates.0.y * CHUNK_SIZE.y as i32 + block_coordinates.0.y as i32;
let z = chunk_coordinates.0.z * CHUNK_SIZE.z as i32 + block_coordinates.0.z as i32;
BlockCoordinates(IVec3::new(x, y, z))
IWorldCoordinates(IVec3::new(x, y, z))
}

#[test]
Expand All @@ -101,6 +101,6 @@ fn test_to_world() {
let chunk_coordinates = ChunkCoordinates(IVec3::ZERO);
assert_eq!(
to_world_coordinates(chunk_coordinates, (1, 2, 3)),
BlockCoordinates(IVec3::new(1, 2, 3))
IWorldCoordinates(IVec3::new(1, 2, 3))
)
}
72 changes: 35 additions & 37 deletions crates/map_generation/src/chunk_visualisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ use bevy_ecs_tilemap::{
tiles::{AnimatedTile, TileBundle, TileColor, TilePos, TileStorage, TileTextureIndex},
};
use camera::CameraLayer;
use common::{
constants::TILE_SIZE,
traits::Neighbors,
types::{BlockCoordinates, ChunkCoordinates},
};
use common::{constants::TILE_SIZE, traits::Neighbors, types::ChunkCoordinates};
use std::ops::{Range, RangeInclusive};

use crate::{
ToChunkAndBlock,
block_type::BlockType,
chunk::{CHUNK_SIZE, to_world_coordinates},
map_generation::WorldMap,
messages::BlockUpdate,
world_map::WorldMap,
};

/// actually spawns chunk visualisations
Expand Down Expand Up @@ -245,45 +242,46 @@ fn spawn_tile_map(
.insert(ChildOf(target));
}

pub(crate) fn on_chunk_visualisation_event(
trigger: On<ChunkVisualisationEvent>,
pub(crate) fn update(
query: Query<(Entity, &ChunkVisualisation)>,
mut message_reader: MessageReader<BlockUpdate>,
mut commands: Commands,
) {
let ChunkVisualisationEvent::SetDirty(coordinates) = trigger.event();
let (chunk_coordinates, block_coordinates) = coordinates.to_chunk_and_block();
for block_update in message_reader.read() {
match block_update {
BlockUpdate::Added => todo!(),
BlockUpdate::Removed(world_coordinates) => {
let (chunk_coordinates, block_coordinates) = world_coordinates.to_chunk_and_block();

let mut all = vec![chunk_coordinates];
if block_coordinates.0.x == 0
|| block_coordinates.0.y == 0
|| block_coordinates.0.x == CHUNK_SIZE.x - 1
|| block_coordinates.0.y == CHUNK_SIZE.y - 1
{
let neighbors: Vec<ChunkCoordinates> = chunk_coordinates
.0
.same_layer_neighbors()
.iter()
.map(|(coordinate, _)| ChunkCoordinates(*coordinate))
.collect();
all.extend(neighbors);
}
for coordinates in all {
if let Some((entity, _)) = query
.iter()
.find(|(_, chunk_vis)| chunk_vis.0 == coordinates)
{
commands
.entity(entity)
.insert(ChunkVisualisation(coordinates));
let mut all = vec![chunk_coordinates];
if block_coordinates.0.x == 0
|| block_coordinates.0.y == 0
|| block_coordinates.0.x == CHUNK_SIZE.x - 1
|| block_coordinates.0.y == CHUNK_SIZE.y - 1
{
let neighbors: Vec<ChunkCoordinates> = chunk_coordinates
.0
.same_layer_neighbors()
.iter()
.map(|(coordinate, _)| ChunkCoordinates(*coordinate))
.collect();
all.extend(neighbors);
}
for coordinates in all {
if let Some((entity, _)) = query
.iter()
.find(|(_, chunk_vis)| chunk_vis.0 == coordinates)
{
commands
.entity(entity)
.insert(ChunkVisualisation(coordinates));
}
}
}
}
}
}

#[derive(Event)]
pub enum ChunkVisualisationEvent {
SetDirty(BlockCoordinates),
}

#[derive(Component, Reflect)]
#[reflect(Component)]
pub(crate) struct ChunkVisualisation(ChunkCoordinates);
Expand Down
4 changes: 3 additions & 1 deletion crates/map_generation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use chunk::{Chunk, ToChunkAndBlock, to_index};
use chunk::ToChunkAndBlock;
use chunk_visualisation::ChunkVisualisation;

pub mod block_type;
mod chunk;
pub mod chunk_visualisation;
pub mod map_generation;
pub mod messages;
pub mod world_map;

pub use map_generation::plugin;
Loading
Loading