From ee0b49df49149bd26b6f79b8515c6baa3c16b797 Mon Sep 17 00:00:00 2001 From: Roman_Levin Date: Mon, 6 Jan 2025 20:18:14 +0300 Subject: [PATCH] added random player generator --- rpgsaga/saga/sagaCode/game.ts | 10 +-- rpgsaga/saga/sagaCode/heroClasses/archer.ts | 6 +- rpgsaga/saga/sagaCode/heroClasses/knight.ts | 10 +-- rpgsaga/saga/sagaCode/heroClasses/mage.ts | 10 +-- rpgsaga/saga/sagaCode/playerClass/player.ts | 14 ++-- rpgsaga/saga/sagaCode/randomGenerator.ts | 71 +++++++++++++++++++++ rpgsaga/saga/sagaCode/randomizer.ts | 3 - rpgsaga/saga/src/index.ts | 22 ++++--- 8 files changed, 111 insertions(+), 35 deletions(-) create mode 100644 rpgsaga/saga/sagaCode/randomGenerator.ts delete mode 100644 rpgsaga/saga/sagaCode/randomizer.ts diff --git a/rpgsaga/saga/sagaCode/game.ts b/rpgsaga/saga/sagaCode/game.ts index 4628a181e..4e5820107 100644 --- a/rpgsaga/saga/sagaCode/game.ts +++ b/rpgsaga/saga/sagaCode/game.ts @@ -2,7 +2,7 @@ import { knight } from "./heroClasses/knight"; import { archer } from "./heroClasses/archer"; import { Player } from "./playerClass/player"; import { mage } from "./heroClasses/mage"; -import { randomNumber } from "./randomizer"; +import { randomNumber } from "./randomGenerator"; import { Effect } from "./effectOfDamage/effect"; export class game { @@ -64,8 +64,9 @@ export class game { console.log(`${player.name} выбирает атаковать!`); player.attack(target); } else { - if (player.statusEffect === true ) { + if (player.statusEffect === 0 ) { const target = this.getRandomTarget(player); + console.log(`${player.name} выбирает атаковать!`); player.attack(target); } else { const target = this.getRandomTarget(player); @@ -80,8 +81,9 @@ export class game { console.log(`${player.name} выбирает атаковать!`); player.attack(target); } else { - if (player.statusEffect === true ) { + if (player.statusEffect === 0 ) { const target = this.getRandomTarget(player); + console.log(`${player.name} выбирает атаковать!`); player.attack(target); } else { const target = this.getRandomTarget(player); @@ -91,7 +93,7 @@ export class game { } } } else if (player instanceof knight) { - if (randomNumber(1,5) !== 5) { + if (randomNumber(1,4) !== 4) { const target = this.getRandomTarget(player); console.log(`${player.name} выбирает атаковать!`); player.attack(target); diff --git a/rpgsaga/saga/sagaCode/heroClasses/archer.ts b/rpgsaga/saga/sagaCode/heroClasses/archer.ts index 38cbe1c38..9ce26656a 100644 --- a/rpgsaga/saga/sagaCode/heroClasses/archer.ts +++ b/rpgsaga/saga/sagaCode/heroClasses/archer.ts @@ -3,7 +3,7 @@ import { Weapon } from "../weapon"; import { Effect } from "../effectOfDamage/effect"; export class archer extends Player { - constructor(name:string,health:number,weapon:Weapon, statusEffect: boolean) { + constructor(name:string,health:number,weapon:Weapon, statusEffect: number) { super(name, health,weapon, statusEffect); } @@ -16,14 +16,14 @@ export class archer extends Player { } useArrowEffect(target: Player, effect: Effect) { - if (this.statusEffect) { + if (this.statusEffect == 0) { console.log(`${this.name} уже использовал способность ранее!`); return; } else { this.activesEffect = new Effect(effect.type, effect.damagePerTurn, effect.duration, target); console.log(`${this.name} использует ${this.activeEffect.type}`); effect.applyEffect(target); - this.statusOfEffect = true; + this.statusEffectDown(); } } diff --git a/rpgsaga/saga/sagaCode/heroClasses/knight.ts b/rpgsaga/saga/sagaCode/heroClasses/knight.ts index c230f3990..cbf65ea80 100644 --- a/rpgsaga/saga/sagaCode/heroClasses/knight.ts +++ b/rpgsaga/saga/sagaCode/heroClasses/knight.ts @@ -1,10 +1,10 @@ import { Player } from "../playerClass/player"; import { Weapon } from "../weapon"; import { Effect } from "../effectOfDamage/effect"; -import { randomNumber } from "../randomizer"; +import { randomNumber } from "../randomGenerator"; export class knight extends Player { - constructor(name:string,health:number,weapon:Weapon,statusEffect: boolean) { + constructor(name:string, health:number, weapon:Weapon, statusEffect: number) { super(name, health, weapon, statusEffect); } @@ -27,14 +27,14 @@ export class knight extends Player { } public useHealEffect() { - if (this.statusEffect) { + if (this.statusEffect == 0) { console.log(`${this.name} уже использовал способность лечения раннее!`); return; } else { const healAmount = 15; this.health += healAmount; - console.log(`${this.name} использует способность лечения и восстанавливает ${healAmount} здоровья. Текущее здоровье: ${this.health}`); - this.statusOfEffect = true; + this.statusEffectDown(); + console.log(`${this.name} использует способность лечения и восстанавливает ${healAmount} здоровья. Текущее здоровье: ${this.health}. Оставшиеся применения: ${this.statusEffect}`); } } diff --git a/rpgsaga/saga/sagaCode/heroClasses/mage.ts b/rpgsaga/saga/sagaCode/heroClasses/mage.ts index a1494cfd5..3f0c3f08b 100644 --- a/rpgsaga/saga/sagaCode/heroClasses/mage.ts +++ b/rpgsaga/saga/sagaCode/heroClasses/mage.ts @@ -3,8 +3,8 @@ import { Player } from "../playerClass/player"; import { Weapon } from "../weapon"; export class mage extends Player { - constructor(name:string,health:number,weapon:Weapon, statusEffect: boolean) { - super(name, health,weapon, statusEffect); + constructor(name:string,health:number,weapon:Weapon, statusEffect: number) { + super(name, health, weapon, statusEffect); } public attack(opponent: Player): string { @@ -15,14 +15,14 @@ export class mage extends Player { } public useFireball(target: Player, effect: Effect) { - if (this.statusEffect) { - console.log(`${this.name} уже использовал ${this.activeEffect.type} раннее`); + if (this.statusEffect == 0) { + console.log(`${this.name} уже использовал Fireball раннее`); return; } else { this.activesEffect = new Effect(effect.type, effect.damagePerTurn, effect.duration, target); console.log(`${this.name} использует ${this.activeEffect.type}`); effect.applyEffect(target); - this.statusOfEffect = true; + this.statusEffectDown(); } } diff --git a/rpgsaga/saga/sagaCode/playerClass/player.ts b/rpgsaga/saga/sagaCode/playerClass/player.ts index e342fa32b..01449651c 100644 --- a/rpgsaga/saga/sagaCode/playerClass/player.ts +++ b/rpgsaga/saga/sagaCode/playerClass/player.ts @@ -5,10 +5,10 @@ export abstract class Player { private _name: string; private _health: number; private _weapon: Weapon; - private _statusEffect: boolean; + private _statusEffect: number; private _activeEffect: Effect; - constructor(name:string, health: number,weapon: Weapon, statusEffect: boolean) { + constructor(name:string, health: number,weapon: Weapon, statusEffect: number) { this._name = name; this._health = health; this._weapon = weapon; @@ -26,18 +26,22 @@ export abstract class Player { this._activeEffect = effect; } - public get statusEffect(): boolean { + public get statusEffect(): number { return this._statusEffect; } - public set statusOfEffect(effectStatus: boolean) { - if (this._statusEffect === true){ + public set statusOfEffect(effectStatus: number) { + if (this._statusEffect == 0){ console.log("Эффект уже был наложен"); } else { this._statusEffect = effectStatus } } + public statusEffectDown(): void { + this._statusEffect--; + } + public get name(): string { return this._name; diff --git a/rpgsaga/saga/sagaCode/randomGenerator.ts b/rpgsaga/saga/sagaCode/randomGenerator.ts new file mode 100644 index 000000000..448787e2d --- /dev/null +++ b/rpgsaga/saga/sagaCode/randomGenerator.ts @@ -0,0 +1,71 @@ +import { knight } from "./heroClasses/knight"; +import { archer } from "./heroClasses/archer"; +import { mage } from "./heroClasses/mage"; +import { Weapon } from "./weapon"; +import { Player } from "./playerClass/player"; + + +export function randomNumber(min: number, max: number): number { + return Math.floor(Math.random() * (max - min + 1)) + min; + } + + export class characterGenerator { + private knightNames: string[] = ["ChaosKnight", "DragonKnight", "Fighter", "GorillaWarrior", "JohnWick"]; + private archerNames: string[] = ["Legolas", "Robin", "SokolEye", "ArrowMan", "Makus"]; + private mageNames: string[] = ["DarkMage", "Gandalf", "Dumbledore", "SaintMage", "Wizard"]; + + private knightWeapons: Weapon[] = [ + new Weapon("Sword", 30), + new Weapon("Axe", 35), + new Weapon("Rapier", 27) + ]; + + private archerWeapons: Weapon[] = [ + new Weapon("Longbow", 25), + new Weapon("Crossbow", 28), + new Weapon("ClassicBow", 20) + ]; + + private mageWeapons: Weapon[] = [ + new Weapon("Staff", 12), + new Weapon("Wand", 17), + new Weapon("MagickStick", 22) + ]; + + createRandomPlayer(): Player { + const playerType = randomNumber(1, 3); // 1 - Knight, 2 - Archer, 3 - Mage + switch (playerType) { + case 1: { + const name = this.knightNames[randomNumber(0, this.knightNames.length - 1)]; + const weapon = this.knightWeapons[randomNumber(0, this.knightWeapons.length - 1)]; + const health = randomNumber(100, 200); + const statusEffect = 2; + return new knight(name, health, weapon, statusEffect); + } + case 2: { + const name = this.archerNames[randomNumber(0, this.archerNames.length - 1)]; + const weapon = this.archerWeapons[randomNumber(0, this.archerWeapons.length - 1)]; + const health = randomNumber(80, 150); + const statusEffect = 1; + return new archer(name, health, weapon, statusEffect); + } + case 3: { + const name = this.mageNames[randomNumber(0, this.mageNames.length - 1)]; + const weapon = this.mageWeapons[randomNumber(0, this.mageWeapons.length - 1)]; + const health = randomNumber(60, 120); + const statusEffect = 1; + return new mage(name, health, weapon, statusEffect); + } + default: + throw new Error("Invalid player type generated."); + } + } + + initializePlayers(count: number): (Player)[] { + const players: (Player)[] = []; + for (let i = 0; i < count; i++) { + players.push(this.createRandomPlayer()); + } + return players; + } +} \ No newline at end of file diff --git a/rpgsaga/saga/sagaCode/randomizer.ts b/rpgsaga/saga/sagaCode/randomizer.ts deleted file mode 100644 index 178de3a0b..000000000 --- a/rpgsaga/saga/sagaCode/randomizer.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function randomNumber(min: number, max: number): number { - return Math.floor(Math.random() * (max - min + 1)) + min; - } \ No newline at end of file diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 58a9c6397..6d3651d3a 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,15 +1,17 @@ -import { knight } from '../sagaCode/heroClasses/knight'; -import { archer } from '../sagaCode/heroClasses/archer'; -import { Weapons } from '../sagaCode/constElements'; -import { mage } from '../sagaCode/heroClasses/mage'; import { game } from '../sagaCode/game'; -import { randomNumber } from '../sagaCode/randomizer'; +import { characterGenerator } from '../sagaCode/randomGenerator'; -const player_1 = new knight('Makus', 150,Weapons.sword,false); -const player_2 = new archer('Леголас', 120,Weapons.icedBow,false); -const player_3 = new mage('Колдун', 110,Weapons.wizardStuff,false); +// const player_1 = new knight('Makus', 150, Weapons.sword, 2); +// const player_2 = new archer('Леголас', 120, Weapons.icedBow, 1); +// const player_3 = new mage('Колдун', 110, Weapons.wizardStuff, 1); +const randomPlayer = new characterGenerator(); +const players = randomPlayer.initializePlayers(2); +players.forEach(player => { + console.log( + `Игрок: ${player.name}, Класс: ${player.constructor.name}, Здоровье: ${player.health}, Оружие: ${player.weapon.type} (${player.weapon.damageAmount} урона), Кол-во эффектов за игру: ${player.statusEffect}`, + ); +}); - -const fight = new game([player_2, player_3]); +const fight = new game(players); fight.start();