Skip to content

Commit

Permalink
Implement TouchOfLifePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Dec 5, 2023
1 parent 3562b4f commit 994be78
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::state::results_screen::ResultsScreenConfig;
use crate::state::splash_screen::SplashScreenConfig;
use crate::state::title_screen::TitleScreenConfig;
use crate::ui::TooltipConfig;
use crate::AppRoot;

pub struct ConfigPlugin;

Expand Down Expand Up @@ -45,6 +46,7 @@ impl Plugin for ConfigPlugin {
..default()
})
.insert_resource(config)
.add_systems(Startup, save_window_to_root)
.add_systems(Update, apply_config.run_if(resource_changed::<Config>()));
}
}
Expand Down Expand Up @@ -83,3 +85,10 @@ fn apply_config(config: Res<Config>, mut window_query: Query<&mut Window, With<P

// TODO: Implement the rest (not important for game jam)
}

fn save_window_to_root(
mut root: ResMut<AppRoot>,
window_query: Query<Entity, With<PrimaryWindow>>,
) {
root.window = window_query.single();
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Plugin for AppPlugin {
// Global entities
#[derive(Resource, Reflect)]
pub struct AppRoot {
window: Entity,
camera: Entity,
tooltip: Entity,
tooltip_text: Entity,
Expand All @@ -69,6 +70,7 @@ pub struct AppRoot {
impl Default for AppRoot {
fn default() -> Self {
Self {
window: Entity::PLACEHOLDER,
camera: Entity::PLACEHOLDER,
tooltip: Entity::PLACEHOLDER,
tooltip_text: Entity::PLACEHOLDER,
Expand Down
50 changes: 45 additions & 5 deletions src/simulation.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,74 @@
use bevy::math::vec2;
use bevy::prelude::*;

use crate::state::editor_screen::ClickSpawnEvent;
use crate::upgrade::UpgradeEvent;
use crate::upgrade::UpgradeList;
use crate::AppRoot;

pub struct SimulationPlugin;

impl Plugin for SimulationPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Simulation>()
.add_systems(Update, count_plugins);
app.register_type::<SpawnEvent>()
.add_event::<SpawnEvent>()
.init_resource::<Simulation>()
.add_systems(Update, (count_upgrades, spawn_on_click));
}
}

#[derive(Resource, Default)]
pub struct Simulation {
pub plugins: usize,
pub upgrades: usize,
pub lines: f64,
pub entities: f64,

pub spawns_per_click: usize,
}

fn count_plugins(
fn count_upgrades(
mut events: EventReader<UpgradeEvent>,
mut simulation: ResMut<Simulation>,
upgrade_list: Res<UpgradeList>,
) {
simulation.plugins += events
simulation.upgrades += events
.read()
.filter(|event| {
// Ignore upgrades that can repeat indefinitely.
upgrade_list.get(event.0).remaining != usize::MAX
})
.count();
}

#[derive(Event, Reflect)]
pub struct SpawnEvent(pub Entity);

fn spawn_on_click(
mut commands: Commands,
mut click_events: EventReader<ClickSpawnEvent>,
mut spawn_events: EventWriter<SpawnEvent>,
root: Res<AppRoot>,
mut simulation: ResMut<Simulation>,
) {
for click_event in click_events.read() {
simulation.entities += simulation.spawns_per_click as f64;
for _ in 0..simulation.spawns_per_click {
let entity = commands
.spawn((
Name::new("Entity"),
SpriteBundle {
sprite: Sprite {
color: Color::RED,
custom_size: Some(vec2(16.0, 16.0)),
..default()
},
transform: Transform::from_translation(click_event.0.extend(0.0)),
..default()
},
))
.set_parent(root.world)
.id();
spawn_events.send(SpawnEvent(entity))
}
}
}
5 changes: 4 additions & 1 deletion src/state/editor_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::state::editor_screen::code_panel::spawn_light_code_panel;
use crate::state::editor_screen::info_bar::spawn_info_bar;
use crate::state::editor_screen::outline_panel::spawn_outline_panel;
use crate::state::editor_screen::scene_view::spawn_scene_view;
pub use crate::state::editor_screen::scene_view::ClickSpawnEvent;
use crate::state::editor_screen::upgrade_panel::spawn_upgrade_panel;
use crate::state::AppState::*;
use crate::upgrade::UpgradeList;
Expand All @@ -33,6 +34,7 @@ impl Plugin for EditorScreenStatePlugin {
.add_plugins((
info_bar::InfoBarPlugin,
outline_panel::OutlinePanelPlugin,
scene_view::SceneViewPlugin,
upgrade_panel::UpgradePanelPlugin,
));
}
Expand Down Expand Up @@ -181,8 +183,9 @@ fn exit_editor_screen(
root: Res<AppRoot>,
mut transform_query: Query<&mut Transform>,
) {
commands.entity(root.ui).despawn_descendants();
commands.remove_resource::<EditorScreenUi>();
commands.entity(root.ui).despawn_descendants();
commands.entity(root.world).despawn_descendants();

// Reset camera
let Ok(mut transform) = transform_query.get_mut(root.camera) else {
Expand Down
5 changes: 2 additions & 3 deletions src/state/editor_screen/outline_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn spawn_outline_panel(commands: &mut Commands, config: &EditorScreenConfig)
Name::new("OutlineHeader"),
TextBundle {
text: Text::from_section(
"Outline",
"",
TextStyle {
font: BOLD_FONT_HANDLE,
color: config.outline_panel_text_color,
Expand All @@ -65,7 +65,6 @@ pub fn spawn_outline_panel(commands: &mut Commands, config: &EditorScreenConfig)
// Hiding this because it looks bad :(
// display: Display::None,
margin: UiRect::bottom(Val::Px(10.0)),
align_self: AlignSelf::Center,
..default()
},
..default()
Expand Down Expand Up @@ -159,7 +158,7 @@ fn update_outline_header(
simulation: Res<Simulation>,
mut info_bar_query: Query<&mut Text, With<IsOutlineHeader>>,
) {
let info = format!("Installed ({})", simulation.plugins);
let info = format!("Installed ({})", simulation.upgrades);

for mut text in &mut info_bar_query {
text.sections[0].value = info.clone();
Expand Down
34 changes: 34 additions & 0 deletions src/state/editor_screen/scene_view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
use bevy::prelude::*;
use bevy_mod_picking::prelude::*;

use crate::state::editor_screen::EditorScreenConfig;
use crate::AppRoot;

pub struct SceneViewPlugin;

impl Plugin for SceneViewPlugin {
fn build(&self, app: &mut App) {
app.register_type::<ClickSpawnEvent>()
.add_event::<ClickSpawnEvent>();
}
}

#[derive(Event, Reflect)]
pub struct ClickSpawnEvent(pub Vec2);

pub fn spawn_scene_view(commands: &mut Commands, _config: &EditorScreenConfig) -> Entity {
let scene_view = commands
Expand All @@ -13,8 +27,28 @@ pub fn spawn_scene_view(commands: &mut Commands, _config: &EditorScreenConfig) -
},
..default()
},
On::<Pointer<Click>>::run(click_spawn),
))
.id();

scene_view
}

fn click_spawn(
mut events: EventWriter<ClickSpawnEvent>,
root: Res<AppRoot>,
window_query: Query<&Window>,
camera_query: Query<(&Camera, &GlobalTransform)>,
) {
let window = window_query.get(root.window).unwrap();
let (camera, camera_gt) = camera_query.get(root.camera).unwrap();

let cursor_pos = window.cursor_position().unwrap();
let world_pos = camera
.viewport_to_world(camera_gt, cursor_pos)
.unwrap()
.origin
.truncate();

events.send(ClickSpawnEvent(world_pos));
}
16 changes: 9 additions & 7 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ impl UpgradeList {
#[derive(Reflect, Clone, Copy)]
pub enum UpgradeKind {
DarkMode,
TouchOfLife,
BurstOfLife,
TouchOfLifePlugin,
BurstOfLifePlugin,
ImportLibrary,
}

fn load_upgrade_list(world: &mut World) {
Expand All @@ -100,24 +101,25 @@ fn load_upgrade_list(world: &mut World) {
enable: Some(world.register_system(dark_mode_enable)),
update: None,

next_upgrade: Some(UpgradeKind::TouchOfLife),
next_upgrade: Some(UpgradeKind::TouchOfLifePlugin),
},
Upgrade {
name: "Touch of Life".to_string(),
name: "TouchOfLifePlugin".to_string(),
description: "Spawns 1 entity wherever you click in the scene view.".to_string(),

base_cost: 1.0,
weight: 0.0,
remaining: 1,

// TODO: This is stil no-op
enable: None,
enable: Some(world.register_system(|mut simulation: ResMut<Simulation>| {
simulation.spawns_per_click += 1
})),
update: None,

next_upgrade: None,
},
Upgrade {
name: "Burst of Life".to_string(),
name: "BurstOfLifePlugin".to_string(),
description: "Spawns 10 entities immediately.".to_string(),

base_cost: 1.0,
Expand Down

0 comments on commit 994be78

Please sign in to comment.