123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- 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<StatefulWidget> createState() => _PageState();
- }
- class _PageState extends State<GameTestPage> 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<List<int>> 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<double>(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<int> data = gameData.split(",").map((e) => int.parse(e)).toList();
- // if (c.isNotEmpty == true) {
- // int length = c.length;
- // List<int> mag = [0, 0, 0, 0];
- // int down = 0;
- // List<int> magTotal = List.filled(mag.length, 0);
- // List<int> 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<bool> magflag = List.filled(mag.length, false);
- // List<int> ffff = [15500,11800, 18000, 13000];
- // List<int> 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<int> 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<int> 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<double> 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;
- }
- }
|