Refactor code for consistency and readability

- Updated import statements to use consistent formatting across files.
- Adjusted method definitions and class constructors for uniform spacing and style.
- Simplified promise handling and error messages in state handlers.
- Enhanced state transition logic in various state handlers.
- Improved quirk animation handling in WaitStateHandler.
- Streamlined animation loading and caching mechanisms in AnimationLoader.
- Updated Vite configuration for aliasing.
This commit is contained in:
2025-05-24 01:14:35 +02:00
parent 658e1e64b2
commit 60aad20b5e
23 changed files with 4217 additions and 1281 deletions

View File

@ -3,8 +3,8 @@
* @module states
*/
import { StateHandler } from './StateHandler.js';
import { States, Emotions } from '../constants.js';
import { StateHandler } from './StateHandler.js'
import { States, Emotions } from '../constants.js'
/**
* Handler for the Sleep state
@ -16,20 +16,20 @@ export class SleepStateHandler extends StateHandler {
* Create a sleep state handler
* @param {OwenAnimationContext} context - The animation context
*/
constructor(context) {
super(States.SLEEP, context);
constructor (context) {
super(States.SLEEP, context)
/**
* Sleep animation clip
* @type {AnimationClip|null}
*/
this.sleepClip = null;
this.sleepClip = null
/**
* Whether the character is in deep sleep
* @type {boolean}
*/
this.isDeepSleep = false;
this.isDeepSleep = false
}
/**
@ -38,24 +38,24 @@ export class SleepStateHandler extends StateHandler {
* @param {string} [_emotion=Emotions.NEUTRAL] - The emotion to enter with (unused)
* @returns {Promise<void>}
*/
async enter(fromState = null, _emotion = Emotions.NEUTRAL) {
console.log(`Entering SLEEP state from ${fromState}`);
async enter (fromState = null, _emotion = Emotions.NEUTRAL) {
console.log(`Entering SLEEP state from ${fromState}`)
// Play sleep transition if available
const sleepTransition = this.context.getClip('wait_2sleep_T');
const sleepTransition = this.context.getClip('wait_2sleep_T')
if (sleepTransition) {
await sleepTransition.play();
await this.waitForClipEnd(sleepTransition);
await sleepTransition.play()
await this.waitForClipEnd(sleepTransition)
}
// Start sleep loop
this.sleepClip = this.context.getClip('sleep_idle_L');
this.sleepClip = this.context.getClip('sleep_idle_L')
if (this.sleepClip) {
await this.sleepClip.play();
this.currentClip = this.sleepClip;
await this.sleepClip.play()
this.currentClip = this.sleepClip
}
this.isDeepSleep = true;
this.isDeepSleep = true
}
/**
@ -64,27 +64,27 @@ export class SleepStateHandler extends StateHandler {
* @param {string} [emotion=Emotions.NEUTRAL] - The emotion to exit with
* @returns {Promise<void>}
*/
async exit(toState = null, _emotion = Emotions.NEUTRAL) {
console.log(`Exiting SLEEP state to ${toState}`);
this.isDeepSleep = false;
async exit (toState = null, _emotion = Emotions.NEUTRAL) {
console.log(`Exiting SLEEP state to ${toState}`)
this.isDeepSleep = false
if (this.currentClip) {
await this.stopCurrentClip();
await this.stopCurrentClip()
}
// Play wake up animation
const wakeUpClip = this.context.getClip('sleep_wakeup_T');
const wakeUpClip = this.context.getClip('sleep_wakeup_T')
if (wakeUpClip) {
await wakeUpClip.play();
await this.waitForClipEnd(wakeUpClip);
await wakeUpClip.play()
await this.waitForClipEnd(wakeUpClip)
}
// Play transition to next state if available
const transitionName = `sleep_2${toState}_T`;
const transition = this.context.getClip(transitionName);
const transitionName = `sleep_2${toState}_T`
const transition = this.context.getClip(transitionName)
if (transition) {
await transition.play();
await this.waitForClipEnd(transition);
await transition.play()
await this.waitForClipEnd(transition)
}
}
@ -93,7 +93,7 @@ export class SleepStateHandler extends StateHandler {
* @param {number} _deltaTime - Time elapsed since last update (ms, unused)
* @returns {void}
*/
update(_deltaTime) {
update (_deltaTime) {
// Sleep state doesn't need regular updates
// Character remains asleep until external stimulus
}
@ -103,12 +103,12 @@ export class SleepStateHandler extends StateHandler {
* @param {string} _message - The user message (unused, just triggers wake up)
* @returns {Promise<void>}
*/
async handleMessage(_message) {
async handleMessage (_message) {
// Any message should wake up the character
if (this.isDeepSleep) {
console.log('Waking up due to user message');
console.log('Waking up due to user message')
// This will trigger a state transition to REACT
await this.context.transitionTo(States.REACT);
await this.context.transitionTo(States.REACT)
}
}
@ -116,25 +116,25 @@ export class SleepStateHandler extends StateHandler {
* Get available transitions from sleep state
* @returns {string[]} Array of available state transitions
*/
getAvailableTransitions() {
return [ States.WAIT, States.REACT ];
getAvailableTransitions () {
return [States.WAIT, States.REACT]
}
/**
* Check if in deep sleep
* @returns {boolean} True if in deep sleep, false otherwise
*/
isInDeepSleep() {
return this.isDeepSleep;
isInDeepSleep () {
return this.isDeepSleep
}
/**
* Force wake up from sleep
* @returns {Promise<void>}
*/
async wakeUp() {
async wakeUp () {
if (this.isDeepSleep) {
await this.context.transitionTo(States.WAIT);
await this.context.transitionTo(States.WAIT)
}
}
}