Page MenuHomec4science

Ac2.dart
No OneTemporary

File Metadata

Created
Tue, May 14, 08:30

Ac2.dart

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:student/widgets/mapShapeMaker.dart';
import 'package:student/widgets/onlineRobotMap.Dart';
import 'package:student/widgets/celluloMap.Dart';
import 'dart:async';
import 'dart:convert';
import 'package:student/Database.dart';
import 'package:student/model/Group.dart';
import 'package:student/model/Cellulo.dart';
import 'package:student/widgets/showAlertDialog.Dart';
import 'package:student/widgets/membersBar.Dart';
import 'package:student/widgets/inactivityDetector.Dart';
import 'package:flutter_appavailability/flutter_appavailability.dart';
import 'package:student/widgets/ShapePainter.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:student/widgets/checkCelluloGame.dart';
import 'package:audioplayer/audioplayer.dart';
import 'package:tip_dialog/tip_dialog.dart';
import 'dart:math' as math;
class Ac2 extends StatefulWidget {
Ac2({Key key}) : super(key: key);
@override
_Ac2State createState() => _Ac2State();
}
class _Ac2State extends State<Ac2> {
// Activity_Independent
var elapseTimer = new Stopwatch();
Timer timerCelluloPosition;
Timer timerCelluloPositiontoserver;
Timer timerCheckCelluloGame;
int currentTurn = 1;
int inactivity = 0;
int tapCounter = 0;
List<int> progress = [0, -2, -2];
List<int> progressElpasedTime = [0, 0, 0];
// Celulo
var celluloxPosition = [0.0, 0.0];
var celluloyPosition = [0.0, 0.0];
List<double> celluloxPositiontopaint = [0.0, 0.0];
List<double> celluloyPositiontopaint = [0.0, 0.0];
bool addtoprint;
Animation<double> animationRobotPath;
// Learning
List<int> gameScore = [0, 0, 0];
List<int> celluloEnergy = [6, 6, 6];
List<int> mistakesSlope = [0, 0, 0];
List<int> mistakesIntrepet = [0, 0, 0];
List<int> mistakesInitialPosition = [0, 0, 0];
int score = 6;
//Activity Dependent
bool enterBorderX = false;
bool enterBorderY = false;
bool enterBorderpolygonX = false;
bool enterBorderpolygonY = false;
bool enterBorderRectX = false;
bool enterBorderRectY = false;
int scoreX = 6;
int scoreY = 6;
int trappedCircleX;
int trappedCircleY;
int trappedRectangleX;
int trappedRectangleY;
int trappedPolygonX;
int trappedPolygonY;
bool reachendY = false;
bool gameoverY = false;
String activityTitle = 'Activity 2';
var linesPath = [
'assets/images/Ac7_function1.svg',
'assets/images/Ac7_function2.svg',
'assets/images/Ac7_function3.svg',
'assets/images/GridOnlyPositive.svg',
];
var startPoint = ['B', 'A', 'B'];
var endPoint = ['C', 'D', 'C'];
var startPointorder = [2, 0, 2];
var endPointorder = [1, 3, 1];
var pointOrder = ['A', 'B', 'C', 'D'];
static List<Offset> pointPosition = [
Offset(80, 80),
Offset(760, 80),
Offset(80, 760),
Offset(760, 760)
];
// map-related
double radiuosStart = 45;
//final Offset startPoint = new Offset(0.0, 0.0);
//inal Offset endPoint = new Offset(0.0, 0.0);
final double mapSizeWidth = 860;
final double mapSizeHeight = 860;
final String mapPath = 'assets/images/GridOnlyPositive.svg';
double coeffScreenMapWidth;
double coeffScreenMapHeight;
var mapShape = [
{
'numCircles': 0,
'originCircles': [
Offset(390, 300),
Offset(160, 70),
Offset(60, 360),
Offset(560, 690)
],
'radiuosCircles': [60.0, 50.0, 50.0, 40.0],
'numRectangles': 3,
'originRectangles': [
Offset(300, 30),
Offset(200, 560),
Offset(400, 760),
Offset(780, 460),
Offset(650, 500)
],
'widthRectangles': [150.0, 100.0, 50.0, 100.0, 100.0],
'heightRectangles': [50.0, 250.0, 300.0, 50.0, 100.0],
'numPolygons': 2,
'sidesofPolygon': [4, 4, 4, 4],
'radiusPolygon': [70.0, 70.0, 50.0, 60.0, 50.0],
'centerPolygon': [
Offset(60, 280),
Offset(500, 140),
Offset(600, 200),
Offset(500, 500),
Offset(680, 400)
],
'startCenter': pointPosition[2],
'endCenter': pointPosition[1]
},
{
'numCircles': 0,
'originCircles': [
Offset(390, 300),
Offset(160, 70),
Offset(60, 360),
Offset(560, 690)
],
'radiuosCircles': [60.0, 50.0, 50.0, 40.0],
'numRectangles': 2,
'originRectangles': [
Offset(300, 30),
Offset(300, 560),
Offset(500, 560),
Offset(750, 360)
],
'widthRectangles': [
150.0,
100.0,
50.0,
150.0,
],
'heightRectangles': [50.0, 250.0, 250.0, 300.0],
'numPolygons': 2,
'sidesofPolygon': [4, 4, 4, 4],
'radiusPolygon': [80.0, 60.0, 50.0, 60.0],
'centerPolygon': [
Offset(100, 330),
Offset(600, 90),
Offset(500, 200),
Offset(400, 350),
],
'startCenter': pointPosition[0],
'endCenter': pointPosition[3]
},
{
'numCircles': 0,
'originCircles': [
Offset(390, 300),
Offset(160, 70),
Offset(60, 360),
Offset(560, 690)
],
'radiuosCircles': [60.0, 50.0, 50.0, 40.0],
'numRectangles': 3,
'originRectangles': [Offset(350, 70), Offset(300, 560), Offset(700, 520)],
'widthRectangles': [350.0, 300.0, 100.0],
'heightRectangles': [50.0, 250.0, 100.0],
'numPolygons': 3,
'sidesofPolygon': [4, 4, 4],
'radiusPolygon': [100.0, 60.0, 70.0],
'centerPolygon': [
Offset(80, 230),
Offset(650, 190),
Offset(400, 250),
],
'startCenter': pointPosition[2],
'endCenter': pointPosition[1]
},
];
@override
void initState() {
super.initState();
dbRef.child('groups').child(group.id).child('tabletStatus').set("YES");
dbRef.child('groups').child(group.id).child('currentActivity').set("Ac2");
elapseTimer.start();
// coeffScreenMapHeight =
// MediaQuery.of(context).size.width * 0.9 / mapSizeWidth;
// coeffScreenMapHeight =
// MediaQuery.of(context).size.height * 0.9 / mapSizeHeight;
timerCelluloPosition =
new Timer.periodic(new Duration(milliseconds: 500), (time) {
if (totalRobots() > 1) {
cellulox.getrobotx().then((val) => setState(() {
if (val != null) {
celluloxPosition[0] = val;
print(celluloxPosition[0]);
if (addtoprint == true) celluloxPositiontopaint.add(val);
}
// print(addtoprint.toString());
}));
cellulox.getroboty().then((val) => setState(() {
if (val != null) {
celluloxPosition[1] = val;
}
}));
celluloy.getrobotx().then((val) => setState(() {
if (val != null) {
celluloyPosition[0] = val;
}
}));
celluloy.getroboty().then((val) => setState(() {
if (val != null) {
celluloyPosition[1] = val;
if (addtoprint == true) celluloyPositiontopaint.add(val);
}
}));
if (celluloxPosition[0] > 800 || celluloyPosition[1] > 800) {
cellulox.setVelocity(0, 0);
celluloy.setVelocity(0, 0);
}
}
});
timerCelluloPositiontoserver =
new Timer.periodic(new Duration(milliseconds: 2800), (time) {
dbRef.child("celluloPosition").push().set(json.encode({
"x": celluloxPosition[0],
"y": celluloyPosition[1],
"acID": "Ac2",
"turn": currentTurn,
"groupID": group.id,
}));
});
timerCheckCelluloGame =
new Timer.periodic(new Duration(milliseconds: 500), (time) {
checkCelluloGame(
mapShape[currentTurn - 1],
Offset((celluloxPosition[0] + celluloyPosition[0]) / 2,
(celluloxPosition[1] + celluloyPosition[1]) / 2),
Offset(celluloxPosition[0], celluloxPosition[1]),
Offset(celluloyPosition[0], celluloyPosition[1]),
1);
});
}
void checkCelluloGame(var mapShape, Offset celluloTargetPosition,
Offset celluloXPosition, Offset celluloYPosition, int id) {
var insideBorder = false;
var insideShape = false;
final double coeffHaptic = 10;
double radiuosBorder = 40;
bool enterShape = false;
var XVelocityCelluloX;
var YVelocityCelluloX;
var XVelocityCelluloY;
var YVelocityCelluloY;
double celluloXYdistances = math.sqrt(
math.pow((celluloXPosition.dx - celluloYPosition.dx), 2) +
math.pow((celluloXPosition.dy - celluloYPosition.dy), 2));
double distanceThreshold = 120;
//print(score.toString());
//print(enterBorder.toString());
if (celluloXYdistances < distanceThreshold) {
cellulox.robotVibrate(10, 10, 0, 100, 100);
celluloy.robotVibrate(10, 10, 0, 100, 100);
// cellulox.setVelocity(-celluloxVelocity[0], -celluloxVelocity[1]);
// celluloy.setVelocity(-celluloyVelocity[0], -celluloyVelocity[1]);
}
var distancePointCenter = math.sqrt(math.pow(
(celluloTargetPosition.dx - mapShape['startCenter'].dx), 2) +
math.pow((celluloTargetPosition.dy - mapShape['startCenter'].dy), 2));
if (distancePointCenter <= radiuosStart) {
scoreX = 6;
scoreY = 6;
}
if (scoreY <= 0 && gameoverY == false) {
tapCounter = tapCounter + 1;
onDataSend();
gameoverY = true;
showAlertDialog(context, 'BLue Robot Does not have energy',
'Tell your friend with Blue Robot to go start point');
}
var distancePointEnd = math.sqrt(
math.pow((celluloTargetPosition.dx - mapShape['endCenter'].dx), 2) +
math.pow((celluloTargetPosition.dy - mapShape['endCenter'].dy), 2));
if (distancePointEnd <= radiuosStart) {
tapCounter = tapCounter + 1;
onDataSend();
showAlertDialog(context, 'Blue Robot reached the goal point',
'Tell your friend to start again or when both robots reached the end, you can go to next turn.');
}
for (int i = 0; i < mapShape['numRectangles']; i++) {
if ((Rect.fromCenter(
center: Offset(mapShape['originRectangles'][i].dx,
mapShape['originRectangles'][i].dy),
width: mapShape['widthRectangles'][i],
height: mapShape['heightRectangles'][i])
.contains(celluloTargetPosition) ==
true)) {
insideBorder = true;
XVelocityCelluloX = coeffHaptic *
(celluloTargetPosition.dx - mapShape['originRectangles'][i].dx);
YVelocityCelluloX = coeffHaptic *
(celluloTargetPosition.dy - mapShape['originRectangles'][i].dy);
XVelocityCelluloY = coeffHaptic *
(celluloTargetPosition.dx - mapShape['originRectangles'][i].dx);
YVelocityCelluloY = coeffHaptic *
(celluloTargetPosition.dy - mapShape['originRectangles'][i].dy);
cellulox.setVelocity(XVelocityCelluloX, YVelocityCelluloX);
celluloy.setVelocity(XVelocityCelluloY, YVelocityCelluloY);
if (enterBorderRectY == false) {
scoreY = scoreY - 1;
scoreX = scoreX - 1;
enterBorderRectY = true;
}
trappedRectangleY = i;
break;
} else if (i == trappedRectangleY) {
enterBorderRectY = false;
}
// if (id == 0) cellulox.clearrobot();
//if (id == 1) celluloy.clearrobot();
}
for (int i = 0; i < mapShape['numPolygons']; i++) {
var distancePointCenter = math.sqrt(math.pow(
(celluloTargetPosition.dx - mapShape['centerPolygon'][i].dx), 2) +
math.pow(
(celluloTargetPosition.dy - mapShape['centerPolygon'][i].dy), 2));
if (distancePointCenter <= mapShape['radiusPolygon'][i] + radiuosBorder) {
insideBorder = true;
XVelocityCelluloX = coeffHaptic *
(celluloTargetPosition.dx - mapShape['centerPolygon'][i].dx);
YVelocityCelluloX = coeffHaptic *
(celluloTargetPosition.dy - mapShape['centerPolygon'][i].dy);
XVelocityCelluloY = coeffHaptic *
(celluloTargetPosition.dx - mapShape['centerPolygon'][i].dx);
YVelocityCelluloY = coeffHaptic *
(celluloTargetPosition.dy - mapShape['centerPolygon'][i].dy);
cellulox.setVelocity(XVelocityCelluloX, YVelocityCelluloX);
celluloy.setVelocity(XVelocityCelluloY, YVelocityCelluloY);
if (enterBorderpolygonY == false) {
scoreY = scoreY - 1;
scoreX = scoreX - 1;
enterBorderpolygonY = true;
}
trappedPolygonY = i;
break;
} else if (i == trappedPolygonY) {
enterBorderpolygonY = false;
}
}
if (enterBorderY == false &&
enterBorderRectY == false &&
enterBorderpolygonY == false &&
id == 1) {
cellulox.clearrobot();
celluloy.clearrobot();
}
colorRobots();
/*
/*
/*
insideBorder = false;
if (distancePointCenter <= mapShape['radiuosCircles'][i]) {
insideShape = true;
} else {
insideShape = false;
enterShape = false;
}
if (insideShape == true) {
if (enterShape == false) {
score = score - 1;
enterShape = true;
}
}
*/
if (id == 0) cellulox.clearrobot();
if (id == 1) celluloy.clearrobot();
}
*/
}
*/
}
void colorRobots() {
for (int i = 0; i < 6; i++) {
cellulox.setColor(0, 0, 0, 0, 0);
}
for (int i = 0; i < scoreX; i++) {
cellulox.setColor(0, 255, 0, 1, i);
}
for (int i = 0; i < 6; i++) {
celluloy.setColor(0, 0, 0, 0, 0);
}
for (int i = 0; i < scoreY; i++) {
celluloy.setColor(0, 0, 255, 1, i);
}
}
@override
void dispose() {
timerCelluloPosition.cancel();
timerCheckCelluloGame.cancel();
elapseTimer.stop();
super.dispose();
}
void onDataSend() {
dbRef.child('attempts').push().set(json.encode({
"numAttempts": tapCounter,
"groupID": group.id,
"acID": "Ac2",
"elpasedTime": elapseTimer.elapsedMilliseconds -
progressElpasedTime[currentTurn - 1],
"progress": {
"turn1": progress[0],
"turn2": progress[1],
"turn3": progress[2]
},
"progressElpasedTime": {
"turn1": progressElpasedTime[0],
"turn2": progressElpasedTime[1],
"turn3": progressElpasedTime[2],
},
"currentTurn": currentTurn,
"inactivity": inactivity,
"mistakes": {
"turn1": {
"slope": mistakesSlope[0],
"initialPoint": mistakesInitialPosition[0]
},
"turn2": {
"slope": mistakesSlope[1],
"initialPoint": mistakesInitialPosition[1]
},
"turn3": {
"slope": mistakesSlope[2],
"initialPoint": mistakesInitialPosition[2]
},
}
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Avoid the hidden obstacles'),
),
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Column(children: <Widget>[
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Container(
height: 100,
width: 600,
child: Stack(children: <Widget>[
Card(
child: ListTile(
title: Text('Guide your friedns to go from ' +
startPoint[currentTurn - 1].toString() +
' to ' +
endPoint[currentTurn - 1].toString()),
),
)
])),
SizedBox(width: 20),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(100)),
color: Colors.blue),
child: FlatButton(
child: Text(
"Next Turn",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
fontSize: 18),
),
onPressed: () => {
tapCounter = 0,
if (currentTurn <= 2)
{
setState(() {
currentTurn = currentTurn + 1;
}),
progress[currentTurn - 2] = 1,
// controller.reset(),
progress[currentTurn - 1] = 0,
onDataSend(),
}
else
{
progress[2] = 1,
onDataSend(),
showAlertDialog(
context, 'Wait for teacher', 'Game has finished! '),
},
},
)),
]),
SizedBox(
height: 50,
),
Container(
width: MediaQuery.of(context).size.width * 0.9,
height: MediaQuery.of(context).size.height * 0.65,
decoration: BoxDecoration(
// borderRadius: BorderRadius.all(Radius.circular(100)),
// color: Colors.blue
),
child: Stack(
children: <Widget>[
CustomPaint(
//size: Size(200, 200),
painter:
//LinePainter2(celluloxPositiontopaint, celluloyPositiontopaint),
MapShapeMaker(
MediaQuery.of(context).size.width *
0.9 /
mapSizeWidth,
MediaQuery.of(context).size.height *
0.65 /
mapSizeHeight,
mapShape[currentTurn - 1]['numRectangles'],
mapShape[currentTurn - 1]['originRectangles'],
mapShape[currentTurn - 1]['widthRectangles'],
mapShape[currentTurn - 1]['heightRectangles'],
mapShape[currentTurn - 1]['numCircles'],
mapShape[currentTurn - 1]['originCircles'],
mapShape[currentTurn - 1]['radiuosCircles'],
mapShape[currentTurn - 1]['numPolygons'],
mapShape[currentTurn - 1]['sidesofPolygon'],
mapShape[currentTurn - 1]['radiusPolygon'],
mapShape[currentTurn - 1]['centerPolygon'],
mapShape[currentTurn - 1]['startCenter'],
mapShape[currentTurn - 1]['endCenter']),
),
Container(
width: MediaQuery.of(context).size.width * 0.9,
height: MediaQuery.of(context).size.height * 0.65,
child: Align(
alignment: Alignment(
2 *
((celluloxPosition[0] + celluloyPosition[0]) /
2 /
mapSizeWidth) -
1,
2 *
((celluloxPosition[1] + celluloyPosition[1]) /
2 /
mapSizeHeight) -
1),
child: Card(
child: SvgPicture.asset(
"assets/images/celluloPurple.svg",
height: 60,
width: 60),
),
),
),
Container(
width: MediaQuery.of(context).size.width * 0.9,
height: MediaQuery.of(context).size.height * 0.65,
child: Align(
alignment: Alignment(
2 * ((celluloxPosition[0]) / mapSizeWidth) - 1,
2 * ((celluloxPosition[1]) / mapSizeHeight) - 1),
child: Card(
child: SvgPicture.asset("assets/images/celluloRed.svg",
height: 60, width: 60),
),
),
),
Container(
width: MediaQuery.of(context).size.width * 0.9,
height: MediaQuery.of(context).size.height * 0.65,
child: Align(
alignment: Alignment(
2 * ((celluloyPosition[0]) / mapSizeWidth) - 1,
2 * ((celluloyPosition[1]) / mapSizeHeight) - 1),
child: Card(
child: SvgPicture.asset("assets/images/celluloBlue.svg",
height: 60, width: 60),
),
),
),
],
)),
SizedBox(
height: 75,
),
MembersBar(),
])));
}
}

Event Timeline