Skip to content

Commit

Permalink
fix: Continuar a resolver bug dos NPCs falando. Ainda n resolvido
Browse files Browse the repository at this point in the history
  • Loading branch information
devpedrofurquim committed Oct 30, 2024
1 parent 9953526 commit 80e663d
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 105 deletions.
3 changes: 3 additions & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:
25 changes: 2 additions & 23 deletions lib/components/baker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,8 @@ class Baker extends AbstractNPC {
);

@override
Color getColorForNPC() => Colors.brown;

@override
void showDialogue() {
if (messageDisplayed) return;

final message = dialogues[currentDialogueIndex];
currentDialogueIndex = (currentDialogueIndex + 1) % dialogues.length;

final npcDialogue = NPCDialogueComponent(
message: message,
npcColor: getColorForNPC(),
);

gameRef.add(npcDialogue);
npcDialogue.showWithTimeout(
Duration(seconds: 2)); // Shorter duration for JournalGuy

messageDisplayed = true;
Future.delayed(Duration(seconds: 2), () {
messageDisplayed = false;
});
}
Color getColorForNPC() =>
Color(0xFF800080); // Use a pure Color instead of MaterialColor

@override
void updateMovement(double dt) {
Expand Down
7 changes: 7 additions & 0 deletions lib/components/dialogue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class NPCDialogueComponent extends PositionComponent with HasGameRef {
textComponent.position = backgroundBox.size / 2 - textComponent.size / 2;
}

@override
void onMount() {
super.onMount();
// Call showWithTimeout here to ensure component is fully loaded
showWithTimeout(Duration(seconds: 2));
}

void showWithTimeout(Duration duration) {
// Show the dialogue and remove it after the specified timeout
Future.delayed(duration, () {
Expand Down
25 changes: 2 additions & 23 deletions lib/components/journalGuy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,8 @@ class JournalGuy extends AbstractNPC {
);

@override
Color getColorForNPC() => Colors.blue;

@override
void showDialogue() {
if (messageDisplayed) return;

final message = dialogues[currentDialogueIndex];
currentDialogueIndex = (currentDialogueIndex + 1) % dialogues.length;

final npcDialogue = NPCDialogueComponent(
message: message,
npcColor: getColorForNPC(),
);

gameRef.add(npcDialogue);
npcDialogue.showWithTimeout(
Duration(seconds: 2)); // Shorter duration for JournalGuy

messageDisplayed = true;
Future.delayed(Duration(seconds: 2), () {
messageDisplayed = false;
});
}
Color getColorForNPC() =>
Color(0xFF800080); // Use a pure Color instead of MaterialColor

@override
void updateMovement(double dt) {
Expand Down
7 changes: 7 additions & 0 deletions lib/components/level.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ class Level extends World with HasGameRef<Moonshiner> {
block =
CollisionBlock(position: position, size: size, isPlayer: true);
break;
case 'NPC': // New case to handle NPC-specific collision blocks
block = CollisionBlock(
position: position,
size: size,
);
break;
default:
block = CollisionBlock(position: position, size: size);
}
Expand All @@ -179,6 +185,7 @@ class Level extends World with HasGameRef<Moonshiner> {
}
}

// Assign the collision blocks to the player, NPCs, and other entities as needed
player.collisionBlocks = collisionBlocks;
npcs.forEach((npc) => npc.collisionBlocks = collisionBlocks);
}
Expand Down
68 changes: 58 additions & 10 deletions lib/components/npc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ abstract class AbstractNPC extends SpriteAnimationGroupComponent
Vector2 velocity = Vector2.zero();
bool movingLeft = true;
bool messageDisplayed = false;
bool hasSpokenOnCollision = false; // Flag to track collision-based dialogue
List<CollisionBlock> collisionBlocks = [];
bool movingUp = true;
double changeDirectionProbability = 0.003;
Expand All @@ -38,14 +39,20 @@ abstract class AbstractNPC extends SpriteAnimationGroupComponent
required this.npcCharacter,
required this.dialogues,
required Vector2 position,
int priority = 50, // Set default priority here
int priority = 50,
}) : super(position: position, priority: priority);

@override
FutureOr<void> onLoad() async {
_loadAllAnimations();
add(RectangleHitbox()..debugMode = false); // Basic collision hitbox
Future<void> onLoad() async {
super.onLoad();
_loadAllAnimations();
add(RectangleHitbox()..debugMode = false);

if (gameRef == null) {
print("Warning: gameRef is null in AbstractNPC");
} else {
print("gameRef is initialized in AbstractNPC");
}
}

void _loadAllAnimations() {
Expand All @@ -63,8 +70,7 @@ abstract class AbstractNPC extends SpriteAnimationGroupComponent
void onCollisionStart(
Set<Vector2> intersectionPoints, PositionComponent other) {
if (other is Player) {
print("AbstractNPC collided with Player");
collidingWithPlayer(); // Call your interaction logic here
collidingWithPlayer();
}
super.onCollisionStart(intersectionPoints, other);
}
Expand All @@ -81,26 +87,68 @@ abstract class AbstractNPC extends SpriteAnimationGroupComponent
}

void collidingWithPlayer() {
if (!messageDisplayed && !gameRef.currentlySpeakingNPC) {
if (!hasSpokenOnCollision &&
!messageDisplayed &&
!gameRef.player.isInteracting) {
print("${npcCharacter} starts speaking.");
gameRef.player.isInteracting = true;
showDialogue();
hasSpokenOnCollision = true;
}
}

void showDialogue(); // Define specific behavior in each subclass
void continueDialogue() {
if (hasSpokenOnCollision && !messageDisplayed) {
showDialogue();
}
}

void showDialogue() {
if (messageDisplayed || gameRef == null) return;

print("${npcCharacter} is showing dialogue.");

Color getColorForNPC(); // Define in each subclass
final message = dialogues[currentDialogueIndex];
currentDialogueIndex = (currentDialogueIndex + 1) % dialogues.length;

final npcDialogue = NPCDialogueComponent(
message: message,
npcColor: getColorForNPC(),
);

gameRef.add(npcDialogue);
npcDialogue.showWithTimeout(Duration(seconds: 2));

messageDisplayed = true;

Future.delayed(Duration(seconds: 2), () {
messageDisplayed = false;
gameRef.player.isInteracting = false;
print("${npcCharacter} finished speaking.");
if (currentDialogueIndex == 0) {
hasSpokenOnCollision = false;
}
});
}

Color getColorForNPC(); // Implemented in subclasses

@override
void update(double dt) {
final Vector2 playerPosition = gameRef.player.position;
final double distanceToPlayer = playerPosition.distanceTo(position);

_checkCollisions();
_checkCollisions(); // Call collision check every frame

if (distanceToPlayer < 50) {
velocity = Vector2.zero();
current = NPCState.idle;
collidingWithPlayer(); // Trigger dialogue if close enough
} else {
// Reset `hasSpokenOnCollision` only when the player moves far enough away
if (distanceToPlayer > 70) {
hasSpokenOnCollision = false;
}
updateMovement(dt);
current = NPCState.walking;
}
Expand Down
25 changes: 2 additions & 23 deletions lib/components/oldLady.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,8 @@ class OldLady extends AbstractNPC {
);

@override
Color getColorForNPC() => Colors.grey;

@override
void showDialogue() {
if (messageDisplayed) return;

final message = dialogues[currentDialogueIndex];
currentDialogueIndex = (currentDialogueIndex + 1) % dialogues.length;

final npcDialogue = NPCDialogueComponent(
message: message,
npcColor: getColorForNPC(),
);

gameRef.add(npcDialogue);
npcDialogue.showWithTimeout(
Duration(seconds: 2)); // Shorter duration for JournalGuy

messageDisplayed = true;
Future.delayed(Duration(seconds: 2), () {
messageDisplayed = false;
});
}
Color getColorForNPC() =>
Color(0xFF800080); // Use a pure Color instead of MaterialColor

@override
void updateMovement(double dt) {
Expand Down
15 changes: 12 additions & 3 deletions lib/components/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Player extends SpriteAnimationGroupComponent
double horizontalMovement = 0;
double verticalMovement = 0;
double moveSpeed = 50;
bool isInteracting = false; // Add this flag to manage interactions
Vector2 velocity = Vector2.zero();
List<CollisionBlock> collisionBlocks = [];
final double _gravity = 8.9;
Expand All @@ -57,9 +58,17 @@ class Player extends SpriteAnimationGroupComponent
return super.onLoad();
}

void interact() {
hasInteracted = true; // This sets up interaction
print("Player interaction activated.");
void interact(List<AbstractNPC> npcs) {
hasInteracted = true; // Set up interaction

// Check each NPC for interaction
for (final npc in npcs) {
if (npc.hasSpokenOnCollision) {
npc.continueDialogue();
break; // Stop after the first valid interaction
}
}
hasInteracted = false; // Reset after interaction
}

@override
Expand Down
25 changes: 2 additions & 23 deletions lib/components/priest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,8 @@ class Priest extends AbstractNPC {
);

@override
Color getColorForNPC() => Colors.purple;

@override
void showDialogue() {
if (messageDisplayed) return;

final message = dialogues[currentDialogueIndex];
currentDialogueIndex = (currentDialogueIndex + 1) % dialogues.length;

final npcDialogue = NPCDialogueComponent(
message: message,
npcColor: getColorForNPC(),
);

gameRef.add(npcDialogue);
npcDialogue.showWithTimeout(
Duration(seconds: 2)); // Shorter duration for JournalGuy

messageDisplayed = true;
Future.delayed(Duration(seconds: 2), () {
messageDisplayed = false;
});
}
Color getColorForNPC() =>
Color(0xFF800080); // Use a pure Color instead of MaterialColor

@override
void updateMovement(double dt) {
Expand Down

0 comments on commit 80e663d

Please sign in to comment.