guardian_controller.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:async';
  2. import 'package:dart_jts/dart_jts.dart';
  3. import 'package:trackoffical_app/model/game_person_data.dart';
  4. import 'package:trackoffical_app/service/game/game_instance_guardian.dart';
  5. import 'package:trackoffical_app/service/game/game_manager_service.dart';
  6. import '../../../model/game_map.dart';
  7. import '../../../model/m_position.dart';
  8. import '../../../service/app.dart';
  9. import '../layer/layer_controller.dart';
  10. class GuardianControllerMock extends LayerController {
  11. var instance = GameInstanceGuardian(underGuardianId: -1);
  12. final selectedPersonIndex = 0.obs;
  13. final Rx<int?> lockUnderGuardianToCenterUserId = Rx(null);
  14. final Rx<MPosition?> guardianPosition = Rx(null);
  15. final Rx<Offset?> guardianPositionOnMap = Rx(null);
  16. final isShowGuardian = true.obs;
  17. final isShowRoute = true.obs;
  18. StreamSubscription<MPosition>? _subscriptionPosition;
  19. GamePersonData? get selectedPersonData {
  20. if (instance.gamePersonData.isEmpty) {
  21. return null;
  22. }
  23. var i = instance.gamePersonData.length - 1;
  24. if (selectedPersonIndex.value < i) {
  25. i = selectedPersonIndex.value;
  26. }
  27. return instance.gamePersonData[i];
  28. }
  29. void setUserById(int userId){
  30. for(var i=0; i< instance.gamePersonData.length;i++){
  31. if(instance.gamePersonData[i].userId==userId){
  32. selectedPersonIndex.value =i;
  33. break;
  34. }
  35. }
  36. }
  37. GamePersonData? getUserById(int userId){
  38. for(var i=0; i< instance.gamePersonData.length;i++){
  39. if(instance.gamePersonData[i].userId==userId){
  40. return instance.gamePersonData[i];
  41. }
  42. }
  43. return null;
  44. }
  45. void lockUnderGuardianToCenter(GamePersonData person){
  46. if(lockUnderGuardianToCenterUserId.value!=null){
  47. lockUnderGuardianToCenterUserId.value=null;
  48. isEnableUserTouchTranslation=true;
  49. }else{
  50. lockUnderGuardianToCenterUserId.value = person.userId;
  51. isEnableUserTouchTranslation=false;
  52. }
  53. }
  54. void moveScreenCenterToGuardianLocation(){
  55. final gp = guardianPositionOnMap.value;
  56. if(gp!= null){
  57. moveOnMapPointToScreen(gp, mapRotateCenter.value);
  58. }
  59. }
  60. @override
  61. GameMap? get gameMap => instance.gameMapData;
  62. @override
  63. void onMapSizeChange(Size size) {
  64. super.onMapSizeChange(size);
  65. mapRotateCenter.value = Offset(size.width / 2, size.height / 2);
  66. }
  67. @override
  68. void onClose() {
  69. super.onClose();
  70. guardianPosition.close();
  71. }
  72. }
  73. class GuardianController extends GuardianControllerMock {
  74. @override
  75. GameInstanceGuardian get instance {
  76. final i = GameManagerService.to.instance!;
  77. if (i is! GameInstanceGuardian) {
  78. throw RuntimeException('未开始监控');
  79. }
  80. return i;
  81. }
  82. Timer? _timerUnderGuardianToScreenCenter;
  83. @override
  84. void onReady() {
  85. super.onReady();
  86. final userId = Get.arguments;
  87. if(userId is int){
  88. setUserById(userId);
  89. }
  90. final locationStream = App.to.locationStream;
  91. App.to.locationStart(500.milliseconds, 0);
  92. _subscriptionPosition?.cancel();
  93. _subscriptionPosition=locationStream.listen((event) {
  94. guardianPosition.value=event;
  95. gameMap?.worldToPixel(event).then((offset){
  96. guardianPositionOnMap.value= offset;
  97. });
  98. });
  99. // guardianPosition.bindStream(Sensor.locationStream.map((event) => event.toModel()));
  100. _timerUnderGuardianToScreenCenter = Timer.periodic(200.milliseconds, (timer) {
  101. final userId = lockUnderGuardianToCenterUserId.value;
  102. if(userId!= null){
  103. final user = getUserById(userId);
  104. if(user!= null){
  105. var p = user.myPositionOnMap;
  106. if(p!= null){
  107. moveOnMapPointToScreen(p, mapRotateCenter.value);
  108. }
  109. }
  110. }
  111. });
  112. }
  113. @override
  114. void onClose() {
  115. super.onClose();
  116. _timerUnderGuardianToScreenCenter?.cancel();
  117. _subscriptionPosition?.cancel();
  118. GameManagerService.to.instanceStop();
  119. App.to.locationStop();
  120. }
  121. }