-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonBattle.ts
More file actions
99 lines (87 loc) · 2.85 KB
/
PokemonBattle.ts
File metadata and controls
99 lines (87 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { ICommand, Attack } from "./types";
import { AttackCommand, UseItemCommand, RunAwayCommand } from "./commandList";
import { BattleService } from "./services/BattleService";
import { ItemService } from "./services/ItemService";
import { PokemonService } from "./services/PokemonService";
export class PokemonBattle {
constructor() {}
private battleService: BattleService = new BattleService();
private itemService: ItemService = new ItemService();
private pokemonService: PokemonService = new PokemonService();
private rl = require('readline-sync');
/*
*
* Starts a Pokemon Battle instance
*
*/
start_battle() {
console.log(`\n\nA wild ${this.battleService.opponent.name} has appeared...\n`)
while (true) {
this.battleService.isBattleActive.subscribe((active: boolean) => {
if (!active) {
process.exit(0);
}
})
this.printGenericOptions();
this.selectOption();
}
}
/*
*
* Prints the console menu options
*
*/
private printGenericOptions(): void {
console.log('\nWhat will you do? (1 - 3):\n');
console.log('1. Attack');
console.log('2. Use an Item');
console.log('3. Run\n');
}
/*
*
* Handles user input and is left completely out of the loop when it comes to handling the command logic
* This command pattern is useful here because it allows for complete decoupling of our logic
*
*/
private selectOption() {
let sel = this.rl.question('Your selection: ');
switch (sel) {
case '1':
console.log(`\n\nChoose an attack (1-${this.pokemonService.attackList.length}):\n`);
this.pokemonService.attackList.forEach((attack: Attack, i: number) => {
console.log(`${i+1}. ${attack.name}`)
})
console.log('\n\n')
let attackChoice = this.rl.question('Your Attack: ');
console.clear();
this.executeCommand(new AttackCommand(this.battleService), this.pokemonService.attackList[attackChoice-1])
break;
case '2':
console.log(`\nChoose an item (1-${this.itemService.itemsList.length}):\n`);
this.itemService.itemsList.forEach((item: string, i: number) => {
console.log(`${i+1}. ${item}`)
})
console.log('\n\n')
let itemChoice = this.rl.question('You chose: ');
console.clear();
this.executeCommand(new UseItemCommand(this.itemService), this.itemService.itemsList[itemChoice-1])
break;
case '3':
console.clear();
this.executeCommand(new RunAwayCommand(this.battleService), Math.random());
break;
default:
console.log('Unknown Option!\n\n')
process.exit(0);
};
}
/*
*
* This class has no idea how/what to do with a given command other than
* to call .execute() on it.
*
*/
executeCommand(command: ICommand, args: any) {
command.execute(args);
}
}