Files
Ragon/Ragon.Server/Sources/Time/RagonActionTimer.cs
T

45 lines
1.1 KiB
C#
Raw Normal View History

2024-05-19 08:00:56 +03:00
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2024-04-13 16:17:31 +03:00
using Ragon.Server.Time;
2024-05-19 08:00:56 +03:00
public class RagonActionTimer : IRagonAction
2024-04-13 16:17:31 +03:00
{
2024-05-19 08:00:56 +03:00
public bool IsDone => _repeatCount == 0;
2024-04-13 16:17:31 +03:00
private Action _callback;
private float _timer;
private float _time;
2024-05-19 08:00:56 +03:00
private float _repeatCount;
public RagonActionTimer(Action callback, float timeInSeconds, int repeat = 1)
2024-04-13 16:17:31 +03:00
{
_callback = callback;
_time = timeInSeconds * 1000;
2024-05-19 08:00:56 +03:00
_repeatCount = repeat;
2024-04-13 16:17:31 +03:00
}
public void Tick(float dt)
{
_timer += dt;
if (_timer >= _time)
2024-04-13 18:25:39 +03:00
{
2024-04-13 16:17:31 +03:00
_callback?.Invoke();
2024-04-13 18:25:39 +03:00
_timer = 0;
}
2024-04-13 16:17:31 +03:00
}
}