Skip to content

Commit 325278d

Browse files
committed
Eliminate game variable
1 parent 4159836 commit 325278d

File tree

7 files changed

+44
-34
lines changed

7 files changed

+44
-34
lines changed

src/ts/buildings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ClickerCookie from "./clickercookie.js";
22
import { capitalize, commaify, clamp } from "./helper.js";
3-
import { game } from "./main.js";
3+
import { Game } from "./main.js";
44
import { hideTooltip } from "./tooltip.js";
55

66
export interface BuildingData {
@@ -137,7 +137,7 @@ export class Building {
137137

138138
// clamping allows between 0 and the height of the window minus the height of the box. also add one from the height of the box because it doesn't work correctly normally, idk why
139139
//! this uses game but shouldn't with the tooltip refactor
140-
tooltip.style.top = clamp(game.mousePos.y - tooltip.offsetHeight/2,0,window.innerHeight-(tooltip.offsetHeight + 1))+"px";
140+
tooltip.style.top = clamp(Game.getMousePosition().y - tooltip.offsetHeight/2,0,window.innerHeight-(tooltip.offsetHeight + 1))+"px";
141141
tooltip.style.right = "346px";
142142
tooltip.style.left = "auto"; // when tooltip is a statistic it sets the left property because it won't work correctly with right, this resets that
143143

src/ts/clickercookie.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Building, BuildingData } from "./buildings.js";
22
import { commaify, makeSlightlyImperfectFloatNice } from "./helper.js";
3-
import { game, Game } from "./main.js";
3+
import { Game } from "./main.js";
44
import { Mod } from "./mods.js";
55
import { Upgrade, UpgradeData, UpgradeSave } from "./upgrades.js";
66
import { SimplePopup } from "./popup.js";
@@ -668,7 +668,7 @@ export default class ClickerCookie extends Mod {
668668
document.getElementById("cookiesStat").innerText = `${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().namePlural}: ${makeSlightlyImperfectFloatNice(this.cookies)}`;
669669
document.getElementById("allTimeCookies").innerText = `All Time ${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().namePlural}: ${makeSlightlyImperfectFloatNice(this.totalCookies)}`;
670670
document.getElementById("cookiesPerSecondStat").innerText = `${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().namePlural} Per Second: ${makeSlightlyImperfectFloatNice(this.cookiesPerSecond)}`;
671-
document.getElementById("buildingsOwnedStat").innerText = `Buildings Owned: ${commaify(game.buildingsOwned)}`; // todo: should this be in Game? How should statistics actually work at all?
671+
document.getElementById("buildingsOwnedStat").innerText = `Buildings Owned: ${commaify(Game.getInstance().buildingsOwned)}`; // todo: should this be in Game? How should statistics actually work at all?
672672
document.getElementById("cookieBeenClickedTimesStat").innerText = `Total ${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().name} Clicks: ${this.cookieBeenClickedTimes}`;
673673
document.getElementById("cookiesPerClickStat").innerText = `${Handlers.CURRENTLY_CLICKED.getCurrentlyClicked().namePlural} Per Click: ${this.cookiesPerClick}`;
674674
}

src/ts/exmod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { Building } from "./buildings.js";
44
import { Identifier } from "./handler.js";
5-
import { game } from "./main.js";
5+
import { Game } from "./main.js";
66
import { Mod } from "./mods.js";
77
import { Upgrade, UpgradeData } from "./upgrades.js";
88
import { Handlers } from "./handlers.js";
@@ -22,7 +22,7 @@ export class NewMod extends Mod {
2222

2323
this.superCookies = 5;
2424

25-
this.banana = new Building(game.clickercookie, {
25+
this.banana = new Building(Game.getInstance().clickercookie, {
2626
name: "banaan",
2727
namePlural: "many bannaa",
2828
quote: "i love anana",
@@ -45,7 +45,7 @@ export class NewMod extends Mod {
4545
}
4646
];
4747
for (const upgradeData of this.UPGRADES_DATA) {
48-
Handlers.UPGRADE.register(new Identifier(this.NAMESPACE, upgradeData.uid), new Upgrade(game.clickercookie, upgradeData));
48+
Handlers.UPGRADE.register(new Identifier(this.NAMESPACE, upgradeData.uid), new Upgrade(Game.getInstance().clickercookie, upgradeData));
4949
}
5050

5151
document.getElementById("cookieCount")!.addEventListener("click", () => {this.superCookies++});

src/ts/handlers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Building } from "./buildings.js";
44
import { Handler, Identifier, UniqueKeyHandler } from "./handler.js";
55
import { url } from "./helper.js";
6-
import { game, Game } from "./main.js";
6+
import { Game } from "./main.js";
77
import { Mod } from "./mods.js";
88
import { Background, CurrentlyClickedObject } from "./personalization.js";
99
import { AdvancedPopup, SimplePopup } from "./popup.js";
@@ -212,7 +212,7 @@ export class ModHandler extends UniqueKeyHandler<Mod> {
212212
(document.getElementById("addModURLForm") as HTMLFormElement).reset();
213213
document.getElementById("importedMessage").style.display = "block";
214214

215-
game.isModded = true;
215+
Game.getInstance().isModded = true;
216216

217217
console.log("Loaded mod from URL: "+url);
218218
}
@@ -236,7 +236,7 @@ export class ModHandler extends UniqueKeyHandler<Mod> {
236236
(document.getElementById("addModURLForm") as HTMLFormElement).reset();
237237
document.getElementById("importedMessage")!.style.display = "block";
238238

239-
game.isModded = true;
239+
Game.getInstance().isModded = true;
240240

241241
console.log("Successfully added mod from file: "+file.name);
242242
};

src/ts/main.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ let optionsUp = false;
4444
// misc
4545
let mobile: boolean; // defined in initialization
4646

47+
interface MousePosition {
48+
x: number;
49+
y: number;
50+
}
51+
const mousePos: MousePosition = {x: undefined, y: undefined};
52+
window.addEventListener("mousemove", (event) => {
53+
mousePos.x = event.clientX;
54+
mousePos.y = event.clientY;
55+
56+
if (inDevelopment && !mobile)
57+
document.getElementById("mousePosDevText").innerText = `Mouse Pos: (${mousePos.x}, ${mousePos.y})`;
58+
});
59+
4760
const helper = {} as {
4861
consoleLogDev(str: string): void
4962
};
@@ -79,7 +92,15 @@ export class Game extends SaveProvider {
7992
public static readonly DEFAULT_BACKGROUND: string = "clickercookie:blue";
8093
public static readonly DEFAULT_CURRENTLY_CLICKED_OBJECT: string = "clickercookie:cookie";
8194

82-
static readonly Versions = Versions;
95+
public static readonly Versions = Versions;
96+
public static getMousePosition(): MousePosition {
97+
return mousePos;
98+
}
99+
100+
private static readonly _INSTANCE = new Game();
101+
public static getInstance() {
102+
return this._INSTANCE;
103+
}
83104

84105
/** Said to be the physical manifestation of the cookie gods themselves... */
85106
public clickercookie: ClickerCookie;
@@ -298,15 +319,6 @@ export class Game extends SaveProvider {
298319
document.getElementById("versionNumber").addEventListener("click", () => {versionSwitch()});
299320
document.getElementById("versionNumber").addEventListener("mouseover", () => {versionNumberMousedOver()});
300321
document.getElementById("versionNumber").addEventListener("mouseout", () => {versionNumberMousedOver(true)});
301-
// window events
302-
window.addEventListener("mousemove", (event) => {
303-
game.mousePos = {
304-
x: event.clientX,
305-
y: event.clientY
306-
}
307-
if (inDevelopment && !mobile)
308-
document.getElementById("mousePosDevText").innerText = `Mouse Pos: (${game.mousePos.x}, ${game.mousePos.y})`;
309-
});
310322

311323
// start intervals (should be right at the end)
312324
this.AUTOSAVE_INTERVAL.start();
@@ -398,15 +410,15 @@ dev.setDevMode = function(value: boolean | "on" | "off") {
398410
dev.setCookies = function(number: number) {
399411
if (!dev.devMode) return "You need developer mode ON to run this command.";
400412

401-
game.clickercookie.cookies = number;
402-
game.clickercookie.totalCookies =+ number;
403-
game.hasCheated = true;
413+
Game.getInstance().clickercookie.cookies = number;
414+
Game.getInstance().clickercookie.totalCookies =+ number;
415+
Game.getInstance().hasCheated = true;
404416
}
405417
dev.setCPS = function(number: number) {
406418
if (!dev.devMode) return "You need developer mode ON to run this command.";
407419

408420
dev.CPSGiven = number;
409-
game.hasCheated = true;
421+
Game.getInstance().hasCheated = true;
410422
}
411423

412424
// ------------------------------------
@@ -501,10 +513,8 @@ tooltip.create = function(x: number, y: number, content: any) {
501513

502514
console.log(`you seem smart, how 'bout you contribute to the project? ${Game.GITHUB_REPO}`);
503515

504-
export const game = new Game();
505-
506-
Handlers.SAVE.register("game", game);
507-
Handlers.MOD.register(game.clickercookie.NAMESPACE, game.clickercookie);
516+
Handlers.SAVE.register("game", Game.getInstance());
517+
Handlers.MOD.register(Game.getInstance().clickercookie.NAMESPACE, Game.getInstance().clickercookie);
508518

509519
// Events
510520
// todo: add to game
@@ -518,7 +528,7 @@ function resizeEventListener() {
518528
resizeEventListener(); // since we do need certain elements like the middle text to have the correct size without having to resize the window, we call this now
519529
window.addEventListener("resize", resizeEventListener);
520530

521-
game.init();
531+
Game.getInstance().init();
522532

523-
console.log("game:", game);
533+
console.log("game:", Game.getInstance());
524534
console.log("mod handler:", Handlers.MOD);

src/ts/saving.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SaveHandler } from "./handlers.js";
2-
import { game, Game } from "./main.js";
2+
import { Game } from "./main.js";
33
import { SimplePopup } from "./popup.js";
44

55
export class Savinator {
@@ -102,7 +102,7 @@ export class Savinator {
102102
console.log(`Loaded save data for game namespace.`);
103103
}
104104

105-
game.AUTOSAVE_INTERVAL.reset();
105+
Game.getInstance().AUTOSAVE_INTERVAL.reset();
106106
}
107107

108108
export() {

src/ts/upgrades.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Building } from "./buildings.js";
22
import { clamp, commaify } from "./helper.js";
33
import { hideTooltip } from "./tooltip.js";
4-
import { game, Game } from "./main.js";
4+
import { Game } from "./main.js";
55
import ClickerCookie from "./clickercookie.js";
66
import { Handlers } from "./handlers.js";
77

@@ -165,7 +165,7 @@ export class Upgrade {
165165
tooltip.style.right = "346px";
166166
// clamping allows between 0 and the height of the window minus the height of the box. also add one from the height of the box because it doesn't work correctly normally, idk why
167167
//! this uses game but shouldn't with the tooltip refactor
168-
tooltip.style.top = clamp(game.mousePos.y - tooltip.offsetHeight/2,0,window.innerHeight-(tooltip.offsetHeight + 1))+"px";
168+
tooltip.style.top = clamp(Game.getMousePosition().y - tooltip.offsetHeight/2,0,window.innerHeight-(tooltip.offsetHeight + 1))+"px";
169169
tooltip.style.left = "auto"; // when tooltip is a statistic it sets the left property because it won't work correctly with right, this resets that
170170
tooltip.style.borderRightWidth = "0px";
171171
}

0 commit comments

Comments
 (0)