Skip to content

Commit

Permalink
refactor: 💡 add step & getKeyDirection
Browse files Browse the repository at this point in the history
  • Loading branch information
panicdragon committed Apr 22, 2023
1 parent fa856f7 commit 6b92381
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
47 changes: 30 additions & 17 deletions lib/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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();
}
}
6 changes: 5 additions & 1 deletion lib/src/push_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -26,11 +28,13 @@ class PushGame {
}

bool changeState(String input) {
step++;
return state.changeState(input);
}

void nextStage() {
_stage++;
step = 0;
state.changeStage(_stage);
}
}

0 comments on commit 6b92381

Please sign in to comment.