Skip to content

Commit

Permalink
refactor: 💡 player move
Browse files Browse the repository at this point in the history
  • Loading branch information
panicdragon committed Apr 18, 2023
1 parent dda8c69 commit a136f67
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 47 deletions.
11 changes: 6 additions & 5 deletions lib/components/player.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,8 +19,8 @@ class Player extends SpriteAnimationComponent with HasGameRef {

Player()
: super(
size: Vector2.all(64.0),
);
size: Vector2.all(64.0),
);

@override
Future<void> onLoad() async {
Expand Down Expand Up @@ -91,4 +92,4 @@ class Player extends SpriteAnimationComponent with HasGameRef {
_moveCount -= _moveCoordinate as int;
position.add(vac);
}
}
}
30 changes: 18 additions & 12 deletions lib/game.dart
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -57,24 +57,30 @@ class MainGame extends FlameGame with KeyboardEvents, HasGameRef {
}

@override
KeyEventResult onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
KeyEventResult onKeyEvent(
RawKeyEvent event, Set<LogicalKeyboardKey> 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);
}
Expand Down
100 changes: 70 additions & 30 deletions lib/src/push_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,39 @@ class PushGame {
}

List<Object> get initStageState {
final List<Object>stageStateList = List<Object>.filled(stageWidth * stageHeight, Object.unknown);
final List<Object> stageStateList =
List<Object>.filled(stageWidth * stageHeight, Object.unknown);
int x, y;
x = y = 0;

for (var dataX in stageDataList) {
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;
Expand All @@ -43,34 +60,47 @@ 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<String, int> 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,
'dy': dy,
};
}

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<String, int> get playerVecPos => { 'x': playerIndex % stageWidth, 'y': playerIndex ~/ stageWidth };
Map<String, int> get playerVecPos =>
{'x': playerIndex % stageWidth, 'y': playerIndex ~/ stageWidth};

List<String> get splitStageStateList {
final List<String>stageStateList = List<String>.filled(stageHeight, '');
final List<String> stageStateList = List<String>.filled(stageHeight, '');

for (int y = 0; y < stageHeight; ++y) {
String line = '';
Expand All @@ -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;
}
}

0 comments on commit a136f67

Please sign in to comment.