Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Fog of vision implementation #41

Merged
merged 6 commits into from Feb 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions src/components.rs
Expand Up @@ -64,3 +64,10 @@ pub enum Team {
/// The opponent's team.
Other,
}

/// Allows a unit to see others.
#[derive(new)]
pub struct LineOfSight {
/// The limit of the unit vision.
pub range: i32,
}
6 changes: 6 additions & 0 deletions src/main.rs
Expand Up @@ -212,6 +212,7 @@ fn main() -> BError {
let mut dispatcher = DispatcherBuilder::new();
dispatcher!(
dispatcher,
fog_of_vision_system,
combine_collision_system,
input_driver::<InputEvent>,
update_collision_resource_system,
Expand Down Expand Up @@ -293,6 +294,8 @@ fn main() -> BError {

world.initialize::<Components<Barrack>>();
world.initialize::<Components<Core>>();
world.initialize::<Components<LineOfSight>>();
world.initialize::<Viewshed>();
world.initialize::<TeamLeaders>();

*world.get_mut::<Option<CollisionResource>>().unwrap() = Some(CollisionResource::new(
Expand Down Expand Up @@ -423,13 +426,15 @@ fn main() -> BError {
Team::Me,
Barrack,
default_stats.clone(),
LineOfSight::new(15),
);
// Creep spawners
centity!(
world,
Point::new(x, y - 1),
CreepSpawner(0, CREEP_SPAWN_TICKS),
Team::Me,
LineOfSight::new(15),
);
}

Expand Down Expand Up @@ -470,6 +475,7 @@ fn main() -> BError {
SpriteIndex(80),
Team::Me,
default_stats.clone(),
LineOfSight::new(6),
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/states/default.rs
Expand Up @@ -40,6 +40,7 @@ impl minigene::State for DefaultState {
&*world.get().unwrap(),
&*world.get().unwrap(),
&*world.get().unwrap(),
Some(&*world.get().unwrap()),
);
}
ctx.set_active_console(0);
Expand Down
23 changes: 23 additions & 0 deletions src/systems/fog_of_vision.rs
@@ -0,0 +1,23 @@
use crate::*;

/// Sets which tiles are visible.
pub fn fog_of_vision_system(
collision_res: &Option<CollisionResource>,
sights: &Components<LineOfSight>,
viewshed: &mut Viewshed,
positions: &mut Components<Point>,
) -> SystemResult {
viewshed.visible_tiles.clear();
for (sight, pos) in join!(&sights && &positions).map(|(s, p)| (s.unwrap(), p.unwrap())) {
if let Some(col_res) = collision_res {
let map = &col_res.map;
viewshed
.visible_tiles
.extend(field_of_view(*pos, sight.range, map));
viewshed.visible_tiles.retain(|p| {
p.x >= 0 && p.x < (map.size().0 as i32) && p.y >= 0 && p.y < (map.size().1 as i32)
});
}
}
Ok(())
}
2 changes: 2 additions & 0 deletions src/systems/mod.rs
Expand Up @@ -8,6 +8,7 @@ mod bear_spawner;
mod creep_spawner;
mod damage_entity;
mod dark_presence;
mod fog_of_vision;
mod game_stats_updater;
mod handle_action_points;
mod kill_entity;
Expand Down Expand Up @@ -47,6 +48,7 @@ pub use self::bear_spawner::*;
pub use self::creep_spawner::*;
pub use self::damage_entity::*;
pub use self::dark_presence::*;
pub use self::fog_of_vision::*;
pub use self::game_stats_updater::*;
pub use self::handle_action_points::*;
pub use self::kill_entity::*;
Expand Down
2 changes: 2 additions & 0 deletions src/systems/spawn_creep.rs
Expand Up @@ -13,6 +13,7 @@ pub fn spawn_creep_system(
teams: &mut Components<Team>,
sprites: &mut Components<Sprite>,
sprite_indices: &mut Components<SpriteIndex>,
sights: &mut Components<LineOfSight>,
) -> SystemResult {
for ev in game_events.iter() {
if let GameEvent::SpawnCreep(pos, team) = ev {
Expand All @@ -24,6 +25,7 @@ pub fn spawn_creep_system(
stats.insert(creep, stat_def.to_statset());
proximity_attacks.insert(creep, ProximityAttack::new(CREEP_ATTACK_RADIUS));
let bg = if *team == Team::Me {
sights.insert(creep, LineOfSight::new(5));
RGBA::named(GREEN)
} else {
RGBA::named(RED)
Expand Down