//! Eat the cakes. Eat them all. An example 3D game. use std::f32::consts::PI; use bevy::prelude::*; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; #[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)] enum GameState { #[default] Playing, GameOver, } #[derive(Resource)] struct BonusSpawnTimer(Timer); fn main() { App::new() .add_plugins(DefaultPlugins) .init_resource::() .insert_resource(BonusSpawnTimer(Timer::from_seconds( 5.0, TimerMode::Repeating, ))) .init_state::() .add_systems(Startup, setup_cameras) .add_systems(OnEnter(GameState::Playing), setup) .add_systems( Update, ( move_player, focus_camera, rotate_bonus, scoreboard_system, spawn_bonus, ) .run_if(in_state(GameState::Playing)), ) .add_systems(OnExit(GameState::Playing), teardown) .add_systems(OnEnter(GameState::GameOver), display_score) .add_systems( Update, gameover_keyboard.run_if(in_state(GameState::GameOver)), ) .add_systems(OnExit(GameState::GameOver), teardown) .run(); } struct Cell { height: f32, } #[derive(Default)] struct Player { entity: Option, i: usize, j: usize, move_cooldown: Timer, } #[derive(Default)] struct Bonus { entity: Option, i: usize, j: usize, handle: Handle, } #[derive(Resource, Default)] struct Game { board: Vec>, player: Player, bonus: Bonus, score: i32, cake_eaten: u32, camera_should_focus: Vec3, camera_is_focus: Vec3, } #[derive(Resource, Deref, DerefMut)] struct Random(ChaCha8Rng); const BOARD_SIZE_I: usize = 14; const BOARD_SIZE_J: usize = 21; const RESET_FOCUS: [f32; 3] = [ BOARD_SIZE_I as f32 / 2.0, 0.0, BOARD_SIZE_J as f32 / 2.0 - 0.5, ]; fn setup_cameras(mut commands: Commands, mut game: ResMut) { game.camera_should_focus = Vec3::from(RESET_FOCUS); game.camera_is_focus = game.camera_should_focus; commands.spawn(Camera3dBundle { transform: Transform::from_xyz( -(BOARD_SIZE_I as f32 / 2.0), 2.0 * BOARD_SIZE_J as f32 / 3.0, BOARD_SIZE_J as f32 / 2.0 - 0.5, ) .looking_at(game.camera_is_focus, Vec3::Y), ..default() }); } fn setup(mut commands: Commands, asset_server: Res, mut game: ResMut) { let mut rng = if std::env::var("GITHUB_ACTIONS") == Ok("true".to_string()) { // We're seeding the PRNG here to make this example deterministic for testing purposes. // This isn't strictly required in practical use unless you need your app to be deterministic. ChaCha8Rng::seed_from_u64(19878367467713) } else { ChaCha8Rng::from_entropy() }; // reset the game state game.cake_eaten = 0; game.score = 0; game.player.i = BOARD_SIZE_I / 2; game.player.j = BOARD_SIZE_J / 2; game.player.move_cooldown = Timer::from_seconds(0.3, TimerMode::Once); commands.spawn(PointLightBundle { transform: Transform::from_xyz(4.0, 10.0, 4.0), point_light: PointLight { intensity: 2_000_000.0, shadows_enabled: true, range: 30.0, ..default() }, ..default() }); // spawn the game board let cell_scene = asset_server.load("models/AlienCake/tile.glb#Scene0"); game.board = (0..BOARD_SIZE_J) .map(|j| { (0..BOARD_SIZE_I) .map(|i| { let height = rng.gen_range(-0.1..0.1); commands.spawn(SceneBundle { transform: Transform::from_xyz(i as f32, height - 0.2, j as f32), scene: cell_scene.clone(), ..default() }); Cell { height } }) .collect() }) .collect(); // spawn the game character game.player.entity = Some( commands .spawn(SceneBundle { transform: Transform { translation: Vec3::new( game.player.i as f32, game.board[game.player.j][game.player.i].height, game.player.j as f32, ), rotation: Quat::from_rotation_y(-PI / 2.), ..default() }, scene: asset_server.load("models/AlienCake/alien.glb#Scene0"), ..default() }) .id(), ); // load the scene for the cake game.bonus.handle = asset_server.load("models/AlienCake/cakeBirthday.glb#Scene0"); // scoreboard commands.spawn( TextBundle::from_section( "Score:", TextStyle { font_size: 40.0, color: Color::srgb(0.5, 0.5, 1.0), ..default() }, ) .with_style(Style { position_type: PositionType::Absolute, top: Val::Px(5.0), left: Val::Px(5.0), ..default() }), ); commands.insert_resource(Random(rng)); } // remove all entities that are not a camera or window fn teardown(mut commands: Commands, entities: Query, Without)>) { for entity in &entities { commands.entity(entity).despawn(); } } // control the game character fn move_player( mut commands: Commands, keyboard_input: Res>, mut game: ResMut, mut transforms: Query<&mut Transform>, time: Res