Skip to content

Commit

Permalink
refactor stats into json database
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonkec committed Aug 16, 2023
1 parent be92ddd commit bec16bd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
11 changes: 11 additions & 0 deletions database/goblinbrawler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"str": 1,
"sta": 1,
"agi": 1,
"dex": 1,
"wis": 1,
"int": 1,
"cha": 1,
"health": 10,
"damage": 2
}
11 changes: 11 additions & 0 deletions database/player.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"str": 1,
"sta": 1,
"agi": 1,
"dex": 1,
"wis": 1,
"int": 1,
"cha": 1,
"health": 100,
"damage": 2
}
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

19 changes: 6 additions & 13 deletions src/ player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DOMCacheGetOrSet } from "./DOMcache.js";
import { Inventory } from "./inventory.js";
//import { Modifier } from "./modifiers.js";
import { updateStatsTable } from "./interface.js";
import { loadJSONFile } from "./utils.js";

export class Player {
constructor() {
Expand All @@ -17,19 +18,11 @@ export class Player {

this.healthBar = DOMCacheGetOrSet("player-health-bar");

this.baseStats = {
str: 1,
sta: 1,
agi: 1,
dex: 1,
wis: 1,
int: 1,
cha: 1,
health: 10,
damage: 1
};

this.cachedStats = { ...this.baseStats };
loadJSONFile('database/player.json').then(data => {
this.baseStats = data;
this.cachedStats = { ...this.baseStats };
});

this.modifiers = [];
}

Expand Down
20 changes: 7 additions & 13 deletions src/enemy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DOMCacheGetOrSet } from "./DOMcache.js";
import { applyDMG } from "./combat.js";
import { loadJSONFile } from "./utils.js";

export class Enemy {
constructor(type, player) {
Expand All @@ -11,20 +12,12 @@ export class Enemy {
this.monsterImage = DOMCacheGetOrSet("monster");
this.healthBar = DOMCacheGetOrSet("enemy-health-bar");

this.baseStats = {
str: 1,
sta: 1,
agi: 1,
dex: 1,
wis: 1,
int: 1,
this.baseStats = [];
loadJSONFile('database/goblinbrawler.json').then(data => {
this.baseStats = data;
this.health = this.calcHP(type, player);
});

cha: 1,
health: 10,
damage: 2
};

this.health = this.calcHP(type, player);

DOMCacheGetOrSet('monster').addEventListener('click', () => applyDMG(this, 1));
DOMCacheGetOrSet('resetMob').addEventListener('click', () => this.resetMob());
Expand All @@ -45,6 +38,7 @@ export class Enemy {
calcHP(type, player){
//swap to this this once I've properly constructed a type system and corresponding database:
//return Math.floor(10 * Math.pow(1.02, type.baseHP * type.kills));
console.log(this.baseStats.health);
let newHP = Math.floor(10 * Math.pow(1.02, this.baseStats.health * player.kills));
this.maxHP = newHP;
return newHP;
Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ export function swapElementsInArray(array, indexA, indexB) {
const newArray = [...array];
[newArray[indexA], newArray[indexB]] = [newArray[indexB], newArray[indexA]];
return newArray;
}

export async function loadJSONFile(filename) {
try {
const response = await fetch(filename);
const jsonData = await response.json();
return jsonData;
} catch (error) {
console.error('Error loading JSON file:', error);
}
}

0 comments on commit bec16bd

Please sign in to comment.