diff --git a/lib/components/player.dart b/lib/components/player.dart index bef05b8..9906f8f 100644 --- a/lib/components/player.dart +++ b/lib/components/player.dart @@ -1,11 +1,12 @@ import 'package:flame/components.dart'; -import '../utility/direction.dart'; import 'package:flame/sprite.dart'; +import '../utility/direction.dart'; + class Player extends SpriteAnimationComponent with HasGameRef { final double _playerSpeed = 300.0; final double _animationSpeed = 0.15; - final double _moveCoordinate = 4; + final double _moveCoordinate = 8; int _moveCount = 0; late final SpriteAnimation _runDownAnimation; @@ -18,8 +19,8 @@ class Player extends SpriteAnimationComponent with HasGameRef { Player() : super( - size: Vector2.all(64.0), - ); + size: Vector2.all(64.0), + ); @override Future onLoad() async { @@ -91,4 +92,4 @@ class Player extends SpriteAnimationComponent with HasGameRef { _moveCount -= _moveCoordinate as int; position.add(vac); } -} \ No newline at end of file +} diff --git a/lib/game.dart b/lib/game.dart index 53061b4..c3b9008 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -1,12 +1,12 @@ import 'package:flame/components.dart'; -import 'package:flutter/services.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flame/game.dart'; import 'package:flame/input.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/services.dart'; -import 'utility/direction.dart'; -import 'src/push_game.dart'; import 'components/player.dart'; +import 'src/push_game.dart'; +import 'utility/direction.dart'; class MainGame extends FlameGame with KeyboardEvents, HasGameRef { PushGame pushGame = PushGame(); @@ -57,24 +57,30 @@ class MainGame extends FlameGame with KeyboardEvents, HasGameRef { } @override - KeyEventResult onKeyEvent(RawKeyEvent event, Set keysPressed) { + KeyEventResult onKeyEvent( + RawKeyEvent event, Set keysPressed) { final isKeyDown = event is RawKeyDownEvent; Direction keyDirection = Direction.none; - if (!isKeyDown || _player.moveCount != 0) return super.onKeyEvent(event, keysPressed); + if (!isKeyDown || _player.moveCount != 0) + return super.onKeyEvent(event, keysPressed); - if (event.logicalKey == LogicalKeyboardKey.keyA || event.logicalKey == LogicalKeyboardKey.arrowLeft) { + if (event.logicalKey == LogicalKeyboardKey.keyA || + event.logicalKey == LogicalKeyboardKey.arrowLeft) { keyDirection = Direction.left; - } else if (event.logicalKey == LogicalKeyboardKey.keyD || event.logicalKey == LogicalKeyboardKey.arrowRight) { + } else if (event.logicalKey == LogicalKeyboardKey.keyD || + event.logicalKey == LogicalKeyboardKey.arrowRight) { keyDirection = Direction.right; - } else if (event.logicalKey == LogicalKeyboardKey.keyW || event.logicalKey == LogicalKeyboardKey.arrowUp) { + } else if (event.logicalKey == LogicalKeyboardKey.keyW || + event.logicalKey == LogicalKeyboardKey.arrowUp) { keyDirection = Direction.up; - } else if (event.logicalKey == LogicalKeyboardKey.keyS || event.logicalKey == LogicalKeyboardKey.arrowDown) { + } else if (event.logicalKey == LogicalKeyboardKey.keyS || + event.logicalKey == LogicalKeyboardKey.arrowDown) { keyDirection = Direction.down; } - pushGame.update(keyDirection.name); - playerMove(isKeyDown, keyDirection); + bool isMove = pushGame.changeState(keyDirection.name); + if (isMove) playerMove(isKeyDown, keyDirection); return super.onKeyEvent(event, keysPressed); } diff --git a/lib/src/push_game.dart b/lib/src/push_game.dart index 5038a34..8fae4fa 100644 --- a/lib/src/push_game.dart +++ b/lib/src/push_game.dart @@ -15,7 +15,8 @@ class PushGame { } List get initStageState { - final ListstageStateList = List.filled(stageWidth * stageHeight, Object.unknown); + final List stageStateList = + List.filled(stageWidth * stageHeight, Object.unknown); int x, y; x = y = 0; @@ -23,14 +24,30 @@ class PushGame { for (var rune in dataX.runes) { final Object t; switch (String.fromCharCode(rune)) { - case '#': t = Object.wall; break; - case ' ': t = Object.space; break; - case 'o': t = Object.block; break; - case 'O': t = Object.blockOnGoal; break; - case '.': t = Object.goal; break; - case 'p': t = Object.man; break; - case 'P': t = Object.manOnGoal; break; - default: t = Object.unknown; break; + case '#': + t = Object.wall; + break; + case ' ': + t = Object.space; + break; + case 'o': + t = Object.block; + break; + case 'O': + t = Object.blockOnGoal; + break; + case '.': + t = Object.goal; + break; + case 'p': + t = Object.man; + break; + case 'P': + t = Object.manOnGoal; + break; + default: + t = Object.unknown; + break; } if (t != Object.unknown) { stageStateList[y * stageWidth + x] = t; @@ -43,17 +60,26 @@ class PushGame { return stageStateList; } - int get playerIndex => stageState.indexWhere((obj) => obj == Object.man || obj == Object.manOnGoal); + int get playerIndex => stageState + .indexWhere((obj) => obj == Object.man || obj == Object.manOnGoal); Map getMoveDirection(String input) { int dx, dy; dx = dy = 0; switch (input) { - case 'left': dx = -1; break; - case 'right': dx = 1; break; - case 'up': dy = -1; break; - case 'down': dy = 1; break; + case 'left': + dx = -1; + break; + case 'right': + dx = 1; + break; + case 'up': + dy = -1; + break; + case 'down': + dy = 1; + break; } return { 'dx': dx, @@ -61,16 +87,20 @@ class PushGame { }; } - bool isCoordinate(tx, ty) => tx < 0 || ty < 0 || tx >= stageWidth || ty >= stageHeight; + bool isWorldOut(tx, ty) => + tx < 0 || ty < 0 || tx >= stageWidth || ty >= stageHeight; - bool isMoveObject(int targetPosition) => stageState[targetPosition] == Object.space || stageState[targetPosition] == Object.goal; + bool isMoveObject(int targetPosition) => + stageState[targetPosition] == Object.space || + stageState[targetPosition] == Object.goal; bool get isClear => stageState.indexWhere((obj) => obj == Object.block) == -1; - Map get playerVecPos => { 'x': playerIndex % stageWidth, 'y': playerIndex ~/ stageWidth }; + Map get playerVecPos => + {'x': playerIndex % stageWidth, 'y': playerIndex ~/ stageWidth}; List get splitStageStateList { - final ListstageStateList = List.filled(stageHeight, ''); + final List stageStateList = List.filled(stageHeight, ''); for (int y = 0; y < stageHeight; ++y) { String line = ''; @@ -97,43 +127,53 @@ class PushGame { } } - void changeState(String input) { + bool changeState(String input) { int? dx = getMoveDirection(input)['dx']; int? dy = getMoveDirection(input)['dy']; int? x = playerVecPos['x']; // modulus operator int? y = playerVecPos['y']; // integer division operator - // post move coordinate int tx = x! + dx!; int ty = y! + dy!; - // Maximum and minimum coordinate checks - if (isCoordinate(tx, ty)) return; + if (isWorldOut(tx, ty)) return false; int p = y * stageWidth + x; // PlayerPosition int tp = ty * stageWidth + tx; // TargetPosition if (isMoveObject(tp)) { - stageState[tp] = (stageState[tp] == Object.goal) ? Object.manOnGoal : Object.man; - stageState[p] = (stageState[p] == Object.manOnGoal) ? Object.goal : Object.space; + stageState[tp] = + (stageState[tp] == Object.goal) ? Object.manOnGoal : Object.man; + stageState[p] = + (stageState[p] == Object.manOnGoal) ? Object.goal : Object.space; } // Blank or goal. People move. - else if (stageState[tp] == Object.block || stageState[tp] == Object.blockOnGoal) { + else if (stageState[tp] == Object.block || + stageState[tp] == Object.blockOnGoal) { // So two squares away is in range. int tx2 = tx + dx; int ty2 = ty + dy; - if (tx2 < 0 || ty2 < 0 || tx2 >= stageWidth || ty2 >= stageHeight) { // Impossible to push. - return; + if (tx2 < 0 || ty2 < 0 || tx2 >= stageWidth || ty2 >= stageHeight) { + // Impossible to push. + return false; } int tp2 = (ty + dy) * stageWidth + (tx + dx); // two squares away if (stageState[tp2] == Object.space || stageState[tp2] == Object.goal) { // sequential replacement - stageState[tp2] = (stageState[tp2] == Object.goal) ? Object.blockOnGoal : Object.block; - stageState[tp] = (stageState[tp] == Object.blockOnGoal) ? Object.manOnGoal : Object.man; - stageState[p] = (stageState[p] == Object.manOnGoal ) ? Object.goal : Object.space; + stageState[tp2] = (stageState[tp2] == Object.goal) + ? Object.blockOnGoal + : Object.block; + stageState[tp] = (stageState[tp] == Object.blockOnGoal) + ? Object.manOnGoal + : Object.man; + stageState[p] = + (stageState[p] == Object.manOnGoal) ? Object.goal : Object.space; } + } else { + return false; } + return true; } }