Skip to content

Commit

Permalink
Reset entity pool on restart, & change project name
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Dec 9, 2023
1 parent 2203c5b commit efd0dd8
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ on:

env:
EXE_NAME: run
GAME_NAME: bevy-jam-4
GAME_NAME: bevy-jam-4-simulator
ADD_BINARIES_TO_GITHUB_RELEASE: true
ITCH_TARGET: pyrious/bevy-jam-4
ITCH_TARGET: pyrious/bevy-jam-4-simulator

jobs:
get-version:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "bevy_jam4"
name = "bevy_jam_4_simulator"
version = "0.1.0"
edition = "2021"
default-run = "run"
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -u
shopt -s globstar

NAME='bevy-jam-4'
NAME='bevy-jam-4-simulator'
EXE='run'
BUILD_DIR='build'

Expand Down
2 changes: 1 addition & 1 deletion src/bin/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use bevy::app::MainScheduleOrder;
use bevy::ecs::schedule::ScheduleLabel;
use bevy::prelude::*;
use bevy_jam4::AppPlugin;
use bevy_jam_4_simulator::AppPlugin;
use bevy_mod_debugdump::schedule_graph::Settings;
use bevy_mod_debugdump::schedule_graph_dot;

Expand Down
2 changes: 1 addition & 1 deletion src/bin/run.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_jam4::AppPlugin;
use bevy_jam_4_simulator::AppPlugin;

fn main() {
App::new().add_plugins(AppPlugin).run();
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Plugin for ConfigPlugin {
}
}

const WINDOW_TITLE: &str = "bevy_jam4";
const WINDOW_TITLE: &str = "Bevy Jam #4 Simulator";

// TODO: DevConfig
#[derive(Resource, Default, Reflect, Serialize, Deserialize)]
Expand Down
56 changes: 36 additions & 20 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use crate::simulation::sprite_pack::SkinSet;
pub use crate::simulation::sprite_pack::SpritePack;
pub use crate::simulation::sprite_pack::SpritePackAssets;
pub use crate::simulation::sprite_pack::SpritePackEvent;
use crate::spawn_logical_entities;
use crate::state::editor_screen::SceneViewBounds;
use crate::state::editor_screen::WrapWithinSceneView;
use crate::state::AppState;
Expand All @@ -27,12 +28,16 @@ pub struct SimulationPlugin;
impl Plugin for SimulationPlugin {
fn build(&self, app: &mut App) {
app.register_type::<SpawnEvent>()
.register_type::<EntityPool>()
.add_plugins(sprite_pack::SpritePackPlugin)
.add_event::<SpawnEvent>()
.add_event::<LinesAddedEvent>()
.init_resource::<EntityPool>()
.init_resource::<Simulation>()
.init_resource::<PassiveCodeTyper>()
.init_resource::<PassiveEntitySpawner>()
.add_systems(Startup, spawn_entity_pool.after(spawn_logical_entities))
.add_systems(OnExit(AppState::EditorScreen), reset_entity_pool)
.add_systems(
Update,
(
Expand Down Expand Up @@ -112,6 +117,9 @@ impl Default for Simulation {
}
}

const ENTITY_CAP: usize = 10_000;

#[derive(Resource, Reflect)]
struct EntityPool {
entities: Vec<Entity>,
old_idx: usize,
Expand All @@ -120,7 +128,7 @@ struct EntityPool {
impl Default for EntityPool {
fn default() -> Self {
Self {
entities: Vec::with_capacity(20_000),
entities: Vec::with_capacity(ENTITY_CAP),
old_idx: 0,
}
}
Expand All @@ -136,23 +144,9 @@ impl EntityPool {
}
}

/// Maximum number of entities that can be spawned in the scene view in a single SpawnEvent.
const MAX_SPAWN_PER_EVENT: usize = 20;

#[derive(Event, Reflect, Clone, Copy)]
pub struct SpawnEvent {
pub position: Vec2,
pub count: f64,
}

fn spawn_entities(
world: &mut World,
mut pool: Local<EntityPool>,
mut reader: Local<ManualEventReader<SpawnEvent>>,
) {
// Fill entity pool initially
let capacity = pool.entities.capacity() - pool.entities.len();
if capacity > 0 {
fn spawn_entity_pool(world: &mut World) {
world.resource_scope(|world: &mut World, mut pool: Mut<EntityPool>| {
let capacity = pool.entities.capacity() - pool.entities.len();
pool.entities.extend(
world.spawn_batch(
std::iter::repeat((
Expand All @@ -175,11 +169,31 @@ fn spawn_entities(
);

let parent = world.resource::<AppRoot>().world;
for entity in pool.entities.iter().copied() {
for &entity in &pool.entities {
world.entity_mut(entity).set_parent(parent);
}
});
}

fn reset_entity_pool(pool: Res<EntityPool>, mut visibility_query: Query<&mut Visibility>) {
for &entity in &pool.entities {
let Ok(mut visibility) = visibility_query.get_mut(entity) else {
continue;
};
*visibility = Visibility::Hidden;
}
}

/// Maximum number of entities that can be spawned in the scene view in a single SpawnEvent.
const MAX_SPAWN_PER_EVENT: usize = 16;

#[derive(Event, Reflect, Clone, Copy)]
pub struct SpawnEvent {
pub position: Vec2,
pub count: f64,
}

fn spawn_entities(world: &mut World, mut reader: Local<ManualEventReader<SpawnEvent>>) {
let mut rng = SmallRng::from_entropy();
for event in reader
.read(world.resource::<Events<_>>())
Expand Down Expand Up @@ -220,8 +234,10 @@ fn spawn_entities(
));
}

// TODO: Technically, we don't need to set the velocity. We can assign random velocities in spawn_entity_pool
for (visibility, transform, velocity, sprite, texture) in bundles {
let mut entity = world.entity_mut(pool.recycle());
let entity = world.resource_mut::<EntityPool>().recycle();
let mut entity = world.entity_mut(entity);
*entity.get_mut::<Visibility>().unwrap() = visibility;
*entity.get_mut::<Transform>().unwrap() = transform;
*entity.get_mut::<Velocity>().unwrap() = velocity;
Expand Down
1 change: 0 additions & 1 deletion src/state/editor_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ fn exit_editor_screen(
mut transform_query: Query<&mut Transform>,
) {
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
2 changes: 2 additions & 0 deletions src/state/results_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::Deserialize;
use serde::Serialize;

use crate::config::Config;
use crate::physics::PhysicsSettings;
use crate::simulation::PassiveCodeTyper;
use crate::simulation::PassiveEntitySpawner;
use crate::simulation::Simulation;
Expand Down Expand Up @@ -266,6 +267,7 @@ fn exit_results_screen(mut commands: Commands, root: Res<AppRoot>) {

// Reset resources so replaying works
commands.insert_resource(Simulation::default());
commands.insert_resource(PhysicsSettings::default());
commands.insert_resource(PassiveCodeTyper::default());
commands.insert_resource(PassiveEntitySpawner::default());
commands.insert_resource(UpgradeOutline::default());
Expand Down
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head>
<meta charset="utf-8" />
<title>bevy_jam4</title>
<title>Bevy Jam #4 Simulator</title>
<link rel="stylesheet" href="style.css" />
</head>

Expand Down

0 comments on commit efd0dd8

Please sign in to comment.