Skip to content

Commit

Permalink
feat: add opt out for pet climb and crawl state
Browse files Browse the repository at this point in the history
  • Loading branch information
SeakMengs committed Jan 23, 2024
1 parent 984bb20 commit ac14182
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "WindowPet",
"private": "false",
"version": "0.0.6",
"version": "0.0.7",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "window_pet"
version = "0.0.6"
version = "0.0.7"
description = "Window Pet"
authors = ["SeakMengs"]
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/app/default/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"language": "en",
"allowPetAboveTaskbar": false,
"allowPetInteraction": true,
"allowPetClimbing": true,
"allowAutoStartUp": false,
"allowOverridePetScale": true,
"petScale": 0.7
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "WindowPet",
"version": "0.0.6"
"version": "0.0.7"
},
"tauri": {
"allowlist": {
Expand Down
1 change: 0 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { confirm } from "@tauri-apps/api/dialog";
import { MantineProvider } from "@mantine/core";
import { PrimaryColor } from "./utils";
import { ColorSchemeType } from "./types/ISetting";
import { Notifications } from "@mantine/notifications";

const PhaserWrapper = React.lazy(() => import("./PhaserWrapper"));
const SettingWindow = React.lazy(() => import("./SettingWindow"));
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useSettingStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const useSettingStore = create<ISettingStoreState>()((set) => ({
setAllowPetInteraction: (newBoolean) => {
set({ allowPetInteraction: newBoolean })
},
allowPetClimbing: defaultSettings.allowPetClimbing ?? true,
setAllowPetClimbing: (newBoolean) => {
set({ allowPetClimbing: newBoolean })
},
allowOverridePetScale: defaultSettings.allowPetInteraction ?? true,
setAllowOverridePetScale: (newBoolean) => {
set({allowOverridePetScale: newBoolean})
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/useSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useQuery } from "react-query";
import { useSettingStore } from "./useSettingStore";
import { getAppSettings } from "../utils/settings";
import { TAppSetting } from "../types/ISetting";
import { isEnabled } from "tauri-plugin-autostart-api";
import i18next from "i18next";
import defaultSettings from "../../src-tauri/src/app/default/settings.json";
import { error } from "tauri-plugin-log-api";
import { ISettingStoreVariables } from "../types/hooks/type";

const { setLanguage, setTheme, setAllowAutoStartUp, setAllowPetAboveTaskbar, setAllowPetInteraction, setAllowOverridePetScale, setPetScale } = useSettingStore.getState();
const { setLanguage, setTheme, setAllowAutoStartUp, setAllowPetAboveTaskbar, setAllowPetInteraction, setAllowOverridePetScale, setPetScale, setAllowPetClimbing } = useSettingStore.getState();

const getSettings = async () => {
let setting: TAppSetting = await getAppSettings({ configName: "settings.json" });
let setting: ISettingStoreVariables = await getAppSettings({ configName: "settings.json" });

if (setting === undefined) {
error("Settings is undefined")
Expand All @@ -23,6 +23,7 @@ const getSettings = async () => {
setAllowAutoStartUp(await isEnabled());
setAllowPetAboveTaskbar(setting.allowPetAboveTaskbar ?? defaultSettings.allowPetAboveTaskbar);
setAllowPetInteraction(setting.allowPetInteraction ?? defaultSettings.allowPetInteraction);
setAllowPetClimbing(setting.allowPetClimbing ?? defaultSettings.allowPetClimbing);
setAllowOverridePetScale(setting.allowOverridePetScale ?? defaultSettings.allowOverridePetScale);
setPetScale(setting.petScale ?? defaultSettings.petScale);
};
Expand Down
4 changes: 3 additions & 1 deletion src/locale/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@
"Pet end must be greater than 0": "Pet end must be greater than 0",
"Pet start must be less than end": "Pet start must be less than end",
"Pet end must be greater than start": "Pet end must be greater than start",
"Spritesheet path provided does not exist": "Spritesheet path provided does not exist"
"Spritesheet path provided does not exist": "Spritesheet path provided does not exist",
"Allow pet climb": "Allow pet climb",
"If allow pet climb turn on, pet will be able to climb on the left, right, and top of the window": "If allow pet climb turn on, pet will be able to climb on the left, right, and top of the window"
}
4 changes: 3 additions & 1 deletion src/locale/kh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@
"Pet end must be greater than 0": "បញ្ចប់សត្វចិញ្ចឹមត្រូវតែធំជាង 0",
"Pet start must be less than end": "ចាប់ផ្តើមសត្វចិញ្ចឹមត្រូវតែតូចជាងបញ្ចប់",
"Pet end must be greater than start": "បញ្ចប់សត្វចិញ្ចឹមត្រូវតែធំជាងចាប់ផ្តើម",
"Spritesheet path provided does not exist": "ទីតាំង Spritesheet ដែលបានផ្ដល់មិនមានទេ"
"Spritesheet path provided does not exist": "ទីតាំង Spritesheet ដែលបានផ្ដល់មិនមានទេ",
"Allow pet climb": "អនុញ្ញាតឱ្យសត្វចិញ្ចឹមតោង",
"If allow pet climb turn on, pet will be able to climb on the left, right, and top of the window": "ប្រសិនបើអនុញ្ញាតឱ្យសត្វតោងកុំព្យូទ័រ សត្វចិញ្ចឹមនឹងអាចឡើងតោងនៅខាងឆ្វេង ស្ដាំ និងកំពូលកុំព្យូទ័រ"
}
30 changes: 25 additions & 5 deletions src/scenes/Pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default class Pets extends Phaser.Scene {
private allowPetAboveTaskbar: boolean = useSettingStore.getState().allowPetAboveTaskbar ?? defaultSettings.allowPetAboveTaskbar
private allowOverridePetScale: boolean = useSettingStore.getState().allowOverridePetScale ?? defaultSettings.allowOverridePetScale
private petScale: number = useSettingStore.getState().petScale ?? defaultSettings.petScale
private allowPetClimbing: boolean = useSettingStore.getState().allowPetClimbing ?? defaultSettings.allowPetClimbing

// delay ms to set ignore cursor events
readonly setIgnoreCursorEventsDelay: number = 50;
Expand Down Expand Up @@ -144,6 +145,11 @@ export default class Pets extends Phaser.Scene {
}

if (up) {
if (!this.allowPetClimbing) {
this.petJumpOrPlayRandomState(pet);
return;
}

if (pet.availableStates.includes('crawl')) {
this.switchState(pet, 'crawl')
return;
Expand Down Expand Up @@ -196,6 +202,16 @@ export default class Pets extends Phaser.Scene {
this.scaleAllPets(this.petScale) :
this.scaleAllPets(defaultSettings.petScale);
break;
case DispatchType.SwitchAllowPetClimbing:
this.allowPetClimbing = event.payload.value as boolean;

// when the user switch from pet climb to not climb, we force pet to jump or play random state
if (!this.allowPetClimbing) {
this.pets.forEach((pet) => {
this.petJumpOrPlayRandomState(pet);
});
}
break;
case DispatchType.ChangePetScale:
this.petScale = event.payload.value as number;
this.scaleAllPets(this.petScale);
Expand All @@ -219,7 +235,7 @@ export default class Pets extends Phaser.Scene {
this.turnOffIgnoreCursorEvents();
return;
}

this.turnOnIgnoreCursorEvents();
});
}
Expand Down Expand Up @@ -256,7 +272,7 @@ export default class Pets extends Phaser.Scene {
if (!this.textures.exists(sprite.name)) {
this.loadSpriteSheet(sprite);
this.load.start();

this.load.once('complete', () => {
this.addPet(sprite, index);
});
Expand Down Expand Up @@ -417,6 +433,11 @@ export default class Pets extends Phaser.Scene {
// when pet is destroyed, pet.anims will be undefined, there is a chance that this function get called because of setTimeout
if (!pet.anims) return;

// prevent pet from playing crawl and climb state if allowPetClimbing is false
if (!this.allowPetClimbing) {
if (state === 'climb' || state === 'crawl') return;
}

const animationKey = this.getStateName(state, pet);
// if current state is the same as the new state, do nothing
if (pet.anims && pet.anims.getName() === animationKey) return;
Expand Down Expand Up @@ -860,7 +881,7 @@ export default class Pets extends Phaser.Scene {
// pet.availableStates = pet.availableStates.filter(state => state !== 'climb');

if (worldBounding.left || worldBounding.right) {
if (pet.availableStates.includes('climb')) {
if (pet.availableStates.includes('climb') && this.allowPetClimbing) {
this.switchState(pet, 'climb');

const lastPetX = pet.x;
Expand All @@ -879,8 +900,7 @@ export default class Pets extends Phaser.Scene {
pet.setPosition(lastPetX + this.getPetRightPosition(pet), pet.y);
this.setPetLookToTheLeft(pet, false);
}
}
else {
} else {
if (worldBounding.down) {
// if pet on the ground and beyond screen and doesn't have climb state, we flip the pet
this.toggleFlipXThenUpdateDirection(pet);
Expand Down
1 change: 1 addition & 0 deletions src/types/IEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum DispatchType {
SwitchAutoWindowStartUp = 'Switch auto window start up',
SwitchPetAboveTaskbar = 'Switch pet above taskbar',
SwitchAllowPetInteraction = 'Switch allow pet interaction',
SwitchAllowPetClimbing = 'Switch allow pet climbing',
AddPet = 'Add pet',
RemovePet = 'RemovePet',
OverridePetScale = 'Override pet scale',
Expand Down
9 changes: 0 additions & 9 deletions src/types/ISetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ export enum ColorSchemeType {

export type ColorScheme = ColorSchemeType.Light | ColorSchemeType.Dark;

export type TAppSetting = {
language: string,
theme: ColorScheme
allowPetAboveTaskbar: boolean,
allowPetInteraction: boolean,
allowOverridePetScale: boolean,
petScale: number,
}

export interface IHandleSettingChange {
(
dispatchType: DispatchType,
Expand Down
23 changes: 14 additions & 9 deletions src/types/hooks/type.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { ColorScheme } from "../ISetting";
import { ISpriteConfig } from "../ISpriteConfig";

export interface ISettingStoreState {
export interface ISettingStoreVariables {
language: string;
setLanguage: (newLanguage: string) => void;
theme: ColorScheme;
setTheme: (newTheme: ColorScheme) => void;
allowPetAboveTaskbar: boolean;
setAllowPetAboveTaskbar: (newBoolean: boolean) => void;
allowAutoStartUp: boolean;
setAllowAutoStartUp: (newBoolean: boolean) => void;
allowPetInteraction: boolean;
setAllowPetInteraction: (newBoolean: boolean) => void;
allowPetClimbing: boolean;
allowOverridePetScale: boolean;
setAllowOverridePetScale: (newBoolean: boolean) => void;
petScale: number;
setPetScale: (petScale: number) => void;
pets: ISpriteConfig[];
setPets: (newPets: ISpriteConfig[]) => void;
defaultPet: ISpriteConfig[];
}

export interface ISettingStoreState extends ISettingStoreVariables{
setLanguage: (newLanguage: string) => void;
setTheme: (newTheme: ColorScheme) => void;
setAllowPetAboveTaskbar: (newBoolean: boolean) => void;
setAllowAutoStartUp: (newBoolean: boolean) => void;
setAllowPetInteraction: (newBoolean: boolean) => void;
setAllowPetClimbing: (newBoolean: boolean) => void;
setAllowOverridePetScale: (newBoolean: boolean) => void;
setPetScale: (petScale: number) => void;
setPets: (newPets: ISpriteConfig[]) => void;
setDefaultPet: (newDefaultPet: ISpriteConfig[]) => void;
}

Expand Down
1 change: 0 additions & 1 deletion src/ui/components/PetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function PetCard({ btnLabel, btnLabelCustom, pet, btnFunction, btnFunctionCustom
my={"md"}
defaultValue={playState}
onChange={(event) => setPlayState(event.currentTarget.value)}
pointer
key={pet.id ?? pet.name}
data={availableStates}
/>
Expand Down
8 changes: 7 additions & 1 deletion src/ui/setting_tabs/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DispatchType } from "../../types/IEvents";

function Settings() {
const { t, i18n } = useTranslation();
const { allowAutoStartUp, allowPetAboveTaskbar, allowPetInteraction, allowOverridePetScale, petScale } = useSettingStore();
const { allowAutoStartUp, allowPetAboveTaskbar, allowPetInteraction, allowOverridePetScale, petScale, allowPetClimbing } = useSettingStore();

const settingSwitches: ISettingsContent[] = [
{
Expand All @@ -35,6 +35,12 @@ function Settings() {
checked: allowPetInteraction,
dispatchType: DispatchType.SwitchAllowPetInteraction,
},
{
title: t("Allow pet climb"),
description: t("If allow pet climb turn on, pet will be able to climb on the left, right, and top of the window"),
checked: allowPetClimbing,
dispatchType: DispatchType.SwitchAllowPetClimbing,
},
{
title: t("Override pet scale"),
description: t("Allow the program to adjust all pet sizes by a fixed amount determined by your preferences, ignoring any individual pet scales"),
Expand Down
7 changes: 6 additions & 1 deletion src/utils/handleSettingChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { info } from "tauri-plugin-log-api";
import { DispatchType } from "../types/IEvents";

export const handleSettingChange: IHandleSettingChange = (dispatchType, newValue) => {
const { setLanguage, setTheme, setAllowAutoStartUp, setAllowPetAboveTaskbar, setAllowPetInteraction, setAllowOverridePetScale, setPetScale } = useSettingStore.getState();
const { setLanguage, setTheme, setAllowAutoStartUp, setAllowPetAboveTaskbar, setAllowPetInteraction, setAllowOverridePetScale, setPetScale, setAllowPetClimbing } = useSettingStore.getState();

info(`Change setting, type: ${dispatchType}, value: ${newValue}`);

Expand Down Expand Up @@ -37,6 +37,11 @@ export const handleSettingChange: IHandleSettingChange = (dispatchType, newValue
setSettings({ setKey: "allowPetInteraction", newValue: newValue });
setAllowPetInteraction(newValue as boolean);
emitUpdatePetsEvent({ dispatchType, newValue });
case DispatchType.SwitchAllowPetClimbing:
setSettings({ setKey: "allowPetClimbing", newValue: newValue });
setAllowPetClimbing(newValue as boolean);
emitUpdatePetsEvent({ dispatchType, newValue });
return
case DispatchType.AddPet:
emitUpdatePetsEvent({ dispatchType, newValue });
return
Expand Down

0 comments on commit ac14182

Please sign in to comment.