Compare commits

3 Commits

Author SHA1 Message Date
bd72373178 boom 2026-04-16 09:51:35 +02:00
1dc3b86994 pixelorama file for sewertiles (need export) 2024-11-14 10:24:41 +02:00
e9a1e1379f Miguel's state of mind 2024-11-14 10:24:11 +02:00
6 changed files with 183 additions and 2 deletions

Binary file not shown.

BIN
assets/fonts/Miracode.ttf Normal file

Binary file not shown.

View File

@@ -7,7 +7,9 @@ use goal::{check_goal, GoalBundle};
use ground_detection::GroundDetectionPlugin; use ground_detection::GroundDetectionPlugin;
use level_structure::{WallBundle, WallPlugin}; use level_structure::{WallBundle, WallPlugin};
use logging::log_positions; use logging::log_positions;
use menu::{spawn_box, MenuPlugin};
use player::PlayerPlugin; use player::PlayerPlugin;
use state::GameState;
mod animations; mod animations;
mod camera; mod camera;
@@ -16,13 +18,15 @@ mod goal;
mod ground_detection; mod ground_detection;
mod level_structure; mod level_structure;
mod logging; mod logging;
mod menu;
mod player; mod player;
mod state;
pub const GRID_SIZE: i32 = 16; pub const GRID_SIZE: i32 = 16;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.insert_state(GameState::MainMenu)
.add_plugins(( .add_plugins((
LdtkPlugin, LdtkPlugin,
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0), RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0),
@@ -39,6 +43,7 @@ fn main() {
scaled_shape_subdivision: 10, scaled_shape_subdivision: 10,
force_update_from_transform_changes: false, force_update_from_transform_changes: false,
}) })
.add_plugins(MenuPlugin)
.add_plugins(PlayerPlugin) .add_plugins(PlayerPlugin)
.add_plugins(WallPlugin) .add_plugins(WallPlugin)
.add_plugins(GroundDetectionPlugin) .add_plugins(GroundDetectionPlugin)
@@ -46,6 +51,7 @@ fn main() {
.register_ldtk_entity::<GoalBundle>("Goal") .register_ldtk_entity::<GoalBundle>("Goal")
.register_ldtk_int_cell::<WallBundle>(1) .register_ldtk_int_cell::<WallBundle>(1)
.add_systems(Startup, setup) .add_systems(Startup, setup)
// .add_systems(Update, spawn_box.run_if(in_state(GameState::MainMenu)))
.add_plugins(CameraPlugin) .add_plugins(CameraPlugin)
.add_plugins(ShockingAnimationPlugin) .add_plugins(ShockingAnimationPlugin)
.add_systems(Update, (translate_grid_coords_entities, check_goal)) .add_systems(Update, (translate_grid_coords_entities, check_goal))

154
src/menu.rs Normal file
View File

@@ -0,0 +1,154 @@
use bevy::color::palettes::css::RED;
use bevy::prelude::*;
use bevy::{
color::Color,
ecs::system::Commands,
ui::{node_bundles::NodeBundle, JustifyContent, Style, UiRect, Val},
};
use crate::state::GameState;
pub(crate) fn spawn_box(mut commands: Commands) {
let container = NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::Center,
..default()
},
..default()
};
let square = NodeBundle {
style: Style {
width: Val::Px(200.),
border: UiRect::all(Val::Px(2.)),
..default()
},
background_color: Color::srgb(0., 0.65, 0.65).into(),
..default()
};
let parent = commands.spawn(container).id();
let child = commands.spawn(square).id();
commands.entity(parent).push_children(&[child]);
}
fn spawn_button(mut commands: Commands, asset_server: Res<AssetServer>) {
let button_bundle = ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(5.0)),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
..default()
},
border_color: BorderColor(Color::BLACK),
border_radius: BorderRadius::MAX,
background_color: NORMAL_BUTTON.into(),
..default()
};
let text = TextBundle::from_section(
"Button",
TextStyle {
font: asset_server.load("fonts/Miracode.ttf"),
font_size: 40.0,
color: Color::srgb(0.9, 0.9, 0.9),
},
);
commands
.spawn(NodeBundle {
style:
Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
..default()
})
.with_children(|parent| {
parent.spawn(button_bundle).with_children(|parent| {
parent.spawn(text);
});
});
}
pub(crate) fn spawn_text(mut commands: Commands) {
let text = "Hello world!";
commands.spawn(
TextBundle::from_section(
text,
TextStyle {
font_size: 100.0,
color: Color::WHITE,
..default()
},
) // Set the alignment of the Text
.with_text_justify(JustifyText::Center)
// Set the style of the TextBundle itself.
.with_style(Style {
position_type: PositionType::Absolute,
bottom: Val::Px(5.0),
right: Val::Px(5.0),
..default()
}),
);
}
const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25);
const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);
fn button_system(
mut interaction_query: Query<
(
&Interaction,
&mut BackgroundColor,
&mut BorderColor,
&Children,
),
(Changed<Interaction>, With<Button>),
>,
button_query: Query<(Entity, &Node)>,
mut text_query: Query<&mut Text>,
mut next_state: ResMut<NextState<GameState>>,
mut commands: Commands,
) {
for (interaction, mut color, mut border_color, children) in &mut interaction_query {
let mut text = text_query.get_mut(children[0]).unwrap();
match *interaction {
Interaction::Pressed => {
text.sections[0].value = "Starting".to_string();
*color = PRESSED_BUTTON.into();
border_color.0 = RED.into();
next_state.set(GameState::InGame);
for (entity_id, _button) in button_query.iter() {
commands.entity(entity_id).despawn();
}
}
Interaction::Hovered => {
text.sections[0].value = "Hover".to_string();
*color = HOVERED_BUTTON.into();
border_color.0 = Color::WHITE;
}
Interaction::None => {
text.sections[0].value = "Button".to_string();
*color = NORMAL_BUTTON.into();
border_color.0 = Color::BLACK;
}
}
}
}
pub struct MenuPlugin;
impl Plugin for MenuPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, button_system)
.add_systems(Startup, spawn_button);
}
}

View File

@@ -6,13 +6,14 @@ use crate::{
animations::{AnimationIndices, AnimationTimer}, animations::{AnimationIndices, AnimationTimer},
colliders::ColliderBundle, colliders::ColliderBundle,
ground_detection::GroundDetection, ground_detection::GroundDetection,
state::GameState,
}; };
pub struct PlayerPlugin; pub struct PlayerPlugin;
impl Plugin for PlayerPlugin { impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Update, player_movement) app.add_systems(Update, player_movement.run_if(in_state(GameState::InGame)))
.register_ldtk_entity::<PlayerBundle>("Player"); .register_ldtk_entity::<PlayerBundle>("Player");
} }
} }

20
src/state.rs Normal file
View File

@@ -0,0 +1,20 @@
use bevy::{
ecs::system::ResMut,
state::state::{NextState, States},
};
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, States)]
pub(crate) enum GameState {
#[default]
MainMenu,
InGame,
GamePaused,
}
fn start_game(mut next_state: ResMut<NextState<GameState>>) {
next_state.set(GameState::InGame);
}
fn pause_game(mut next_state: ResMut<NextState<GameState>>) {
next_state.set(GameState::GamePaused);
}