Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: animations stutter after followPath interruption #3

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import {
activate,
stopWalking,
followPath,
create,
handleWalkAway,
playAnimation,
import {
activate,
stopWalking,
followPath,
create,
handleWalkAway,
playAnimation,
showDebug,
getData,
changeIdleAnim,
talkBubble,
createDialogWindow,
openDialogWindow,
closeDialogWindow
} from "./npc";
} from "./npc";
import { talk } from "./dialog";
import { Dialog, NPCPathType, NPCType } from "./types";
import {NpcUtilsUi} from './ui'
import { NpcUtilsUi } from './ui'

import { closeBubble, closeBubbleEndAll } from "./bubble";

export {
activate,
stopWalking,
followPath,
create,
handleWalkAway,
playAnimation,
showDebug,
activate,
stopWalking,
followPath,
create,
handleWalkAway,
playAnimation,
showDebug,
talk,
Dialog,
getData,
Expand All @@ -40,4 +40,6 @@ export {
openDialogWindow,
closeDialogWindow,
NpcUtilsUi
}
}

export const debugLabel: string = 'NPC-Toolkit'
37 changes: 21 additions & 16 deletions src/npc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import ReactEcs from '@dcl/sdk/react-ecs'
import { Animator, AvatarShape, engine, Entity, GltfContainer, InputAction,MeshCollider,MeshRenderer,PBAvatarShape,PBGltfContainer,pointerEventsSystem,Transform, TransformType } from '@dcl/sdk/ecs'
import { Color3, Quaternion, Vector3 } from '@dcl/sdk/math'
import { Dialog, FollowPathData, ImageData, NPCData, NPCPathType, NPCState, NPCType, TriggerData } from './types';
import * as utils from '@dcl-sdk/utils'
import * as utils from '@dcl-sdk/utils';
import { Animator, AvatarShape, engine, Entity, GltfContainer, InputAction, MeshCollider, MeshRenderer, PBAvatarShape, PBGltfContainer, pointerEventsSystem, Transform, TransformType } from '@dcl/sdk/ecs';
import { Color3, Quaternion, Vector3 } from '@dcl/sdk/math';
import { bubbles, closeBubble, createDialogBubble, openBubble } from './bubble';
import { IsFollowingPath, TrackUserFlag } from './components';
import { faceUserSystem, handleBubbletyping, handleDialogTyping, handlePathTimes, inputListenerSystem } from './systems';
import { addDialog, closeDialog, findDialogByName, npcDialogComponent, openDialog } from './dialog';
import { bubbles, closeBubble, createDialogBubble, openBubble } from './bubble';
import { faceUserSystem, handleBubbletyping, handleDialogTyping, handlePathTimes, inputListenerSystem } from './systems';
import { Dialog, FollowPathData, ImageData, NPCData, NPCPathType, NPCState, NPCType, TriggerData } from './types';
import { darkTheme, lightTheme } from './ui';

export const walkingTimers: Map<Entity,number> = new Map()
Expand Down Expand Up @@ -357,6 +356,7 @@ function walkNPC(npc:Entity, npcData:any, type:NPCPathType, duration:number, pat
}

if (npcData.walkingAnim) {
clearAnimationTimer(npc)
Animator.playSingleAnimation(npc, npcDataComponent.get(npc).walkingAnim, true)
npcData.lastPlayedAnim = npcDataComponent.get(npc).walkingAnim
}
Expand All @@ -377,6 +377,7 @@ export function stopWalking(npc:Entity, duration?: number, finished?:boolean) {
if(npcData.path){
Animator.stopAllAnimations(npc, true)
if(npcDataComponent.get(npc).walkingAnim){
clearAnimationTimer(npc)
Animator.playSingleAnimation(npc, npcDataComponent.get(npc).walkingAnim,true)
npcData.lastPlayedAnim = npcDataComponent.get(npc).walkingAnim
}
Expand Down Expand Up @@ -410,6 +411,7 @@ export function stopPath(npc:Entity){

let npcData = npcDataComponent.get(npc)
if (npcData.walkingAnim) {
clearAnimationTimer(npc)
Animator.playSingleAnimation(npc, npcDataComponent.get(npc).idleAnim)
npcData.lastPlayedAnim = npcData.idleAnim
}
Expand Down Expand Up @@ -525,21 +527,15 @@ export function playAnimation(npc:Entity, anim:string, noLoop?:boolean, duration
utils.paths.stopPath(npc)
}

if(animTimers.has(npc)){
utils.timers.clearTimeout(animTimers.get(npc) as number)
animTimers.delete(npc)
}
clearAnimationTimer(npc)

Animator.stopAllAnimations(npc, true)
Animator.playSingleAnimation(npc, anim, true)
if(duration){
console.log('have a duration to play animation')
if(animTimers.has(npc)){
utils.timers.clearTimeout(animTimers.get(npc) as number)
animTimers.delete(npc)
}
clearAnimationTimer(npc)
animTimers.set(npc, utils.timers.setTimeout(()=>{
animTimers.delete(npc)
clearAnimationTimer(npc)
Animator.stopAllAnimations(npc, true)
if(npcData.idleAnim){
Animator.playSingleAnimation(npc, npcData.idleAnim)
Expand Down Expand Up @@ -597,4 +593,13 @@ export function closeDialogWindow(window:Entity){
if(window){
closeDialog(dialog)
}
}

function clearAnimationTimer(npc: Entity): boolean {
if (animTimers.has(npc)) {
utils.timers.clearTimeout(animTimers.get(npc) as number)
animTimers.delete(npc)
return true
}
return false
}
Loading