-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refatora NPC classes. Existem bugs agora...
- Loading branch information
1 parent
fff9933
commit 9953526
Showing
9 changed files
with
382 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'dart:math'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:moonshiner_game/components/dialogue.dart'; | ||
import 'package:moonshiner_game/components/npc.dart'; | ||
|
||
class Baker extends AbstractNPC { | ||
Baker({required Vector2 position}) | ||
: super( | ||
npcCharacter: 'Baker', | ||
dialogues: [ | ||
"Fresh bread today!", | ||
"A hard day’s work, but worth it.", | ||
"Care for a slice?", | ||
], | ||
position: position, | ||
); | ||
|
||
@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; | ||
}); | ||
} | ||
|
||
@override | ||
void updateMovement(double dt) { | ||
if (movingLeft) { | ||
velocity.x = -moveSpeed; | ||
if (scale.x > 0) flipHorizontallyAroundCenter(); | ||
} else { | ||
velocity.x = moveSpeed; | ||
if (scale.x < 0) flipHorizontallyAroundCenter(); | ||
} | ||
|
||
if (Random().nextDouble() < 0.02) { | ||
movingLeft = !movingLeft; | ||
velocity = Vector2.zero(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class NPCDialogueComponent extends PositionComponent with HasGameRef { | ||
final String message; | ||
final Color npcColor; // Unique color representing the NPC | ||
late TextComponent textComponent; | ||
late RectangleComponent backgroundBox; | ||
late RectangleComponent npcIndicatorBox; | ||
|
||
NPCDialogueComponent({ | ||
required this.message, | ||
required this.npcColor, | ||
}) { | ||
priority = 100; // Display above other components | ||
} | ||
|
||
@override | ||
Future<void> onLoad() async { | ||
await super.onLoad(); | ||
|
||
// Style the dialogue text | ||
textComponent = TextComponent( | ||
text: message, | ||
textRenderer: TextPaint( | ||
style: const TextStyle( | ||
color: Colors.white, | ||
fontSize: 24.0, // Increased font size | ||
fontFamily: 'Arial', | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
); | ||
|
||
// Background box for the text with additional padding | ||
backgroundBox = RectangleComponent( | ||
size: Vector2(textComponent.width + 70, textComponent.height + 40), | ||
paint: Paint()..color = Colors.black.withOpacity(0.85), | ||
) | ||
..position = | ||
Vector2(30, 10) // Position with padding for the NPC indicator | ||
..anchor = Anchor.topLeft; | ||
|
||
// NPC color indicator box | ||
npcIndicatorBox = RectangleComponent( | ||
size: Vector2(30, textComponent.height + 40), // Increased size | ||
paint: Paint()..color = npcColor, | ||
) | ||
..position = Vector2(0, 10) // Positioned on the left with padding | ||
..anchor = Anchor.topLeft; | ||
|
||
// Add components to the dialogue component | ||
add(npcIndicatorBox); | ||
add(backgroundBox); | ||
add(textComponent); | ||
|
||
// Position the dialogue component at the bottom of the screen | ||
position = Vector2( | ||
gameRef.size.x / 2 - backgroundBox.width / 2, | ||
gameRef.size.y - backgroundBox.height - 80, // Move it up a bit | ||
); | ||
|
||
// Center the text inside the background box | ||
textComponent.position = backgroundBox.size / 2 - textComponent.size / 2; | ||
} | ||
|
||
void showWithTimeout(Duration duration) { | ||
// Show the dialogue and remove it after the specified timeout | ||
Future.delayed(duration, () { | ||
removeFromParent(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'dart:math'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:moonshiner_game/components/dialogue.dart'; | ||
import 'package:moonshiner_game/components/npc.dart'; | ||
|
||
class JournalGuy extends AbstractNPC { | ||
JournalGuy({required Vector2 position}) | ||
: super( | ||
npcCharacter: 'Journal Guy', | ||
dialogues: [ | ||
"News of the day! Get your news here!", | ||
"Rumor has it, strange things are happening.", | ||
"Can’t keep secrets in this town.", | ||
], | ||
position: position, | ||
); | ||
|
||
@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; | ||
}); | ||
} | ||
|
||
@override | ||
void updateMovement(double dt) { | ||
if (movingLeft) { | ||
velocity.x = moveSpeed * 1.5; | ||
if (scale.x < 0) flipHorizontallyAroundCenter(); | ||
} else { | ||
velocity.x = -(moveSpeed * 1.5); | ||
if (scale.x > 0) flipHorizontallyAroundCenter(); | ||
} | ||
|
||
if (Random().nextDouble() < 0.05) { | ||
movingLeft = !movingLeft; | ||
velocity = Vector2.zero(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.