Skip to content

Commit

Permalink
Organize code into labelled sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
blujai831 committed Oct 28, 2024
1 parent 00e14bd commit 0b704a3
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "./style.css";

// Constants

const GAME_TITLE = "Feed the Fire";
const GAME_PREMISE = `
You have discovered a new way to generate energy
Expand All @@ -18,19 +20,6 @@ const BASIC_ACTION_DESCRIPTION = "🔥 Makes things hotter.";
const ITEM_COST_GROWTH_BASE = 1.15;
const DECIMAL_PRECISION = 1;

function arrayToRecord<K extends string | symbol | number, V, T>(
ts: readonly T[], k: (t: T) => K, v: (t: T) => V
) {
return Object.fromEntries(ts.map(t => [k(t), v(t)])) as Record<K, V>;
}

interface Item {
name: ItemName,
cost: number,
rate: number,
description: string
}

const availableItems = [
{name: 'Cellphone', cost: 10, rate: 0.1, description: "&#x1F4F1; " +
"Hello? Hello? Can you hear me?"},
Expand All @@ -44,12 +33,25 @@ const availableItems = [
"Invented to... hm... No, wait, this hasn't been invented yet."}
] as const;

type ItemName = typeof availableItems[number]['name'];
// Interfaces and utility functions

function getItemRealCost(item: Item, alreadyHave: number): number {
return item.cost*(ITEM_COST_GROWTH_BASE**alreadyHave);
function arrayToRecord<K extends string | symbol | number, V, T>(
ts: readonly T[], k: (t: T) => K, v: (t: T) => V
) {
return Object.fromEntries(ts.map(t => [k(t), v(t)])) as Record<K, V>;
}

interface Item {
name: ItemName,
cost: number,
rate: number,
description: string
}

type ItemName = typeof availableItems[number]['name'];

// Game logic

let gameState = {
globalWarmingIndex: 0,
globalWarmingRate: 0,
Expand All @@ -60,6 +62,10 @@ function doBasicAction(state: typeof gameState) {
state.globalWarmingIndex += 1;
}

function getItemRealCost(item: Item, alreadyHave: number): number {
return item.cost*(ITEM_COST_GROWTH_BASE**alreadyHave);
}

function canAffordItem(state: typeof gameState, item: Item): boolean {
return state.globalWarmingIndex >=
getItemRealCost(item, state.itemQuantities[item.name]);
Expand All @@ -79,6 +85,8 @@ function tickGameState(state: typeof gameState, interval: number) {
state.globalWarmingIndex += state.globalWarmingRate*interval/MSEC_PER_SEC;
}

// UI

document.title = GAME_TITLE;
const app: HTMLDivElement = document.querySelector('#app')!;

Expand Down

0 comments on commit 0b704a3

Please sign in to comment.