Skip to content

Commit

Permalink
Update packages, add appStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcticFqx committed Dec 16, 2024
1 parent 630a69f commit 94ae06f
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dist-ssr
bin/
src/neutralino.js
.tmp
.storage

# Uncompressed music
*.wav
Expand Down
22 changes: 11 additions & 11 deletions game.config.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"team": "Chocobois",
"title": "Game Jam Template",
"description": "We make game, but faster!",
"itch": {
"upload": false,
"username": "username",
"game": "game-name"
},
"neutralino": {
"allow": ["app.exit", "window.center"]
}
"team": "Chocobois",
"title": "Game Jam Template",
"description": "We make game, but faster!",
"itch": {
"upload": false,
"username": "username",
"game": "game-name"
},
"neutralino": {
"allow": ["app.exit", "window.center", "storage.*"]
}
}
64 changes: 32 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
{
"name": "chocobois-jam-template",
"version": "0.0.0",
"type": "module",
"license": "MIT",
"scripts": {
"start": "vite-node automation/dev.ts",
"dev": "vite-node automation/dev.ts",
"dev-neu": "vite-node automation/neu-dev.ts",
"build": "rimraf dist && vite build",
"postinstall": "vite-node automation/setup.ts"
},
"dependencies": {
"@neutralinojs/lib": "^5.3.0",
"phaser": "^3.80.1",
"phaser3-rex-plugins": "^1.80.6"
},
"devDependencies": {
"@electron/asar": "^3.2.10",
"@neutralinojs/neu": "^11.2.2",
"png-to-ico": "^2.1.8",
"resedit": "^2.0.2",
"rimraf": "^6.0.1",
"sharp": "^0.33.4",
"terser": "^5.31.6",
"typescript": "^5.5.4",
"vite": "^5.4.1",
"vite-node": "^2.0.5",
"vite-plugin-checker": "^0.7.2",
"vite-plugin-image-optimizer": "^1.1.8",
"vite-plugin-zip-pack": "^1.2.4",
"vite-tsconfig-paths": "^5.0.1"
}
"name": "chocobois-jam-template",
"version": "0.0.0",
"type": "module",
"license": "MIT",
"scripts": {
"start": "vite-node automation/dev.ts",
"dev": "vite-node automation/dev.ts",
"dev-neu": "vite-node automation/neu-dev.ts",
"build": "rimraf dist && vite build",
"postinstall": "vite-node automation/setup.ts"
},
"dependencies": {
"@neutralinojs/lib": "^5.5.0",
"phaser": "^3.87.0",
"phaser3-rex-plugins": "^1.80.12"
},
"devDependencies": {
"@electron/asar": "^3.2.10",
"@neutralinojs/neu": "^11.2.2",
"png-to-ico": "^2.1.8",
"resedit": "^2.0.3",
"rimraf": "^6.0.1",
"sharp": "^0.33.5",
"terser": "^5.37.0",
"typescript": "^5.7.2",
"vite": "^6.0.3",
"vite-node": "^2.0.5",
"vite-plugin-checker": "^0.8.0",
"vite-plugin-image-optimizer": "^1.1.8",
"vite-plugin-zip-pack": "^1.2.4",
"vite-tsconfig-paths": "^5.1.4"
}
}
44 changes: 23 additions & 21 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ import { TitleScene } from "@/scenes/TitleScene";
import { GameScene } from "@/scenes/GameScene";
import OutlinePipelinePlugin from "phaser3-rex-plugins/plugins/outlinepipeline-plugin.js";

const config: Phaser.Types.Core.GameConfig = {
type: Phaser.WEBGL,
width: 1920,
height: 1080,
mipmapFilter: "LINEAR_MIPMAP_LINEAR",
roundPixels: false,
scale: {
mode: Phaser.Scale.FIT,
},
scene: [PreloadScene, TitleScene, GameScene],
export async function Game() {
const config: Phaser.Types.Core.GameConfig = {
type: Phaser.WEBGL,
width: 1920,
height: 1080,
mipmapFilter: "LINEAR_MIPMAP_LINEAR",
roundPixels: false,
scale: {
mode: Phaser.Scale.FIT,
},
scene: [PreloadScene, TitleScene, GameScene],

plugins: {
global: [
{
key: "rexOutlinePipeline",
plugin: OutlinePipelinePlugin,
start: true,
},
],
},
};
plugins: {
global: [
{
key: "rexOutlinePipeline",
plugin: OutlinePipelinePlugin,
start: true,
},
],
},
};

const game = new Phaser.Game(config);
const game = new Phaser.Game(config);
}
3 changes: 1 addition & 2 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<meta property="og:description" content="%VITE_GAME_DESCRIPTION%">
<meta name="color-scheme" content="dark light">
<meta name="theme-color" content="#2ecc71">
<script src="./neu-main.ts" type="module"></script>
<script src="./game.ts" type="module"></script>
<script src="./main.ts" type="module"></script>
</head>
<body>
<p class="fontPreloader">Loading Bundle</p>
Expand Down
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "@/util/neu";
import "@/util/storage";
import { Game } from "./game";

Game();
14 changes: 0 additions & 14 deletions src/neu-main.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/util/neu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Neutralino from "@neutralinojs/lib";

export const isNeutralino = !!window.NL_TOKEN;

if (isNeutralino) {
Neutralino.init();
Neutralino.events.on("windowClose", () => {
Neutralino.app.exit();
});
Neutralino.window.center();
}
112 changes: 112 additions & 0 deletions src/util/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { storage } from "@neutralinojs/lib";
import { isNeutralino } from "@/util/neu";

type AppStorage = {
readonly length: number;
getItem: (key: string) => Promise<string | null>;
setItem: (key: string, value: string) => Promise<void>;
removeItem: (key: string) => Promise<void>;
key: (index: number) => Promise<string | null>;
clear: () => Promise<void>;
};

const keyRegex = new RegExp("^[a-zA-Z-_0-9]{1,50}$");
const keyError =
"Invalid storage key format. The key should match regex: ^[a-zA-Z-_0-9]{1,50}$";
const keyCheck = (key: string) => {
if (!key.match(keyRegex)) {
throw keyError;
}
};

class NeutralinoStorageClass {
#length: number;

get length() {
return this.#length;
}
constructor(resolve: (value: AppStorage | PromiseLike<AppStorage>) => void) {
this.#length = 0;
storage
.getKeys()
.then((keys) => {
this.#length = keys.length;
resolve(this);
})
.catch(() => {
resolve(this);
});
}
async setItem(key: string, value: string) {
keyCheck(key);
await storage.setData(key, value);
const keys = await storage.getKeys();
this.#length = keys.length;
}
async removeItem(key: string) {
keyCheck(key);
await storage.setData(key, undefined!);
const keys = await storage.getKeys();
this.#length = keys.length;
}
async getItem(key: string) {
keyCheck(key);
try {
return await storage.getData(key);
} catch (e) {
return null;
}
}
async key(index: number): Promise<string | null> {
try {
const keys = await storage.getKeys();
return keys[index] ?? null;
} catch (e) {
return null;
}
}
async clear() {
const keys = await storage.getKeys();
await Promise.all(keys.map((key) => storage.setData(key, undefined!)));
this.#length = keys.length;
}
}

const LocalStorage = {
async removeItem(key: string) {
return localStorage.removeItem(key);
},
async getItem(key: string) {
keyCheck(key);
return localStorage.getItem(key);
},
async setItem(key: string, value: string) {
keyCheck(key);
localStorage.setItem(key, value);
},
async key(index: number) {
return localStorage.key(index);
},
async clear() {
return localStorage.clear();
},
get length() {
return localStorage.length;
},
};

const AppStorage: AppStorage = await new Promise((resolve) => {
if (isNeutralino) {
new NeutralinoStorageClass(resolve);
} else {
resolve(LocalStorage);
}
});

declare global {
var appStorage: AppStorage;
}

globalThis.appStorage = AppStorage;

export default AppStorage;

0 comments on commit 94ae06f

Please sign in to comment.