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 4, 2024
1 parent cb0b050 commit fe4f90b
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 124 deletions.
4 changes: 2 additions & 2 deletions lib/characters/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flame/components.dart';
import 'package:pacman/game.dart';


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


Character({
Expand All @@ -16,7 +16,7 @@ class Character extends SpriteAnimationComponent with HasGameReference<PacmanGam
animation: animation,
);

final Vector2 velocity = Vector2.zero();
Vector2 velocity = Vector2.zero();
double _moveSpeed = 100.0;


Expand Down
3 changes: 1 addition & 2 deletions lib/characters/enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:pacman/config/constants.dart';
import 'package:pacman/game.dart';
import 'package:pacman/characters/player.dart';

import 'character.dart';


class Enemy extends Character with HasGameRef<PacmanGame> {
class Enemy extends Character {


Enemy() {
Expand Down
179 changes: 101 additions & 78 deletions lib/characters/player.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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 @@ -17,13 +19,6 @@ class Player extends Character with KeyboardHandler {
}

LogicalKeyboardKey? lastPressedKey;
PathChecker pathChecker = PathChecker();


bool canMoveLeft = true;
bool canMoveRight = true;
bool canMoveTop = true;
bool canMoveBottom = true;


@override
Expand All @@ -41,120 +36,148 @@ class Player extends Character with KeyboardHandler {
),
);

size = Vector2.all(tileSize - 5);
size = Vector2.all(tileSize);

add(RectangleHitbox());

}

Ray2? ray;
Ray2? reflection;
static const numberOfRays = 4;
final List<Ray2> rays = [];
final List<RaycastResult<ShapeHitbox>> results = [];

}
get getOrigin => absolutePosition;

bool canMoveTop = true;
bool canMoveBottom = true;
bool canMoveLeft = true;
bool canMoveRight = true;

late Ray2 rightRay;
late Ray2 leftRay;

@override
void update(double dt) {

final response = pathChecker.check(position, Level.map);
gameRef.collisionDetection.raycastAll(
getOrigin,
numberOfRays: numberOfRays,
rays: rays,
out: results,
maxDistance: 300
);

canMoveRight = response.$1;
canMoveLeft = response.$2;
canMoveTop = response.$3;
canMoveBottom = response.$4;
for (final result in results) {

print(response);
getRayDirection(result);

continueMoving(dt);
}

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

}



void getRayDirection(RaycastResult<ShapeHitbox> ray) {

if (!ray.isActive) {
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;
}


}

@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
if (event is RawKeyDownEvent) {

// if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
// if (velocity.x <= 0){
// flipHorizontallyAroundCenter();
// }
// }
// else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
// if (velocity.x <= -1) {
// flipHorizontallyAroundCenter();
// }
// }
// else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
// velocity
// ..y = -moveSpeed
// ..x = 0;
// }
// else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
// velocity
// ..y = moveSpeed
// ..x = 0;
// }

lastPressedKey = event.logicalKey;

}
return false;
}


void continueMoving(dt) {
if (canMoveLeft && lastPressedKey == LogicalKeyboardKey.arrowLeft || lastPressedKey == LogicalKeyboardKey.keyA) {
velocity
..x = -moveSpeed
..y = 0;
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
..x = moveSpeed
..y = 0;
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
..y = -moveSpeed
..x = 0;
else if (canMoveTop && (lastPressedKey == LogicalKeyboardKey.arrowUp || lastPressedKey == LogicalKeyboardKey.keyW)) {
velocity = Vector2(0, -moveSpeed);
position += velocity * dt;
return;
}
else if (canMoveBottom && lastPressedKey == LogicalKeyboardKey.arrowDown || lastPressedKey == LogicalKeyboardKey.keyS) {
velocity
..y = moveSpeed
..x = 0;
else if (canMoveBottom && (lastPressedKey == LogicalKeyboardKey.arrowDown || lastPressedKey == LogicalKeyboardKey.keyS)) {
velocity = Vector2(0, moveSpeed);
position += velocity * dt;

return;
}

if (lastPressedKey == LogicalKeyboardKey.escape) {
velocity
..y = 0
..x = 0;
else if (lastPressedKey == LogicalKeyboardKey.escape) {
velocity = Vector2.zero();
}
else {
velocity = Vector2.zero();
}


}



@override
void onCollisionStart(
Set<Vector2> intersectionPoints,
PositionComponent other,
) {
final myCenter =
Vector2(position.x + tileSize / 2, position.y + tileSize / 2);
if (other is Wall) {
void render(Canvas canvas) {
super.render(canvas);
renderResult(canvas, getOrigin, results, paint);
}

lastPressedKey = null;
final diffX = myCenter.x - other.x;
final diffY = myCenter.y - other.y;
position = Vector2(position.x + diffX / 20, position.y + diffY / 20);


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,
);
}

super.onCollisionStart(intersectionPoints, other);
canvas.drawCircle(originOffset, 5, paint);
}


Expand Down
18 changes: 7 additions & 11 deletions lib/components/wall.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,19 @@ 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(size: Vector2.all(tileSize)));

}



// @override
// void onCollisionStart(Set<Vector2> intersectionPoints, PositionComponent other) {
// super.onCollisionStart(intersectionPoints, other);
//
//
// }



}

33 changes: 22 additions & 11 deletions lib/game.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
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/level/map.dart';


class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisionDetection {


PacmanGame({
required this.viewportResolution,
}) : super(
camera: CameraComponent.withFixedResolution(
width: viewportResolution.x,
height: viewportResolution.y,
),
);

final Vector2 viewportResolution;

@override
Future<void> onLoad() async {
await super.onLoad();
Expand All @@ -20,19 +36,14 @@ class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisi
'stars.png',
]);

addAll([
FpsTextComponent(),
add(FpsTextComponent());

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

Level()
..center = size / 5,

// Enemy()
// ..center = size / 1.8,
// Enemy()
// ..center = size / 3,
// Enemy()
// ..center = size / 1.5,
]);

}

Expand Down
Loading

0 comments on commit fe4f90b

Please sign in to comment.