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.

GameLoop.cs 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 (localPlayer == null) localPlayer = new Player(PlayerType.Local);
  29. if (!State.IsPlayingGame) return;
  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. }