Advertisement
MyGameIsJeo

GoNumber Structure

Nov 28th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <Serializable> Public Structure GoNumber : Implements IXmlSerializable
  2.     <XmlElement(ElementName:="Value")> Public Property Value As Single
  3.         Get
  4.             Return Go
  5.         End Get
  6.         Set(value As Single)
  7.             Go = value
  8.             If Clamped Then
  9.                 If Go <= Minimum Then Go = Minimum
  10.                 If Go >= Maximum Then Go = Maximum
  11.             End If
  12.             Now = Go
  13.         End Set
  14.     End Property
  15.  
  16.     Public Property GoName As String
  17.         Get
  18.             Return _name
  19.         End Get
  20.         Set(value As String)
  21.             _name = value
  22.         End Set
  23.     End Property
  24.  
  25.  
  26.     Public Property Go As Single
  27.         Get
  28.             Return _Go
  29.         End Get
  30.         Set(Value As Single)
  31.             _Go = Value
  32.             ' SlideFunction = AddressOf DoSlide
  33.        End Set
  34.     End Property
  35.  
  36.     Private _Go As Single
  37.     Public Now, Last, DefaultValue, Minimum, Maximum, Percentage As Single
  38.     Public Clamped As Boolean
  39.     Public Precision As Single
  40.     Public Change, Percent, Start, InverseDifference As Single
  41.     Public Velocity, Acceleration, Difference As Single
  42.     Public Direction As Integer
  43.     Public SpaceName As String
  44.     Private _name As String
  45.  
  46. #Region "Initializers"
  47.  
  48.     Public Sub New(_Value As Single)
  49.         _name = ""
  50.         Now = _Value : Go = _Value : Last = _Value : DefaultValue = _Value
  51.         Minimum = 0
  52.         Maximum = 1
  53.         Precision = 1
  54.         Difference = 0.0001
  55.     End Sub
  56.     Public Sub New(_Value As Single, Optional _Min As Single = Nothing, Optional _Max As Single = Nothing,
  57.                    Optional _Precision As Single = 1)
  58.         Now = _Value : Go = _Value : Last = _Value : DefaultValue = _Value
  59.         Precision = IIf(_Precision = 0, 1, _Precision)
  60.         Difference = 0.0001
  61.  
  62.         Type = _GoType
  63.         If Type = GoType.ZeroOne Then
  64.             Minimum = 0 : Maximum = 1 : Precision = 0.01
  65.         End If
  66.         If _Min <> Nothing Then Minimum = _Min
  67.         If _Max <> Nothing Then Maximum = _Max
  68.         If Minimum <> Nothing And Maximum <> Nothing Then Clamped = True
  69.     End Sub
  70.  
  71. #End Region
  72.  
  73.     Public Sub [Set](_Value As Single)
  74.         _Go = _Value
  75.     End Sub
  76.     Public Sub GoNow(NewValue As Single)
  77.         Now = NewValue
  78.         Go = NewValue
  79.     End Sub
  80.  
  81.     Public Sub SlideWays(Up As Single, Down As Single, Optional ByVal Difference As Single = 0.0001)
  82.         Slide(IIf(Now < Go, Up, Down)) ', Difference)
  83.    End Sub
  84.     Public Sub Slide(ByVal Percentage As Single, Optional ByVal Difference As Single = 0.0001)
  85.  
  86.         Change = Go - Now
  87.         ' Adjust Velocity smoothly
  88.        Velocity += ((Change - Velocity) * 0.5) * Time.Factor100
  89.         'Move now towards
  90.        Now += ((Velocity) * Percentage) * Time.Factor100
  91.         'If the new difference is less than specified Difference, force Here to Go
  92.        'Formerly: If Math.Abs(Changed) < Difference Then Now = Go
  93.        Now = Now * (-(Math.Abs(Change) >= Difference)) +
  94.                Go * (-(Math.Abs(Change) < Difference))
  95.  
  96.     End Sub
  97.     Public Sub SlideClamp(ByVal Percentage As Single, Optional ByVal Difference As Single = 0.0001)
  98.         Slide(Clamp(Percentage, 0, 1))
  99.     End Sub
  100.     Public Sub SlideZero(Outward As Single, ToZero As Single)
  101.  
  102.         ' Negate Conditional Expressions to make them 0/1
  103.        Slide(-(Go > 0) * (Outward * -(Go >= Now)) +
  104.               -(Go <= 0) * (ToZero * -(Go <= Now)) +
  105.                (ToZero * -(Go = 0)))
  106.  
  107.         ' Formerly Before Branchless Optimization:
  108.        'If Go = 0 Then
  109.        '    Slide(ToZero)
  110.        'Else
  111.        '    Slide(IIf(Go > 0, IIf(Go >= Now, Outward, ToZero), IIf(Go <= Now, Outward, ToZero)))
  112.        'End If
  113.  
  114.     End Sub
  115.  
  116.  
  117. #Region "Glide Functions"
  118.  
  119.     Public Sub GlideStart(_Now As Single, _Go As Single, _Velocity As Single,
  120.                           Optional _Acceleration As Single = 0)
  121.         Now = _Now : Start = _Now : Go = _Go : Difference = Go - Now
  122.         Velocity = _Velocity : Acceleration = _Acceleration
  123.  
  124.         Direction = Math.Sign(Difference)
  125.         If Difference = 0 Then Difference = 1
  126.         InverseDifference = 1 / Difference
  127.         Change = 0
  128.     End Sub
  129.  
  130.     Public Sub Glide()
  131.         'If Go = Now Then Go = Now + 1
  132.        Velocity += Acceleration * Time.Factor
  133.         Change += Velocity * Time.Factor
  134.         Percent = ABS(Change * InverseDifference) ' Divide via multiplying a float
  135.        Now = Start + (Sine(Percent * 90) * Difference)
  136.         If ABS(Go - Now) < 0.1 Then Velocity *= 0.8
  137.         If ABS(Go - Now) < 0.01 Then
  138.             Velocity = 0
  139.             Acceleration = 0
  140.         End If
  141.         'If Velocity < 0.01 Then Velocity = 0
  142.    End Sub
  143.  
  144. #End Region
  145.  
  146. #Region "Operators and XML"
  147.     Public Shared Operator =(This As GoNumber, That As Single) As Boolean
  148.         Return This.Go = That
  149.     End Operator
  150.     Public Shared Operator <>(This As GoNumber, That As Single) As Boolean
  151.         Return This.Go <> That
  152.     End Operator
  153.     Public Shared Operator =(This As GoNumber, That As GoNumber) As Boolean
  154.         Return This.Go = That.Go
  155.     End Operator
  156.     Public Shared Operator <>(This As GoNumber, That As GoNumber) As Boolean
  157.         Return This.Go <> That.Go
  158.     End Operator
  159.     Public Sub SetName(NewName As String)
  160.         _name = NewName
  161.     End Sub
  162.     Public Function GetSchema() As XmlSchema Implements IXmlSerializable.GetSchema
  163.         Return Nothing ' This Will Probably Always Be Nothing
  164.    End Function
  165.     Public Sub ReadXml(Reader As XmlReader) Implements IXmlSerializable.ReadXml
  166.         With Reader
  167.             _Go = .GetAttribute("Value")
  168.             'Minimum = .GetAttribute("Min")
  169.            'Maximum = .GetAttribute("Max")
  170.            'Precision = .GetAttribute("Precision")
  171.        End With
  172.     End Sub
  173.     Public Sub WriteXml(Writer As XmlWriter) Implements IXmlSerializable.WriteXml
  174.         'Writer.WriteAttributeString("Value", Go)
  175.        With Writer
  176.             .WriteStartElement(GoName)
  177.             .WriteAttributeString("Value", _Go)
  178.             '.WriteAttributeString("Min", Minimum)
  179.            '.WriteAttributeString("Max", Maximum)
  180.            '.WriteAttributeString("Precision", Precision)
  181.            .WriteEndElement()
  182.         End With
  183.     End Sub
  184.     Public Overrides Function ToString() As String
  185.         Return $"{_name} = {Math.Round(Now, 2)}{vbTab} -> {Math.Round(Go, 2)} {vbTab}({Math.Round(Velocity, 2)})"
  186.     End Function
  187. #End Region
  188.  
  189. End Structure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement