diff --git a/lib/characters/character.dart b/lib/characters/character.dart index c87a61b..0993535 100644 --- a/lib/characters/character.dart +++ b/lib/characters/character.dart @@ -3,7 +3,7 @@ import 'package:flame/components.dart'; import 'package:pacman/game.dart'; -class Character extends SpriteAnimationComponent with HasGameReference, CollisionCallbacks { +class Character extends SpriteAnimationComponent with HasGameRef, CollisionCallbacks { Character({ @@ -16,7 +16,7 @@ class Character extends SpriteAnimationComponent with HasGameReference { +class Enemy extends Character { Enemy() { diff --git a/lib/characters/player.dart b/lib/characters/player.dart index ad106b5..e230eab 100644 --- a/lib/characters/player.dart +++ b/lib/characters/player.dart @@ -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'; @@ -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 @@ -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 rays = []; + final List> 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 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 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 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> 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); } diff --git a/lib/components/wall.dart b/lib/components/wall.dart index 5d5956a..f293a10 100644 --- a/lib/components/wall.dart +++ b/lib/components/wall.dart @@ -32,23 +32,19 @@ class Wall extends PositionComponent with HasGameRef, 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 intersectionPoints, PositionComponent other) { - // super.onCollisionStart(intersectionPoints, other); - // - // - // } - - - } diff --git a/lib/game.dart b/lib/game.dart index 4453503..4750275 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -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 onLoad() async { await super.onLoad(); @@ -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, - ]); } diff --git a/lib/level/map.dart b/lib/level/map.dart index 4e0d1d2..0dd6124 100644 --- a/lib/level/map.dart +++ b/lib/level/map.dart @@ -14,16 +14,16 @@ class Level extends PositionComponent with HasGameRef { "#.....p....#..........#", "#.##.###.#.#.#.###.##.#", "#..........#..........#", - "#.####.#########.# #..#", - "#.# #.....# # ##", - "#.# #.# # ####### ####", - "#.# # # # # #", - "#.####### ### # ## ## #", - "#.......# #.....#", - "#####.#.########.######", - "# #...# #...........#", - "# ###.#################", - "# #...................#", + "#.####.#########.#.#..#", + "#.#.#.....#......#....#", + "#.#.##.#.#..#.##.##.###", + "#.#....#...#..#...#...#", + "#.##.####.###.#.##.##.#", + "#.......#........#....#", + "#####.#.#####.##.##.###", + "#.....#...#...........#", + "#.###.##.##.###.#####.#", + "#.....................#", "#######################" ]; @@ -43,19 +43,20 @@ class Level extends PositionComponent with HasGameRef { position: Vector2(x * tileSize, y * tileSize)) ); - } - if (char == '.') { - add(Dot() - ..position = Vector2(x * tileSize, y * tileSize) - ..center = Vector2(x * tileSize + 15, y * tileSize + 15) - ); - } - + continue; + } + // if (char == '.') { + // add(Dot() + // ..position = Vector2(x * tileSize, y * tileSize) + // ..center = Vector2(x * tileSize + 15, y * tileSize + 15) + // ); + // continue; + // } if (char == 'p') { add(Player() ..position = Vector2(x * tileSize, y * tileSize) - ..center = Vector2(x * tileSize + 4, y * tileSize + 4) + ..center = Vector2(x * tileSize, y * tileSize) ); } diff --git a/lib/main.dart b/lib/main.dart index 3ea5a7b..fb09da1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,8 +4,26 @@ import 'package:pacman/game.dart'; void main() { + WidgetsFlutterBinding.ensureInitialized(); + runApp(const Game()); + +} - runApp(GameWidget(game: PacmanGame())); +class Game extends StatelessWidget { + const Game({super.key}); + + @override + Widget build(BuildContext context) { + return GameWidget( + game: PacmanGame( + viewportResolution: Vector2( + MediaQuery.of(context).size.width, + MediaQuery.of(context).size.height, + ), + ), + ); + } } +