Skip to content

Commit

Permalink
Merge branch 'master' into lunacys/metaprogression
Browse files Browse the repository at this point in the history
  • Loading branch information
lunacys authored Sep 8, 2024
2 parents 1d73f26 + cb90b93 commit 6b8e257
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 6 deletions.
3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
vs-rs = wrap vs-rs-unwrapped;
in {
devShells.default = craneLib.devShell {
inherit LD_LIBRARY_PATH buildInputs;
inherit LD_LIBRARY_PATH;
buildInputs = buildInputs ++ [pkgs.rust-analyzer];
};

packages = {
Expand Down
23 changes: 23 additions & 0 deletions vs-assets/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub struct GameAssets {
pub money_texture: Handle<Image>,
}

#[derive(Default, Resource)]
pub struct UiAssets {
pub health_bar: Handle<Image>,
pub health_bar_outline: Handle<Image>,
}

#[derive(Default, Resource)]
pub struct Configs {
pub enemy_config: Handle<EnemyConfig>,
Expand All @@ -53,6 +59,7 @@ pub struct Configs {
pub struct GameAssetFolders {
pub tiles_folder: Handle<LoadedFolder>,
pub rooms_folder: Handle<LoadedFolder>,
pub ui_folder: Handle<LoadedFolder>,
pub tileset_main: Handle<TsxTilesetAsset>,
pub tiles_loaded: bool,
pub rooms_loaded: bool,
Expand Down Expand Up @@ -90,13 +97,15 @@ fn start_loading(
info!("Loading game asset folders");
let tiles_folder_handle = asset_server.load_folder("textures");
let rooms_folder_handle = asset_server.load_folder("rooms");
let ui_folder_handle = asset_server.load_folder("ui");
let tileset_main = asset_server.load("tilesheet.tsx");

configs.enemy_config = asset_server.load("configs/enemies.json");

let asset_folders = GameAssetFolders {
tiles_folder: tiles_folder_handle,
rooms_folder: rooms_folder_handle,
ui_folder: ui_folder_handle,
tileset_main,
..default()
};
Expand Down Expand Up @@ -152,6 +161,20 @@ fn setup_game_assets(

commands.insert_resource(game_assets);

let health_bar = asset_server
.get_handle::<Image>("ui/health_bar.png")
.unwrap();
let health_bar_outline = asset_server
.get_handle::<Image>("ui/health_bar_outline.png")
.unwrap();

let ui_assets = UiAssets {
health_bar,
health_bar_outline,
};

commands.insert_resource(ui_assets);

for (id, map) in rooms.iter() {
info!(
"Map id {} ({}x{})",
Expand Down
Binary file added vs-rs/assets/ui/health_bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added vs-rs/assets/ui/health_bar_outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions vs-rs/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ fn close_on_esc(
}
}

#[derive(Component)]
struct CapybarasText;

fn add_enemy_count(mut commands: Commands) {
commands.spawn(
commands.spawn((
TextBundle::from_section(
"Capybaras: ",
TextStyle {
Expand All @@ -68,7 +71,8 @@ fn add_enemy_count(mut commands: Commands) {
left: Val::Px(5.0),
..default()
}),
);
CapybarasText,
));

commands.spawn((
TextBundle::from_sections([
Expand All @@ -92,7 +96,7 @@ fn add_enemy_count(mut commands: Commands) {

fn update_enemy_count(
enemies: Query<&Enemy, (With<Enemy>, Without<Player>)>,
mut text: Query<&mut Text, Without<FpsText>>,
mut text: Query<&mut Text, (With<CapybarasText>, Without<FpsText>)>,
) {
if let Ok(mut text) = text.get_single_mut() {
text.sections[0].value = format!("Capybaras: {}", enemies.iter().count());
Expand Down
3 changes: 3 additions & 0 deletions vs-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ mod player;
mod prelude;
mod stats;
mod worlds;
mod ui;

use crate::enemy::EnemyPlugin;
use camera::CameraMovementPlugin;
#[cfg(debug_assertions)]
use debug::DebugPlugin;
use player::PlayerPlugin;
use ui::UiPlugin;

pub const FRAMERATE: f64 = 60.0;
pub const FIXED_TIMESTEP: f64 = 1.0 / FRAMERATE;
Expand Down Expand Up @@ -60,6 +62,7 @@ fn main() {
.add_plugins(FrameTimeDiagnosticsPlugin)
.add_plugins(SimpleTileMapPlugin)
.add_plugins(input::InputPlugin)
.add_plugins(UiPlugin)
.add_plugins(CameraMovementPlugin)
.add_plugins(PlayerPlugin)
.add_plugins(PhysicsPlugin)
Expand Down
11 changes: 9 additions & 2 deletions vs-rs/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::enemy::Enemy;
use crate::input::PlayerControls;
use crate::stats::*;
use crate::AppState;
use crate::ui::health_bar::spawn_health_bar;
use behaviors::SteerSeek;
use bevy::sprite::Anchor;
use bevy::{input::gamepad::GamepadSettings, prelude::*};
Expand All @@ -13,6 +14,7 @@ use std::time::Duration;
use steering::SteeringBundle;
use steering::SteeringTargetVec2;
use vs_assets::plugin::GameAssets;
use vs_assets::plugin::UiAssets;

pub struct PlayerPlugin;

Expand Down Expand Up @@ -44,6 +46,7 @@ struct Direction(Vec2);
struct PlayerBundle {
player: Player,
health: Health,
max_health: MaxHealth,
inv_timer: PlTimer,
direction: Direction,
exp: Experience,
Expand All @@ -55,6 +58,7 @@ impl PlayerBundle {
Self {
player: Player,
health: Health(100),
max_health: MaxHealth(100),
inv_timer: PlTimer(Timer::new(Duration::from_millis(500), TimerMode::Once)),
direction: Direction(Vec2::ZERO),
exp: Experience(0),
Expand All @@ -63,7 +67,7 @@ impl PlayerBundle {
}
}

fn spawn(mut commands: Commands, assets: Res<GameAssets>) {
fn spawn(mut commands: Commands, assets: Res<GameAssets>, ui_assets: Res<UiAssets>) {
let player_tileset = &assets.player_tilesheet;

commands
Expand Down Expand Up @@ -112,7 +116,10 @@ fn spawn(mut commands: Commands, assets: Res<GameAssets>) {
},
));
})
.observe(on_collision);
.observe(on_collision)
.with_children(|c| {
spawn_health_bar(c, ui_assets);
});
}

fn handle_input(
Expand Down
3 changes: 3 additions & 0 deletions vs-rs/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use bevy::prelude::Component;
#[derive(Component, Clone, Debug)]
pub struct Health(pub i64);

#[derive(Component, Clone, Debug)]
pub struct MaxHealth(pub i64);

#[derive(Component, Clone, Debug)]
pub struct Damage(pub i64);

Expand Down
16 changes: 16 additions & 0 deletions vs-rs/src/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use bevy::prelude::*;

pub mod game_timer;
pub mod health_bar;

use game_timer::GameTimerPlugin;
use health_bar::HealthBarPlugin;

pub struct UiPlugin;

impl Plugin for UiPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(GameTimerPlugin)
.add_plugins(HealthBarPlugin);
}
}
61 changes: 61 additions & 0 deletions vs-rs/src/ui/game_timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use bevy::prelude::*;
use bevy::text::*;
use bevy::time::Stopwatch;
use std::time::Duration;
use bevy::sprite::Anchor;

pub struct GameTimerPlugin;

impl Plugin for GameTimerPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn)
.insert_resource(GameTimer(Stopwatch::default()))
.add_systems(Update, update_timer);
}
}

#[derive(Resource)]
struct GameTimer(Stopwatch);

#[derive(Component)]
struct GameTimerText;

fn spawn (mut commands: Commands) {
commands.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
justify_self: JustifySelf::Center,
..Default::default()
},
z_index: ZIndex::Global(2),
..Default::default()
}).with_children(|parent| {
parent.spawn((
TextBundle::from_section(
"00:00",
TextStyle {
font_size: 50.0,
color: Color::BLACK,
..default()
},
).with_text_justify(JustifyText::Center),
GameTimerText,
));
});
}

fn update_timer(
time: Res<Time>,
mut timer: ResMut<GameTimer>,
mut query: Query<&mut Text, With<GameTimerText>>,
) {
timer.0.tick(time.delta());

let elapsed_secs = timer.0.elapsed_secs();
let minutes = (elapsed_secs / 60.0).floor() as u32;
let seconds = (elapsed_secs % 60.0).floor() as u32;

for mut text in query.iter_mut() {
text.sections[0].value = format!("{:02}:{:02}", minutes, seconds);
}
}
64 changes: 64 additions & 0 deletions vs-rs/src/ui/health_bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use bevy::prelude::*;
use bevy::sprite::Anchor;
use crate::stats::*;
use crate::player::*;
use vs_assets::plugin::UiAssets;

pub struct HealthBarPlugin;

impl Plugin for HealthBarPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, update);
}
}

const WIDTH: f32 = 75.;
const HEIGHT: f32 = 10.;

#[derive(Component)]
struct HealthBar;

pub fn spawn_health_bar(
child_builder: &mut ChildBuilder,
assets: Res<UiAssets>,
) {
let bar_texture: Handle<Image> = assets.health_bar.clone();
let bar_outline_texture: Handle<Image> = assets.health_bar_outline.clone();

let sprite = Sprite {
custom_size: Some(Vec2::new(WIDTH, HEIGHT)),
anchor: Anchor::CenterLeft,
color: Color::rgb(0.8, 0.1, 0.1),
..Default::default()
};

let transform = Transform::from_translation(Vec3::new(-WIDTH / 2., -35., 100.));

let health_bar_bundle = SpriteBundle {
texture: bar_texture,
sprite: sprite.clone(),
transform: transform.clone(),
..Default::default()
};
child_builder.spawn((health_bar_bundle, HealthBar));

let health_bar_outline_bundle = SpriteBundle {
texture: bar_outline_texture,
transform,
sprite,
..Default::default()
};
child_builder.spawn(health_bar_outline_bundle);
}

fn update(
mut hp_bar: Query<&mut Sprite, With<HealthBar>>,
health: Query<(&Health, &MaxHealth), With<Player>>,
) {
if let Ok(mut sprite) = hp_bar.get_single_mut() {
if let Ok((health, max_health)) = health.get_single() {
let percent = f32::clamp(health.0 as f32 / max_health.0 as f32, 0., 1.);
sprite.custom_size = Some(Vec2::new(WIDTH * percent, HEIGHT));
}
}
}

0 comments on commit 6b8e257

Please sign in to comment.