diff --git a/lib/characters/character.dart b/lib/characters/character.dart index 288740e..c87a61b 100644 --- a/lib/characters/character.dart +++ b/lib/characters/character.dart @@ -1,8 +1,9 @@ +import 'package:flame/collisions.dart'; import 'package:flame/components.dart'; import 'package:pacman/game.dart'; -class Character extends SpriteAnimationComponent with HasGameReference { +class Character extends SpriteAnimationComponent with HasGameReference, CollisionCallbacks { Character({ @@ -16,6 +17,14 @@ class Character extends SpriteAnimationComponent with HasGameReference _moveSpeed; } diff --git a/lib/characters/enemy.dart b/lib/characters/enemy.dart index 757f127..a3cab61 100644 --- a/lib/characters/enemy.dart +++ b/lib/characters/enemy.dart @@ -4,13 +4,20 @@ 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/game.dart'; import 'package:pacman/characters/player.dart'; import 'character.dart'; -class Enemy extends Character with HasGameRef, CollisionCallbacks{ +class Enemy extends Character with HasGameRef { + + + Enemy() { + setMoveSpeed = enemySpeed; + } + @override @@ -32,7 +39,6 @@ class Enemy extends Character with HasGameRef, CollisionCallbacks{ } - @override void update(double dt) { position += velocity * dt; @@ -69,13 +75,13 @@ class Enemy extends Character with HasGameRef, CollisionCallbacks{ } - @override void onCollisionStart( Set intersectionPoints, PositionComponent other, ) { super.onCollisionStart(intersectionPoints, other); + if (other is Player) { FlameAudio.play('pacman_death.wav'); } diff --git a/lib/characters/player.dart b/lib/characters/player.dart index 1d77161..97fab39 100644 --- a/lib/characters/player.dart +++ b/lib/characters/player.dart @@ -2,12 +2,18 @@ import 'dart:async'; import 'package:flame/collisions.dart'; import 'package:flame/components.dart'; import 'package:flutter/services.dart'; +import 'package:pacman/components/wall.dart'; +import 'package:pacman/config/constants.dart'; import 'character.dart'; class Player extends Character with KeyboardHandler { + Player() { + setMoveSpeed = playerSpeed; + } + LogicalKeyboardKey? lastPressedKey; @@ -15,6 +21,8 @@ class Player extends Character with KeyboardHandler { FutureOr onLoad() async { super.onLoad(); + debugMode = true; + animation = SpriteAnimation.fromFrameData( game.images.fromCache('ember.png'), SpriteAnimationData.sequenced( @@ -23,7 +31,8 @@ class Player extends Character with KeyboardHandler { stepTime: 0.12, ), ); - size = Vector2.all(30); + + size = Vector2.all(tileSize - 4); add(RectangleHitbox()); @@ -75,7 +84,7 @@ class Player extends Character with KeyboardHandler { } - void continueMoving() { + void continueMoving() { if (lastPressedKey == LogicalKeyboardKey.arrowLeft) { velocity ..x = -moveSpeed @@ -101,4 +110,14 @@ class Player extends Character with KeyboardHandler { } + @override + void onCollisionStart(Set intersectionPoints, PositionComponent other) { + super.onCollisionStart(intersectionPoints, other); + + if (other is Wall) { + + } + + } + } diff --git a/lib/components/dot.dart b/lib/components/dot.dart index c3ec9ae..cf60fd0 100644 --- a/lib/components/dot.dart +++ b/lib/components/dot.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:flame/collisions.dart'; import 'package:flame/components.dart'; import 'package:flame/sprite.dart'; @@ -18,7 +20,11 @@ class Dot extends SpriteAnimationComponent with HasGameRef, Collisio image: await game.images.load('stars.png'), rows: 4, columns: 4, - ).createAnimation(row: 0, to: 4, stepTime: 0.1); + ).createAnimation( + row: 0, + to: 4, + stepTime: Random().nextDouble() * 0.1 + 0.1 + ); add(CircleHitbox()); diff --git a/lib/components/wall.dart b/lib/components/wall.dart new file mode 100644 index 0000000..3d5c543 --- /dev/null +++ b/lib/components/wall.dart @@ -0,0 +1,46 @@ +import 'dart:ui'; + +import 'package:flame/collisions.dart'; +import 'package:flame/components.dart'; +import 'package:pacman/config/constants.dart'; +import 'package:pacman/game.dart'; + + + +class Wall extends PositionComponent with HasGameRef, CollisionCallbacks { + + + final paint = Paint() + ..color = const Color(0xFF0000FF) + ..strokeWidth = 2.0 + ..style = PaintingStyle.stroke; + + + @override + Future onLoad() async { + await super.onLoad(); + + add( + RectangleComponent( + size: Vector2.all(tileSize), + paint: paint, + ), + ); + + add(RectangleHitbox(size: Vector2.all(tileSize))); + + } + + + + // @override + // void onCollisionStart(Set intersectionPoints, PositionComponent other) { + // super.onCollisionStart(intersectionPoints, other); + // + // + // } + + + +} + diff --git a/lib/config/constants.dart b/lib/config/constants.dart new file mode 100644 index 0000000..c460712 --- /dev/null +++ b/lib/config/constants.dart @@ -0,0 +1,7 @@ + + +const double tileSize = 30.0; + +const double playerSpeed = 100.0; + +const double enemySpeed = 100.0; \ No newline at end of file diff --git a/lib/game.dart b/lib/game.dart index 5d0249b..dfe7c0a 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -13,7 +13,7 @@ class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisi Future onLoad() async { await super.onLoad(); - debugMode = true; + debugMode = false; await images.loadAll([ 'ember.png', @@ -24,7 +24,8 @@ class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisi addAll([ FpsTextComponent(), - Level(), + Level() + ..center = size / 5, Enemy() ..center = size / 1.8, diff --git a/lib/level/map.dart b/lib/level/map.dart index be0c719..a343135 100644 --- a/lib/level/map.dart +++ b/lib/level/map.dart @@ -1,16 +1,12 @@ -import 'dart:math'; import 'package:flame/components.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter/painting.dart'; import 'package:pacman/components/dot.dart'; +import 'package:pacman/components/wall.dart'; +import 'package:pacman/config/constants.dart'; +import 'package:pacman/game.dart'; -class Level extends PositionComponent { - - - - +class Level extends PositionComponent with HasGameRef { @@ -18,73 +14,43 @@ class Level extends PositionComponent { Future onLoad() async { await super.onLoad(); - - final random = Random(); - - - addAll([ - ...List.generate(10, (index) => Dot() - ..center = Vector2( - random.nextDouble() * 1000, - random.nextDouble() * 1000, - )) - ]); - - - - - - - - - } - - - @override - void render(Canvas canvas) { - // Define the Pacman map here final List pacmanMap = [ - "###############################", - "#...........#...#...........#.#", - "#.###.#####.#.#.#.#####.###.#.#", - "#.# #.# #.#.#.# #.# #.#.#", - "#.# #.# ###.#.#.### #.# #.#.#", - "#.# #.# # # #.# #.#.#", - "#.# #.# # ####### #######.#.#.#", - "#.# # # # # # #...#", - "#.####### ### # ### ### # ###.#", - "#.......# # # .#", - "#####.#.######### ####### #####", - "# #...# #..............#...#.#", - "# ###.##################.#####.#", - "# #..............#............#", - "# #############################" + "#######################", + "#..........#..........#", + "#.##.###.#.#.#.###.##.#", + "#..........#..........#", + "#.####.#########.# #..#", + "#.# #.....# # ##", + "#.# #.# # ####### ####", + "#.# # # # # #", + "#.####### ### # ## ## #", + "#.......# #.....#", + "#####.#.########.######", + "# #...# #...........#", + "# ###.#################", + "# #...................#", + "#######################" ]; - const tileSize = 50.0; - final paint = Paint()..color = Colors.blue; - for (var y = 0; y < pacmanMap.length; y++) { for (var x = 0; x < pacmanMap[y].length; x++) { final char = pacmanMap[y][x]; + if (char == '#') { - canvas.drawRect( - Rect.fromLTWH( - x * tileSize, - y * tileSize, - 40, - 40, - ), - paint, - ); + add(Wall() + ..position = Vector2(x * tileSize, y * tileSize )); } + if (char == '.') { + add(Dot() + ..position = Vector2(x * tileSize * 1.02, y * tileSize)); + } + } } - } - + } } diff --git a/lib/main.dart b/lib/main.dart index 3a5ef2b..d3811b1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,11 +1,10 @@ -import 'package:flame_audio/flame_audio.dart'; import 'package:flutter/material.dart'; import 'package:flame/game.dart'; import 'package:pacman/game.dart'; void main() { - FlameAudio.play('pacman_beginning.wav'); + // FlameAudio.play('pacman_beginning.wav'); runApp(GameWidget(game: PacmanGame()));