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 6, 2024
1 parent fe4f90b commit 5536d0a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 79 deletions.
123 changes: 58 additions & 65 deletions lib/characters/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 'character.dart';


Expand Down Expand Up @@ -38,12 +39,11 @@ class Player extends Character with KeyboardHandler {

size = Vector2.all(tileSize);

// add(RectangleHitbox());

}

Ray2? ray;
Ray2? reflection;
static const numberOfRays = 4;

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

Expand All @@ -54,31 +54,26 @@ class Player extends Character with KeyboardHandler {
bool canMoveLeft = true;
bool canMoveRight = true;

late Ray2 rightRay;
late Ray2 leftRay;

@override
void update(double dt) {

gameRef.collisionDetection.raycastAll(
getOrigin,
numberOfRays: numberOfRays,
rays: rays,
out: results,
maxDistance: 300
);

for (final result in results) {

getRayDirection(result);

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

continueMoving(dt);
super.update(dt);

}
return super.update(dt);

}


void getRayDirection(RaycastResult<ShapeHitbox> ray) {
Expand All @@ -87,26 +82,17 @@ class Player extends Character with KeyboardHandler {
return;
}

final distance = ray.intersectionPoint!.distanceTo(absolutePosition) / tileSize;

const safetyDistance = 0.65;

canMoveRight = true;
canMoveLeft = true;



if(ray.normal!.x == -1 && distance <= safetyDistance) {
canMoveRight = false;
}

if(ray.normal!.x == 1 && distance >= safetyDistance) {
canMoveLeft = false;
}
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);

}


@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
if (event is RawKeyDownEvent) {
Expand All @@ -116,46 +102,53 @@ class Player extends Character with KeyboardHandler {
}


void continueMoving(dt) {
if (canMoveLeft && (lastPressedKey == LogicalKeyboardKey.arrowLeft || lastPressedKey == LogicalKeyboardKey.keyA)) {
velocity = Vector2(-moveSpeed, 0);
position += velocity * dt;
return;
}
else if (canMoveRight && (lastPressedKey == LogicalKeyboardKey.arrowRight || lastPressedKey == LogicalKeyboardKey.keyD)) {
velocity = Vector2(moveSpeed, 0);
position += velocity * dt;
return;
}
else if (canMoveTop && (lastPressedKey == LogicalKeyboardKey.arrowUp || lastPressedKey == LogicalKeyboardKey.keyW)) {
velocity = Vector2(0, -moveSpeed);
position += velocity * dt;
return;
void continueMoving(double dt) {
switch (lastPressedKey) {
case LogicalKeyboardKey.arrowLeft:
case LogicalKeyboardKey.keyA:
if (canMoveLeft) {
velocity = Vector2(-moveSpeed, 0);
position += velocity * dt;
}
break;
case LogicalKeyboardKey.arrowRight:
case LogicalKeyboardKey.keyD:
if (canMoveRight) {
velocity = Vector2(moveSpeed, 0);
position += velocity * dt;
}
break;
case LogicalKeyboardKey.arrowUp:
case LogicalKeyboardKey.keyW:
if (canMoveTop) {
velocity = Vector2(0, -moveSpeed);
position += velocity * dt;
}
break;
case LogicalKeyboardKey.arrowDown:
case LogicalKeyboardKey.keyS:
if (canMoveBottom) {
velocity = Vector2(0, moveSpeed);
position += velocity * dt;
}
break;
case LogicalKeyboardKey.escape:
velocity = Vector2.zero();
break;
default:
velocity = Vector2.zero();
break;
}
else if (canMoveBottom && (lastPressedKey == LogicalKeyboardKey.arrowDown || lastPressedKey == LogicalKeyboardKey.keyS)) {
velocity = Vector2(0, moveSpeed);
position += velocity * dt;
return;
}
else if (lastPressedKey == LogicalKeyboardKey.escape) {
velocity = Vector2.zero();
}
else {
velocity = Vector2.zero();
}

}



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



void renderResult(
Canvas canvas,
Vector2 origin,
Expand Down
25 changes: 16 additions & 9 deletions lib/components/wall.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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 @@ -11,7 +13,6 @@ class Wall extends PositionComponent with HasGameRef<PacmanGame>, CollisionCallb


Wall({
required this.coordinates,
Vector2? position
}) : super(
position: position
Expand All @@ -21,7 +22,9 @@ class Wall extends PositionComponent with HasGameRef<PacmanGame>, CollisionCallb
..color = const Color(0xFF0000FF)
..strokeWidth = 2.0
..style = PaintingStyle.stroke;
final Vector2 coordinates;

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


@override
Expand All @@ -32,16 +35,20 @@ class Wall extends PositionComponent with HasGameRef<PacmanGame>, CollisionCallb
RectangleComponent(
size: Vector2.all(tileSize),
paint: paint,
children: [
RectangleHitbox(
position: Vector2.zero(),
anchor: Anchor.center,
size: Vector2.all(6),
)
]
),
);


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

}


Expand Down
1 change: 1 addition & 0 deletions lib/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisi

add(FpsTextComponent());


add(
Level()
..anchor = Anchor.center
Expand Down
12 changes: 7 additions & 5 deletions lib/level/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Level extends PositionComponent with HasGameRef<PacmanGame> {

static const List<String> map = [
"#######################",
"#.....p....#..........#",
"#.##.###.#.#.#.###.##.#",
"#..........#..........#",
"#.##p###.#.#.#.###.##.#",
"#..........#..........#",
"#.####.#########.#.#..#",
"#.#.#.....#......#....#",
Expand All @@ -38,11 +38,13 @@ class Level extends PositionComponent with HasGameRef<PacmanGame> {

if (char == '#') {

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


continue;

}
Expand Down

0 comments on commit 5536d0a

Please sign in to comment.