Skip to content

Commit

Permalink
Ingame health UI (#50)
Browse files Browse the repository at this point in the history
* ui setup

* health component

* issues with ui texture atlas

* working health indicator

* player lose health
  • Loading branch information
MrPicklePinosaur authored Aug 30, 2022
1 parent ea1ee94 commit 9c188a4
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 12 deletions.
Binary file modified assets/tilesheet/bandit_hideout.ase
Binary file not shown.
Binary file modified assets/tilesheet/bandit_hideout.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 assets/tilesheet/heart.ase
Binary file not shown.
Binary file added assets/tilesheet/heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use super::{
};
use crate::{
ability::AbilityPlugin, ai::AIPlugin, attack::AttackPlugin, enviro::EnviroPlugin,
item::ItemPlugin, map::MapPlugin, room::RoomPlugin, weapon::WeaponPlugin,
item::ItemPlugin, map::MapPlugin, room::RoomPlugin, screens::ScreensPlugin,
weapon::WeaponPlugin,
};

pub fn app() {
Expand All @@ -34,6 +35,7 @@ pub fn app() {

app.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.insert_resource(ImageSettings::default_nearest())
.insert_resource(Msaa { samples: 1 })
.insert_resource(window_descriptor);
// .insert_resource(LogSettings {
// level: bevy::log::Level::DEBUG,
Expand All @@ -59,7 +61,8 @@ pub fn app() {
.add_plugin(HealthBarPlugin)
.add_plugin(EnviroPlugin)
.add_plugin(WeaponPlugin)
.add_plugin(GridPlugin);
.add_plugin(GridPlugin)
.add_plugin(ScreensPlugin);

app.run();
}
2 changes: 1 addition & 1 deletion src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};

#[derive(Deref)]
pub struct SpriteSheet(Handle<TextureAtlas>);
pub struct SpriteSheet(pub Handle<TextureAtlas>);

#[derive(Debug, Deref)]
pub struct PrefabData(pub HashMap<String, HandleUntyped>);
Expand Down
7 changes: 5 additions & 2 deletions src/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::Deserialize;
use crate::{
enemy::DamageEnemyEvent,
grid::{CellType, Grid},
player::DamagePlayerEvent,
utils::{ok_or_continue, variant_eq, Dir},
};

Expand Down Expand Up @@ -69,7 +70,8 @@ impl Plugin for AttackPlugin {

fn process_attack(
mut events: EventReader<AttackEvent>,
mut writer: EventWriter<DamageEnemyEvent>,
mut enemy_writer: EventWriter<DamageEnemyEvent>,
mut player_writer: EventWriter<DamagePlayerEvent>,
grid: Res<Grid>,
) {
for AttackEvent {
Expand All @@ -86,10 +88,11 @@ fn process_attack(

match cell_entity {
CellType::Enemy(entity) => {
writer.send(DamageEnemyEvent { entity: *entity });
enemy_writer.send(DamageEnemyEvent { entity: *entity });
},
CellType::Player(entity) => {
info!("player hit!");
player_writer.send(DamagePlayerEvent { entity: *entity });
},
_ => {},
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod movement;
mod player;
mod prefab;
mod room;
mod screens;
mod spritesheet_constants;
mod ui;
mod utils;
Expand Down
7 changes: 4 additions & 3 deletions src/player/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use bevy_bobs::prefab::PrefabId;

#[derive(Component)]
pub struct Inventory {
pub weapon: PrefabId,
pub weapon_primary: PrefabId,
pub weapon_secondary: PrefabId,
pub armor: PrefabId,
pub ability: PrefabId,
pub slots: Vec<PrefabId>,
capactiy: u32,
// pub slots: Vec<PrefabId>,
// capactiy: u32,
}

impl Inventory {}
23 changes: 19 additions & 4 deletions src/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub struct SpawnPlayerEvent {
pub prefab_id: PrefabId,
}

pub struct DamagePlayerEvent {
pub entity: Entity,
}

pub struct PlayerPlugin;

impl Plugin for PlayerPlugin {
Expand All @@ -70,6 +74,7 @@ impl Plugin for PlayerPlugin {
.add_loopless_state(PlayerState::Move)
.add_event::<SpawnPlayerEvent>()
.add_event::<PlayerMovedEvent>()
.add_event::<DamagePlayerEvent>()
.add_system(controller.run_in_state(GameState::PlayerInput))
.add_system(
move_controller
Expand All @@ -87,6 +92,7 @@ impl Plugin for PlayerPlugin {
.add_enter_system(GameState::PlayerInput, on_turn_start)
.add_exit_system(GameState::PlayerInput, reset_on_turn_end)
.add_system(spawn)
.add_system(take_damage)
.add_system(update_move_indicator.run_in_state(GameState::PlayerInput))
.add_system(spawn_from_ldtk);
}
Expand Down Expand Up @@ -128,10 +134,6 @@ fn spawn(
..default()
},
texture_atlas: asset_sheet.clone(),
transform: Transform {
translation: to_world_coords(spawn_pos).extend(BEING_LAYER),
..default()
},
..default()
})
.insert(Player)
Expand Down Expand Up @@ -349,3 +351,16 @@ fn spawn_from_ldtk(
}
}
}

fn take_damage(
mut cmd: Commands,
mut events: EventReader<DamagePlayerEvent>,
mut query: Query<(&mut Health, &GridEntity)>,
) {
for DamagePlayerEvent { entity } in events.iter() {
let (mut health, grid_entity) = query.get_mut(*entity).unwrap();

health.take(1);
if health.is_zero() {}
}
}
58 changes: 58 additions & 0 deletions src/screens/components/health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::thread::current;

use autodefault::*;
use bevy::prelude::*;
use bevy_bobs::component::health::Health;

use crate::{
assets::SpriteSheet, player::Player, screens::utils::FONT_PATH,
spritesheet_constants::SpriteIndex, utils::ok_or_return,
};

#[derive(Component)]
struct HealthNode;

pub struct HealthPlugin;

impl Plugin for HealthPlugin {
fn build(&self, app: &mut App) {
app.add_system(update);
}
}

#[autodefault]
#[allow(non_snake_case)]
pub fn HealthBar(cmd: &mut ChildBuilder) -> Entity {
cmd.spawn()
.insert(HealthNode)
.insert_bundle(NodeBundle {
style: Style {
display: Display::Flex,
},
})
.id()
}

#[autodefault]
fn update(
mut cmd: Commands,
mut player_query: Query<&Health, With<Player>>,
mut ui_query: Query<(Entity, &mut HealthNode), Without<Player>>,
asset_server: Res<AssetServer>,
) {
let health = ok_or_return!(player_query.get_single());

// TODO kinda inefficnet to despawn all health nodes and respawn every frame
for (entity, mut health_node) in ui_query.iter_mut() {
cmd.entity(entity).despawn_descendants();

for i in 0..health.current() {
cmd.entity(entity).with_children(|parent| {
parent.spawn().insert_bundle(ImageBundle {
image: UiImage(asset_server.load("tilesheet/heart.png")),
style: Style {},
});
});
}
}
}
7 changes: 7 additions & 0 deletions src/screens/components/inventory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use bevy::prelude::*;

pub struct InventoryPlugin;

impl Plugin for InventoryPlugin {
fn build(&self, app: &mut App) {}
}
14 changes: 14 additions & 0 deletions src/screens/components/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub mod health;
pub mod inventory;

use bevy::prelude::*;

use self::{health::HealthPlugin, inventory::InventoryPlugin};

pub struct ComponentPlugin;

impl Plugin for ComponentPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(InventoryPlugin).add_plugin(HealthPlugin);
}
}
35 changes: 35 additions & 0 deletions src/screens/ingame.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use autodefault::*;
use bevy::prelude::*;
use iyes_loopless::prelude::*;

use super::{
components::health::HealthBar,
state::ScreenState,
utils::{destroy_ui, UIRoot},
};
use crate::{assets::SpriteSheet, spritesheet_constants::SpriteIndex};

pub struct IngamePlugin;

impl Plugin for IngamePlugin {
fn build(&self, app: &mut App) {
app.add_enter_system(ScreenState::Ingame, render_ui)
.add_exit_system(ScreenState::Ingame, destroy_ui);
}
}

#[autodefault]
fn render_ui(mut cmd: Commands) {
cmd.spawn()
.insert(UIRoot)
.insert_bundle(NodeBundle {
color: UiColor(Color::NONE),
style: Style {
align_self: AlignSelf::FlexEnd,
// justify_content: JustifyContent::Center,
},
})
.with_children(|mut parent| {
HealthBar(&mut parent);
});
}
28 changes: 28 additions & 0 deletions src/screens/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod components;
mod ingame;
mod state;
mod utils;

use bevy::prelude::*;
use iyes_loopless::state::NextState;

use self::{
components::ComponentPlugin,
ingame::IngamePlugin,
state::{ScreenState, StatePlugin},
};

pub struct ScreensPlugin;

impl Plugin for ScreensPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(StatePlugin)
.add_plugin(IngamePlugin)
.add_plugin(ComponentPlugin)
.add_startup_system(debug);
}
}

fn debug(mut cmd: Commands) {
cmd.insert_resource(NextState(ScreenState::Ingame));
}
18 changes: 18 additions & 0 deletions src/screens/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use bevy::prelude::*;
use iyes_loopless::prelude::*;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ScreenState {
MainMenu,
Settings,
LevelSelect,
Ingame,
}

pub struct StatePlugin;

impl Plugin for StatePlugin {
fn build(&self, app: &mut App) {
app.add_loopless_state(ScreenState::MainMenu);
}
}
13 changes: 13 additions & 0 deletions src/screens/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use bevy::prelude::*;

pub const FONT_PATH: &str = "fonts/arcadeclassic.ttf";

/// Marker component for the root node of each screen
#[derive(Component)]
pub struct UIRoot;

/// Clean up UI when switching screens
pub fn destroy_ui(mut cmd: Commands, query: Query<Entity, With<UIRoot>>) {
let e = query.single();
cmd.entity(e).despawn_recursive();
}
7 changes: 7 additions & 0 deletions src/spritesheet_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ pub enum SpriteIndex {
// MoveIndicatorN = sprite!(5, 10),
// MoveIndicatorS = sprite!(6, 10),
// MoveIndicatorE = sprite!(7, 10),
ItemSlotBg = sprite!(0, 11),
WeaponSlot = sprite!(1, 11),
ArmorSlot = sprite!(2, 11),
AbilitySlot = sprite!(3, 11),

HeartFull = sprite!(0, 12),
HeartEmpty = sprite!(0, 13),
}

// ui
Expand Down

0 comments on commit 9c188a4

Please sign in to comment.