Occlusion attempt 1
This commit is contained in:
Vendored
+45
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Debug executable 'dorf'",
|
||||||
|
"cargo": {
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"--bin=dorf",
|
||||||
|
"--package=dorf"
|
||||||
|
],
|
||||||
|
"filter": {
|
||||||
|
"name": "dorf",
|
||||||
|
"kind": "bin"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Debug unit tests in executable 'dorf'",
|
||||||
|
"cargo": {
|
||||||
|
"args": [
|
||||||
|
"test",
|
||||||
|
"--no-run",
|
||||||
|
"--bin=dorf",
|
||||||
|
"--package=dorf"
|
||||||
|
],
|
||||||
|
"filter": {
|
||||||
|
"name": "dorf",
|
||||||
|
"kind": "bin"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ opt-level = 3
|
|||||||
# Enable only a small amount of optimization in debug mode
|
# Enable only a small amount of optimization in debug mode
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 1
|
opt-level = 1
|
||||||
|
debug = true
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 601 B |
+14
-16
@@ -1,12 +1,12 @@
|
|||||||
use crate::citizen::Citizen;
|
use crate::citizen::Citizen;
|
||||||
|
use crate::constants::*;
|
||||||
use crate::item::{Item, ItemBundle};
|
use crate::item::{Item, ItemBundle};
|
||||||
use crate::tiles::TilePrefab;
|
use crate::tiles::TilePrefab;
|
||||||
use crate::constants::*;
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use rand::prelude::*;
|
|
||||||
use noise::{NoiseFn, Perlin};
|
use noise::{NoiseFn, Perlin};
|
||||||
|
use rand::prelude::*;
|
||||||
|
|
||||||
pub const Z_INDEX:f32 = 0.;
|
pub const Z_INDEX: f32 = 0.;
|
||||||
|
|
||||||
pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
|
pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
setup_tilemap(&mut commands, &asset_server);
|
setup_tilemap(&mut commands, &asset_server);
|
||||||
@@ -26,21 +26,13 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
||||||
let perlin = Perlin::new(0);
|
let perlin = Perlin::new(0);
|
||||||
|
|
||||||
for x in -20..20 {
|
for x in -15..15 {
|
||||||
for y in -20..20 {
|
for y in -15..15 {
|
||||||
for z in -2..10 {
|
for z in -2..15 {
|
||||||
let position = Vec3::new(
|
let position = Vec3::new(x as f32 * TILE_SIZE, y as f32 * TILE_SIZE, -z as f32);
|
||||||
x as f32 * TILE_SIZE,
|
|
||||||
y as f32 * TILE_SIZE,
|
|
||||||
-z as f32,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Generate Perlin noise value
|
// Generate Perlin noise value
|
||||||
let noise_value = perlin.get([
|
let noise_value = perlin.get([x as f64 * 0.1, y as f64 * 0.1, z as f64 * 0.1]);
|
||||||
x as f64 * 0.1,
|
|
||||||
y as f64 * 0.1,
|
|
||||||
z as f64 * 0.1,
|
|
||||||
]);
|
|
||||||
|
|
||||||
let mut items: Vec<ItemBundle> = vec![];
|
let mut items: Vec<ItemBundle> = vec![];
|
||||||
if noise_value < -0.4 {
|
if noise_value < -0.4 {
|
||||||
@@ -73,4 +65,10 @@ fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for x in -15..15 {
|
||||||
|
for y in -15..15 {
|
||||||
|
let position = Vec3::new(x as f32 * TILE_SIZE, y as f32 * TILE_SIZE, -14.);
|
||||||
|
commands.spawn(TilePrefab::bedrock(position, asset_server));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -24,7 +24,7 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.insert_resource(ClearColor(Color::srgb(0.0, 0.0, 0.0)))
|
.insert_resource(ClearColor(Color::srgb(0.0, 0.0, 0.0)))
|
||||||
.add_systems(Startup, (game::setup_level, camera::spawn_panning_camera))
|
.add_systems(Startup, (game::setup_level, camera::spawn_panning_camera))
|
||||||
.add_systems(PostStartup, tile::tile_sprite_visibility_update)
|
.add_systems(PostStartup, tile::tile_sprite_occlusion_update)
|
||||||
.add_systems(Startup, cursor::setup_curor)
|
.add_systems(Startup, cursor::setup_curor)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
FixedUpdate,
|
FixedUpdate,
|
||||||
|
|||||||
+35
-7
@@ -1,16 +1,23 @@
|
|||||||
use crate::item::Item;
|
use crate::item::Item;
|
||||||
use crate::{constants::*, game};
|
use crate::{constants::*, game};
|
||||||
|
use bevy::math::vec2;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::process::exit;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[require(Sprite)]
|
#[require(Sprite)]
|
||||||
pub struct Tile {
|
pub struct Tile {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
|
pub opaque: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Tile {
|
impl Default for Tile {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self { id: 1 }
|
Self {
|
||||||
|
id: 1,
|
||||||
|
opaque: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,22 +58,32 @@ pub struct DoorTile {
|
|||||||
pub locked: bool,
|
pub locked: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tile_sprite_visibility_update(
|
pub fn tile_sprite_occlusion_update(
|
||||||
mut query: Query<(&mut Visibility, &Transform), With<Tile>>,
|
mut query: Query<(&mut Visibility, &Transform, &Tile), With<Tile>>,
|
||||||
tile_query: Query<(&Transform, &Tile)>,
|
tile_query: Query<(&Transform, &Tile)>,
|
||||||
) {
|
) {
|
||||||
for (mut visibility, transform) in query.iter_mut() {
|
let mut z_map: HashMap<(i32, i32), i32> = HashMap::new();
|
||||||
|
|
||||||
|
for (mut visibility, transform, tile) in query.iter_mut() {
|
||||||
*visibility = Visibility::Hidden;
|
*visibility = Visibility::Hidden;
|
||||||
|
|
||||||
if transform.translation.z > game::Z_INDEX {
|
if transform.translation.z > game::Z_INDEX {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if let Some(z_val) = z_map.get(&(
|
||||||
|
transform.translation.x.round() as i32,
|
||||||
|
transform.translation.y.round() as i32,
|
||||||
|
)) {
|
||||||
|
if *z_val >= transform.translation.z as i32 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for x_offset in -1..=1 {
|
for x_offset in -1..=1 {
|
||||||
for y_offset in -1..=1 {
|
for y_offset in -1..=1 {
|
||||||
for z_offset in -1..=0 {
|
for z_offset in -1..=1 {
|
||||||
if x_offset == 0 && y_offset == 0 && z_offset == 0 {
|
if x_offset == 0 && y_offset == 0 && z_offset == 0 {
|
||||||
continue; // Skip the current tile
|
continue; // don't check against itself
|
||||||
}
|
}
|
||||||
let neighbor_position = transform.translation
|
let neighbor_position = transform.translation
|
||||||
+ Vec3::new(
|
+ Vec3::new(
|
||||||
@@ -75,13 +92,24 @@ pub fn tile_sprite_visibility_update(
|
|||||||
z_offset as f32 * TILE_SIZE,
|
z_offset as f32 * TILE_SIZE,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some((neighbor_transform, neighbor_tile)) =
|
if let Some((_, neighbor_tile)) =
|
||||||
tile_query.iter().find(|(neighbor_transform, _)| {
|
tile_query.iter().find(|(neighbor_transform, _)| {
|
||||||
neighbor_transform.translation == neighbor_position
|
neighbor_transform.translation == neighbor_position
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
|
// id == 0 is sky. So if bordering a sky tile (i.e. not deep underground)
|
||||||
if neighbor_tile.id == 0 {
|
if neighbor_tile.id == 0 {
|
||||||
*visibility = Visibility::Visible;
|
*visibility = Visibility::Visible;
|
||||||
|
// if opaque, do not look deeper on this X,Y
|
||||||
|
if tile.opaque {
|
||||||
|
z_map.insert(
|
||||||
|
(
|
||||||
|
transform.translation.x.round() as i32,
|
||||||
|
transform.translation.y.round() as i32,
|
||||||
|
),
|
||||||
|
transform.translation.z.round() as i32,
|
||||||
|
);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-6
@@ -1,6 +1,6 @@
|
|||||||
use bevy::prelude::*;
|
|
||||||
use crate::tile::{Tile, TileState};
|
|
||||||
use crate::constants::*;
|
use crate::constants::*;
|
||||||
|
use crate::tile::{Tile, TileState};
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Bundle)]
|
#[derive(Bundle)]
|
||||||
pub struct TilePrefab {
|
pub struct TilePrefab {
|
||||||
@@ -18,7 +18,10 @@ impl TilePrefab {
|
|||||||
image: asset_server.load("dirt.png"),
|
image: asset_server.load("dirt.png"),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
tile: Tile {id:1},
|
tile: Tile {
|
||||||
|
id: 1,
|
||||||
|
opaque: true,
|
||||||
|
},
|
||||||
tile_state: TileState {
|
tile_state: TileState {
|
||||||
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
||||||
},
|
},
|
||||||
@@ -32,7 +35,10 @@ impl TilePrefab {
|
|||||||
image: asset_server.load("grass.png"),
|
image: asset_server.load("grass.png"),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
tile: Tile {id:2},
|
tile: Tile {
|
||||||
|
id: 2,
|
||||||
|
opaque: true,
|
||||||
|
},
|
||||||
tile_state: TileState {
|
tile_state: TileState {
|
||||||
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
||||||
},
|
},
|
||||||
@@ -46,7 +52,10 @@ impl TilePrefab {
|
|||||||
image: asset_server.load("rock.png"),
|
image: asset_server.load("rock.png"),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
tile: Tile {id:3},
|
tile: Tile {
|
||||||
|
id: 3,
|
||||||
|
opaque: true,
|
||||||
|
},
|
||||||
tile_state: TileState {
|
tile_state: TileState {
|
||||||
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
||||||
},
|
},
|
||||||
@@ -59,7 +68,26 @@ impl TilePrefab {
|
|||||||
image: asset_server.load("sky.png"),
|
image: asset_server.load("sky.png"),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
tile: Tile {id:0},
|
tile: Tile {
|
||||||
|
id: 0,
|
||||||
|
opaque: false,
|
||||||
|
},
|
||||||
|
tile_state: TileState {
|
||||||
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn bedrock(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||||
|
TilePrefab {
|
||||||
|
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||||
|
sprite: Sprite {
|
||||||
|
image: asset_server.load("bedrock.png"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
tile: Tile {
|
||||||
|
id: 4,
|
||||||
|
opaque: true,
|
||||||
|
},
|
||||||
tile_state: TileState {
|
tile_state: TileState {
|
||||||
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user