diff --git a/lib/game.dart b/lib/game.dart index 7fbc5d1..600049f 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -57,6 +57,7 @@ class MainGame extends FlameGame with KeyboardEvents, HasGameRef { for(var crate in _crateList) { add(crate); } + if (pushGame.state.width > 20) { camera.followComponent(_player); } else { @@ -125,6 +126,22 @@ class MainGame extends FlameGame with KeyboardEvents, HasGameRef { return super.onKeyEvent(event, keysPressed); } + keyDirection = getKeyDirection(event); + bool isMove = pushGame.changeState(keyDirection.name); + if (isMove) { + playerMove(isKeyDown, keyDirection); + if (pushGame.state.isCrateMove) { + createMove(); + } + if (pushGame.state.isClear) { + drawNextStage(); + } + } + return super.onKeyEvent(event, keysPressed); + } + + Direction getKeyDirection(RawKeyEvent event) { + Direction keyDirection = Direction.none; if (event.logicalKey == LogicalKeyboardKey.keyA || event.logicalKey == LogicalKeyboardKey.arrowLeft) { keyDirection = Direction.left; @@ -138,23 +155,7 @@ class MainGame extends FlameGame with KeyboardEvents, HasGameRef { event.logicalKey == LogicalKeyboardKey.arrowDown) { keyDirection = Direction.down; } - - bool isMove = pushGame.changeState(keyDirection.name); - if (isMove) { - playerMove(isKeyDown, keyDirection); - - if (pushGame.state.isCrateMove) { - final targetCrate = _crateList.firstWhere((crate) => crate.coordinate == pushGame.state.crateMoveBeforeVec); - targetCrate.move(pushGame.state.crateMoveAfterVec); - targetCrate.goalCheck(pushGame.state.goalVecList); - } - if (pushGame.state.isClear) { - pushGame.nextStage(); - allReset(); - draw(); - } - } - return super.onKeyEvent(event, keysPressed); + return keyDirection; } void playerMove(bool isKeyDown, Direction keyDirection) { @@ -165,4 +166,16 @@ class MainGame extends FlameGame with KeyboardEvents, HasGameRef { _player.direction = Direction.none; } } + + void createMove() { + final targetCrate = _crateList.firstWhere((crate) => crate.coordinate == pushGame.state.crateMoveBeforeVec); + targetCrate.move(pushGame.state.crateMoveAfterVec); + targetCrate.goalCheck(pushGame.state.goalVecList); + } + + void drawNextStage() { + pushGame.nextStage(); + allReset(); + draw(); + } } diff --git a/lib/src/push_game.dart b/lib/src/push_game.dart index b3720ee..0fd8d86 100644 --- a/lib/src/push_game.dart +++ b/lib/src/push_game.dart @@ -2,14 +2,16 @@ import 'stage_state.dart'; class PushGame { late int _stage; + late int step; late StageState state; - PushGame({int stage = 1}) { + PushGame({int stage = 1, this.step = 0}) { _stage = stage; state = StageState(stage: stage); } int get stage => _stage; + bool get isFinalStage => state.dataList.length == stage; void draw() { for (var splitStageState in state.splitStageStateList) { @@ -26,11 +28,13 @@ class PushGame { } bool changeState(String input) { + step++; return state.changeState(input); } void nextStage() { _stage++; + step = 0; state.changeStage(_stage); } }