123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- using gameapi.Library;
- using Microsoft.Extensions.Hosting;
- public class TickController : IHostedService
- {
- private Timer timer;
- private GameState game;
- private DateTime lastTime;
- public TickController()
- {
- this.game = GameState.GetInstance();
- lastTime = DateTime.UtcNow;
- }
- public Task StartAsync(CancellationToken cancellationToken)
- {
- this.timer = new Timer(o => TickUpdate(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(100));
- return Task.CompletedTask;
- }
- public async void TickUpdate()
- {
- List<Task> tasksToComplete = new List<Task>();
- var population = this.game.GetAllPlayers();
- var currentTime = DateTime.UtcNow;
- var delta = 1 + currentTime.Subtract(lastTime).TotalMilliseconds / 1000;
- // foreach (var player in population)
- // {
- // player.Update(delta);
- // tasksToComplete.Add(this.game.MessageAllOtherPlayersInSector(player, "move " + player.GetShipData()));
- // }
- await Task.WhenAll(tasksToComplete);
- lastTime = currentTime;
- }
- public Task StopAsync(CancellationToken cancellationToken)
- {
- timer.Change(Timeout.Infinite, 0);
- return Task.CompletedTask;
- }
- }
|