Battery Mechanic
Relevant source files
The following files were used as context for generating this wiki page:
The Battery mechanic is a micro-management feature introduced in version 0.32. It provides players with temporary boosts to their money, experience, and time multipliers. Players interact with the battery by hovering over it to charge it. Once fully charged, the multiplier effects are applied. The battery can be upgraded to increase its capacity, charging power, and the potency of its effects.
This system is designed as an active gameplay element that rewards player attention, contrasting with the game's otherwise idle nature. Its state, including level and current charge, is persisted across game sessions.
Sources: index.html:712-720, app/js/battery.js
Core Concepts
The battery system operates on a few fundamental principles:
- Charging: The battery gains charge when the player hovers their mouse over its UI element.
- Discharging: The battery loses charge over time when not being actively charged.
- Effects: When fully charged, the battery applies a significant boost to the player's multipliers. This effect is removed when the battery fully discharges.
- Upgrading: The player can spend in-game money to upgrade the battery, which improves its capacity and charge power.
The entire mechanic is managed by the skidinc.battery object.
Sources: index.html:212-215, app/js/battery.js
Architecture and Data Flow
The battery system is integrated into the main game loop and responds to direct user input. The flow begins with the user hovering over the battery element, which triggers the charging logic. The game's core loop continuously checks the battery's state to apply or remove effects and update the UI.
The following diagram illustrates the high-level data flow of the battery mechanic.
Key Components and Logic
The skidinc.battery Object
All state and configuration for the battery mechanic are stored within the global skidinc.battery object.
| Property | Type | Description |
|---|---|---|
level | Number | The current level of the battery. |
time | Number | The current charge amount. |
maxTime | Number | The maximum charge capacity. |
chargePower | Number | The amount of charge gained per tick while hovering. |
upgradePrice | Number | The cost to upgrade to the next level. |
isCharging | Boolean | Flag indicating if the user is currently hovering over the battery. |
isCharged | Boolean | Flag indicating if the battery is fully charged. |
effect | Object | Contains the multiplier values to be applied when charged. |
Sources: app/js/battery.js:3-13
Game Loop Integration
The battery's state is updated on every game tick via the main game loop.
Sources: app/js/core.js, [app/js/battery.js]
The skidinc.core.loop() function, which runs every 50ms (20 FPS), calls skidinc.battery.core(). This function is responsible for:
- Checking if the battery is not being charged (
isChargingisfalse). - If not charging, it decrements the
timeproperty, simulating discharge. - If the
timedrops to zero, it callsremoveEffect()to remove the active multipliers. - Finally, it calls
skidinc.battery.stats()to refresh the UI with the current values.
Sources: app/js/core.js:11, 25, app/js/battery.js:15-32
Charging Mechanism
Charging is initiated by user interaction with the UI.
- When the user's mouse enters the
#batterydiv, an event listener setsisChargingtotrueand callscharge(). - The
charge()function increasesskidinc.battery.timebyskidinc.battery.chargePower. - If the charge (
time) meets or exceeds the capacity (maxTime), theisChargedflag is set totrueand theeffect()function is called to apply multipliers. - When the mouse leaves the div,
isChargingis set tofalse, and the battery begins to discharge in the main game loop.
Sources: app/js/battery.js:34-53, 103-109
Multiplier Effects
When the battery is fully charged, it applies static multipliers to the player's stats. These effects are defined in the skidinc.battery.effect object.
| Stat | Multiplier |
|---|---|
| Money | 1.25 (25% boost) |
| Exp | 1.25 (25% boost) |
| Time | 1.10 (10% boost) |
The effect() function adds these multipliers to the player's base multipliers, while removeEffect() subtracts them.
Sources: app/js/battery.js:12, 55-75, index.html:718-720
Upgrading the Battery
Players can upgrade the battery using the buy battery command, which is handled by the skidinc.buy module.
The skidinc.battery.upgrade() function is called when a purchase is successful. It increments the battery's level and scales its maxTime, chargePower, and upgradePrice according to fixed formulas.
Sources: app/js/buy.js:10, 25, app/js/battery.js:77-90
User Interface (UI)
The battery UI is located in a dedicated tab within the main stats panel.
Sources: [index.html:71, 211-246]
| Element ID | Purpose | Data Source |
|---|---|---|
#stats-battery-title | The tab link to open the battery panel. | N/A |
#battery-level | The visual representation of the charge level (a colored bar). | skidinc.battery.time / skidinc.battery.maxTime |
#battery #level | Displays the current battery level. | skidinc.battery.level |
#battery #upgrade | Displays the cost of the next upgrade. | skidinc.battery.upgradePrice |
#battery #charge | Displays the current charge and max capacity. | skidinc.battery.time and skidinc.battery.maxTime |
#battery #charge-power | Displays the charge power. | skidinc.battery.chargePower |
#battery #money | Displays the money multiplier effect. | skidinc.battery.effect.money |
#battery #exp | Displays the experience multiplier effect. | skidinc.battery.effect.exp |
#battery #time | Displays the time multiplier effect. | skidinc.battery.effect.time |
The skidinc.battery.stats() function is responsible for updating these UI elements with the latest data from the skidinc.battery object. The width of the #battery-level div is set to (time / maxTime) * 100% to visually represent the current charge.
Sources: index.html:211-246, app/js/battery.js:92-101
State Persistence
The battery's progress is saved as part of the main game save object. Only the essential properties required to restore its state are persisted.
The diagram below shows the battery entity within the larger saved game state.
The skidinc.save.save() function collects the level and time of the battery. When loading, skidinc.save.load() restores these values. The other properties like maxTime and chargePower are recalculated based on the loaded level, ensuring consistency with the game's balancing formulas. This same data structure is used by the Electron save-manager.js for desktop builds.
Sources: app/js/save.js:18, 57, src/renderer/save-manager.js:150-153