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 7, 2024
1 parent 5536d0a commit a81b6f1
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 129 deletions.
9 changes: 9 additions & 0 deletions lib/characters/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import 'package:flame/components.dart';
import 'package:pacman/game.dart';



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

class Character extends SpriteAnimationComponent with HasGameRef<PacmanGame>, CollisionCallbacks {


Expand Down
115 changes: 48 additions & 67 deletions lib/characters/player.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import 'dart:async';
import 'dart:math';
import 'dart:ui';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/geometry.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pacman/components/wall.dart';
import 'package:pacman/config/constants.dart';
import 'package:pacman/level/map.dart';
import 'package:pacman/utils/path_checker.dart';
import 'character.dart';


Expand All @@ -19,7 +14,6 @@ class Player extends Character with KeyboardHandler {
setMoveSpeed = playerSpeed;
}

LogicalKeyboardKey? lastPressedKey;


@override
Expand All @@ -39,60 +33,72 @@ class Player extends Character with KeyboardHandler {

size = Vector2.all(tileSize);

// add(RectangleHitbox());

}
lastPosition = position;


final List<Ray2> rays = [];
final List<RaycastResult<ShapeHitbox>> results = [];

get getOrigin => absolutePosition;
}

final pathChecker = PathChecker();
LogicalKeyboardKey? lastPressedKey;
bool canMoveTop = true;
bool canMoveBottom = true;
bool canMoveLeft = true;
bool canMoveRight = true;

Vector2 lastPosition = Vector2.zero();
Direction lastDirection = Direction.stop;

@override
void update(double dt) {
super.update(dt);

// gameRef.collisionDetection.raycastAll(
// getOrigin,
// numberOfRays: 4,
// maxDistance: tileSize * 2,
// out: results,
// );
//
// for (final result in results) {
// getRayDirection(result);
// }
final pos = position;

continueMoving(dt);
dynamic x = pos.x.round();
dynamic y = pos.y.round();

return super.update(dt);

}
if ((y % tileSize).toInt() == 0) {

if (lastDirection == Direction.up) {
canMoveTop = pathChecker.canMove(Vector2(x, y), Level.map, Direction.up);
lastPosition.y = y;
}

if (lastDirection == Direction.down) {
canMoveBottom = pathChecker.canMove(Vector2(x, y), Level.map, Direction.down);
lastPosition.y = y;
}

}


if ((x % tileSize).toInt() == 0) {

if (lastDirection == Direction.left) {
canMoveLeft = pathChecker.canMove(Vector2(x, y), Level.map, Direction.left);
lastPosition.x = x;
}

void getRayDirection(RaycastResult<ShapeHitbox> ray) {
if (lastDirection == Direction.right) {
canMoveRight = pathChecker.canMove(Vector2(x, y), Level.map, Direction.right);
lastPosition.x = x;
}

if (!ray.isActive) {
return;
}

final distance = (ray.intersectionPoint?.distanceTo(absolutePosition) ?? 0) / tileSize;
const safetyDistance = 0.80;

canMoveRight = !(ray.normal!.x == -1 && distance <= safetyDistance);
canMoveLeft = !(ray.normal!.x == 1 && distance <= safetyDistance);
canMoveBottom = !(ray.normal!.y == -1 && distance <= safetyDistance);
canMoveTop = !(ray.normal!.y == 1 && distance <= safetyDistance);
// if ((x % 10).toInt() == 0) {
// canMoveLeft = pathChecker.canMove(pos, Level.map, Direction.left);
// canMoveRight = pathChecker.canMove(pos, Level.map, Direction.right);
// }

continueMoving(dt);

}



@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
if (event is RawKeyDownEvent) {
Expand All @@ -109,68 +115,43 @@ class Player extends Character with KeyboardHandler {
if (canMoveLeft) {
velocity = Vector2(-moveSpeed, 0);
position += velocity * dt;
lastDirection = Direction.left;
}
break;
case LogicalKeyboardKey.arrowRight:
case LogicalKeyboardKey.keyD:
if (canMoveRight) {
velocity = Vector2(moveSpeed, 0);
position += velocity * dt;
lastDirection = Direction.right;
}
break;
case LogicalKeyboardKey.arrowUp:
case LogicalKeyboardKey.keyW:
if (canMoveTop) {
velocity = Vector2(0, -moveSpeed);
position += velocity * dt;
lastDirection = Direction.up;
}
break;
case LogicalKeyboardKey.arrowDown:
case LogicalKeyboardKey.keyS:
if (canMoveBottom) {
velocity = Vector2(0, moveSpeed);
position += velocity * dt;
lastDirection = Direction.down;
}
break;
case LogicalKeyboardKey.escape:
velocity = Vector2.zero();
lastDirection = Direction.stop;
break;
default:
velocity = Vector2.zero();
lastDirection = Direction.stop;
break;
}
}


@override
void render(Canvas canvas) {
super.render(canvas);
renderResult(canvas, getOrigin, results, paint);
}


void renderResult(
Canvas canvas,
Vector2 origin,
List<RaycastResult<ShapeHitbox>> results,
Paint paint,
) {

final originOffset = origin.toOffset();

for (final result in results) {
if (!result.isActive) {
continue;
}
final intersectionPoint = result.intersectionPoint!.toOffset();
canvas.drawLine(
originOffset,
intersectionPoint,
paint,
);
}

canvas.drawCircle(originOffset, 5, paint);
}


Expand Down
29 changes: 7 additions & 22 deletions lib/components/wall.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:ui';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/palette.dart';
import 'package:flame/text.dart';
import 'package:pacman/config/constants.dart';
import 'package:pacman/game.dart';

Expand All @@ -12,41 +11,27 @@ import 'package:pacman/game.dart';
class Wall extends PositionComponent with HasGameRef<PacmanGame>, CollisionCallbacks {


Wall({
Vector2? position
}) : super(
position: position
);

final paint = Paint()
..color = const Color(0xFF0000FF)
..strokeWidth = 2.0
..style = PaintingStyle.stroke;

Vector2 coordinates = Vector2.zero();
String id = "";


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

add(
RectangleComponent(
size: Vector2.all(tileSize),
paint: paint,
),
);


add(RectangleHitbox(
collisionType: CollisionType.active,
position: coordinates / tileSize / 10,
RectangleHitbox(
size: Vector2.all(tileSize),
)
..debugColor = BasicPalette.red.color
..debugMode = true
..priority = 1
..paint = paint
..debugColor = BasicPalette.red.color.withOpacity(0.4)
..debugMode = false
..priority = 1
..renderShape = true

);

}
Expand Down
46 changes: 36 additions & 10 deletions lib/game.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'dart:async';
import 'dart:ui';

import 'package:flame/camera.dart';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:pacman/config/constants.dart';
import 'package:pacman/characters/player.dart';
import 'package:pacman/level/map.dart';


class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisionDetection {

class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasQuadTreeCollisionDetection {


PacmanGame({
Expand All @@ -19,34 +19,60 @@ class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisi
width: viewportResolution.x,
height: viewportResolution.y,
),
world: MyWorld(),
);

final Vector2 viewportResolution;

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

debugMode = false;

// FlameAudio.play('pacman_beginning.wav');

initializeCollisionDetection(
mapDimensions: Rect.fromLTWH(0, 0, viewportResolution.x, viewportResolution.y),
minimumDistance: 10,
);


await images.loadAll([
'ember.png',
'water_enemy.png',
'stars.png',
]);

add(FpsTextComponent());
add(ScreenHitbox());


add(
Level()
..anchor = Anchor.center
);
}


}


}
class MyWorld extends World with HasGameRef<PacmanGame>, HasCollisionDetection {

late final Player player;

}
@override
FutureOr<void> onLoad() {

super.onLoad();

add(player = Player());

gameRef.camera.follow(player, maxSpeed: 200);

addAll([

Level()

]);

}

}
13 changes: 2 additions & 11 deletions lib/level/map.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

import 'package:flame/components.dart';
import 'package:pacman/characters/player.dart';
import 'package:pacman/components/dot.dart';
import 'package:pacman/components/wall.dart';
import 'package:pacman/config/constants.dart';
import 'package:pacman/game.dart';
Expand Down Expand Up @@ -39,14 +37,10 @@ class Level extends PositionComponent with HasGameRef<PacmanGame> {
if (char == '#') {

add(Wall()
..position = Vector2(x * tileSize, y * tileSize)
..coordinates = Vector2(x * tileSize, y * tileSize)
..id = [x, y].toString()
..position = Vector2((x + position.x) * tileSize, y * tileSize)
);


continue;

}
// if (char == '.') {
// add(Dot()
Expand All @@ -56,10 +50,7 @@ class Level extends PositionComponent with HasGameRef<PacmanGame> {
// continue;
// }
if (char == 'p') {
add(Player()
..position = Vector2(x * tileSize, y * tileSize)
..center = Vector2(x * tileSize, y * tileSize)
);
(gameRef.world as MyWorld).player.position = Vector2(x * tileSize, y * tileSize);
}

}
Expand Down
Loading

0 comments on commit a81b6f1

Please sign in to comment.