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;
 | 
			
		||||
          }),
 | 
			
		||||
        );
 | 
			
		||||
    _durationSubscription = player.onDurationChanged.listen((duration) {
 | 
			
		||||
 | 
			
		||||
    // listen for new values from player
 | 
			
		||||
    _durationSubscription =
 | 
			
		||||
        player.onDurationChanged.listen((Duration duration) {
 | 
			
		||||
      setState(() => _duration = duration);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (ESenseInput.instance.connected) {
 | 
			
		||||
      _buttonSubscription = ESenseInput.instance.buttonEvents().listen((event) {
 | 
			
		||||
        if (!event.pressed) {
 | 
			
		||||
          if (_isPlaying) {
 | 
			
		||||
            player.pause();
 | 
			
		||||
            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);
 | 
			
		||||
    _positionSubscription =
 | 
			
		||||
        player.onPositionChanged.listen((Duration position) {
 | 
			
		||||
      setState(() => _position = position);
 | 
			
		||||
      for (final note in notes) {
 | 
			
		||||
        _noteHitCheck(note, position);
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    player.onPlayerComplete.listen((void _) {
 | 
			
		||||
| 
						 | 
				
			
			@ -112,60 +96,16 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
 | 
			
		|||
      Navigator.pushReplacement(context, route);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    player.onPositionChanged.listen((Duration p) {
 | 
			
		||||
      // print('Current position: $p');
 | 
			
		||||
      setState(() => _position = p);
 | 
			
		||||
      for (final note in notes) {
 | 
			
		||||
        note.position = note.time - p.inMilliseconds / 60000.0;
 | 
			
		||||
        if (note.wasHit != null) {
 | 
			
		||||
          continue;
 | 
			
		||||
    // listen for esense button and pause/resume
 | 
			
		||||
    if (ESenseInput.instance.connected) {
 | 
			
		||||
      _buttonSubscription = ESenseInput.instance.buttonEvents().listen((event) {
 | 
			
		||||
        if (!event.pressed) {
 | 
			
		||||
          _pauseResume();
 | 
			
		||||
        }
 | 
			
		||||
        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');
 | 
			
		||||
      if (arrowIndex < 0 || arrowIndex > 3) {
 | 
			
		||||
        return;
 | 
			
		||||
| 
						 | 
				
			
			@ -176,52 +116,115 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
 | 
			
		|||
    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
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    return KeyboardListener(
 | 
			
		||||
      focusNode: _focusNode,
 | 
			
		||||
      autofocus: true,
 | 
			
		||||
      onKeyEvent: (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;
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      onKeyEvent: _keyboardHandler,
 | 
			
		||||
      child: Scaffold(
 | 
			
		||||
          appBar: AppBar(
 | 
			
		||||
            leading: IconButton(
 | 
			
		||||
              icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
 | 
			
		||||
              onPressed: () {
 | 
			
		||||
                if (_isPlaying) {
 | 
			
		||||
                  player.pause();
 | 
			
		||||
                  setState(() {
 | 
			
		||||
                    _isPlaying = false;
 | 
			
		||||
                  });
 | 
			
		||||
                } else {
 | 
			
		||||
                  player.resume();
 | 
			
		||||
                  setState(() {
 | 
			
		||||
                    _isPlaying = true;
 | 
			
		||||
                  });
 | 
			
		||||
                }
 | 
			
		||||
              },
 | 
			
		||||
              onPressed: _pauseResume,
 | 
			
		||||
            ),
 | 
			
		||||
            title: Text(widget.simfile.tags['TITLE']!),
 | 
			
		||||
            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