Spawn the gameplay elements

Now to start wiring them in for basic operation, then to pipe it all
through the WebSocket.
This commit is contained in:
2025-10-21 08:35:38 -05:00
parent 8b290fdd0d
commit 129e9ccc5e

View File

@@ -12,6 +12,8 @@ use crate::ui::{despawn_main_menu, spawn_main_menu};
mod ui;
const PADDLE_GAP: f32 = 20.0; // gap between the paddles and the wall (window border)
fn main() {
App::new()
.add_plugins(DefaultPlugins)
@@ -23,6 +25,7 @@ fn main() {
.add_observer(ui::button_hover_stop)
// TODO: System to operate buttons & other UI widgets
// .add_systems(Update, )
.add_systems(OnEnter(GameState::Playing), setup_game)
.add_systems(
Update,
(
@@ -65,17 +68,38 @@ fn spawn_camera(mut commands: Commands) {
}
/// Initialize the scene
fn setup(
fn setup_game(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
window: Single<&Window>,
) {
commands.spawn(Camera2d);
// ball
commands.spawn((
Mesh2d(meshes.add(Circle::new(10.0))),
MeshMaterial2d(materials.add(Color::srgb(1.0, 0.0, 0.0))),
Transform::default(),
));
let paddle_mesh = meshes.add(Rectangle::new(10.0, 100.0));
let paddle_material = materials.add(Color::WHITE);
// Player 1
commands.spawn((
// TODO: Marker component for player
// Maybe one for each player?
// Maybe it can hold the WebSocket, too.
// *Maybe* I can have one struct with an Option<Ws> to know which
// player is the local one (the one with a socket).
Mesh2d(paddle_mesh.clone()),
MeshMaterial2d(paddle_material.clone()),
Transform::from_xyz(-window.width() / 2.0 + PADDLE_GAP, 0.0, 1.0),
));
// Player 2
commands.spawn((
Mesh2d(paddle_mesh),
MeshMaterial2d(paddle_material),
Transform::from_xyz(window.width() / 2.0 - PADDLE_GAP, 0.0, 1.0),
));
}
/// ECS Component to hold the WebSocket. I guess there's going to be a magic