~ruther/NosSmooth

ref: 762fc34c97e8b1f6e9290426b8bc2a78122e28ba NosSmooth/Core/NosSmooth.Game/Data/Info/Health.cs -rw-r--r-- 2.5 KiB
762fc34c — Rutherther Merge pull request #82 from Rutherther/feat/serialize-stackalloc 2 years ago
                                                                                
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
//
//  Health.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace NosSmooth.Game.Data.Info;

/// <summary>
/// Represents the health or mana of an entity.
/// </summary>
public class Health
{
    private byte? _percentage;
    private long? _amount;
    private long? _maximum;

    /// <summary>
    /// Gets or sets the percentage of the health.
    /// </summary>
    public byte? Percentage
    {
        get => _percentage;
        set
        {
            _percentage = value;
            if (value is null)
            {
                return;
            }

            var maximum = _maximum;
            if (maximum is not null)
            {
                _amount = (long)((value / 100.0) * maximum);
                return;
            }

            var amount = _amount;
            if (amount is not null)
            {
                _maximum = (long)(amount / (value / 100.0));
            }
        }
    }

    /// <summary>
    /// Gets or sets the health amount.
    /// </summary>
    public long? Amount
    {
        get => _amount;
        set
        {
            _amount = value;
            if (value is null)
            {
                return;
            }

            var maximum = _maximum;
            if (maximum is not null)
            {
                _percentage = (byte)(((double)value / maximum) * 100);
                return;
            }

            var percentage = _percentage;
            if (percentage is not null)
            {
                _maximum = (long)(value / (percentage / 100.0));
            }
        }
    }

    /// <summary>
    /// Gets or sets the maximum health.
    /// </summary>
    public long? Maximum
    {
        get => _maximum;
        set
        {
            _maximum = value;
            if (value is null)
            {
                return;
            }

            var amount = _amount;
            var percentage = _percentage;

            if (amount is not null)
            {
                if (amount > value)
                {
                    amount = _amount = value;
                    _percentage = 100;
                    return;
                }

                _percentage = (byte)((amount / (double)value) * 100);
                return;
            }

            if (percentage is not null)
            { // ? would this be correct?
                _amount = (long)((percentage / 100.0) * value);
            }
        }
    }
}
Do not follow this link