Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from warioddly/master
Browse files Browse the repository at this point in the history
Add map generator
  • Loading branch information
warioddly authored Feb 3, 2024
2 parents d1670b3 + 3f6fad1 commit 5d3599a
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 74 deletions.
13 changes: 11 additions & 2 deletions lib/characters/character.dart
Original file line number Diff line number Diff line change
@@ -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<PacmanGame> {
class Character extends SpriteAnimationComponent with HasGameReference<PacmanGame>, CollisionCallbacks {


Character({
Expand All @@ -16,6 +17,14 @@ class Character extends SpriteAnimationComponent with HasGameReference<PacmanGam
);

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


set setMoveSpeed(double speed) {
_moveSpeed = speed;
}


get moveSpeed => _moveSpeed;

}
12 changes: 9 additions & 3 deletions lib/characters/enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<PacmanGame>, CollisionCallbacks{
class Enemy extends Character with HasGameRef<PacmanGame> {


Enemy() {
setMoveSpeed = enemySpeed;
}



@override
Expand All @@ -32,7 +39,6 @@ class Enemy extends Character with HasGameRef<PacmanGame>, CollisionCallbacks{
}



@override
void update(double dt) {
position += velocity * dt;
Expand Down Expand Up @@ -69,13 +75,13 @@ class Enemy extends Character with HasGameRef<PacmanGame>, CollisionCallbacks{
}



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

if (other is Player) {
FlameAudio.play('pacman_death.wav');
}
Expand Down
23 changes: 21 additions & 2 deletions lib/characters/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ 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;


@override
FutureOr<void> onLoad() async {
super.onLoad();

debugMode = true;

animation = SpriteAnimation.fromFrameData(
game.images.fromCache('ember.png'),
SpriteAnimationData.sequenced(
Expand All @@ -23,7 +31,8 @@ class Player extends Character with KeyboardHandler {
stepTime: 0.12,
),
);
size = Vector2.all(30);

size = Vector2.all(tileSize - 4);

add(RectangleHitbox());

Expand Down Expand Up @@ -75,7 +84,7 @@ class Player extends Character with KeyboardHandler {
}


void continueMoving() {
void continueMoving() {
if (lastPressedKey == LogicalKeyboardKey.arrowLeft) {
velocity
..x = -moveSpeed
Expand All @@ -101,4 +110,14 @@ class Player extends Character with KeyboardHandler {
}


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

if (other is Wall) {

}

}

}
8 changes: 7 additions & 1 deletion lib/components/dot.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/sprite.dart';
Expand All @@ -18,7 +20,11 @@ class Dot extends SpriteAnimationComponent with HasGameRef<PacmanGame>, 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());

Expand Down
46 changes: 46 additions & 0 deletions lib/components/wall.dart
Original file line number Diff line number Diff line change
@@ -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<PacmanGame>, CollisionCallbacks {


final paint = Paint()
..color = const Color(0xFF0000FF)
..strokeWidth = 2.0
..style = PaintingStyle.stroke;


@override
Future<void> onLoad() async {
await super.onLoad();

add(
RectangleComponent(
size: Vector2.all(tileSize),
paint: paint,
),
);

add(RectangleHitbox(size: Vector2.all(tileSize)));

}



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



}

7 changes: 7 additions & 0 deletions lib/config/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


const double tileSize = 30.0;

const double playerSpeed = 100.0;

const double enemySpeed = 100.0;
5 changes: 3 additions & 2 deletions lib/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisi
Future<void> onLoad() async {
await super.onLoad();

debugMode = true;
debugMode = false;

await images.loadAll([
'ember.png',
Expand All @@ -24,7 +24,8 @@ class PacmanGame extends FlameGame with HasKeyboardHandlerComponents, HasCollisi
addAll([
FpsTextComponent(),

Level(),
Level()
..center = size / 5,

Enemy()
..center = size / 1.8,
Expand Down
90 changes: 28 additions & 62 deletions lib/level/map.dart
Original file line number Diff line number Diff line change
@@ -1,90 +1,56 @@
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<PacmanGame> {



@override
Future<void> 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<String> 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));
}

}
}
}


}


}
Expand Down
3 changes: 1 addition & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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()));

Expand Down

0 comments on commit 5d3599a

Please sign in to comment.