Compare commits

...

4 Commits

Author SHA1 Message Date
270d44bfa7 Update README.
All checks were successful
continuous-integration/drone/push Build is passing
2022-05-18 10:32:59 -05:00
b3a06abf0f Various visibilityu/exploration tweaks and consistency fixes. 2022-05-18 10:28:42 -05:00
480e3cc6a5 Consistently convert coordinates. 2022-05-18 10:28:19 -05:00
34b10aaca4 Divide, not multiply. 2022-05-18 10:27:52 -05:00
5 changed files with 23 additions and 31 deletions

View File

@ -10,19 +10,18 @@ _Blackout_ uses a few other libraries for its magic:
* [bevy_openal](https://github.com/lightsoutgames/bevy_openal): Audio * [bevy_openal](https://github.com/lightsoutgames/bevy_openal): Audio
* [bevy_input_actionmap](https://github.com/lightsoutgames/bevy_input_actionmap): Mapping inputs to actions * [bevy_input_actionmap](https://github.com/lightsoutgames/bevy_input_actionmap): Mapping inputs to actions
* [mapgen](https://docs.rs/mapgen/latest/mapgen/): Map generation * [here_be_dragons](https://docs.rs/here_be_dragons): Map generation
* [shadowcast](https://docs.rs/shadowcast): Visibility calculations
It provides, including but not limited to: It provides, including but not limited to:
* commands * commands
* `RunIfExists` extension for only running a given command if its entity exists * `RunIfExists` extension for only running a given command if its entity exists
* core * core
* `Coordinates` component for managing 2-D positions
* `PointLike` trait for distance and other calculations between `Coordinates` and `Coordinates`-like things (I.e. `(f32, f32)`, `(i32, i32)`, ...) * `PointLike` trait for distance and other calculations between `Coordinates` and `Coordinates`-like things (I.e. `(f32, f32)`, `(i32, i32)`, ...)
* `Angle` type for handling angles in degrees/radians * `Angle` type for handling angles in degrees/radians
* `Direction` type for working with cardinal/movement directions, converting to/from angles, etc. * `Direction` type for working with cardinal/movement directions, converting to/from angles, etc.
* Configuration for syncing pixel-to-meter ratios between OpenAL and Rapier * Configuration for syncing pixel-to-meter ratios between OpenAL and Rapier
* Systems that try keeping the mess of Bevy/Blackout/Rapier position types synced
* `Player` component for use in other modules/plugins * `Player` component for use in other modules/plugins
* log * log
* Simple log component, automatically read via text-to-speech when appended to * Simple log component, automatically read via text-to-speech when appended to

View File

@ -222,19 +222,19 @@ pub trait PointLike {
fn y(&self) -> f32; fn y(&self) -> f32;
fn x_i32(&self) -> i32 { fn x_i32(&self) -> i32 {
self.x() as i32 self.x().trunc() as i32
} }
fn y_i32(&self) -> i32 { fn y_i32(&self) -> i32 {
self.y() as i32 self.y().trunc() as i32
} }
fn x_usize(&self) -> usize { fn x_usize(&self) -> usize {
self.x() as usize self.x().trunc() as usize
} }
fn y_usize(&self) -> usize { fn y_usize(&self) -> usize {
self.y() as usize self.y().trunc() as usize
} }
fn f32(&self) -> (f32, f32) { fn f32(&self) -> (f32, f32) {
@ -248,6 +248,7 @@ pub trait PointLike {
fn floor(&self) -> (f32, f32) { fn floor(&self) -> (f32, f32) {
(self.x().floor(), self.y().floor()) (self.x().floor(), self.y().floor())
} }
fn to_index(&self, width: usize) -> usize { fn to_index(&self, width: usize) -> usize {
((self.y_i32() * width as i32) + self.x_i32()) as usize ((self.y_i32() * width as i32) + self.x_i32()) as usize
} }

View File

@ -337,7 +337,7 @@ fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
for (map, revealed_tiles) in map.iter() { for (map, revealed_tiles) in map.iter() {
let point = **exploring; let point = **exploring;
let idx = point.to_index(map.width); let idx = point.to_index(map.width);
let shape = Collider::cuboid(0.49, 0.49); let shape = Collider::cuboid(0.5 - f32::EPSILON, 0.5 - f32::EPSILON);
let known = revealed_tiles[idx]; let known = revealed_tiles[idx];
let visible = viewshed.is_point_visible(exploring); let visible = viewshed.is_point_visible(exploring);
let fog_of_war = known && !visible; let fog_of_war = known && !visible;

View File

@ -250,7 +250,7 @@ fn sound_icon_exploration_focus_removed(
if let Ok(children) = children.get(entity) { if let Ok(children) = children.get(entity) {
for child in children.iter() { for child in children.iter() {
if let Ok(mut icon) = query.get_mut(*child) { if let Ok(mut icon) = query.get_mut(*child) {
icon.gain *= ICON_GAIN; icon.gain /= ICON_GAIN;
} }
} }
} }

View File

@ -4,10 +4,7 @@ use std::{
marker::PhantomData, marker::PhantomData,
}; };
use bevy::{ use bevy::{core::FixedTimestep, prelude::*};
core::{FixedTimestep, FloatOrd},
prelude::*,
};
use bevy_rapier2d::na::{self, Point2}; use bevy_rapier2d::na::{self, Point2};
use coord_2d::{Coord, Size}; use coord_2d::{Coord, Size};
use shadowcast::{vision_distance, Context, InputGrid}; use shadowcast::{vision_distance, Context, InputGrid};
@ -61,23 +58,21 @@ impl Viewshed {
map: &Map<D>, map: &Map<D>,
visible_query: &Query<&Visible>, visible_query: &Query<&Visible>,
events: &mut EventWriter<VisibilityChanged>, events: &mut EventWriter<VisibilityChanged>,
cache: &mut HashMap<(FloatOrd, FloatOrd), (u8, HashSet<Entity>)>, cache: &mut HashMap<Coord, (u8, HashSet<Entity>)>,
) { ) {
let mut context: Context<u8> = Context::default(); let mut context: Context<u8> = Context::default();
let vision_distance = vision_distance::Circle::new(self.range); let vision_distance = vision_distance::Circle::new(self.range);
let shape = Collider::cuboid(0.5, 0.5); let shape = Collider::cuboid(0.5 - f32::EPSILON, 0.5 - f32::EPSILON);
let origin = Point2::new(start.x(), start.y()); let origin = Point2::new(start.x().trunc(), start.y().trunc());
let coord = Coord::new(start.x_i32(), start.y_i32());
let mut new_visible_entities = HashSet::new(); let mut new_visible_entities = HashSet::new();
let visibility_grid = VisibilityGrid( let visibility_grid = VisibilityGrid(
map, map,
RefCell::new(Box::new(|coord: Coord| { RefCell::new(Box::new(|coord: Coord| {
let dest = Point2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5); let dest = Point2::new(coord.x as f32, coord.y as f32);
if na::distance(&origin, &dest) > self.range as f32 { if na::distance(&origin, &dest) >= self.range as f32 {
return u8::MAX; return u8::MAX;
} }
if let Some((opacity, entities)) = cache.get(&(FloatOrd(dest.x), FloatOrd(dest.y))) if let Some((opacity, entities)) = cache.get(&coord) {
{
for e in entities { for e in entities {
new_visible_entities.insert(*e); new_visible_entities.insert(*e);
} }
@ -89,15 +84,12 @@ impl Viewshed {
} }
let tile = map.at(coord.x as usize, coord.y as usize); let tile = map.at(coord.x as usize, coord.y as usize);
if tile.blocks_visibility() { if tile.blocks_visibility() {
cache.insert( cache.insert(coord, (u8::MAX, HashSet::new()));
(FloatOrd(dest.x), FloatOrd(dest.y)),
(u8::MAX, HashSet::new()),
);
return u8::MAX; return u8::MAX;
} }
let mut opacity = 0; let mut opacity = 0;
let mut entities = HashSet::new(); let mut entities = HashSet::new();
let shape_pos = Vec2::new(dest.x, dest.y); let shape_pos = Vec2::new(dest.x + 0.5, dest.y + 0.5);
rapier_context.intersections_with_shape( rapier_context.intersections_with_shape(
shape_pos, shape_pos,
0., 0.,
@ -118,14 +110,14 @@ impl Viewshed {
if entities.contains(&viewer_entity) { if entities.contains(&viewer_entity) {
0 0
} else { } else {
cache.insert((FloatOrd(dest.x), FloatOrd(dest.y)), (opacity, entities)); cache.insert(coord, (opacity, entities));
opacity opacity
} }
})), })),
); );
let mut new_visible = HashSet::new(); let mut new_visible = HashSet::new();
context.for_each_visible( context.for_each_visible(
coord, Coord::new(start.x_i32(), start.y_i32()),
&visibility_grid, &visibility_grid,
&visibility_grid, &visibility_grid,
vision_distance, vision_distance,
@ -370,10 +362,10 @@ fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
fn update_revealed_tiles<D: 'static + Clone + Default + Send + Sync>( fn update_revealed_tiles<D: 'static + Clone + Default + Send + Sync>(
mut map: Query<(&Map<D>, &mut RevealedTiles)>, mut map: Query<(&Map<D>, &mut RevealedTiles)>,
viewers: Query<&Viewshed, With<Player>>, viewers: Query<&Viewshed, (With<Player>, Changed<Viewshed>)>,
) { ) {
for (map, mut revealed_tiles) in map.iter_mut() {
for viewshed in viewers.iter() { for viewshed in viewers.iter() {
for (map, mut revealed_tiles) in map.iter_mut() {
for v in viewshed.visible_points.iter() { for v in viewshed.visible_points.iter() {
let idx = v.to_index(map.width); let idx = v.to_index(map.width);
if idx >= revealed_tiles.len() { if idx >= revealed_tiles.len() {