Follow the leader
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

57 lines
1.9KB

  1. using System;
  2. using RobocraftX.Common;
  3. using Gamecraft.Blocks.HUDFeedbackBlocks;
  4. using GamecraftModdingAPI;
  5. using GamecraftModdingAPI.Engines;
  6. using GamecraftModdingAPI.Events;
  7. using GamecraftModdingAPI.Players;
  8. using GamecraftModdingAPI.Tasks;
  9. using GamecraftModdingAPI.Utility;
  10. using Svelto.ECS;
  11. using Unity.Jobs;
  12. using Leadercraft.Server;
  13. namespace Leadercraft.Scoring
  14. {
  15. internal class GameLoop : SimpleEventHandlerEngine
  16. {
  17. private static Player localPlayer;
  18. public GameLoop() : base (OnGameEnter, (_) => {}, EventType.GameSwitchedTo, "LeadercraftGameLoopGameEngine") { }
  19. public static void OnGameEnter(EntitiesDB entitiesDB)
  20. {
  21. // schedule game loop async task
  22. Action loop = () => { loopPass(entitiesDB); };
  23. ISchedulable looper = new Repeatable(loop, () => { return State.IsInGame; }, LeadercraftPlugin.LoopDelay);
  24. Scheduler.Schedule(looper);
  25. }
  26. private static void loopPass(EntitiesDB entitiesDB)
  27. {
  28. if (!State.IsPlayingGame) return;
  29. if (localPlayer == null && Player.Exists(PlayerType.Local)) localPlayer = new Player(PlayerType.Local);
  30. FilteredChannelDataStruct[] channelInfo = entitiesDB.QueryEntities<FilteredChannelDataStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP).ToFastAccess(out uint count);
  31. for (uint i = 0; i < count; i++)
  32. {
  33. FilteredChannelDataStruct data = channelInfo[i];
  34. if(data.channelSignals.any && entitiesDB.Exists<GameOverHudBlockEntityStruct>(data.ID))
  35. {
  36. GameOverHudTextEntityStruct hudText = entitiesDB.QueryEntity<GameOverHudTextEntityStruct>(data.ID);
  37. if (((string)hudText.headerText).ToLower().Contains("win") || ((string)hudText.bodyText).ToLower().Contains("win"))
  38. {
  39. State.StopPlayingGame();
  40. //State.GamePlayTime.TotalSeconds
  41. UploadJob scoreJob = new UploadJob(State.Score, State.GamePlayTime.TotalSeconds, localPlayer.Position, Tools.UserId, Tools.UserName, Tools.GameId);
  42. scoreJob.RunInNewThread();
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }