Added comments on PositionSpecifier usage

This commit is contained in:
juliancoffee 2021-07-24 21:41:07 +03:00
parent 4811ede2ea
commit 4adc045627
5 changed files with 69 additions and 26 deletions

View File

@ -2,7 +2,6 @@ use crate::comp::Body;
use serde::{Deserialize, Serialize};
use specs::{Component, DerefFlaggedStorage};
use specs_idvs::IdvStorage;
use std::cmp;
pub const ENERGY_PER_LEVEL: u32 = 50;
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
@ -119,7 +118,7 @@ impl Energy {
}
/// Returns the fraction of energy an entity has remaining
pub fn fraction(&self) -> f32 { self.current as f32 / cmp::max(self.maximum, 1) as f32 }
pub fn fraction(&self) -> f32 { self.current as f32 / self.maximum.max(1) as f32 }
}
pub struct EnergyChange {

View File

@ -2,7 +2,6 @@
use crate::comp::Body;
use crate::{uid::Uid, DamageSource};
use serde::{Deserialize, Serialize};
use std::cmp;
#[cfg(not(target_arch = "wasm32"))]
use specs::{Component, DerefFlaggedStorage};
@ -143,7 +142,7 @@ impl Health {
}
/// Returns the fraction of health an entity has remaining
pub fn fraction(&self) -> f32 { self.current as f32 / cmp::max(self.maximum, 1) as f32 }
pub fn fraction(&self) -> f32 { self.current as f32 / self.maximum.max(1) as f32 }
}
#[cfg(not(target_arch = "wasm32"))]

View File

@ -316,6 +316,8 @@ widget_ids! {
}
}
/// Specifier to use with `Position::position`
/// Read its documentation for more
// TODO: extend as you need it
#[derive(Clone, Copy)]
pub enum PositionSpecifier {
@ -326,6 +328,52 @@ pub enum PositionSpecifier {
RightFrom(widget::Id, f64),
}
/// Trait which enables you to declare widget position
/// to use later on widget creation.
/// It is implemented for all widgets which are implement Positionable,
/// so you can easily change your code to use this method.
///
/// Consider this example:
/// ```text
/// let slot1 = slot_maker
/// .fabricate(hotbar::Slot::One, [40.0; 2])
/// .filled_slot(self.imgs.skillbar_slot)
/// .bottom_left_with_margins_on(state.ids.frame, 0.0, 0.0);
/// if condition {
/// call_slot1(slot1);
/// } else {
/// call_slot2(slot1);
/// }
/// let slot2 = slot_maker
/// .fabricate(hotbar::Slot::Two, [40.0; 2])
/// .filled_slot(self.imgs.skillbar_slot)
/// .right_from(state.ids.slot1, slot_offset);
/// if condition {
/// call_slot1(slot2);
/// } else {
/// call_slot2(slot2);
/// }
/// ```
/// Despite being identical, you can't easily deduplicate code
/// which uses slot1 and slot2 as they are calling methods to position itself.
/// This can be solved if you declare position and use it later like so
/// ```text
/// let slots = [
/// (hotbar::Slot::One, BottomLeftWithMarginsOn(state.ids.frame, 0.0, 0.0)),
/// (hotbar::Slot::Two, RightFrom(state.ids.slot1, slot_offset)),
/// ];
/// for (slot, pos) in slots {
/// let slot = slot_maker
/// .fabricate(slot, [40.0; 2])
/// .filled_slot(self.imgs.skillbar_slot)
/// .position(pos);
/// if condition {
/// call_slot1(slot);
/// } else {
/// call_slot2(slot);
/// }
/// }
/// ```
pub trait Position {
fn position(self, request: PositionSpecifier) -> Self;
}

View File

@ -886,47 +886,46 @@ impl<'a> Widget for Skillbar<'a> {
}
}
#[rustfmt::skip]
fn ability_description(tool: &ToolKind) -> Option<(&str, &str)> {
match tool {
ToolKind::Hammer => Some((
"Smash of Doom",
concat!(
"\n",
"An AOE attack with knockback.\n",
"Leaps to position of cursor.",
),
"\n\
An AOE attack with knockback.\n\
Leaps to position of cursor.",
)),
ToolKind::Axe => Some((
"Axe Jump",
concat!("\n", "A jump with the slashing leap to position of cursor."),
"\n\
A jump with the slashing leap to position of cursor.",
)),
ToolKind::Staff => Some((
"Ring of Fire",
concat!("\n", "Explodes the gound with fire shockwave."),
"\n\
Explodes the gound with fire shockwave.",
)),
ToolKind::Sword => Some((
"Whirlwind",
concat!("\n", "Move forward while spinning with your sword."),
"\n\
Move forward while spinning with your sword.",
)),
ToolKind::Bow => Some((
"Burst",
concat!("\n", "Launches a burst of arrows into your target"),
"\n\
Launches a burst of arrows into your target",
)),
ToolKind::Sceptre => Some((
"Thorn Bulwark",
concat!(
"\n",
"Protects you and your group with thorns\n",
"for a short amount of time.",
),
"\n\
Protects you and your group with thorns\n\
for a short amount of time.",
)),
ToolKind::Debug => Some((
"Possessing Arrow",
concat!(
"\n",
"Shoots a poisonous arrow.\n",
"Lets you control your target."
),
"\n\
Shoots a poisonous arrow.\n\
Lets you control your target.",
)),
_ => None,
}

View File

@ -111,9 +111,7 @@ fn main() {
Panic Payload: {:?}\n\
PanicInfo: {}\n\
Game version: {} [{}]",
logs_dir
.join("voxygen.log.<date>")
.display(),
logs_dir.join("voxygen.log.<date>").display(),
reason,
panic_info,
common::util::GIT_HASH.to_string(),