https://github.com/xshadowblade/emath.js
A library for incremental games based on break_eternity.js
https://github.com/xshadowblade/emath.js
game game-development idle-game incremental-game javascript library modular npm savegame tool typescript
Last synced: 10 months ago
JSON representation
A library for incremental games based on break_eternity.js
- Host: GitHub
- URL: https://github.com/xshadowblade/emath.js
- Owner: xShadowBlade
- License: mit
- Created: 2023-09-30T01:19:19.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-11T21:44:54.000Z (about 1 year ago)
- Last Synced: 2025-05-14T15:16:33.025Z (10 months ago)
- Topics: game, game-development, idle-game, incremental-game, javascript, library, modular, npm, savegame, tool, typescript
- Language: TypeScript
- Homepage: https://xshadowblade.github.io/emath.js/
- Size: 12.6 MB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README

eMath.js is a JavaScript library designed to provide tools for incremental game development, built upon break_eternity.js. It provides classes for upgrades, saving/loading, and more!
## Abstract
This project started when I was trying to create my first incremental game. I found it difficult to implement certain systems like upgrades and saving. When I eventually made those systems, I wanted to make a package so I could streamline those tools. After a few months of development, I have finally developed it into a presentable state (I should have started it with v0.1.0 instead of v1.0.0 . . .).
Note: This package uses break_eternity.js by exporting the class `Decimal` directly. You should import the `Decimal` class from `emath.js` instead of `break_eternity.js`. For example,
```js
import { Decimal } from "emath.js";
const num1 = new Decimal(10);
const num2 = num1.add(5);
const num3 = new Decimal(20);
const num4 = Decimal.pow(num1, num3);
```
This was done in order to implement the saving/loading system using [`class-transformer`](https://github.com/typestack/class-transformer) and [`reflect-metadata`](https://github.com/rbuckton/reflect-metadata) (which are the only dependencies of this package).
You cannot import directly from `break_eternity.js` as the package [targets a version of javascript (es5)](https://github.com/Patashu/break_eternity.js/issues/114) that is not supported by `class-transformer` and `reflect-metadata`.
## Example Usage
This is a simple example of how to use the package. It creates a game with a currency called "coins" and an upgrade for that currency. It also includes hotkeys for gaining coins and buying upgrades, as well as saving and loading the game.
> Note: This example uses javascript. It is recommended to use typescript for better type checking.
```js
import { Decimal } from "emath.js";
import { Game } from "emath.js/game";
// For CDN usage:
// const { Decimal, Game } = eMath;
// Initialize game
const coinGame = new Game();
// Create a new currency with upgrades
const coins = coinGame.addCurrency("coins", [
{
id: "upg1Coins", // Unique ID
cost: level => level.mul(10), // Cost of 10 times the level
maxLevel: new Decimal(1000),
effect: (level, upgradeContext, currencyContext) => {
// `level` is the level of the upgrade
// `upgradeContext` is the context of the upgrade (this upgrade)
// `currencyContext` is the context of the currency (coins in this case)
// Access the `boost` object to add a boost
currencyContext.boost.setBoost({
id: "boostUpg1Coins", // Unique ID of the boost
// Effect of the boost, which is additive, 11 times the level of the upgrade
value: n => n.plus(level.mul(11)).sub(1),
});
},
},
// Add more upgrades here ...
]);
// Initialize / Load game
coinGame.init();
coinGame.dataManager.loadData();
// Gain coins
coins.gain();
// Buy (max) upgrades
coins.buyUpgrade("upg1Coins");
// Hotkeys
coinGame.keyManager.addKey([
{
id: "gainCoins",
name: "Gain Coins",
key: "g",
onDownContinuous: () => coins.gain(),
},
{
id: "buyUpgrades",
name: "Buy Upgrades",
key: "b",
onDownContinuous: () => coins.buyUpgrade("upg1Coins"),
},
]);
// Saving and Loading
window.addEventListener("beforeunload", () => {
coinGame.dataManager.saveData();
});
coinGame.eventManager.setEvent("autoSave", "interval", 30000, () => {
coinGame.dataManager.saveData();
console.log("Auto Saved!");
});
```
## Installation
### Install via npm (recommended)
```bash
npm install emath.js
```
### Include using CDN
Include the following script in your HTML file. It will set the global variable `eMath` to the `window` object.
> Note: There is no development build for CDN, as it is used for nodejs.
> Replace `@latest` with the version you want to use. (e.g. `@8.3.0`), or use `@latest` for the latest version.
#### emath.js
```html
```
#### emath.js/game
Also includes `"emath.js"` so you only need to include this script.
```html
```
#### emath.js/presets
Sets the global variable `eMathPresets` to the `window` object.
> Note: This does not include either `"emath.js"` or `"emath.js/game"`.
```html
```
---
Check out the [documentation](https://xshadowblade.github.io/emath.js/)!