NoEntities even though there clearly are #7487
-
so i'm making a tetris clone for myself to practice rust and bevy. target: wasm32-unknown-unknown use bevy::prelude::*;
use std::time::Duration;
#[derive(Component)]
struct PieceComponent;
#[derive(Component)]
struct MainCamera;
#[derive(Resource)]
struct InputHoldDelayTimer {
hold_start_timer: Timer,
hold_small_delay_timer: Timer,
held_key: Option<KeyCode>,
}
impl InputHoldDelayTimer {
fn tick(&mut self, delta: Duration) {
self.hold_start_timer.tick(delta);
self.hold_small_delay_timer.tick(delta);
}
}
#[derive(Resource)]
struct GravityTimer {
timer: Timer,
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(spawn_camera)
.add_startup_system(setup)
.add_startup_system(spawn_test_piece)
.add_system(piece_system)
.run();
}
fn spawn_camera(mut commands: Commands) {
commands.spawn((Camera2dBundle::default(), MainCamera));
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
camera: Query<&Transform, With<MainCamera>>,
windows: Res<Windows>,
) {
commands.insert_resource({
let mut r = InputHoldDelayTimer {
hold_start_timer: Timer::new(Duration::from_secs_f32(1.0 / 8.0), TimerMode::Once),
hold_small_delay_timer: Timer::new(
Duration::from_secs_f32(1.0 / 32.0),
TimerMode::Repeating,
),
held_key: None,
};
r.hold_start_timer.pause();
r.hold_small_delay_timer.pause();
r
});
commands.insert_resource(GravityTimer {
timer: Timer::new(Duration::from_secs_f32(0.5), TimerMode::Repeating),
});
commands.spawn(SpriteBundle {
texture: asset_server.load("sprites/chinese/UI.png"),
transform: Transform::from_translation(window_to_world(
Vec2 { x: 0.0, y: 0.0 },
windows.primary(),
camera.single(),
)),
..default()
});
}
fn spawn_test_piece(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
SpriteBundle {
texture: asset_server.load("sprites/chinese/pieces/L_Piece.png"),
transform: Transform::from_xyz(-16.0, 32.0 * 6.0, 1.0),
..default()
},
PieceComponent {},
));
}
fn piece_system(
mut commands: Commands,
mut piece: Query<(&PieceComponent, &mut Transform)>,
time: Res<Time>,
keys: Res<Input<KeyCode>>,
mut hold_timer: ResMut<InputHoldDelayTimer>,
mut gravity_timer: ResMut<GravityTimer>,
) {
for (_, mut transform) in &mut piece {
if let Some(key) = hold_timer.held_key {
if !keys.pressed(key) {
hold_timer.hold_start_timer.pause();
hold_timer.hold_start_timer.reset();
hold_timer.hold_small_delay_timer.pause();
hold_timer.hold_small_delay_timer.reset();
}
}
if hold_timer.hold_start_timer.finished() {
if hold_timer.hold_small_delay_timer.just_finished() {
if let Some(key) = hold_timer.held_key {
match key {
KeyCode::Right => {
transform.translation.x += 32.0;
}
KeyCode::Left => {
transform.translation.x -= 32.0;
}
KeyCode::Down => {
transform.translation.y -= 32.0;
}
_ => {}
};
}
}
} else {
if keys.any_pressed(vec![KeyCode::Right, KeyCode::Left, KeyCode::Down]) {
hold_timer.hold_start_timer.unpause();
hold_timer.hold_small_delay_timer.unpause();
}
if keys.just_pressed(KeyCode::Right) {
transform.translation.x += 32.0;
hold_timer.held_key = Some(KeyCode::Right);
}
if keys.just_pressed(KeyCode::Left) {
transform.translation.x -= 32.0;
hold_timer.held_key = Some(KeyCode::Left);
}
if keys.just_pressed(KeyCode::Down) {
transform.translation.y -= 32.0;
hold_timer.held_key = Some(KeyCode::Down);
}
}
if gravity_timer.timer.just_finished() {
transform.translation.y -= 32.0;
}
hold_timer.tick(time.delta());
gravity_timer.timer.tick(time.delta());
}
}
// yeeted from stackoverflow
fn window_to_world(position: Vec2, window: &Window, camera: &Transform) -> Vec3 {
// Center in screen space
let norm = Vec3::new(
position.x - window.width() / 2.,
position.y - window.height() / 2.,
0.,
);
// Apply camera transform
*camera * norm
// Alternatively:
//camera.mul_vec3(norm)
} Panic message:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Commands are only applied at the end of the stage, so if you have two You need |
Beta Was this translation helpful? Give feedback.
Commands are only applied at the end of the stage, so if you have two
startup
systems, they won't see spawned entities from the other.You need
add_startup_system_to_stage(StartupStage::PostStartup)
for the second one, or put the first one inStartupStage::PreStartup
.