import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'dart:math'; import 'dart:ui'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get_it/get_it.dart'; import 'package:path_provider/path_provider.dart'; import 'package:provider/provider.dart'; import 'package:sport/application.dart'; import 'package:sport/bean/game.dart'; import 'package:sport/game/sdk_parse.dart'; import 'package:sport/pages/web/statistics.dart'; import 'package:sport/provider/bluetooth.dart'; import 'package:sport/provider/user_model.dart'; import 'package:sport/services/Converter.dart'; import 'package:sport/services/api/inject_api.dart'; import 'package:sport/services/app_subscription_state.dart'; import 'package:sport/utils/toast.dart'; import 'package:wakelock/wakelock.dart'; import 'package:web_socket_channel/io.dart'; import 'package:webview_flutter/webview_flutter.dart'; class GameTestPage extends StatefulWidget { const GameTestPage({Key? key}) : super(key: key); @override State createState() => _PageState(); } class _PageState extends State with SubscriptionState, InjectApi, TickerProviderStateMixin { late Bluetooth bluetooth; Timer? _timer; Timer? _timerGameMode; bool _onPageFinished = false; int _onPageProgress = 0; int _cmd = 0; int _timeSecond = 0; bool _gamePlaying = false; bool _openGameData = false; int _stepFreq = 0, _stepCount = 0; Statistics? _statistics; late String playGroup; int sample = 30; List> c = []; @override void initState() { super.initState(); playGroup = "${DateTime.now().millisecondsSinceEpoch}"; bluetooth = GetIt.I(); bluetooth.gameInit(100); bluetooth.setupGameMode4h5(true); AnimationController _animationController = new AnimationController(duration: Duration(milliseconds: 200), vsync: this); _timer = Timer.periodic(Duration(milliseconds: 8), (timer) { _timeSecond++; // if (_onPageFinished != true) return; // if(_xy < -1400){ // _type = Random().nextInt(3); // _xy = 0; // _xx = Random().nextInt(300) + 100; // } // _xy -= 3; // _jump = max(0, --_jump); // _down = max(0, --_down); // if(_down > 0){ // int dd = 30 - _down; // _speed = _speed + 4; // }else { // _speed = max(0, _speed - 4); // } // print("xy $_xx $_xy"); // double width = MediaQuery.of(context).size.longestSide; // double target= _dx -100; // target = max(-width/2, min(target, width/2)); // _animationController.stop(); // print("111111111111111111111111 222222222222222222222222222222222 ${target}" ); // Tween(begin: _dx, end: target) // .animate(_animationController).addListener(() { // setState(() { // print("111111111111111111111111 ${_animationController.value}" ); // _dx += _animationController.value; // }); // }); // _animationController.forward(from: 0); double px = 5.0; if(_xx > _dx){ _dx += px; }else if(_xx < _dx){ _dx -= px; } double range = screen/2; _dx = max(-range, min(_dx, range)); if(_xx < -range){ _xx = -range.toInt(); } if(_xx > range){ _xx = range.toInt(); } setState(() { }); }); _timerGameMode = Timer.periodic(Duration(seconds: 60), (timer) { if (_onPageFinished == true) bluetooth.setupGameMode4h5(true); _onPageFinished = true; }); addSubscription(bluetooth.sdkMotionStream.listen((event) { if (_onPageFinished != true) return; if (_gamePlaying == true) { // String gameData = sdk.getGameDataStr(); // if(gameData.length < 10)return; // List data = gameData.split(",").map((e) => int.parse(e)).toList(); // if (c.isNotEmpty == true) { // int length = c.length; // List mag = [0, 0, 0, 0]; // int down = 0; // List magTotal = List.filled(mag.length, 0); // List magLast = List.filled(mag.length, 0); // for (var i = 0; i < 2; i++) { // magTotal[i * 2] = c[0][i * 12 + 9]; // magTotal[i * 2 + 1] = c[0][i * 12 + 9 + 1]; // for (var j = 1; j < length; j++) { // int d1 = c[j][i * 12 + 9] - c[j-1][i* 12 + 9]; // int d2 = c[j][i * 12 + 9 + 1] - c[j-1][i * 12 + 9 + 1]; // mag[i * 2] += d1.abs() > 100 ? 1 : -1; // mag[i * 2 + 1] += d2.abs() > 100 ? 1 : -1; // // magLast[i * 2] = c[j][i * 12 + 9]; // magLast[i * 2 + 1] = c[j][i * 12 + 9 + 1]; // } // } // List magflag = List.filled(mag.length, false); // List ffff = [15500,11800, 18000, 13000]; // List fffff = [15500,19000, 21000, 22000]; // for (var i = 0; i < mag.length; i++) { // // int p = mag[i]; // // line[i] = magTotal[i] / length; // // if(p >= 15){ // // magflag[i] = true; // // }else if(magflag1[i]){ // // if(magLast[i] > 14000){ // // magflag[i] = true; // // } // // } // if(magLast[i] > ffff[i]){ // magflag[i] = true; // } // // if(magLast[i] > fffff[i]){ // // down ++; // // } // } // print("test data $mag $magflag $line $magLast"); // // List result = sdk.getMotion(); // print("sdk -- motion $result"); // if (result.any((element) => element == 3)) { // down = 5; // } // // setState(() { // _rf = magflag[0]; // _rb = magflag[1]; // _lf = magflag[2]; // _lb = magflag[3]; // // double olddx = _dx; // // if(_lb && _rb){ // // _dx -= 2; // // } // // if(_lf && _rf){ // // _dx += 2; // // } // // // // if(data[11] == 0 && data[11 + 12] == 0){ // // _jump = 30; // // } // int d = magflag.map((e) => e ? 1 : 0).reduce((value, element) => value + element); // print("test d _down $down"); // if(down > 1){ // _down = 30; // // _speed += 150; // }else{ // if(_lb){ // _dx -= 2; // } // if(_lf){ // _dx -= 2; // } // if(_rb){ // _dx += 2; // } // if(_rf){ // _dx += 2; // } // // if(_dx < -100){ // _dx = -100; // } // if(_dx > 100){ // _dx = 100; // } // // _ori = olddx - _dx; // } // if(_speed > 500){ // _speed = 500; // } // // if(_lf && _lb && _rb){ // // _down = 30; // // } // // if(_lf && _lb && _rf){ // // _speed += 4; // // } // // int d = magflag.map((e) => e ? 1 : 0).reduce((value, element) => value + element); // // print("test d _down $d"); // // if(magflag.map((e) => e ? 1 : 0).reduce((value, element) => value + element) >=3){ // // _down = 30; // // } // // }); // } // c.add(data); // if (c.length > sample) { // c.removeAt(0); // } List result = event; // print("11111111111111111 $result"); // setState(() { // double width = MediaQuery.of(context).size.longestSide; // if(data[23] == 1){ // _xttttb = data[12].toDouble(); // _xttttyb = left; // }else{ // left = _xttttyb -(_xttttb - data[12].toDouble()); // _dx = max(-width/2, min((left + right) / 2, width/2)); // print("left = $_xtttt ${data[0].toDouble()} $_dx ${data[11]}"); // } // if(data[11] == 1){ // _xtttt = data[0].toDouble(); // _xtttty = right; // }else{ // right = _xtttty -(_xtttt - data[0].toDouble()); // _dx = max(-width/2, min((left + right) / 2, width/2)); // print("right = $_xtttt ${data[0].toDouble()} $_dx ${data[11]}"); // } double offset = 100; // result[0] = 200; if(result[0] > 0){ _xx -= result[0] ; } if(result[1] > 0){ _xx +=result[1] ; } // if(result[2] > 0){ // _dy -=offset; // }if(result[3] > 0){ // _dy +=offset; // } // }); } })); WidgetsBinding.instance?.addPostFrameCallback((timeStamp) { _onGameStart(); _onPageFinished = true; screen = MediaQuery.of(context).size.longestSide; print("1111111111111111111111 screen $screen"); }); SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]); } double screen =0; double right = 0, left = 0; @override void dispose() { SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]); _timer?.cancel(); _timerGameMode?.cancel(); bluetooth.setupGameMode4h5(false); super.dispose(); } _initGame() { bluetooth.setupGameMode4h5(true); bluetooth.gameInit(100); } _onGameStart() { _gamePlaying = true; _initGame(); } _onGameEnd({int level = 0, double score = 0, int record = 0, int mode = 0, int opponentId = 0, int isCancel = 0}) { _gamePlaying = false; } bool _lf = false, _lb = false; bool _rf = false, _rb = false; int _down = 0; int _jump = 0; double _dx = 0; double _dy = 0; int _xx = 0; int _xy = 0; int _type = 0; double _ori = 0; int _speed = 0; List line = [0, 0, 0, 0]; double _xtttt = 0; double _xtttty = 0; double _xttttb = 0; double _xttttyb = 0; @override Widget build(BuildContext context) { return Scaffold( body: CustomPaint( painter: _GamePainter(lf:_lf, lb:_lb, rf:_rf, rb:_rb, dx: _dx, xx:_xx, xy: _dy.toInt(), down: _down,jump: _jump, type: _type, ori: _ori,speed: _speed), child: Container( width: double.infinity, height: double.infinity, ), ), ); } } class _GamePainter extends CustomPainter { bool lf = false, lb = false; bool rf = false, rb = false; double dx = 0; int xx = 0; int xy = 0; int down = 0; int jump = 0; int type = 0; double a = 0; int speed = 0; double ori = 0; _GamePainter({this.lf = false, this.lb = false, this.rf = false, this.rb = false, this.dx = 0,this.xx = 0, this.xy = 0, this.down = 0, this.jump = 0, this.type = 0, this.ori = 0, this.speed = 0}); final Paint _paintSkate = Paint() ..color = Colors.blue ..strokeWidth = 0.5 ..isAntiAlias = true; final Paint _paintSkateDown = Paint() ..color = Colors.yellow ..strokeWidth = 0.5 ..isAntiAlias = true; final Paint _paintSkateJump = Paint() ..color = Colors.purple ..strokeWidth = 0.5 ..isAntiAlias = true; final Paint _paintPoint = Paint() ..color = Colors.black ..isAntiAlias = true; final Paint _paintPointPressed = Paint() ..color = Colors.red ..isAntiAlias = true; final Paint _paintLine = Paint() ..color = Colors.black ..isAntiAlias = true; final Paint _paintLineOver = Paint() ..color = Colors.yellow ..isAntiAlias = true; @override void paint(Canvas canvas, Size size) { final bg = Rect.fromLTWH(0, 0, size.width, size.height); final center = bg.center; print("bg $bg, center: $center"); canvas.save(); var c = center.translate(dx, xy.toDouble() + size.height / 2); canvas.translate(c.dx, c.dy); // // if(ori > 0){ // canvas.rotate(-0.1); // }else if(ori < 0){ // canvas.rotate(0.1); // } // var skate = Rect.fromCenter(center: Offset(0,0 - this.speed.toDouble()), width: 100, height: 250); // // canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkate); // if(jump > 0){ // canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkateJump); // }else if(down > 0){ // canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkateDown); // } // canvas.drawCircle(skate.center.translate(-20, -20), 15, lb ? _paintPointPressed : _paintPoint); // canvas.drawCircle(skate.center.translate(20, -40), 15, lf ? _paintPointPressed : _paintPoint); // // canvas.drawCircle(skate.center.translate(-20, 60), 15, rb ? _paintPointPressed : _paintPoint); // canvas.drawCircle(skate.center.translate(20, 40), 15, rf ? _paintPointPressed : _paintPoint); // // canvas.translate(-c.dx, -c.dy); var x1 = Offset(0,0 - this.speed.toDouble()); // var skate = Rect.fromCenter(center: Offset(0,0 - this.speed.toDouble()), width: 50, height: 150); for(var i = 0; i < 2; i++){ var skate = Rect.fromCenter(center: Offset(x1.dx + (i == 0?-30.0:30.0),x1.dy), width: 40, height: 200); canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkate); if(jump > 0){ canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkateJump); }else if(down > 0){ canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkateDown); } canvas.drawCircle(skate.center.translate(0, 30), 15, (i == 0 ? lb : rb) ? _paintPointPressed : _paintPoint); canvas.drawCircle(skate.center.translate(0, -30), 15, (i == 0 ? lf : rf) ? _paintPointPressed : _paintPoint); } // canvas.drawCircle(skate.center.translate(-20, -20), 15, lb ? _paintPointPressed : _paintPoint); // canvas.drawCircle(skate.center.translate(20, -40), 15, lf ? _paintPointPressed : _paintPoint); // // canvas.drawCircle(skate.center.translate(-20, 60), 15, rb ? _paintPointPressed : _paintPoint); // canvas.drawCircle(skate.center.translate(20, 40), 15, rf ? _paintPointPressed : _paintPoint); canvas.translate(-c.dx, -c.dy); // canvas.translate(size.width / 2, size.height / 2); canvas.restore(); // if(type == 0) { // var line = Rect.fromLTWH(xx.toDouble(), -xy.toDouble() - 400, 10, 500); // canvas.drawRect(line, _paintLine); // }else if(type == 1) { // var line = Rect.fromLTWH(0, -xy.toDouble() - 100, size.width, 200); // canvas.drawRect(line, _paintSkateJump); // }else if(type == 2) { // var line = Rect.fromLTWH(0, -xy.toDouble() - 200, size.width, 400); // canvas.drawRect(line, _paintSkateDown); // } // // var line = Rect.fromLTWH(xx.toDouble(), -xy.toDouble() - 400, 10, 500); // canvas.drawRect(line, _paintLine); final text = TextPainter( text: TextSpan( style: TextStyle(fontSize: 12.0, color: Colors.red), text: "${dx}", ), textAlign: TextAlign.left, textDirection: TextDirection.ltr, ) ..layout(minWidth: 100); text.paint(canvas, Offset(50, 50)); } @override bool shouldRepaint(covariant CustomPainter oldDelegate) { return true; } }