Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
/// Z-value for tile layer 1.
pub const TILE_LAYER_1_Z: f32 = 1.0;
pub const TILE_LAYER_1_Z: f32 = 1.;

/// Z-value for tile layer 2.
pub const TILE_LAYER_2_Z: f32 = 2.0;
pub const TILE_LAYER_2_Z: f32 = 2.;

/// Z-value for tile layer 3.
pub const TILE_LAYER_3_Z: f32 = 3.0;
pub const TILE_LAYER_3_Z: f32 = 3.;

/// Z-value for all projectiles.
pub const PROJECTILES_Z: f32 = 20.0;
pub const PROJECTILES_Z: f32 = 20.;

/// Z-value for player character(s).
pub const PLAYER_Z: f32 = 10.0;
pub const PLAYER_Z: f32 = 10.;

/// Z-value for enemy characters.
pub const ENEMY_Z: f32 = 10.0;
pub const ENEMY_Z: f32 = 10.;

/// Z-value for loot drops.
pub const LOOT_DROPS_Z: f32 = 10.0;
pub const LOOT_DROPS_Z: f32 = 10.;

/// Z-value for FPS Counter.
pub const FPS_COUNTER_Z: i32 = 100;

/// Location of save file.
pub const SAVE_FILE: &'static str = "save/save_file.json";
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ use sound::sound_plugin::SoundPlugin;
use sprites::add_sprite;
use start_game::GamePlugin;
use std::time::Duration;
use tools::damage_tracking::DamageTracker;
use tools::rng::{GameRng, RngPlugin};
use tools::{damage_tracking::DamageTracker, fps_counter_plugin::FPSCouterPlugin};
use ui::{start_menu::StartMenuPlugin, upgrade_plugin::UpgradePlugin};
use winit::window::Icon;

fn main() {
App::new()
.add_plugins(FPSCouterPlugin)
.add_plugins(GamePlugin)
.add_plugins(SoundPlugin)
.insert_state(AppState::MainMenu)
Expand Down
5 changes: 3 additions & 2 deletions src/mechanics/damage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ fn overlapping(
} else {
let proj = v.dot(mid_angle);
let proj_ortho = v.dot(mid_angle.rotate(Vec2::new(0., 1.)));
0. <= proj && proj <= mid_angle.length_squared()
&& proj_ortho*proj_ortho <= radius2*radius2*mid_angle.length_squared()
0. <= proj
&& proj <= mid_angle.length_squared()
&& proj_ortho * proj_ortho <= radius2 * radius2 * mid_angle.length_squared()
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/mobs/boss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub fn wizard_bundle(x: f32, y: f32) -> impl Bundle {
TakeDamageHitbox(Circle {
radius: Vec2::new(WIZARD_HEIGHT as f32, WIZARD_WIDTH as f32).length() / 2.,
}),
DealDamageHitbox::Cone(Cone { mid_angle: Vec2::new(50., 50.), angular_width: (1./8.)*PI }),
DealDamageHitbox::Cone(Cone {
mid_angle: Vec2::new(50., 50.),
angular_width: (1. / 8.) * PI,
}),
Transform::from_xyz(x, y, ENEMY_Z),
SpriteKind::Character(Character::Wizard),
EndGameIfDead,
Expand Down
5 changes: 1 addition & 4 deletions src/start_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ impl Plugin for RunningPlugin {
fn build(&self, app: &mut App) {
const STATE: GameState = GameState::Running;
app.add_plugins(CooldownPlugin)
.add_plugins((
DamagePlugin,
DebugPlugin
))
.add_plugins((DamagePlugin, DebugPlugin))
.add_systems(
Update,
(
Expand Down
8 changes: 5 additions & 3 deletions src/tools/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn show_weakness(
blue: 0.,
alpha: 0.3,
}));
for (TakeDamageHitbox(damage::Circle{radius}), ent) in &q {
for (TakeDamageHitbox(damage::Circle { radius }), ent) in &q {
if let Some(mut inent) = commands.get_entity(ent) {
inent.insert(ShowWeaknessHitbox);
inent.with_children(|parent| {
Expand Down Expand Up @@ -74,10 +74,12 @@ fn show_damaging(
angular_width,
}) => {
let mut tf = Transform::from_xyz(0., 0., 100.);
tf.rotate_z(2.*PI-mid_angle.to_angle().rem_euclid(2.*PI));
tf.rotate_z(2. * PI - mid_angle.to_angle().rem_euclid(2. * PI));
inent.with_children(|parent| {
parent.spawn((
Mesh2d(meshes.add(CircularSector::new(mid_angle.length(), *angular_width))),
Mesh2d(
meshes.add(CircularSector::new(mid_angle.length(), *angular_width)),
),
MeshMaterial2d(color.clone()),
tf,
));
Expand Down
158 changes: 158 additions & 0 deletions src/tools/fps_counter_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use bevy::{
app::Plugin,
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
prelude::*,
};
use test_game::FPS_COUNTER_Z;

/// Plugin for enabling a fps counter in the game.
/// This fps counter can be toggled on/off by pressing the `f12` key.
pub struct FPSCouterPlugin;

impl Plugin for FPSCouterPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(FrameTimeDiagnosticsPlugin)
.add_systems(Startup, setup_fps_counter)
.add_systems(
Update,
(
fps_counter_showhide,
fps_text_update_system,
fps_color_update_system,
),
);
}
}

/// Marker to find the container entity so we can show/hide the FPS counter
#[derive(Component)]
struct FpsRoot;

/// Marker to find the text entity so we can update it
#[derive(Component)]
struct FpsText;

#[derive(Component)]
struct ColorText;

fn setup_fps_counter(mut commands: Commands, asset_server: Res<AssetServer>) {
commands
.spawn((
FpsRoot,
Node {
position_type: PositionType::Absolute,
right: Val::Percent(1.),
top: Val::Percent(1.),
bottom: Val::Auto,
left: Val::Auto,
padding: UiRect::all(Val::Px(4.0)),
..Default::default()
},
ZIndex(FPS_COUNTER_Z),
BackgroundColor(Color::Srgba(Srgba {
red: 0.0,
green: 0.0,
blue: 0.0,
alpha: 0.8,
})),
))
.with_children(|child| {
child
.spawn((
Text::new("FPS: "),
TextFont {
font: asset_server.load("font/pixel-font.ttf"),
font_size: 16.0,
..Default::default()
},
ColorText,
))
.with_child((
TextSpan::default(),
TextFont {
font: asset_server.load("font/pixel-font.ttf"),
font_size: 16.0,
..Default::default()
},
FpsText,
ColorText,
));
});
}

fn fps_text_update_system(
diagnostics: Res<DiagnosticsStore>,
mut query_span: Query<&mut TextSpan, With<FpsText>>,
) {
for mut span in &mut query_span {
// try to get a "smoothed" FPS value from Bevy
if let Some(value) = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FPS)
.and_then(|fps| fps.smoothed())
{
**span = format!("{value:>4.0}");
}
}
}
fn fps_color_update_system(
diagnostics: Res<DiagnosticsStore>,
mut query_color: Query<&mut TextColor, With<ColorText>>,
) {
for mut color in &mut query_color {
// try to get a "smoothed" FPS value from Bevy
if let Some(value) = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FPS)
.and_then(|fps| fps.smoothed())
{
color.0 = if value >= 120.0 {
// Above 120 FPS, use green color
Color::Srgba(Srgba {
red: 0.,
green: 1.,
blue: 0.,
alpha: 1.,
})
} else if value >= 60.0 {
// Between 60-120 FPS, gradually transition from yellow to green
Color::Srgba(Srgba {
red: 1. - (value - 60. / (120. - 60.)) as f32,
green: 1.,
blue: 0.,
alpha: 1.,
})
} else if value >= 30.0 {
// Between 30-60 FPS, gradually transition from red to yellow
Color::Srgba(Srgba {
red: 1.,
green: ((value - 30.0) / (60.0 - 30.0)) as f32,
blue: 0.,
alpha: 1.,
})
} else {
// Below 30 FPS, use red color
Color::Srgba(Srgba {
red: 1.,
green: 0.,
blue: 0.,
alpha: 1.,
})
}
}
}
}

/// Toggle the FPS counter when pressing F12
fn fps_counter_showhide(
mut q: Query<&mut Visibility, With<FpsRoot>>,
kbd: Res<ButtonInput<KeyCode>>,
) {
if kbd.just_pressed(KeyCode::F12) {
if let Some(mut vis) = q.iter_mut().next() {
println!("f12 pressed: {:?}", vis);
*vis = match *vis {
Visibility::Hidden => Visibility::Visible,
_ => Visibility::Hidden,
};
}
}
}
1 change: 1 addition & 0 deletions src/tools/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod damage_tracking;
pub mod debug;
pub mod fps_counter_plugin;
pub mod rng;
58 changes: 38 additions & 20 deletions src/ui/start_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const GAME_TITLE: &str = "To be Announced";
enum MenuButtonAction {
Play,
Upgrade,
ExitGame,
}
#[derive(Component)]
struct MainMenuScreen;
Expand Down Expand Up @@ -47,13 +48,14 @@ pub fn render_start_menu(mut commands: Commands, asset_server: Res<AssetServer>)
.spawn((
Node {
width: Val::Percent(70.0),
height: Val::Percent(50.0),
height: Val::Percent(90.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::SpaceEvenly,
flex_direction: FlexDirection::Column,
margin: UiRect {
left: Val::Percent(0.0),
right: Val::Percent(0.0),
top: Val::Percent(10.0),
top: Val::Percent(5.0),
bottom: Val::Percent(0.0),
},
..default()
Expand All @@ -73,14 +75,8 @@ pub fn render_start_menu(mut commands: Commands, asset_server: Res<AssetServer>)
grandchild
.spawn((
Node {
width: Val::Px(300.0),
height: Val::Px(100.0),
margin: UiRect {
left: Val::Percent(0.0),
right: Val::Percent(0.0),
top: Val::Percent(15.0),
bottom: Val::Percent(0.0),
},
width: Val::Px(350.),
height: Val::Px(100.),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
Expand All @@ -93,7 +89,7 @@ pub fn render_start_menu(mut commands: Commands, asset_server: Res<AssetServer>)
great_grandchild.spawn((
Text::new("Start Game"),
TextFont {
font_size: 30.0,
font_size: 30.,
font: asset_server.load("font/pixel-font.ttf"),
..Default::default()
},
Expand All @@ -103,14 +99,8 @@ pub fn render_start_menu(mut commands: Commands, asset_server: Res<AssetServer>)
grandchild
.spawn((
Node {
width: Val::Px(300.0),
height: Val::Px(100.0),
margin: UiRect {
left: Val::Percent(0.0),
right: Val::Percent(0.0),
top: Val::Percent(15.0),
bottom: Val::Percent(0.0),
},
width: Val::Px(350.),
height: Val::Px(100.),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
Expand All @@ -123,7 +113,31 @@ pub fn render_start_menu(mut commands: Commands, asset_server: Res<AssetServer>)
great_grandchild.spawn((
Text::new("Upgrades"),
TextFont {
font_size: 30.0,
font_size: 30.,
font: asset_server.load("font/pixel-font.ttf"),
..Default::default()
},
TextColor(css::WHITE.into()),
));
});
grandchild
.spawn((
Node {
width: Val::Px(350.),
height: Val::Px(100.),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
Button,
BackgroundColor(css::MIDNIGHT_BLUE.into()),
MenuButtonAction::ExitGame,
))
.with_children(|great_grandchild| {
great_grandchild.spawn((
Text::new("Exit Game"),
TextFont {
font_size: 30.,
font: asset_server.load("font/pixel-font.ttf"),
..Default::default()
},
Expand All @@ -141,6 +155,7 @@ fn handle_button_click(
>,
mut app_state: ResMut<NextState<AppState>>,
mut sound_event: EventWriter<PlaySoundEffectEvent>,
mut exit: EventWriter<AppExit>,
) {
for (interaction, menu_button_action, mut background_color) in &mut interaction_query {
match *interaction {
Expand All @@ -151,6 +166,9 @@ fn handle_button_click(
match menu_button_action {
MenuButtonAction::Play => app_state.set(AppState::InGame),
MenuButtonAction::Upgrade => app_state.set(AppState::Upgrade),
MenuButtonAction::ExitGame => {
exit.send(AppExit::Success);
}
}
}
Interaction::Hovered => {
Expand Down
Loading
Loading