desktop support
This commit is contained in:
3
static/js/desktop-structuredclone.js
Normal file
3
static/js/desktop-structuredclone.js
Normal file
@@ -0,0 +1,3 @@
|
||||
function structuredClone(val) {
|
||||
return JSON.parse(JSON.stringify(val));
|
||||
}
|
34
static/js/seagull-desktop.js
Normal file
34
static/js/seagull-desktop.js
Normal file
@@ -0,0 +1,34 @@
|
||||
var desktop_mode = true;
|
||||
|
||||
async function prepare_gamestate() {
|
||||
var gamestate_loaded = null;
|
||||
try {
|
||||
gamestate_loaded = await window.pywebview.api.load_data("gamestate");
|
||||
} catch (exc) {
|
||||
console.error("no gamestate");
|
||||
gamestate_loaded = null;
|
||||
}
|
||||
|
||||
if (gamestate_loaded == null) {
|
||||
record_log("Welcome to Seagull Game! We haven't found a save in your app data, so we're starting a new game!");
|
||||
gamestate = structuredClone(gamestate_default);
|
||||
}
|
||||
else {
|
||||
console.log(gamestate_loaded);
|
||||
gamestate = JSON.parse(gamestate_loaded);
|
||||
record_log("Welcome back! Game loaded.")
|
||||
}
|
||||
}
|
||||
|
||||
function save_game() {
|
||||
window.pywebview.api.save_data("gamestate", JSON.stringify(gamestate));
|
||||
record_log("Game saved.");
|
||||
}
|
||||
|
||||
var tick_meter_running = true;
|
||||
|
||||
function reset_game() {
|
||||
tick_meter_running = false;
|
||||
window.pywebview.api.delete_data("gamestate");
|
||||
window.location.reload();
|
||||
}
|
27
static/js/seagull-web.js
Normal file
27
static/js/seagull-web.js
Normal file
@@ -0,0 +1,27 @@
|
||||
var desktop_mode = false;
|
||||
|
||||
function prepare_gamestate() {
|
||||
var gamestate_loaded = window.localStorage.getItem("gamestate");
|
||||
|
||||
if (gamestate_loaded == null) {
|
||||
record_log("Welcome to Seagull Game! We haven't found a save in your browser storage, so we're starting a new game!");
|
||||
gamestate = structuredClone(gamestate_default);
|
||||
}
|
||||
else {
|
||||
gamestate = JSON.parse(gamestate_loaded);
|
||||
record_log("Welcome back! Game loaded.")
|
||||
}
|
||||
}
|
||||
|
||||
function save_game() {
|
||||
window.localStorage.setItem("gamestate", JSON.stringify(gamestate));
|
||||
record_log("Game saved.");
|
||||
}
|
||||
|
||||
var tick_meter_running = true;
|
||||
|
||||
function reset_game() {
|
||||
tick_meter_running = false;
|
||||
window.localStorage.removeItem("gamestate");
|
||||
window.location.reload();
|
||||
}
|
@@ -14,22 +14,14 @@ const gamestate_default = {
|
||||
"name": "Nameless",
|
||||
"level": 1,
|
||||
"shinies": 0,
|
||||
"autosave": 35
|
||||
"colony": 1,
|
||||
"food": 0,
|
||||
"autosave": 35,
|
||||
"story_beat": 0,
|
||||
"xp": 0,
|
||||
"xp_next": 50
|
||||
};
|
||||
|
||||
function prepare_gamestate() {
|
||||
var gamestate_loaded = window.localStorage.getItem("gamestate");
|
||||
|
||||
if (gamestate_loaded == null) {
|
||||
record_log("Welcome to Seagull Game! We haven't found a save in your browser storage, so we're starting a new game!");
|
||||
gamestate = structuredClone(gamestate_default);
|
||||
}
|
||||
else {
|
||||
gamestate = JSON.parse(gamestate_loaded);
|
||||
record_log("Welcome back! Game loaded.")
|
||||
}
|
||||
}
|
||||
|
||||
var bool_log_alt = false
|
||||
function record_log(text) {
|
||||
const div_logrow = document.createElement("div");
|
||||
@@ -50,18 +42,40 @@ function record_log(text) {
|
||||
page_elements["div_log"].append(div_logrow);
|
||||
}
|
||||
|
||||
function save_game() {
|
||||
window.localStorage.setItem("gamestate", JSON.stringify(gamestate));
|
||||
record_log("Game saved.");
|
||||
function update_ui() {
|
||||
page_elements["lbl_name"].innerHTML = gamestate["name"];
|
||||
page_elements["lbl_tick"].innerHTML = gamestate["tick"];
|
||||
page_elements["lbl_colony"].innerHTML = gamestate["colony"];
|
||||
page_elements["lbl_shinies"].innerHTML = gamestate["shinies"];
|
||||
page_elements["lbl_food"].innerHTML = gamestate["food"];
|
||||
}
|
||||
|
||||
var tick_meter_running = true;
|
||||
var dev_toolbox_open = false;
|
||||
function dev_toolbox(open) {
|
||||
if (open != dev_toolbox_open) {
|
||||
if (open) {
|
||||
var div_toolbox = document.createElement("div");
|
||||
page_elements["div_toolbox"] = div_toolbox;
|
||||
div_toolbox.setAttribute("id", "dev_toolbox");
|
||||
fetch("/dev/get-toolbox")
|
||||
.then((response) => response.text())
|
||||
.then((resp) => {div_toolbox.innerHTML = resp})
|
||||
page_elements["div_sidebar"].appendChild(div_toolbox);
|
||||
}
|
||||
else {
|
||||
var div_toolbox = page_elements["div_toolbox"];
|
||||
page_elements["div_sidebar"].removeChild(div_toolbox);
|
||||
div_toolbox.remove()
|
||||
delete page_elements["div_toolbox"];
|
||||
}
|
||||
}
|
||||
|
||||
dev_toolbox_open = open;
|
||||
}
|
||||
|
||||
async function game_tick() {
|
||||
gamestate["tick"] += 1;
|
||||
ticks_since_last_save += 1;
|
||||
// temp
|
||||
page_elements["lbl_colony"].innerHTML = ticks_since_last_save;
|
||||
page_elements["lbl_tick"].innerHTML = gamestate["tick"];
|
||||
var tickdata = await fetch("/tick")
|
||||
.then(res => {
|
||||
@@ -95,22 +109,42 @@ async function game_tick() {
|
||||
save_game();
|
||||
ticks_since_last_save = 0;
|
||||
}
|
||||
|
||||
update_ui();
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function (ev) {
|
||||
var start_event = "";
|
||||
var target = null;
|
||||
if (desktop_mode) {
|
||||
// pywebview's native JS is nerfed in a few places and needs the additional python API
|
||||
// which gets loaded after initial DOM via injections
|
||||
start_event = "pywebviewready";
|
||||
target = window;
|
||||
}
|
||||
else {
|
||||
// in web mode, browsers are expected to have working local storage
|
||||
start_event = "DOMContentLoaded";
|
||||
target = document;
|
||||
}
|
||||
|
||||
|
||||
target.addEventListener(start_event, function (ev) {
|
||||
page_elements["div_log"] = document.querySelector("#main-log");
|
||||
page_elements["div_sidebar"] = document.querySelector("#main-sidebar");
|
||||
page_elements["div_name"] = document.querySelector("#side-seagull-name");
|
||||
page_elements["div_name_editor"] = document.querySelector("#side-seagull-name-editor");
|
||||
page_elements["lbl_name"] = document.querySelector("#lbl-seagull-name");
|
||||
page_elements["lbl_colony"] = document.querySelector("#lbl-seagull-colony");
|
||||
page_elements["lbl_shinies"] = document.querySelector("#lbl-seagull-shinies");
|
||||
page_elements["lbl_food"] = document.querySelector("#lbl-seagull-food");
|
||||
page_elements["edt_name"] = document.querySelector("#edt-seagull-name");
|
||||
page_elements["lbl_tick"] = document.querySelector("#main-day-counter");
|
||||
page_elements["lbl_xp"] = document.querySelector("#lbl-seagull-xp-current");
|
||||
page_elements["lbl_xp_next"] = document.querySelector("#lbl-seagull-xp-next");
|
||||
|
||||
prepare_gamestate();
|
||||
prepare_gamestate().then(update_ui());
|
||||
|
||||
record_log("seagull game ver. " + ver_string);
|
||||
page_elements["lbl_name"].innerHTML = gamestate["name"];
|
||||
page_elements["lbl_tick"].innerHTML = gamestate["tick"];
|
||||
|
||||
const interval = setInterval(() => {
|
||||
if (tick_meter_running) { game_tick(); }
|
||||
|
Reference in New Issue
Block a user