|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Xml.Linq; |
| 4 | + |
| 5 | +namespace ScreenSaverConections |
| 6 | +{ |
| 7 | + class CPoint : IDisposable |
| 8 | + { |
| 9 | + private readonly Settings _Settings; |
| 10 | + public readonly bool Bound; |
| 11 | + public readonly int Group; |
| 12 | + public bool Visible = true; |
| 13 | + public float Alpha = 1; |
| 14 | + private float _AlphaSpeed = 0.025f; |
| 15 | + private float _XStart; |
| 16 | + private float _YStart; |
| 17 | + public float X; |
| 18 | + public float Y; |
| 19 | + private float _Speed; |
| 20 | + private float _Time = 0; |
| 21 | + private float _Counter = 0; |
| 22 | + private float _Acc = 0; |
| 23 | + private float _Direction; |
| 24 | + private float _RotateSpeed = 0; |
| 25 | + public Color _Color; |
| 26 | + private readonly int _Width; |
| 27 | + private readonly int _Height; |
| 28 | + private readonly Random _Rnd; |
| 29 | + |
| 30 | + private readonly int _MaxDistanceWhenBound = 10; |
| 31 | + |
| 32 | + |
| 33 | + public CPoint(int x, int y, int width, int height, Random rnd, Color color, Settings settings, bool bound, int group = 0) |
| 34 | + { |
| 35 | + _Settings = settings; |
| 36 | + Bound = bound; |
| 37 | + Group = group; |
| 38 | + _XStart = x; |
| 39 | + _YStart = y; |
| 40 | + X = x; |
| 41 | + Y = y; |
| 42 | + _Width = width; |
| 43 | + _Height = height; |
| 44 | + _Rnd = rnd; |
| 45 | + _Speed = _Settings.SpeedMax / 2; |
| 46 | + _Direction = (float)(rnd.Next(360) / 180d * Math.PI); |
| 47 | + _Color = color; |
| 48 | + } |
| 49 | + |
| 50 | + public void Update() |
| 51 | + { |
| 52 | + ChangeSpeed(); |
| 53 | + Move(); |
| 54 | + if (Visible) Alpha = Math.Min(Alpha + _AlphaSpeed, 1); |
| 55 | + else Alpha = Math.Max(Alpha - _AlphaSpeed, 0); |
| 56 | + } |
| 57 | + private void Move() |
| 58 | + { |
| 59 | + X += (float)(Math.Cos(_Direction) * _Speed); |
| 60 | + Y += (float)(Math.Sin(_Direction) * _Speed); |
| 61 | + |
| 62 | + if (X > _Width) _Direction = (float)(Math.PI - _Direction); |
| 63 | + if (X < 0) _Direction = (float)(Math.PI - (_Direction - Math.PI) + Math.PI); |
| 64 | + if (Y > _Height) _Direction = (float)(Math.PI - (_Direction + Math.PI / 2) - Math.PI / 2); |
| 65 | + if (Y < 0) _Direction = (float)(Math.PI - (_Direction + Math.PI / 2) - Math.PI / 2); |
| 66 | + X = Math.Max(Math.Min(X, _Width), 0); |
| 67 | + Y = Math.Max(Math.Min(Y, _Height), 0); |
| 68 | + |
| 69 | + if (Bound) |
| 70 | + { |
| 71 | + var speed = _Settings.DEV_Presentation_BoundSpeed / Program.SizeMul; |
| 72 | + var speedX = speed * (_XStart - X) / _MaxDistanceWhenBound; |
| 73 | + var speedY = speed * (_YStart - Y) / _MaxDistanceWhenBound; |
| 74 | + X += speedX; |
| 75 | + Y += speedY; |
| 76 | + } |
| 77 | + } |
| 78 | + private void ChangeSpeed() |
| 79 | + { |
| 80 | + _Speed += _Acc; |
| 81 | + _Speed = Math.Max(Math.Min(_Speed, _Settings.SpeedMax), -_Settings.SpeedMax); |
| 82 | + _Direction += _RotateSpeed; |
| 83 | + if (_Counter > _Time) |
| 84 | + { |
| 85 | + _Time = _Rnd.Next(_Settings.TimeMin, _Settings.TimeMax); |
| 86 | + _Counter = 0; |
| 87 | + var nextAcc = _Rnd.Next(0, (int)_Settings.SpeedMax) / 10f; |
| 88 | + if (_Speed == _Settings.SpeedMax) _Acc = -nextAcc; |
| 89 | + else if (_Speed == -_Settings.SpeedMax) _Acc = nextAcc; |
| 90 | + else |
| 91 | + { |
| 92 | + if (_Rnd.Next(2) == 1) nextAcc *= -1; |
| 93 | + _Acc = nextAcc; |
| 94 | + } |
| 95 | + _RotateSpeed = (float)(_Rnd.Next(0, (int)(_Settings.RotateSpeedMax * 360)) / 180d / Math.PI); |
| 96 | + if (_Rnd.Next(2) == 1) _RotateSpeed *= -1; |
| 97 | + } |
| 98 | + _Counter++; |
| 99 | + } |
| 100 | + |
| 101 | + public void Draw(IGraphics g) |
| 102 | + { |
| 103 | + if (Alpha == 0) return; |
| 104 | + var c = _Color; |
| 105 | + if (Alpha != 1) c = Color.FromArgb((int)(Alpha * 255), c); |
| 106 | + g.FillEllipse(c, X, Y, _Settings.PointRadius, _Settings.PointRadius); |
| 107 | + } |
| 108 | + |
| 109 | + public void Dispose() |
| 110 | + { |
| 111 | + } |
| 112 | + |
| 113 | + internal void SetStartPos(int x, int y) |
| 114 | + { |
| 115 | + _XStart = x; |
| 116 | + _YStart = y; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments