Commit d35b8fbc authored by Anton Weber's avatar Anton Weber Committed by Rahix
Browse files

add fix internet task

parent 254f8e0d
pub mod bottle_angel;
mod definitions;
pub mod network_switch;
pub use definitions::AngelShift;
pub use definitions::AngelShiftImpl;
pub use definitions::ShiftMetadata;
pub fn generate_random_shift(rng: &mut impl rand::Rng) -> AngelShift {
AngelShift(match rng.gen_range(0usize, 1) {
AngelShift(match rng.gen_range(0usize, 2) {
0 => Box::new(bottle_angel::BottleAngelShift::generate(rng)),
1 => Box::new(network_switch::NetworkSwitchShift::generate(rng)),
_ => unreachable!(),
})
}
use crate::colliders;
use crate::components;
use crate::entities;
use crate::resources;
use crate::sprites;
use crate::svg_loader;
pub struct NetworkSwitchShift {
hours: usize,
level: String,
}
impl NetworkSwitchShift {
pub fn generate(rng: &mut impl rand::Rng) -> NetworkSwitchShift {
use rand::seq::SliceRandom;
let compatible_levels = ["assembly-hall-1.svg"];
NetworkSwitchShift {
hours: rng.gen_range(1, 3),
level: compatible_levels.choose(rng).unwrap().to_string(),
}
}
}
pub struct NetworkSwitchAngelState {
reconnected_switches: usize,
switches_in_map: usize,
}
impl NetworkSwitchAngelState {
fn new(switches_in_map: usize) -> NetworkSwitchAngelState {
NetworkSwitchAngelState {
reconnected_switches: 0,
switches_in_map,
}
}
}
impl super::AngelShiftImpl for NetworkSwitchShift {
fn metadata(&self) -> super::ShiftMetadata {
super::ShiftMetadata {
title: "Fix the internet".to_owned(),
description: "Check what's wrong with the network switches.".to_owned(),
hours: self.hours,
}
}
fn level_name(&self) -> &str {
&self.level
}
fn init_gameworld(
&self,
world: &mut legion::World,
resources: &mut legion::Resources,
schedule_builder: &mut legion::systems::Builder,
level: &svg_loader::SvgLevel,
) {
entities::create_network_switches(world, level);
resources.insert(NetworkSwitchAngelState::new(4));
schedule_builder
.add_thread_local(reconnect_switches_system())
.add_thread_local(update_network_switch_shift_system(self.hours));
}
}
#[legion::system]
#[read_component(colliders::Collider)]
#[read_component(components::NetworkSwitch)]
#[write_component(components::Sprite)]
pub fn reconnect_switches(
world: &mut legion::world::SubWorld,
cmd: &mut legion::systems::CommandBuffer,
#[resource] player: &resources::Player,
#[resource] collision_world: &colliders::CollisionWorld,
#[resource] angel_state: &mut NetworkSwitchAngelState,
) {
use legion::IntoQuery;
let collider = <&colliders::Collider>::query()
.get(world, player.0)
.unwrap();
let mut switches = <(&components::NetworkSwitch, &mut components::Sprite)>::query();
for pair in collision_world
.world
.proximities_with(collider.handle.unwrap(), false)
.unwrap()
{
if pair.3 != ncollide2d::query::Proximity::Intersecting {
continue;
}
let entity = *collision_world.world.objects.get(pair.1).unwrap().data();
if let Ok((_, sprite)) = switches.get_mut(world, entity) {
*sprite = components::Sprite::new(sprites::Sprite::NetworkSwitchConnected);
cmd.remove_component::<components::NetworkSwitch>(entity);
angel_state.reconnected_switches += 1;
}
}
}
#[legion::system]
#[write_component(components::Player)]
pub fn update_network_switch_shift(
#[state] hours_to_award: &mut usize,
world: &mut legion::world::SubWorld,
#[resource] player: &mut resources::Player,
#[resource] angel_state: &mut NetworkSwitchAngelState,
#[resource] game_manager: &mut resources::GameManager,
) {
use legion::IntoQuery;
if angel_state.reconnected_switches >= angel_state.switches_in_map {
let player = <&mut components::Player>::query()
.get_mut(world, player.0)
.unwrap();
player.collected_hours += *hours_to_award as u32;
*hours_to_award = 0;
game_manager.request_return_to_heaven();
}
}
mod edge;
mod matebottledrop;
mod movable;
mod network_switch;
mod node;
mod obstacle;
mod orbitbody;
......@@ -13,6 +14,7 @@ pub use edge::draw_edges_system;
pub use edge::Edge;
pub use matebottledrop::Matebottledrop;
pub use movable::Movable;
pub use network_switch::NetworkSwitch;
pub use node::Node;
pub use node::{draw_nodes_system, update_nodes_system};
pub use obstacle::Obstacle;
......
pub struct NetworkSwitch;
mod matebottledrop;
mod network_switch;
mod obstacle;
mod player;
pub use matebottledrop::create_drop_points;
pub use network_switch::create_network_switches;
pub use obstacle::create_stationary_obstacles;
pub use player::create_player;
use crate::colliders;
use crate::components;
use crate::sprites;
use crate::svg_loader;
use rand::seq::SliceRandom;
pub fn create_network_switches(world: &mut legion::World, level: &svg_loader::SvgLevel) {
let spawn_locations = level
.spawnpoints
.get("switch")
.expect("no network switch spawn in this map");
if spawn_locations.len() < 4 {
panic!("too few network switch spawns");
}
for droppoint in spawn_locations.choose_multiple(&mut rand::thread_rng(), 4) {
world.push((
components::NetworkSwitch,
components::Position::new(droppoint.x, droppoint.y),
colliders::Collider::new_sensor_circle(70.0),
components::Sprite::new(sprites::Sprite::NetworkSwitchDisconnected),
));
}
}
......@@ -6,6 +6,8 @@ pub enum Sprite {
Player,
BottleDropPointFull,
BottleDropPointEmpty,
NetworkSwitchConnected,
NetworkSwitchDisconnected,
}
impl Sprite {
......@@ -14,6 +16,8 @@ impl Sprite {
Sprite::Player => "player.svg",
Sprite::BottleDropPointFull => "droppoint-full.svg",
Sprite::BottleDropPointEmpty => "droppoint-empty.svg",
Sprite::NetworkSwitchConnected => "switch-connected.svg",
Sprite::NetworkSwitchDisconnected => "switch-disconnected.svg",
}
}
......@@ -35,6 +39,8 @@ impl Sprite {
Sprite::Player,
Sprite::BottleDropPointFull,
Sprite::BottleDropPointEmpty,
Sprite::NetworkSwitchConnected,
Sprite::NetworkSwitchDisconnected,
] {
sprite.load_and_register(renderer).await;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment