Skip to content

Commit

Permalink
feat: Refatora NPC classes. Existem bugs agora...
Browse files Browse the repository at this point in the history
  • Loading branch information
devpedrofurquim committed Oct 29, 2024
1 parent fff9933 commit 9953526
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 281 deletions.
61 changes: 61 additions & 0 deletions lib/components/baker.dart
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();
}
}
}
73 changes: 73 additions & 0 deletions lib/components/dialogue.dart
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();
});
}
}
61 changes: 61 additions & 0 deletions lib/components/journalGuy.dart
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();
}
}
}
55 changes: 12 additions & 43 deletions lib/components/level.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame_tiled/flame_tiled.dart';
import 'package:moonshiner_game/components/background_tile.dart';
import 'package:moonshiner_game/components/baker.dart';
import 'package:moonshiner_game/components/collision_block.dart';
import 'package:moonshiner_game/components/journalGuy.dart';
import 'package:moonshiner_game/components/oldLady.dart';
import 'package:moonshiner_game/components/player.dart';
import 'package:moonshiner_game/components/priest.dart';
import 'package:moonshiner_game/moonshiner.dart';
import 'npc.dart';
import 'door.dart';
Expand All @@ -15,7 +19,7 @@ import 'wife.dart';
class Level extends World with HasGameRef<Moonshiner> {
final String levelName;
final Player player;
List<NPC> npcs = [];
List<AbstractNPC> npcs = [];
late TiledComponent level;
List<CollisionBlock> collisionBlocks = [];

Expand Down Expand Up @@ -97,55 +101,20 @@ class Level extends World with HasGameRef<Moonshiner> {
add(wife);
break;
case 'NPC':
NPC npc;
AbstractNPC npc;
final npcType = spawnPoint.name;

// Instantiate the specific NPC based on the name
if (npcType == 'Priest') {
npc = NPC(
npcCharacter: 'Priest',
dialogues: [
"The Lord sees all.",
"Bless you, my child.",
"Evil lurks in strange places."
],
position: position,
);
npc = Priest(position: position);
} else if (npcType == 'Baker') {
npc = NPC(
npcCharacter: 'Baker',
dialogues: [
"Fresh bread today!",
"A hard day’s work, but worth it.",
"Care for a slice?"
],
position: position,
);
npc = Baker(position: position);
} else if (npcType == 'Old Lady') {
npc = NPC(
npcCharacter: 'Old Lady',
dialogues: [
"In my day, things were different.",
"I’ve lived here my whole life.",
"Be careful, dear."
],
position: position,
);
npc = OldLady(position: position);
} else if (npcType == 'Journal Guy') {
npc = NPC(
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,
);
npc = JournalGuy(position: position);
} else {
npc = NPC(
npcCharacter: 'Wanderer',
dialogues: ["I'm just here, wandering around."],
position: position,
);
npc = Baker(position: position); // Default NPC
}

npc.collisionBlocks = collisionBlocks;
Expand Down
Loading

0 comments on commit 9953526

Please sign in to comment.