Skip to content
Draft
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
13 changes: 2 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ bevy = { version = "0.12.1", default-features = false, features = [
"default_font",
"png",
] }

bevy_egui = { version = "0.24.0" }
bevy_save = { version = "0.13.0" }
bevy_turborand = { version = "0.7.0" }

# WASM builds require extra dependencies for logging and persisting state to local storage.
Expand Down
25 changes: 25 additions & 0 deletions assets/shaders/world.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#import bevy_sprite::mesh2d_bindings::mesh

struct MapData {
world_size: vec2<u32>,
tile_size: vec2<f32>
atlas_size: vec2<f32>,

}

@group(1) @binding(0)
var<uniform> map: MapData;

@group(1) @binding(1)
var atlas_texture: texture2d<f32>;

#import bevy_sprite::mesh2d_bindings::{Vertex, VertexOutput}

@vertex
fn vertex_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.position = map.tile_size * in.position;
out.uv = in.uv * map.atlas_size;
return out;
}

3 changes: 0 additions & 3 deletions rendering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@ bevy = { version = "0.12.1", default-features = false, features = [
"default_font",
"png",
] }
bevy_ecs_tilemap = { git = "https://github.com/MeoMix/bevy_ecs_tilemap", branch = "main", features = [
"atlas",
] }
bevy_turborand = { version = "0.7.0" }
9 changes: 0 additions & 9 deletions rendering/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bevy::{prelude::*, utils::HashMap};
use bevy_ecs_tilemap::tiles::TilePos;

use simulation::common::{grid::Grid, position::Position, Zone};

Expand Down Expand Up @@ -212,11 +211,3 @@ pub fn on_despawn<Model: Component, Z: Zone>(
}
}
}

// TODO: Better home?
pub fn grid_to_tile_pos(grid: &Grid, position: Position) -> TilePos {
TilePos {
x: position.x as u32,
y: (grid.height() - position.y - 1) as u32,
}
}
18 changes: 5 additions & 13 deletions rendering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@ use self::{
cleanup_background, spawn_background, spawn_background_tilemap, update_sky_background,
Background, BackgroundTilemap,
},
element::{
cleanup_elements, on_spawn_element, on_update_element_exposure,
on_update_element_position, rerender_elements,
sprite_sheet::{
check_element_sprite_sheet_loaded, start_load_element_sprite_sheet, ElementTilemap,
},
element::sprite_sheet::{
check_element_sprite_sheet_loaded, start_load_element_sprite_sheet, ElementTilemap,
},
nest::{mark_nest_hidden, mark_nest_visible},
pheromone::{
cleanup_pheromones, on_spawn_pheromone, on_update_pheromone_visibility,
rerender_pheromones,
},
world::WorldViewPlugin,
},
};
use bevy::prelude::*;
use bevy_ecs_tilemap::TilemapPlugin;
use pointer::{handle_pointer_tap, initialize_pointer_resources, remove_pointer_resources};
use simulation::{
app_state::AppState,
Expand All @@ -67,7 +63,7 @@ pub struct RenderingPlugin;
/// This occurs because the simulation may tick multiple times before rendering systems run.
impl Plugin for RenderingPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((RenderingCameraPlugin, TilemapPlugin));
app.add_plugins((RenderingCameraPlugin, WorldViewPlugin));
app.add_state::<VisibleGridState>();

// Keep this off to prevent spritesheet bleed at various `projection.scale` levels.
Expand Down Expand Up @@ -160,7 +156,7 @@ fn build_nest_systems(app: &mut App) {
Update,
(
// Spawn
(on_spawn_ant, on_spawn_element, on_spawn_pheromone),
(on_spawn_ant, on_spawn_pheromone),
// Despawn
(
on_despawn::<Ant, AtNest>,
Expand All @@ -186,8 +182,6 @@ fn build_nest_systems(app: &mut App) {
),
on_ant_wake_up,
on_tick_emote,
on_update_element_position,
on_update_element_exposure,
on_update_pheromone_visibility,
),
)
Expand All @@ -202,7 +196,6 @@ fn build_nest_systems(app: &mut App) {
(
(spawn_background_tilemap, apply_deferred, spawn_background).chain(),
rerender_ants,
rerender_elements,
rerender_pheromones,
mark_nest_visible,
)
Expand Down Expand Up @@ -234,7 +227,6 @@ fn build_nest_systems(app: &mut App) {
cleanup_ants,
despawn_view_by_model::<Element>,
despawn_view::<ElementTilemap>,
cleanup_elements,
despawn_view_by_model::<Pheromone>,
cleanup_pheromones,
)
Expand Down
114 changes: 2 additions & 112 deletions rendering/src/nest_rendering/background/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;

use crate::common::{grid_to_tile_pos, VisibleGrid};
use crate::common::VisibleGrid;

use simulation::{
app_state::AppState,
Expand Down Expand Up @@ -96,7 +95,6 @@ fn get_sky_gradient_color(
}

pub fn update_sky_background(
mut sky_tile_query: Query<(&mut TileColor, &Position), With<SkyBackground>>,
// TODO: Relying on Local<> while trying to support "Reset Sandbox" without the ability to remove systems entirely is challenging.
// Probably rewrite this to be a resource instead of a Local.
mut last_run_time_info: Local<TimeInfo>,
Expand Down Expand Up @@ -143,129 +141,21 @@ pub fn update_sky_background(
sunrise_decimal_hours,
sunset_decimal_hours,
);
for (mut tile_color, position) in sky_tile_query.iter_mut() {
let t_y: f32 = position.y as f32 / nest.surface_level() as f32;
let color = interpolate_color(north_color, south_color, t_y);

*tile_color = color.into();
}

*last_run_time_info = time_info;
}

pub fn spawn_background_tilemap(mut commands: Commands, nest_query: Query<&Grid, With<Nest>>) {
let grid = nest_query.single();

let map_size = TilemapSize {
x: grid.width() as u32,
y: grid.height() as u32,
};
let grid_size = TilemapGridSize { x: 1.0, y: 1.0 };
let map_type = TilemapType::default();

commands.spawn((
BackgroundTilemap,
TilemapBundle {
grid_size,
size: map_size,
storage: TileStorage::empty(map_size),
physical_tile_size: TilemapPhysicalTileSize { x: 1.0, y: 1.0 },
// Doesn't need to be 128x128 here since not reading from spritesheet.
tile_size: TilemapTileSize { x: 1.0, y: 1.0 },
map_type: TilemapType::Square,
// Background tiles go at z: 0 because they should render behind elements/ants.
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
},
));

}

// Spawn non-interactive background (sky blue / tunnel brown)
pub fn spawn_background(
mut commands: Commands,
nest_query: Query<(&Grid, &Nest)>,
mut tilemap_query: Query<(Entity, &mut TileStorage), With<BackgroundTilemap>>,
story_time: Res<StoryTime>,
) {
let (grid, nest) = nest_query.single();
let air_height = nest.surface_level() + 1;

let (tilemap_entity, mut tile_storage) = tilemap_query.single_mut();

let current_decimal_hours = story_time.as_time_info().get_decimal_hours();
let (sunrise_decimal_hours, sunset_decimal_hours) =
story_time.get_sunrise_sunset_decimal_hours();

let (north_color, south_color) = get_sky_gradient_color(
current_decimal_hours,
sunrise_decimal_hours,
sunset_decimal_hours,
);

let width = grid.width();
let height = air_height;

for x in 0..width {
for y in 0..height {
let position = Position::new(x, y);

let t_y: f32 = position.y as f32 / nest.surface_level() as f32;
let color = interpolate_color(north_color, south_color, t_y);
let tile_pos = grid_to_tile_pos(grid, position);

let tile_entity = commands
.spawn((
TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
color: color.into(),
..default()
},
position,
SkyBackground,
Background,
AtNest,
))
.id();

tile_storage.set(&tile_pos, tile_entity);
}
}

// Create background sprites
let width = grid.width();
let height = grid.height() - air_height;
let y_offset = air_height;

let top_color: Color = Color::rgba(0.373, 0.290, 0.165, 1.0);
let bottom_color = Color::rgba(0.24, 0.186, 0.106, 1.0);

for x in 0..width {
for y in 0..height {
let position = Position::new(x, y + y_offset);

let color = interpolate_color(top_color, bottom_color, y as f32 / height as f32);

let tile_pos = grid_to_tile_pos(grid, position);

let tile_entity = commands
.spawn((
TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
color: color.into(),
..default()
},
position,
TunnelBackground,
Background,
AtNest,
))
.id();

tile_storage.set(&tile_pos, tile_entity);
}
}
}

pub fn cleanup_background() {
Expand Down
Loading