@@ -43,25 +43,83 @@ public override void _Ready()
4343 }
4444 }
4545
46+ public override void _Process ( double delta )
47+ {
48+ ProcessShake ( delta ) ;
49+ }
50+
4651 public void Init ( Texture2D texture , string name )
4752 {
4853 Sprite . Texture = texture ;
4954 UniqName = name ;
5055 }
5156
57+ //Juice - https://www.youtube.com/watch?v=LGt-jjVf-ZU
58+ private int _limiter ;
59+ private const float BaseShake = 100f ;
60+ private float _shakeFade = 10f ;
61+ private float _shakeStrength ;
62+ private float _baseAnimDuration = 0.1f ;
63+
64+ private void ProcessShake ( double delta )
65+ {
66+ _limiter = ( _limiter + 1 ) % 3 ;
67+ if ( _limiter != 1 )
68+ return ;
69+
70+ if ( _shakeStrength > 0 )
71+ {
72+ _shakeStrength = ( float ) Mathf . Lerp ( _shakeStrength , 0 , _shakeFade * delta ) ;
73+ }
74+ Position =
75+ StartPos
76+ + new Vector2 (
77+ ( float ) GD . RandRange ( - _shakeStrength , _shakeStrength ) ,
78+ ( float ) GD . RandRange ( - _shakeStrength , _shakeStrength )
79+ ) ;
80+ }
81+
82+ protected virtual void DamageAnimate ( int amount )
83+ { //TODO: Make animate in time with bpm
84+ float damageAnimDir = ( GetViewportRect ( ) . Size / 2 - Position ) . Normalized ( ) . X ;
85+ float scalar = ( float ) amount / _maxHealth ;
86+ _shakeStrength = ( scalar * BaseShake ) ;
87+
88+ Color flashColor = Colors . White * 99 ; //White = neutral modulate, very white is higher contrast white
89+ if ( amount < 0 ) //If healing
90+ {
91+ flashColor = Colors . Green ;
92+ damageAnimDir = 0 ;
93+ _shakeStrength = 0 ;
94+ }
95+
96+ var tween = CreateTween ( ) ;
97+ tween . SetTrans ( Tween . TransitionType . Spring ) ;
98+ tween . TweenProperty ( this , "modulate" , flashColor , _baseAnimDuration ) ;
99+ tween . Chain ( ) . TweenProperty ( this , "modulate" , Colors . White , _baseAnimDuration ) ;
100+ tween . Parallel ( ) ;
101+ tween
102+ . TweenProperty (
103+ this ,
104+ "position:x" ,
105+ - ( damageAnimDir * 2 ) * ( 2 + scalar ) ,
106+ _baseAnimDuration
107+ )
108+ . AsRelative ( ) ;
109+ tween . Chain ( ) . TweenProperty ( this , "position" , StartPos , 2 * _baseAnimDuration ) ;
110+ }
111+
52112 public virtual void TakeDamage ( int amount )
53113 {
54- if ( _currentHealth <= 0 )
55- return ; //TEMP Only fire once.
56114 amount = Math . Max ( 0 , amount ) ; //Should not be able to heal from damage.
115+ if ( _currentHealth <= 0 || amount == 0 )
116+ return ; //TEMP Only fire once.
57117 _currentHealth = _healthBar . ChangeHP ( - amount ) ;
118+ DamageAnimate ( amount ) ;
58119 if ( _currentHealth <= 0 )
59120 {
60121 Defeated ? . Invoke ( this ) ;
61122 }
62-
63- if ( amount == 0 )
64- return ;
65123 TextParticle newText = new TextParticle ( ) ;
66124 newText . Modulate = Colors . Red ;
67125 Sprite . AddChild ( newText ) ;
@@ -71,9 +129,10 @@ public virtual void TakeDamage(int amount)
71129 public virtual void Heal ( int amount )
72130 {
73131 _currentHealth = _healthBar . ChangeHP ( amount ) ;
74-
132+ amount = Math . Max ( 0 , amount ) ; //Should not be able to damage from heal.
75133 if ( amount == 0 )
76134 return ;
135+ DamageAnimate ( - amount ) ;
77136 TextParticle newText = new TextParticle ( ) ;
78137 newText . Modulate = Colors . Green ;
79138 Sprite . AddChild ( newText ) ;
0 commit comments