style: refactored level into smaller methods
This commit is contained in:
		
							parent
							
								
									7bf1ead030
								
							
						
					
					
						commit
						d52f60ef92
					
				| 
						 | 
					@ -72,35 +72,19 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
 | 
				
			||||||
            _position = value;
 | 
					            _position = value;
 | 
				
			||||||
          }),
 | 
					          }),
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    _durationSubscription = player.onDurationChanged.listen((duration) {
 | 
					
 | 
				
			||||||
 | 
					    // listen for new values from player
 | 
				
			||||||
 | 
					    _durationSubscription =
 | 
				
			||||||
 | 
					        player.onDurationChanged.listen((Duration duration) {
 | 
				
			||||||
      setState(() => _duration = duration);
 | 
					      setState(() => _duration = duration);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (ESenseInput.instance.connected) {
 | 
					    _positionSubscription =
 | 
				
			||||||
      _buttonSubscription = ESenseInput.instance.buttonEvents().listen((event) {
 | 
					        player.onPositionChanged.listen((Duration position) {
 | 
				
			||||||
        if (!event.pressed) {
 | 
					      setState(() => _position = position);
 | 
				
			||||||
          if (_isPlaying) {
 | 
					      for (final note in notes) {
 | 
				
			||||||
            player.pause();
 | 
					        _noteHitCheck(note, position);
 | 
				
			||||||
            setState(() {
 | 
					      }
 | 
				
			||||||
              _isPlaying = false;
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
          } else {
 | 
					 | 
				
			||||||
            player.resume();
 | 
					 | 
				
			||||||
            setState(() {
 | 
					 | 
				
			||||||
              _isPlaying = true;
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    _positionSubscription = player.onPositionChanged.listen(
 | 
					 | 
				
			||||||
      (p) => setState(() => _position = p),
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    player.onDurationChanged.listen((Duration d) {
 | 
					 | 
				
			||||||
      // print('Max duration: $d');
 | 
					 | 
				
			||||||
      setState(() => _duration = d);
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    player.onPlayerComplete.listen((void _) {
 | 
					    player.onPlayerComplete.listen((void _) {
 | 
				
			||||||
| 
						 | 
					@ -112,60 +96,16 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
 | 
				
			||||||
      Navigator.pushReplacement(context, route);
 | 
					      Navigator.pushReplacement(context, route);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    player.onPositionChanged.listen((Duration p) {
 | 
					    // listen for esense button and pause/resume
 | 
				
			||||||
      // print('Current position: $p');
 | 
					    if (ESenseInput.instance.connected) {
 | 
				
			||||||
      setState(() => _position = p);
 | 
					      _buttonSubscription = ESenseInput.instance.buttonEvents().listen((event) {
 | 
				
			||||||
      for (final note in notes) {
 | 
					        if (!event.pressed) {
 | 
				
			||||||
        note.position = note.time - p.inMilliseconds / 60000.0;
 | 
					          _pauseResume();
 | 
				
			||||||
        if (note.wasHit != null) {
 | 
					 | 
				
			||||||
          continue;
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (note.position.abs() < 0.5 * 1.0 / 60.0) {
 | 
					      });
 | 
				
			||||||
          InputDirection esenseDirection =
 | 
					    }
 | 
				
			||||||
              ESenseInput.instance.getInputDirection(note.direction);
 | 
					 | 
				
			||||||
          inputDirection.up |= esenseDirection.up;
 | 
					 | 
				
			||||||
          inputDirection.down |= esenseDirection.down;
 | 
					 | 
				
			||||||
          inputDirection.left |= esenseDirection.left;
 | 
					 | 
				
			||||||
          inputDirection.right |= esenseDirection.right;
 | 
					 | 
				
			||||||
          bool keypressCorrect = false;
 | 
					 | 
				
			||||||
          switch (note.direction) {
 | 
					 | 
				
			||||||
            case ArrowDirection.up:
 | 
					 | 
				
			||||||
              keypressCorrect = inputDirection.up;
 | 
					 | 
				
			||||||
              break;
 | 
					 | 
				
			||||||
            case ArrowDirection.down:
 | 
					 | 
				
			||||||
              keypressCorrect = inputDirection.down;
 | 
					 | 
				
			||||||
              break;
 | 
					 | 
				
			||||||
            case ArrowDirection.right:
 | 
					 | 
				
			||||||
              keypressCorrect = inputDirection.right;
 | 
					 | 
				
			||||||
              break;
 | 
					 | 
				
			||||||
            case ArrowDirection.left:
 | 
					 | 
				
			||||||
              keypressCorrect = inputDirection.left;
 | 
					 | 
				
			||||||
              break;
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
          if (keypressCorrect) {
 | 
					 | 
				
			||||||
            print("you hit!");
 | 
					 | 
				
			||||||
            note.wasHit = true;
 | 
					 | 
				
			||||||
            _animationController.reset();
 | 
					 | 
				
			||||||
            _animationController.forward();
 | 
					 | 
				
			||||||
            inputDirection.reset();
 | 
					 | 
				
			||||||
            setState(() {
 | 
					 | 
				
			||||||
              hitOrMissMessage = 'Great!';
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
        } else if (note.position < -0.5 * 1.0 / 60.0) {
 | 
					 | 
				
			||||||
          print("Missed");
 | 
					 | 
				
			||||||
          note.wasHit = false;
 | 
					 | 
				
			||||||
          _animationController.reset();
 | 
					 | 
				
			||||||
          _animationController.forward();
 | 
					 | 
				
			||||||
          inputDirection.reset();
 | 
					 | 
				
			||||||
          setState(() {
 | 
					 | 
				
			||||||
            hitOrMissMessage = 'Missed';
 | 
					 | 
				
			||||||
          });
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    widget.simfile.chartSimplest!.beats.forEach((time, noteData) {
 | 
					    widget.simfile.chartSimplest?.beats.forEach((time, noteData) {
 | 
				
			||||||
      int arrowIndex = noteData.indexOf('1');
 | 
					      int arrowIndex = noteData.indexOf('1');
 | 
				
			||||||
      if (arrowIndex < 0 || arrowIndex > 3) {
 | 
					      if (arrowIndex < 0 || arrowIndex > 3) {
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
| 
						 | 
					@ -176,52 +116,115 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
 | 
				
			||||||
    player.play(DeviceFileSource(widget.simfile.audioPath!));
 | 
					    player.play(DeviceFileSource(widget.simfile.audioPath!));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  void dispose() {
 | 
				
			||||||
 | 
					    _animationController.dispose();
 | 
				
			||||||
 | 
					    _durationSubscription?.cancel();
 | 
				
			||||||
 | 
					    _positionSubscription?.cancel();
 | 
				
			||||||
 | 
					    _buttonSubscription?.cancel();
 | 
				
			||||||
 | 
					    player.dispose();
 | 
				
			||||||
 | 
					    super.dispose();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void _pauseResume() {
 | 
				
			||||||
 | 
					    if (_isPlaying) {
 | 
				
			||||||
 | 
					      player.pause();
 | 
				
			||||||
 | 
					      setState(() {
 | 
				
			||||||
 | 
					        _isPlaying = false;
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      player.resume();
 | 
				
			||||||
 | 
					      setState(() {
 | 
				
			||||||
 | 
					        _isPlaying = true;
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void _noteHitCheck(Note note, Duration time) {
 | 
				
			||||||
 | 
					    note.position = note.time - time.inMilliseconds / 60000.0;
 | 
				
			||||||
 | 
					    if (note.wasHit != null) {
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (note.position.abs() < 0.5 * 1.0 / 60.0) {
 | 
				
			||||||
 | 
					      InputDirection esenseDirection =
 | 
				
			||||||
 | 
					          ESenseInput.instance.getInputDirection(note.direction);
 | 
				
			||||||
 | 
					      inputDirection.up |= esenseDirection.up;
 | 
				
			||||||
 | 
					      inputDirection.down |= esenseDirection.down;
 | 
				
			||||||
 | 
					      inputDirection.left |= esenseDirection.left;
 | 
				
			||||||
 | 
					      inputDirection.right |= esenseDirection.right;
 | 
				
			||||||
 | 
					      bool keypressCorrect = false;
 | 
				
			||||||
 | 
					      switch (note.direction) {
 | 
				
			||||||
 | 
					        case ArrowDirection.up:
 | 
				
			||||||
 | 
					          keypressCorrect = inputDirection.up;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					        case ArrowDirection.down:
 | 
				
			||||||
 | 
					          keypressCorrect = inputDirection.down;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					        case ArrowDirection.right:
 | 
				
			||||||
 | 
					          keypressCorrect = inputDirection.right;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					        case ArrowDirection.left:
 | 
				
			||||||
 | 
					          keypressCorrect = inputDirection.left;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (keypressCorrect) {
 | 
				
			||||||
 | 
					        print("you hit!");
 | 
				
			||||||
 | 
					        note.wasHit = true;
 | 
				
			||||||
 | 
					        _animationController.reset();
 | 
				
			||||||
 | 
					        _animationController.forward();
 | 
				
			||||||
 | 
					        inputDirection.reset();
 | 
				
			||||||
 | 
					        setState(() {
 | 
				
			||||||
 | 
					          hitOrMissMessage = 'Great!';
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } else if (note.position < -0.5 * 1.0 / 60.0) {
 | 
				
			||||||
 | 
					      print("Missed");
 | 
				
			||||||
 | 
					      note.wasHit = false;
 | 
				
			||||||
 | 
					      _animationController.reset();
 | 
				
			||||||
 | 
					      _animationController.forward();
 | 
				
			||||||
 | 
					      inputDirection.reset();
 | 
				
			||||||
 | 
					      setState(() {
 | 
				
			||||||
 | 
					        hitOrMissMessage = 'Missed';
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void _keyboardHandler(event) {
 | 
				
			||||||
 | 
					    bool isDown = false;
 | 
				
			||||||
 | 
					    if (event is KeyDownEvent) {
 | 
				
			||||||
 | 
					      isDown = true;
 | 
				
			||||||
 | 
					    } else if (event is KeyUpEvent) {
 | 
				
			||||||
 | 
					      isDown = false;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    switch (event.logicalKey) {
 | 
				
			||||||
 | 
					      case LogicalKeyboardKey.arrowUp:
 | 
				
			||||||
 | 
					        inputDirection.up = isDown;
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      case LogicalKeyboardKey.arrowDown:
 | 
				
			||||||
 | 
					        inputDirection.down = isDown;
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      case LogicalKeyboardKey.arrowLeft:
 | 
				
			||||||
 | 
					        inputDirection.left = isDown;
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      case LogicalKeyboardKey.arrowRight:
 | 
				
			||||||
 | 
					        inputDirection.right = isDown;
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context) {
 | 
					  Widget build(BuildContext context) {
 | 
				
			||||||
    return KeyboardListener(
 | 
					    return KeyboardListener(
 | 
				
			||||||
      focusNode: _focusNode,
 | 
					      focusNode: _focusNode,
 | 
				
			||||||
      autofocus: true,
 | 
					      autofocus: true,
 | 
				
			||||||
      onKeyEvent: (event) {
 | 
					      onKeyEvent: _keyboardHandler,
 | 
				
			||||||
        bool isDown = false;
 | 
					 | 
				
			||||||
        if (event is KeyDownEvent) {
 | 
					 | 
				
			||||||
          isDown = true;
 | 
					 | 
				
			||||||
        } else if (event is KeyUpEvent) {
 | 
					 | 
				
			||||||
          isDown = false;
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
          return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        switch (event.logicalKey) {
 | 
					 | 
				
			||||||
          case LogicalKeyboardKey.arrowUp:
 | 
					 | 
				
			||||||
            inputDirection.up = isDown;
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
          case LogicalKeyboardKey.arrowDown:
 | 
					 | 
				
			||||||
            inputDirection.down = isDown;
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
          case LogicalKeyboardKey.arrowLeft:
 | 
					 | 
				
			||||||
            inputDirection.left = isDown;
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
          case LogicalKeyboardKey.arrowRight:
 | 
					 | 
				
			||||||
            inputDirection.right = isDown;
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      child: Scaffold(
 | 
					      child: Scaffold(
 | 
				
			||||||
          appBar: AppBar(
 | 
					          appBar: AppBar(
 | 
				
			||||||
            leading: IconButton(
 | 
					            leading: IconButton(
 | 
				
			||||||
              icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
 | 
					              icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
 | 
				
			||||||
              onPressed: () {
 | 
					              onPressed: _pauseResume,
 | 
				
			||||||
                if (_isPlaying) {
 | 
					 | 
				
			||||||
                  player.pause();
 | 
					 | 
				
			||||||
                  setState(() {
 | 
					 | 
				
			||||||
                    _isPlaying = false;
 | 
					 | 
				
			||||||
                  });
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                  player.resume();
 | 
					 | 
				
			||||||
                  setState(() {
 | 
					 | 
				
			||||||
                    _isPlaying = true;
 | 
					 | 
				
			||||||
                  });
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
              },
 | 
					 | 
				
			||||||
            ),
 | 
					            ),
 | 
				
			||||||
            title: Text(widget.simfile.tags['TITLE']!),
 | 
					            title: Text(widget.simfile.tags['TITLE']!),
 | 
				
			||||||
            actions: [
 | 
					            actions: [
 | 
				
			||||||
| 
						 | 
					@ -273,14 +276,4 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
 | 
				
			||||||
          ])),
 | 
					          ])),
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  @override
 | 
					 | 
				
			||||||
  void dispose() {
 | 
					 | 
				
			||||||
    _animationController.dispose();
 | 
					 | 
				
			||||||
    _durationSubscription?.cancel();
 | 
					 | 
				
			||||||
    _positionSubscription?.cancel();
 | 
					 | 
				
			||||||
    _buttonSubscription?.cancel();
 | 
					 | 
				
			||||||
    player.dispose();
 | 
					 | 
				
			||||||
    super.dispose();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue