Skip to content

Commit

Permalink
feat: Implementa feature de missions no LevelOne e arruma bug de muda…
Browse files Browse the repository at this point in the history
…nça de levels
  • Loading branch information
devpedrofurquim committed Dec 24, 2024
1 parent ae81498 commit f893cc1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 31 deletions.
64 changes: 39 additions & 25 deletions lib/components/level_one.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import 'level.dart';

class LevelOne extends Level {
bool developerMessageVisible = true;

// Mission state
bool talkedToWife = false;
bool saidGoodbyeToFather = false;
bool saidGoodbyeToMother = false;

LevelOne({required String levelName, required Player player})
: super(levelName: levelName, player: player);

Expand All @@ -20,26 +26,19 @@ class LevelOne extends Level {

print("LevelOne onLoad called");

player.onLevelTransition = () => endLevelAndMoveToMoonshiner();
player.onLevelTransition = _tryToMoveToNextLevel;

if (!gameRef.hasShownLevelOneIntro) {
_showIntroMessageWhenReady();
gameRef.hasShownLevelOneIntro = true;
}

// Call the customized background setup for LevelOne
_setupBackground();

// Customize dialogues for the Wife in LevelOne
_customizeWifeDialogues();

// Spawn the Wife's parents (Father and Mom)
_spawnParents();
}

// Override _setupBackground for LevelOne
void _setupBackground() {
// Define the background specific to LevelOne
final backgroundTile = BackgroundTile(position: Vector2(0, 50));
add(backgroundTile);
}
Expand All @@ -53,6 +52,30 @@ class LevelOne extends Level {
"Talk to your wife to start your journey to Moonshiner.");
}

void _tryToMoveToNextLevel() {
if (talkedToWife && saidGoodbyeToFather && saidGoodbyeToMother) {
endLevelAndMoveToMoonshiner();
} else {
// Build the message for incomplete tasks
List<String> incompleteTasks = [];
if (!talkedToWife) {
incompleteTasks.add("Talk to your wife.");
}
if (!saidGoodbyeToFather) {
incompleteTasks.add("Say goodbye to her father.");
}
if (!saidGoodbyeToMother) {
incompleteTasks.add("Say goodbye to her mother.");
}

// Join the incomplete tasks into a single line
String message = incompleteTasks.join(" ");

// Show the message
gameRef.showDeveloperMessage(message);
}
}

void endLevelAndMoveToMoonshiner() {
gameRef.showDeveloperMessage("Time to head to Moonshiner!");

Expand All @@ -63,7 +86,6 @@ class LevelOne extends Level {

void _customizeWifeDialogues() {
try {
// Locate the Wife instance and update her dialogues
final wife = children.firstWhere((child) => child is Wife) as Wife;
wife.dialogues = [
"Are you ready for this journey, my love?",
Expand All @@ -78,57 +100,49 @@ class LevelOne extends Level {
"Let’s make the most of our new life, one day at a time."
];

// Add interaction logic
wife.onPlayerInteraction = () {
if (developerMessageVisible) {
gameRef.hideDeveloperMessage();
developerMessageVisible = false;
}
talkedToWife = true; // Mark mission as completed
print("Talked to wife: $talkedToWife"); // Debug log
};
} catch (e) {
// Handle the case where Wife component is not found
print("Wife component not found in children.");
}
}

void _spawnParents() {
// Position them based on your map or desired coordinates
final fatherPosition = Vector2(100, 200); // Example position
final motherPosition = Vector2(150, 200); // Example position
final fatherPosition = Vector2(100, 200);
final motherPosition = Vector2(150, 200);

// Create the father and mother NPCs
final wifesFather = WifesFather(position: fatherPosition);
final wifesMom = WifesMom(position: motherPosition);

// Add them to the game world
add(wifesFather);
add(wifesMom);

// Optional: Handle goodbye logic (see next section)
wifesFather.onPlayerInteraction = _onGoodbyeFather;
wifesMom.onPlayerInteraction = _onGoodbyeMother;
}

bool saidGoodbyeToFather = false;
bool saidGoodbyeToMother = false;

void _onGoodbyeFather() {
saidGoodbyeToFather = true;
saidGoodbyeToFather = true; // Mark mission as completed
print("Said goodbye to father: $saidGoodbyeToFather"); // Debug log
_checkAllGoodbyes();
}

void _onGoodbyeMother() {
saidGoodbyeToMother = true;
saidGoodbyeToMother = true; // Mark mission as completed
print("Said goodbye to mother: $saidGoodbyeToMother"); // Debug log
_checkAllGoodbyes();
}

void _checkAllGoodbyes() {
if (saidGoodbyeToFather && saidGoodbyeToMother) {
gameRef
.showDeveloperMessage("You said goodbye to everyone. Time to leave!");
Future.delayed(Duration(seconds: 2), () {
endLevelAndMoveToMoonshiner();
});
}
}
}
10 changes: 8 additions & 2 deletions lib/components/npc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,21 @@ abstract class AbstractNPC extends SpriteAnimationGroupComponent
}
other.isInteractingWithNPC = true;
hasSpokenOnCollision = true;

// Trigger the interaction callback if assigned
if (onPlayerInteraction != null) {
onPlayerInteraction!(); // Call the assigned interaction callback
print("onPlayerInteraction triggered for NPC: $npcCharacter");
}
}
super.onCollisionStart(intersectionPoints, other);
}

@override
void onCollisionEnd(PositionComponent other) {
if (other is Player && messageDisplayed) {
// Clear dialogue when player leaves
if (dialogueComponent != null) {
// Clear dialogue when the player leaves
if (dialogueComponent != null && dialogueComponent!.isMounted) {
gameRef.remove(dialogueComponent!);
dialogueComponent = null;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/components/wife.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ class Wife extends SpriteAnimationGroupComponent
@override
void onCollisionEnd(PositionComponent other) {
if (other is Player && messageDisplayed) {
// Clear dialogue when player leaves
if (dialogueComponent != null) {
// Clear dialogue when the player leaves
if (dialogueComponent != null && dialogueComponent!.isMounted) {
gameRef.remove(dialogueComponent!);
dialogueComponent = null;
}
Expand Down
28 changes: 26 additions & 2 deletions lib/moonshiner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Moonshiner extends FlameGame
TapCallbacks {
// UI Components
late JoystickComponent joyStick;
Level? currentLevel; // Define currentLevel as nullable
late Button interactButton;
List<AbstractNPC> activeNpcs = []; // Track active NPCs in the current level
late HUDMessage hudMessage;
Expand Down Expand Up @@ -74,6 +75,11 @@ class Moonshiner extends FlameGame

add(developerMessageComponent);

// Automatically remove the message after 3 seconds
Future.delayed(Duration(seconds: 3), () {
developerMessageComponent.removeFromParent();
});

print("Developer message added to the game.");
}

Expand Down Expand Up @@ -101,12 +107,18 @@ class Moonshiner extends FlameGame
}
});

// Ensure previous level is unloaded
if (currentLevel != null) {
currentLevel!.unload();
currentLevel = null;
}

// Initialize player and set position
player = Player(character: 'Guy', position: Vector2(100, 100));

late final level;
late final Level level;

// Create new level instance
// Create new level instance based on the index
if (index == 0) {
level = LevelOne(
levelName: levelNames[index],
Expand All @@ -119,10 +131,14 @@ class Moonshiner extends FlameGame
);
}

// Set current level to the new level
currentLevel = level;

// Set up the camera for the new level and add level to the game
_setupCamera(level);
add(level);

// Play the background music for the new level
playBackgroundMusicForLevel(levelNames[index]);
}

Expand Down Expand Up @@ -227,7 +243,15 @@ class Moonshiner extends FlameGame

// Delay loading the next level until the fade-in completes
Future.delayed(Duration(seconds: 1), () {
// Unload the current level to ensure all components are cleared
if (currentLevel != null) {
currentLevel!.unload();
}

// Increment the level index and loop back if necessary
currentLevelIndex = (currentLevelIndex + 1) % levelNames.length;

// Load the next level
_loadLevel(currentLevelIndex);

// Remove the fade effect after the transition
Expand Down
Binary file added moonshiner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f893cc1

Please sign in to comment.