Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Add path finder for player move
Browse files Browse the repository at this point in the history
  • Loading branch information
warioddly committed Feb 18, 2024
1 parent 329ae63 commit 2974ccf
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 444 deletions.
15 changes: 10 additions & 5 deletions lib/characters/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import 'package:flame/components.dart';
import 'package:pacman/game.dart';


enum Direction {
left,
right,
up,
down,
idle,
}

class Character extends SpriteAnimationComponent with HasGameRef<PacmanGame> {


Expand All @@ -17,14 +25,11 @@ class Character extends SpriteAnimationComponent with HasGameRef<PacmanGame> {
);

Vector2 velocity = Vector2.zero();
double _moveSpeed = 100.0;
double moveSpeed = 100.0;
Direction direction = Direction.idle;


set setMoveSpeed(double speed) {
_moveSpeed = speed;
}


get moveSpeed => _moveSpeed;

}
2 changes: 1 addition & 1 deletion lib/characters/enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Enemy extends Character {


Enemy() {
setMoveSpeed = enemySpeed;
moveSpeed = enemySpeed;
}


Expand Down
111 changes: 101 additions & 10 deletions lib/characters/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@ import 'package:flame/components.dart';
import 'package:flame/palette.dart';
import 'package:flutter/services.dart';
import 'package:pacman/core/constants.dart';
import 'package:pacman/level/map.dart';
import 'character.dart';


class Player extends Character with KeyboardHandler {


Player() {
setMoveSpeed = playerSpeed;
}

class Player extends Character with KeyboardHandler, CollisionCallbacks {


@override
FutureOr<void> onLoad() async {
super.onLoad();

debugMode = true;
moveSpeed = playerSpeed;

animation = SpriteAnimation.fromFrameData(
game.images.fromCache('ember.png'),
Expand All @@ -45,18 +40,114 @@ class Player extends Character with KeyboardHandler {

}

LogicalKeyboardKey? lastPressedKey;


@override
void update(double dt) {
position += velocity * dt;
keepMoving(dt);
super.update(dt);
}


@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
if (event is RawKeyDownEvent) {
lastPressedKey = event.logicalKey;
switch(event.logicalKey) {
case LogicalKeyboardKey.arrowUp || LogicalKeyboardKey.keyW:
direction = Direction.up;
break;
case LogicalKeyboardKey.arrowDown || LogicalKeyboardKey.keyS:
direction = Direction.down;
break;
case LogicalKeyboardKey.arrowLeft || LogicalKeyboardKey.keyA:
direction = Direction.left;
break;
case LogicalKeyboardKey.arrowRight || LogicalKeyboardKey.keyD:
direction = Direction.right;
break;
default:
direction = Direction.idle;
break;
}
}
return false;
}


bool canMoveInDirection(Direction direction) {
return true;
int nextX = position.x ~/ tileSize;
int nextY = position.y ~/ tileSize;
final map = Level.map;

switch (direction) {
case Direction.left:
nextX -= 1;
break;
case Direction.right:
nextX += 1;
break;
case Direction.up:
nextY -= 1;
break;
case Direction.down:
nextY += 1;
break;
default:
return false; // Неверное направление
}

// Проверка находится ли следующая клетка внутри карты и не является ли стеной
if (nextX < 0 || nextX >= map[0].length || nextY < 0 || nextY >= map.length || map[nextY][nextX] == '#') {
return false;
}

return true;
}


void keepMoving(double dt) {

switch (direction) {
case Direction.left:
if (canMoveInDirection(Direction.left)) {
velocity = Vector2(-moveSpeed, 0);
}
else {
velocity = Vector2.zero();
}
break;
case Direction.right:
if (canMoveInDirection(Direction.right)) {
velocity = Vector2(moveSpeed, 0);
}
else {
velocity = Vector2.zero();
}
break;
case Direction.up:
if (canMoveInDirection(Direction.up)) {
velocity = Vector2(0, -moveSpeed);
}
else {
velocity = Vector2.zero();
}
break;
case Direction.down:
if (canMoveInDirection(Direction.down)) {
velocity = Vector2(0, moveSpeed);
}
else {
velocity = Vector2.zero();
}
break;
case Direction.idle:
velocity = Vector2.zero();
break;
}

}

}

Loading

0 comments on commit 2974ccf

Please sign in to comment.