1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
//! Attribute component. This is a simple integer representing the attribute of an entity.
//!
//! Has properties like `max_attribute` and `current_attribute`. And you can perform math
//! operations with integers, floats, and other `Attribute` components directly to influence
//! the `current_attribute` value.
//!
//! # Examples
//!
//! ```no_run
//! use bevy::prelude::*;
//! use game_library::Attribute;
//!
//! #[derive(Component)]
//! struct Health(Attribute);
//!
//! /// Spawns a basic entity with health
//! pub fn setup_health(mut commands: Commands) {
//! commands.spawn((Health(Attribute::new(10_u32)),));
//! }
//!
//! /// A system to run on [Update] which checks if the entity is dead
//! pub fn check_dead(mut query: Query<&mut Health>) {
//! for mut health in &mut query {
//! if health.0.is_empty() {
//! // Do something here
//! }
//! }
//! }
//! ```
use bevy::{
ecs::{component::Component, system::Resource},
reflect::Reflect,
};
use bevy_inspector_egui::inspector_options::{InspectorOptions, ReflectInspectorOptions};
use serde::{Deserialize, Serialize};
use crate::progress_bar::Percentage;
/// Attribute component. This is a simple integer representing the attribute of an entity.
/// Has properties like `max_attribute` and `current_attribute`. We support operations with
/// integers, floats, and other `Attribute` components.
///
/// We use positive integers for attribute, and negative integers for damage. There's no reason
/// to support negative attribute and negative `max_attribute`, so we don't. We also won't allow
/// `current_attribute` to be greater than `max_attribute`.
///
/// Because we use integers, we can't represent fractional attribute. It's a bit of a tradeoff,
/// but it's a design decision (and I guess it can be changed later at great effort).
///
/// All operations that are performed directly between a `Attribute` component and another value will
/// use the `current_attribute` value. For example, if you add a `Attribute` component with `current_attribute`
/// of 10 to a `Attribute` component with `current_attribute` of 20, the result will be a `Attribute` component
/// with `current_attribute` of 30. This holds true for all operations (addition, subtraction, multiplication,
/// division, etc.).
///
/// What is supported:
///
/// * Affecting (adding/subtracting) attribute with integers, floats, and other `Attribute` components.
/// * Scaling the current attribute with integers, floats, and other `Attribute` components
/// * Scaling the max attribute with integers, floats, and other `Attribute` components
/// * Scaling the current attribute by a percentage of the max attribute (with integers, floats, and other `Attribute` components)
/// * Comparing attribute with integers, floats, and other `Attribute` components.
/// * Getting the percentage of attribute remaining (as a rounded integer between 0 and 100 OR a precise
/// float between 0 and 1).
/// * Check `is_empty` to see if the entity is dead (i.e. `current_attribute` is 0).
/// * Check `is_full` to see if the entity is at full attribute (i.e. `current_attribute` is equal to `max_attribute`).
#[derive(
Resource,
Debug,
Component,
Clone,
Copy,
Serialize,
Deserialize,
Default,
Reflect,
InspectorOptions,
)]
#[reflect(InspectorOptions)]
pub struct Attribute {
/// The maximum value for the attribute of the entity
pub max: u32,
/// The current value for the attribute of the entity
pub current: u32,
}
impl Attribute {
/// Minimum value is 0
pub const MIN: u32 = 0;
/// Returns true if the current attribute is equal to 0
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
/// assert_eq!(attribute.is_empty(), false);
///
/// attribute.set(0_u32);
/// assert_eq!(attribute.is_empty(), true);
/// ```
#[must_use]
pub const fn is_empty(&self) -> bool {
self.current == 0
}
/// Returns true if the current attribute is equal to the max attribute.
///
/// ## Note
///
/// If max attribute is 0, this will always return true (because current attribute will always be 0
/// if max attribute is 0)
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
/// assert_eq!(attribute.is_full(), true);
///
/// attribute.set(0_u32);
/// assert_eq!(attribute.is_full(), false);
///
/// attribute.set_max(0_u32);
/// assert_eq!(attribute.is_full(), true);
/// ```
#[must_use]
pub const fn is_full(&self) -> bool {
self.current == self.max
}
/// Returns the percentage of attribute remaining as a float between 0.00 and 1.00
///
/// ## Note
///
/// This will always return 1.00 if max attribute is 0 (because current attribute will always be 0
/// if max attribute is 0)
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
/// assert_eq!(attribute.remaining(), 1.0);
///
/// attribute.set(5_u32);
/// assert_eq!(attribute.remaining(), 0.5);
///
/// attribute.set(0_u32);
/// assert_eq!(attribute.remaining(), 0.0);
///
/// attribute.set_max(0_u32);
/// assert_eq!(attribute.remaining(), 1.0);
/// ```
#[must_use]
pub fn remaining(&self) -> f32 {
// Avoid division by zero
if self.max == 0 {
return 1.0;
}
// Get percentage but round to 2 decimal places
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let result = (f64::from(self.current) / f64::from(self.max) * 100.0).floor() / 100.0;
#[allow(clippy::cast_possible_truncation)]
let result = result as f32;
result
}
/// Returns the percentage of attribute remaining as an integer between 0% and 100%
///
/// ## Note
///
/// This will always return 100% if max attribute is 0 (because current attribute will always be 0
/// if max attribute is 0)
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
/// assert_eq!(attribute.percentage_remaining(), 100);
///
/// attribute.set(5_u32);
/// assert_eq!(attribute.percentage_remaining(), 50);
///
/// attribute.set(0_u32);
/// assert_eq!(attribute.percentage_remaining(), 0);
///
/// attribute.set_max(0_u32);
/// assert_eq!(attribute.percentage_remaining(), 100);
/// ```
#[must_use]
pub fn percentage_remaining(&self) -> u32 {
// Avoid division by zero
if self.max == 0 {
return 100;
}
// Percentage but floored and as an integer between 0 and 100
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let result = (f64::from(self.current) / f64::from(self.max) * 100.0).floor() as u32;
result
}
/// Creates a new `Attribute` component with the given `max_attribute` and `current_attribute`.
///
/// ## Arguments
///
/// * `max_attribute` - The maximum attribute of the entity. This will be converted into a `u32` and clamped
///
/// ## Returns
///
/// * Self - The new `Attribute` component
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let attribute = Attribute::new(10_u32);
/// ```
pub fn new(max_attribute: impl Into<u32>) -> Self {
let clamped_max_attribute = max_attribute.into().max(Self::MIN);
Self {
max: clamped_max_attribute,
current: clamped_max_attribute,
}
}
/// Creates a new `Attribute` component with the given `max_attribute` and `current_attribute`.
///
/// ## Arguments
///
/// * `current_attribute` - The current attribute of the entity. This will be converted into a `u32` and clamped
/// * `max_attribute` - The maximum attribute of the entity. This will be converted into a `u32` and clamped
///
/// ## Returns
///
/// * Self - The new `Attribute` component
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let attribute = Attribute::new_with_current(5_u32, 10_u32);
///
/// assert_eq!(attribute.current, 5_u32);
/// assert_eq!(attribute.max, 10_u32);
/// ```
pub fn new_with_current(
current_attribute: impl Into<u32>,
max_attribute: impl Into<u32>,
) -> Self {
let clamped_max_attribute = max_attribute.into().max(Self::MIN);
let clamped_current_attribute = current_attribute
.into()
.clamp(Self::MIN, clamped_max_attribute);
Self {
max: clamped_max_attribute,
current: clamped_current_attribute,
}
}
/// Adds the given amount to the max attribute, while clamping max attribute within acceptable bounds.
/// Also ensures that `current_attribute` is not greater than `max_attribute` (in cases where a negative
/// amount is added here).
///
/// ## Arguments
///
/// * `amount` - The amount to add to the max attribute. This can be a positive or negative number, which
/// will increase or decrease the max attribute respectively.
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
///
/// attribute.add_to_max(5);
/// assert_eq!(attribute.max, 15_u32);
/// assert_eq!(attribute.current, 10_u32);
///
/// attribute.add_to_max(-5);
/// assert_eq!(attribute.max, 10_u32);
/// assert_eq!(attribute.current, 10_u32);
///
/// attribute.set(15_u32);
/// attribute.add_to_max(25);
/// assert_eq!(attribute.max, 35_u32);
/// assert_eq!(attribute.current, 10_u32);
/// ```
pub fn add_to_max(&mut self, amount: impl Into<i64>) {
let amount = amount.into();
let new_max_attribute = i64::from(self.max) + amount;
if let Ok(value) =
u32::try_from(new_max_attribute.clamp(i64::from(Self::MIN), i64::from(u32::MAX)))
{
self.max = value;
} else {
tracing::warn!("attribute value {} too big for u32", new_max_attribute);
self.max = u32::MAX;
}
self.current = self.current.min(self.max);
}
/// Scales the max attribute by the given amount, while clamping max attribute within acceptable bounds. Also
/// ensures that `current_attribute` is not greater than `max_attribute` (in cases where the scale value is
/// less than 1; i.e. the max attribute is decreased).
///
/// ## Arguments
///
/// * `amount` - The amount to scale the max attribute by. This should be a positive number. See the sister
/// function `scale_max_attribute_by_percentage` for a function that takes a percentage (integer value).
///
/// ## Note
///
/// - Really large numbers will just cause max attribute to be set to `Attribute::MAX`.
/// - Negative numbers will cause max attribute to be set to 0.
/// - This is a sister function to `scale_max_attribute_by_percentage` which takes a percentage value instead
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
///
/// attribute.scale_max(2.0);
/// assert_eq!(attribute.max, 20_u32);
/// assert_eq!(attribute.current, 10_u32);
///
/// attribute.scale_max(0.5);
/// assert_eq!(attribute.max, 10_u32);
/// assert_eq!(attribute.current, 10_u32);
/// ```
pub fn scale_max(&mut self, amount: impl Into<f64> + std::cmp::PartialOrd<f64>) {
// Guard against negative numbers (which would cause the max attribute to be negative)
if amount < 0.0 {
tracing::warn!("refused to scale by a negative number; set max_attribute to 0 instead");
self.max = 0;
self.current = 0;
return;
}
let amount = amount.into();
let new_max_attribute = f64::from(self.max) * amount;
let float_value = new_max_attribute.clamp(f64::from(Self::MIN), f64::from(u32::MAX));
#[allow(
clippy::cast_possible_truncation,
clippy::cast_lossless,
clippy::cast_sign_loss
)]
let value = float_value.floor() as u32;
self.max = value;
}
/// Scales the max attribute by the given percentage, while clamping max attribute within acceptable bounds. Also
/// ensures that `current_attribute` is not greater than `max_attribute` (in cases where the scale value is
/// less than 100; i.e. the max attribute is decreased).
///
/// ## Arguments
///
/// * `amount` - The percentage to scale the max attribute by. This should be a positive number. Really large numbers
/// will just cause max attribute to be set to `u32::MAX`.
///
/// ## Note
///
/// - Really large numbers will just cause max attribute to be set to `Attribute::MAX`.
/// - Negative numbers will cause max attribute to be set to 0.
/// - This is a sister function to `scale_max_attribute` which takes a float value instead of a percentage.
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
///
/// attribute.scale_max_by_percentage(200);
/// assert_eq!(attribute.max, 20_u32);
/// assert_eq!(attribute.current, 10_u32);
///
/// attribute.scale_max_by_percentage(50);
/// assert_eq!(attribute.max, 10_u32);
/// assert_eq!(attribute.current, 10_u32);
/// ```
pub fn scale_max_by_percentage(&mut self, amount: impl Into<i32>) {
self.scale_max(f64::from(amount.into()) / 100.0);
}
/// Adds the given amount to the current attribute, while clamping current attribute within acceptable bounds.
/// (i.e. between 0 and `max_attribute`). This will not affect `max_attribute`.
///
/// ## Arguments
///
/// * `amount` - The amount to add to the current attribute. This can be a positive or negative number, which
/// will increase or decrease the current attribute respectively.
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new_with_current(5_u32, 10_u32);
///
/// assert_eq!(attribute.current, 5_u32);
///
/// attribute.add_to_current(5);
///
/// assert_eq!(attribute.current, 10_u32);
/// ```
pub fn add_to_current(&mut self, amount: impl Into<i64>) {
let amount = amount.into();
let new_current_attribute = i64::from(self.current) + amount;
#[allow(clippy::cast_sign_loss)]
if let Ok(value) =
u32::try_from(new_current_attribute.clamp(i64::from(Self::MIN), i64::from(self.max)))
{
self.current = value;
} else {
tracing::warn!("attribute value {} too big for u32", new_current_attribute);
self.current = u32::MAX;
}
}
/// Scales the current attribute by the given amount, while clamping current attribute within acceptable bounds.
/// (i.e. between 0 and `max_attribute`). This will not affect `max_attribute`. This takes a float value; e.g.,
/// `0.5` will scale the current attribute by 50% (i.e. half the current attribute), while `2.0` will scale the
/// current attribute by 200% (i.e. double the current attribute).
///
/// There's a similar function `add_to_current_attribute_by_scale_max_attribute` which applies the same logic but
/// uses the `max_attribute` as the scale value to arrive at an amount to change the current attribute by.
///
/// ## Arguments
///
/// * `amount` - The amount to scale the current attribute by. This should be a positive number. See the sister
/// function `scale_current_attribute_by_percentage` for a function that takes a percentage (integer value).
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new_with_current(5_u32, 10_u32);
///
/// assert_eq!(attribute.current, 5_u32);
///
/// attribute.scale_current(2.0);
///
/// assert_eq!(attribute.current, 10_u32);
/// ```
pub fn scale_current(&mut self, amount: impl Into<f64> + std::cmp::PartialOrd<f64>) {
// Guard against negative numbers (which would cause the current attribute to be negative)
if amount < 0.0 {
tracing::warn!(
"refused to scale by a negative number; set current_attribute to 0 instead"
);
self.current = 0;
return;
}
let amount = amount.into();
let new_current_attribute = f64::from(self.current) * amount;
let float_value = new_current_attribute.clamp(f64::from(Self::MIN), f64::from(self.max));
#[allow(
clippy::cast_possible_truncation,
clippy::cast_lossless,
clippy::cast_sign_loss
)]
let value = float_value.floor() as u32;
self.current = value;
}
/// Scales the current attribute by the given percentage, while clamping current attribute within acceptable bounds.
///
/// There's a similar function `add_to_current_attribute_by_scale_max_attribute_percentage` which applies the same logic but
/// uses the `max_attribute` as the scale value to arrive at an amount to change the current attribute by.
///
/// ## Arguments
///
/// * `amount` - The percentage to scale the current attribute by. This should be a positive number. Really large numbers
/// will just cause current attribute to be set to `u32::MAX`.
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new_with_current(5_u32, 10_u32);
///
/// assert_eq!(attribute.current, 5_u32);
///
/// attribute.scale_current_by_percentage(200);
///
/// assert_eq!(attribute.current, 10_u32);
/// ```
pub fn scale_current_by_percentage(&mut self, amount: impl Into<i32>) {
self.scale_current(f64::from(amount.into()) / 100.0);
}
/// Adds to current attribute by scaling the `max_attribute` by the given amount, while clamping current attribute within
/// acceptable bounds. (i.e. between 0 and `max_attribute`). This will not affect `max_attribute`. This takes a float
/// and expects negatives to be used to decrease the current attribute, and positives to be used to increase the
/// current attribute.
///
/// There's a similar function `scale_current_attribute_by_scale_max_attribute_percentage` which applies the same logic but
/// expects a percentage value (e.g. 50) instead of a float (e.g. 0.5).
///
/// ## Arguments
///
/// * `amount` - The amount to scale the max attribute by. If this is positive, the current attribute will increase. If
/// this is negative, the current attribute will decrease.
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new_with_current(5_u32, 100_u32);
///
/// assert_eq!(attribute.current, 5_u32);
/// assert_eq!(attribute.max, 100_u32);
///
/// attribute.add_to_current_by_scale_max(0.05);
///
/// assert_eq!(attribute.current, 10_u32);
/// assert_eq!(attribute.max, 100_u32);
/// ```
pub fn add_to_current_by_scale_max(
&mut self,
amount: impl Into<f64> + std::cmp::PartialOrd<f64>,
) {
// Turn the amount into an i64 so we can easily affect the current attribute
let amount = amount.into() * f64::from(self.max);
#[allow(clippy::cast_possible_truncation)]
self.add_to_current(amount as i64);
}
/// Adds to current attribute by scaling the `max_attribute` by the given percentage, while clamping current attribute within
/// acceptable bounds. (i.e. between 0 and `max_attribute`). This will not affect `max_attribute`. This takes a percentage
/// and expects negatives to be used to decrease the current attribute, and positives to be used to increase the
/// current attribute.
///
/// There's a similar function `add_to_current_attribute_by_scale_max_attribute` which applies the same logic but
/// expects a float value (e.g. 0.5) instead of a percentage (e.g. 50).
///
/// ## Arguments
///
/// * `amount` - The percentage to scale the max attribute by. If this is positive, the current attribute will increase. If
/// this is negative, the current attribute will decrease.
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new_with_current(5_u32, 100_u32);
///
/// assert_eq!(attribute.current, 5_u32);
/// assert_eq!(attribute.max, 100_u32);
///
/// attribute.add_to_current_by_scale_max_percentage(20);
///
/// assert_eq!(attribute.current, 25_u32);
/// assert_eq!(attribute.max, 100_u32);
/// ```
pub fn add_to_current_by_scale_max_percentage(&mut self, amount: impl Into<i32>) {
self.add_to_current_by_scale_max(f64::from(amount.into()) / 100.0);
}
/// Set the current value directly.
///
/// # Arguments
///
/// * `value` - The value to set the current attribute to. This will be clamped between 0 and `max_attribute`.
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
///
/// assert_eq!(attribute.current, 10_u32);
/// assert_eq!(attribute.max, 10_u32);
///
/// attribute.set(5_u32);
/// assert_eq!(attribute.current, 5_u32);
/// assert_eq!(attribute.max, 10_u32);
/// ```
pub fn set(&mut self, value: impl Into<u32>) {
let Ok(value) = u32::try_from(value) else {
tracing::warn!("set: value won't turn into u32");
return;
};
self.current = value.min(self.max);
}
/// Set the max value directly.
///
/// # Arguments
///
/// * `value` - The value to set the max attribute to. This will be clamped between 0 and `u32::MAX`.
///
/// # Examples
///
/// ```no_run
/// use game_library::Attribute;
///
/// let mut attribute = Attribute::new(10_u32);
///
/// assert_eq!(attribute.current, 10_u32);
/// assert_eq!(attribute.max, 10_u32);
///
/// attribute.set_max(5_u32);
///
/// assert_eq!(attribute.current, 5_u32);
/// assert_eq!(attribute.max, 5_u32);
///
/// attribute.set_max(10_u32);
///
/// assert_eq!(attribute.current, 5_u32);
/// assert_eq!(attribute.max, 10_u32);
/// ```
pub fn set_max(&mut self, value: impl Into<u32>) {
let Ok(value) = u32::try_from(value) else {
tracing::warn!("set_max: value won't turn into u32");
return;
};
self.max = value.max(0_u32);
// Ensure current is not greater than max
self.current = self.current.min(self.max);
}
}
impl std::ops::AddAssign<u32> for Attribute {
fn add_assign(&mut self, rhs: u32) {
self.add_to_current(rhs);
}
}
impl std::ops::AddAssign<i32> for Attribute {
fn add_assign(&mut self, rhs: i32) {
self.add_to_current(rhs);
}
}
impl std::ops::AddAssign<f32> for Attribute {
fn add_assign(&mut self, rhs: f32) {
#[allow(clippy::cast_possible_truncation)]
self.add_to_current(rhs as i32);
}
}
impl std::ops::AddAssign<f64> for Attribute {
fn add_assign(&mut self, rhs: f64) {
#[allow(clippy::cast_possible_truncation)]
self.add_to_current(rhs as i32);
}
}
impl std::ops::AddAssign<Self> for Attribute {
fn add_assign(&mut self, rhs: Self) {
self.add_to_current(rhs.current);
}
}
impl std::ops::SubAssign<u32> for Attribute {
fn sub_assign(&mut self, rhs: u32) {
let sub = i64::from(rhs);
self.add_to_current(-sub);
}
}
impl std::ops::SubAssign<i32> for Attribute {
fn sub_assign(&mut self, rhs: i32) {
let sub = i64::from(rhs);
self.add_to_current(-sub);
}
}
impl std::ops::SubAssign<f32> for Attribute {
fn sub_assign(&mut self, rhs: f32) {
#[allow(clippy::cast_possible_truncation)]
let sub = rhs.floor() as i64;
self.add_to_current(-sub);
}
}
impl std::ops::SubAssign<f64> for Attribute {
fn sub_assign(&mut self, rhs: f64) {
#[allow(clippy::cast_possible_truncation)]
let sub = rhs.floor() as i64;
self.add_to_current(-sub);
}
}
impl std::ops::SubAssign<Self> for Attribute {
fn sub_assign(&mut self, rhs: Self) {
let sub = i64::from(rhs.current);
self.add_to_current(-sub);
}
}
impl std::ops::MulAssign<u32> for Attribute {
fn mul_assign(&mut self, rhs: u32) {
let mul = f64::from(rhs);
self.scale_current(mul);
}
}
impl std::ops::MulAssign<i32> for Attribute {
fn mul_assign(&mut self, rhs: i32) {
let mul = f64::from(rhs);
self.scale_current(mul);
}
}
impl std::ops::MulAssign<f32> for Attribute {
fn mul_assign(&mut self, rhs: f32) {
let mul = f64::from(rhs);
self.scale_current(mul);
}
}
impl std::ops::MulAssign<f64> for Attribute {
fn mul_assign(&mut self, rhs: f64) {
self.scale_current(rhs);
}
}
impl std::ops::MulAssign<Self> for Attribute {
fn mul_assign(&mut self, rhs: Self) {
let mul = f64::from(rhs.current);
self.scale_current(mul);
}
}
impl std::ops::DivAssign<u32> for Attribute {
fn div_assign(&mut self, rhs: u32) {
if rhs == 0 {
tracing::error!("avoided division by zero; set current_attribute to 0 instead");
self.current = 0;
}
let div = f64::from(rhs);
self.scale_current(1.0 / div);
}
}
impl std::ops::DivAssign<i32> for Attribute {
fn div_assign(&mut self, rhs: i32) {
if rhs <= 0 {
tracing::error!("avoided division by zero; set current_attribute to 0 instead");
self.current = 0;
}
let div = f64::from(rhs);
self.scale_current(1.0 / div);
}
}
impl std::ops::DivAssign<f32> for Attribute {
fn div_assign(&mut self, rhs: f32) {
if rhs <= 0.0 {
tracing::error!("avoided division by zero; set current_attribute to 0 instead");
self.current = 0;
}
let div = f64::from(rhs);
self.scale_current(1.0 / div);
}
}
impl std::ops::DivAssign<f64> for Attribute {
fn div_assign(&mut self, rhs: f64) {
if rhs <= 0.0 {
tracing::error!("avoided division by zero; set current_attribute to 0 instead");
self.current = 0;
}
self.scale_current(1.0 / rhs);
}
}
impl std::ops::DivAssign<Self> for Attribute {
fn div_assign(&mut self, rhs: Self) {
if rhs.current == 0 {
tracing::error!("avoided division by zero; set current_attribute to 0 instead");
self.current = 0;
}
let div = f64::from(rhs.current);
self.scale_current(1.0 / div);
}
}
impl std::cmp::PartialOrd<Self> for Attribute {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl std::cmp::Ord for Attribute {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.current.cmp(&other.current)
}
}
impl From<f32> for Attribute {
fn from(value: f32) -> Self {
if value < 0.0 {
tracing::warn!("refused to create Attribute with negative value; set to 0 instead");
return Self::new(0_u32);
}
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let value = value.floor() as i32;
Self::from(value)
}
}
impl From<f64> for Attribute {
fn from(value: f64) -> Self {
if value < 0.0 {
tracing::warn!("refused to create Attribute with negative value; set to 0 instead");
return Self::new(0_u32);
}
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let value = value.floor() as i32;
Self::from(value)
}
}
impl From<Attribute> for f32 {
fn from(value: Attribute) -> Self {
#[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
let result = value.current as Self;
result
}
}
impl From<Attribute> for f64 {
fn from(value: Attribute) -> Self {
Self::from(value.current)
}
}
impl std::fmt::Display for Attribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/{}", self.current, self.max)
}
}
impl From<u64> for Attribute {
fn from(value: u64) -> Self {
u32::try_from(value).map_or_else(
|_| {
tracing::warn!("attribute value {} too big for u32", value);
Self::new(u32::MAX)
},
Self::new,
)
}
}
impl From<u32> for Attribute {
fn from(value: u32) -> Self {
Self::new(value)
}
}
impl From<u16> for Attribute {
fn from(value: u16) -> Self {
Self::new(value)
}
}
impl From<u8> for Attribute {
fn from(value: u8) -> Self {
Self::new(value)
}
}
impl From<i64> for Attribute {
fn from(value: i64) -> Self {
if value < 0 {
tracing::warn!("refused to create Attribute with negative value; set to 0 instead");
return Self::new(0_u32);
}
u32::try_from(value).map_or_else(
|_| {
tracing::warn!("attribute value {} too big for u32", value);
Self::new(u32::MAX)
},
Self::new,
)
}
}
impl From<i32> for Attribute {
fn from(value: i32) -> Self {
if value < 0 {
tracing::warn!("refused to create Attribute with negative value; set to 0 instead");
return Self::new(0_u32);
}
#[allow(clippy::cast_sign_loss)]
Self::new(value as u32)
}
}
impl From<i16> for Attribute {
fn from(value: i16) -> Self {
if value < 0 {
tracing::warn!("refused to create Attribute with negative value; set to 0 instead");
return Self::new(0_u32);
}
#[allow(clippy::cast_sign_loss)]
Self::new(value as u32)
}
}
impl From<i8> for Attribute {
fn from(value: i8) -> Self {
if value < 0 {
tracing::warn!("refused to create Attribute with negative value; set to 0 instead");
return Self::new(0_u32);
}
#[allow(clippy::cast_sign_loss)]
Self::new(value as u32)
}
}
impl From<Attribute> for u64 {
fn from(value: Attribute) -> Self {
Self::from(value.current)
}
}
impl From<Attribute> for u32 {
fn from(value: Attribute) -> Self {
value.current
}
}
impl From<Attribute> for u16 {
fn from(value: Attribute) -> Self {
Self::try_from(value.current).map_or_else(
|_| {
tracing::warn!("attribute value {} too big for u16", value.current);
Self::MAX
},
|value| value,
)
}
}
impl From<Attribute> for u8 {
fn from(value: Attribute) -> Self {
Self::try_from(value.current).map_or_else(
|_| {
tracing::warn!("attribute value {} too big for u8", value.current);
Self::MAX
},
|value| value,
)
}
}
impl From<Attribute> for i64 {
fn from(value: Attribute) -> Self {
Self::from(value.current)
}
}
impl From<Attribute> for i32 {
fn from(value: Attribute) -> Self {
Self::try_from(value.current).map_or_else(
|_| {
tracing::warn!("attribute value {} too big for i32", value.current);
Self::MAX
},
|value| value,
)
}
}
impl From<Attribute> for i16 {
fn from(value: Attribute) -> Self {
Self::try_from(value.current).map_or_else(
|_| {
tracing::warn!("attribute value {} too big for i16", value.current);
Self::MAX
},
|value| value,
)
}
}
impl From<Attribute> for i8 {
fn from(value: Attribute) -> Self {
Self::try_from(value.current).map_or_else(
|_| {
tracing::warn!("attribute value {} too big for i8", value.current);
Self::MAX
},
|value| value,
)
}
}
impl std::cmp::PartialEq<u32> for Attribute {
fn eq(&self, other: &u32) -> bool {
self.current == *other
}
}
impl std::cmp::PartialEq for Attribute {
fn eq(&self, other: &Self) -> bool {
self.current == other.current
}
}
impl std::cmp::Eq for Attribute {}
impl Percentage for Attribute {
fn percentage(&self) -> f32 {
self.remaining()
}
}