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 9, 2024
1 parent a81b6f1 commit af4c279
Show file tree
Hide file tree
Showing 15 changed files with 761 additions and 216 deletions.
13 changes: 2 additions & 11 deletions lib/characters/character.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import 'package:flame/collisions.dart';

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 {
class Character extends SpriteAnimationComponent with HasGameRef<PacmanGame> {


Character({
Expand Down
36 changes: 17 additions & 19 deletions lib/characters/enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import 'dart:math';

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/characters/player.dart';
import 'package:pacman/core/constants.dart';

import 'character.dart';

Expand Down Expand Up @@ -74,22 +72,22 @@ class Enemy extends Character {
}


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

if (other is Player) {
FlameAudio.play('pacman_death.wav');
}

if (other is Enemy) {
randomMovement();
}

}
// @override
// void onCollisionStart(
// Set<Vector2> intersectionPoints,
// PositionComponent other,
// ) {
// super.onCollisionStart(intersectionPoints, other);
//
// if (other is Player) {
// FlameAudio.play('pacman_death.wav');
// }
//
// if (other is Enemy) {
// randomMovement();
// }
//
// }



Expand Down
121 changes: 12 additions & 109 deletions lib/characters/player.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:async';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/palette.dart';
import 'package:flutter/services.dart';
import 'package:pacman/config/constants.dart';
import 'package:pacman/level/map.dart';
import 'package:pacman/utils/path_checker.dart';
import 'package:pacman/core/constants.dart';
import 'character.dart';


Expand Down Expand Up @@ -33,70 +33,19 @@ class Player extends Character with KeyboardHandler {

size = Vector2.all(tileSize);

lastPosition = position;
add(
RectangleHitbox(
size: Vector2.all(tileSize),
)
..debugColor = BasicPalette.red.color.withOpacity(0.4)
..debugMode = true
..priority = 1
);


}

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

final pos = position;

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


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

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

}


// if ((x % 10).toInt() == 0) {
// canMoveLeft = pathChecker.canMove(pos, Level.map, Direction.left);
// canMoveRight = pathChecker.canMove(pos, Level.map, Direction.right);
// }

continueMoving(dt);

}



@override
Expand All @@ -108,52 +57,6 @@ class Player extends Character with KeyboardHandler {
}


void continueMoving(double dt) {
switch (lastPressedKey) {
case LogicalKeyboardKey.arrowLeft:
case LogicalKeyboardKey.keyA:
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;
}

}



}

Loading

0 comments on commit af4c279

Please sign in to comment.