Skip to content

Commit

Permalink
partial effects framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonkec committed Aug 20, 2023
1 parent ad044c2 commit d07e6f9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 34 deletions.
11 changes: 4 additions & 7 deletions database/actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,15 @@
"utility": [
{
"id": 7,
"title": "Haste",
"description": "Greatly increase your agility for a short duration.",
"effectDuration": 5,
"statType": "agi",
"statBoost": 5
"title": "Berserker's Strength",
"description": "Greatly increase your strength for a short duration.",
"appliedEffect": 1
},
{
"id": 8,
"title": "Clarity",
"description": "Temporarily boost the rate of your mana regeration.",
"effectDuration": 3,
"manaAmount": 1
"appliedEffect": 2
}
]
}
29 changes: 29 additions & 0 deletions database/effects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"id": 1,
"type": "buff",
"name": "Berserker's Strength",
"attribute": "str",
"value": 5,
"duration": 1,
"isBuff": true
},
{
"id": 2,
"type": "buff",
"name": "Clarity",
"attribute": "mana",
"value": 2,
"duration": 60,
"isBuff": true
},
{
"id": 3,
"type": "debuff",
"name": "Poison",
"attribute": "health",
"value": -1,
"duration": 5,
"isBuff": false
}
]
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/combat.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function calculateMitigation(damage, source, target){
}

//deprecating this asap and moving to processAction once enemy abilities are implemented
export function calculateAttackDMG(source, target) {
export function calculateAttackDMG(source) {
let dmgAMT = source.getStat('damage');
return dmgAMT;
}
Expand Down
27 changes: 27 additions & 0 deletions src/effects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class Effect {
constructor(id, type, name, attribute, value, duration, isBuff) {
this.id = id;
this.type = type;
this.name = name;
this.attribute = attribute;
this.value = value;
this.duration = duration;
this.isBuff = isBuff;
}
}

export function loadEffectsFromJSON () {
const effects = [];

fetch('database/effects.json')
.then(response => response.json())
.then(effectsArray => {
effectsArray.forEach(effectData => {
const { id, type, name, attribute, value, duration, isBuff } = effectData;
const effect = new Effect(id, type, name, attribute, value, duration, isBuff);
effects.push(effect);
});
console.log(effects);
return effects;
});
}
4 changes: 4 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Player } from "./player.js";
import { Shop } from './shop.js';
import { populateDOMEventCache, updateAllOfTheThings } from "./events.js";
import { ActionQueueManager } from './actions.js';
import { loadEffectsFromJSON } from "./effects.js";

// references necessary for function calls from modules
window.openTab = openTab;
Expand All @@ -35,13 +36,16 @@ const states = {
death: () => new DeathState(enemy, player, actionQueueManager)
};

const CombatEffects = loadEffectsFromJSON();

// initialize game objects
let player = new Player();
let enemy = new Enemy(50, player);
let shop = new Shop(player);
const actionQueueManager = new ActionQueueManager();
const stateMachine = new StateMachine(states, changeStateButton);


// run initialization events
populateDOMEventCache(player);
actionQueueManager.populateActionCards();
Expand Down
46 changes: 21 additions & 25 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Player {
this.coins = 1000;
this.coinGens = 0;
this.kills = 0;

this.inventory = new Inventory(this);
this.inventoryStats = {};

Expand All @@ -26,39 +27,34 @@ export class Player {
this.cachedStats = { ...this.baseStats };
});

this.modifiers = [];
this.activeEffects = [];
}

// addModifier(modifier) {
// this.modifiers.push(modifier);
// this.recalculateCachedStats(Object.keys(modifier.statsAffected));
// }
//undecided on how to structure effects, going with a record+int approach for now
applyEffect(effect) {
this.activeEffects.push({
effect: effect,
remainingDuration: effect.duration
});
}

// removeModifier(modifierId) {
// const index = this.modifiers.findIndex(mod => mod.id === modifierId);
// if (index !== -1) {
// const [removedModifier] = this.modifiers.splice(index, 1);
// this.recalculateCachedStats(Object.keys(removedModifier.statsAffected));
// }
// }
updateEffects() {
for (let i = this.activeEffects.length - 1; i >= 0; i--) {
const activeEffect = this.activeEffects[i];
activeEffect.remainingDuration--;

if (activeEffect.remainingDuration <= 0) {
//remove effect here, but does this cancel it midway in the current tick?
//this might remove it and steal the benefit/penalty of the final tick - TBD
this.activeEffects.splice(i, 1);
}
}
}

getStat(statName) {
return this.cachedStats[statName];
}

// recalculateCachedStats(statsAffectedArray) {
// for (let stat of statsAffectedArray) {
// this.cachedStats[stat] = this.baseStats[stat];

// for (let modifier of this.modifiers) {
// if (modifier.statsAffected[stat] && (!modifier.condition || modifier.condition())) {
// this.cachedStats[stat] += modifier.statsAffected[stat];
// }
// }
// }
// }


recalculateCachedStats(statsArray) {
for (const stat in statsArray) {
this.cachedStats[stat] = this.baseStats[stat] + this.inventoryStats[stat];
Expand Down

0 comments on commit d07e6f9

Please sign in to comment.