Skip to content

Commit

Permalink
added random player generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman_Levin committed Jan 6, 2025
1 parent 025c4d9 commit ee0b49d
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 35 deletions.
10 changes: 6 additions & 4 deletions rpgsaga/saga/sagaCode/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions rpgsaga/saga/sagaCode/heroClasses/archer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Expand All @@ -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();
}
}

Expand Down
10 changes: 5 additions & 5 deletions rpgsaga/saga/sagaCode/heroClasses/knight.ts
Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -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}`);
}

}
Expand Down
10 changes: 5 additions & 5 deletions rpgsaga/saga/sagaCode/heroClasses/mage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
}

Expand Down
14 changes: 9 additions & 5 deletions rpgsaga/saga/sagaCode/playerClass/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
71 changes: 71 additions & 0 deletions rpgsaga/saga/sagaCode/randomGenerator.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 0 additions & 3 deletions rpgsaga/saga/sagaCode/randomizer.ts

This file was deleted.

22 changes: 12 additions & 10 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit ee0b49d

Please sign in to comment.