Skip to content

Commit

Permalink
feat: implement upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
golota60 committed May 18, 2022
1 parent ddb4135 commit a9e49a9
Show file tree
Hide file tree
Showing 4 changed files with 356 additions and 36 deletions.
30 changes: 25 additions & 5 deletions src/helpers/gameplay.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import chalk, { ChalkInstance } from 'chalk';
import { initialAbilities, initialInfra } from './initialValues.js';
import { initialInfra, upgradesInOrder } from './initialValues.js';

//TODO: convert those to funs which load save states when that is implemented
export interface Ability {
export interface Upgrade {
level: number;
desc: string;
}
export type AbilitiesState = Record<string, Ability>;
export type UpgradesState = Record<string, Upgrade>;

export interface Infrastructure {
level: number;
Expand All @@ -18,6 +18,7 @@ export interface Infrastructure {
getColor: () => ChalkInstance;
alias: string; // How the value should be displayed
buyKey: number;
multiplier: number;

//todo: loadcost, loadmoneypersec?
}
Expand All @@ -29,18 +30,22 @@ export interface GameState {
version: string | undefined;
mode: 'main' | 'shop';
infrastructure: InfrastructureState;
abilities: AbilitiesState;
money: number;
bulkMode: BulkModeType;
clickPower: number;
moneyPerSecUpgrade: number;
upgradesBought: Array<any>;
}

export const getFreshGameState = (): GameState => ({
version: process.env.npm_package_version,
mode: 'main',
money: 0,
infrastructure: initialInfra,
abilities: initialAbilities,
bulkMode: 1,
clickPower: 1,
moneyPerSecUpgrade: 1,
upgradesBought: [],
});

// This can't be a getter cause we need the exact reference
Expand All @@ -63,6 +68,21 @@ export const calcMoneyPerSec = () => {
return Math.floor(sum);
};

export const getNextUpgrade = () => {
return upgradesInOrder?.[gameState.upgradesBought.length] || undefined;
};
export const buyUpgrade = () => {
const next = getNextUpgrade();
if (next) {
updateGameState({
...gameState,
upgradesBought: [...gameState.upgradesBought, next],
money: gameState.money - next.price,
});
next.action();
}
};

export const swapBulkMode = () => {
updateGameState({
...gameState,
Expand Down

0 comments on commit a9e49a9

Please sign in to comment.