Files
space-game/src/plugins/game.rs
2025-07-05 16:04:43 -04:00

163 lines
4.2 KiB
Rust

use crate::{
components::{
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, MainCamera},
player::PlayerDrag,
tags::{Player, Ship},
},
plugins::{state_management::StateManagementPlugin, *},
states::{
game::*,
input::{InputState, PlayerState},
menu::MenuState,
play::PlayStartupSystems,
},
};
use bevy::{
prelude::*,
window::{CursorGrabMode, PrimaryWindow},
};
use bevy_asset_loader::prelude::*;
use bevy_rapier3d::prelude::*;
#[derive(Default)]
pub struct GamePlugin;
impl Plugin for GamePlugin {
fn build(&self, app: &mut bevy::app::App) {
app.add_plugins((
FollowCamPlugin,
CameraPlugin,
StateManagementPlugin,
MainMenuPlugin,
// ShipPlugin,
TypesPlugin,
PlayerPlugin,
));
app.add_plugins((
RapierPhysicsPlugin::<NoUserData>::default(),
#[cfg(feature = "dev-phys")]
RapierDebugRenderPlugin::default(),
));
app.add_systems(Update, (setup_scene, spawn_ship).chain().in_set(PlayStartupSystems));
}
}
fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut window: Single<&mut Window, With<PrimaryWindow>>,
) {
window.cursor_options.visible = false;
window.cursor_options.grab_mode = CursorGrabMode::Locked;
let player_eye = commands
.spawn((
Name::new("Eye"),
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
CameraPitch::default(),
))
.id();
let mut player_commands = commands.spawn((
Name::new("Player"),
Player,
PlayerDrag(0.5),
Collider::capsule_y(0.5, 0.5),
RigidBody::KinematicPositionBased,
KinematicCharacterController::default(),
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
));
player_commands.add_child(player_eye);
commands.spawn((
Name::new("Camera"),
Transform::from_xyz(0.0, 1.3, 0.0),
Visibility::default(),
MainCamera,
Camera3d::default(),
CameraMode::Player,
CameraAttachment(player_eye),
));
commands.spawn((
DirectionalLight {
shadows_enabled: true,
..default()
},
Transform::default().looking_to(
Dir3::from_xyz(-1.0, -1.0, -1.0).expect("Invaid Direction for light"),
Dir3::Y,
),
));
let cube = meshes.add(Cuboid::new(100.0, 0.1, 100.0));
let material = materials.add(Color::WHITE);
commands.spawn((
Mesh3d(cube),
MeshMaterial3d(material),
Transform::default(),
RigidBody::Fixed,
Collider::cuboid(100.0, 0.1, 100.0),
));
}
fn spawn_ship(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let window_material = materials.add(Color::linear_rgba(1.0, 0.0, 1.0, 0.5));
let material = materials.add(Color::BLACK);
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
MeshMaterial3d(material.clone()),
Name::new("Ship"),
Ship,
Velocity::zero(),
Damping::default(),
Transform::from_xyz(0.0, 1.0, 0.0),
RigidBody::Dynamic,
children![
(
Name::new("Back Wall"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
),
(
Name::new("Front Window"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(window_material),
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
),
(
Name::new("Right Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Left Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Roof"),
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
Collider::cuboid(3.0, 0.1, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 2.0, 0.0),
)
],
));
}