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

Followpath #21

Merged
merged 3 commits into from
Oct 26, 2022
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
96 changes: 74 additions & 22 deletions src/npc/move.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NPCState } from '../utils/types'
import { NPCLerpType, NPCState } from '../utils/types'
import { NPC } from './npc'

@Component('npclerpData')
Expand All @@ -10,10 +10,12 @@ export class NPCLerpData {
totalDuration: number = 0
speed: number[] = []
loop: boolean = false
type: NPCLerpType = NPCLerpType.SMOOTH_PATH //default
onFinishCallback?: () => void
onReachedPointCallback?: () => void
constructor(path: Vector3[]) {
constructor(path: Vector3[],type:NPCLerpType=NPCLerpType.SMOOTH_PATH) {
this.path = path
if(type !== undefined) this.type = type
NPCWalkSystem.createAndAddToEngine()
}

Expand All @@ -31,37 +33,87 @@ export class NPCWalkSystem implements ISystem {
update(dt: number) {
for (let npcE of walkingNPCGroup.entities) {
const npc = (npcE as NPC)
//try{
if (npc.state == NPCState.FOLLOWPATH) {
let transform = npc.getComponent(Transform)
let path = npc.getComponent(NPCLerpData)
if (path.fraction < 1) {

if(path.type !== undefined && path.type == NPCLerpType.SMOOTH_PATH){
//stop exactly at each point
if (path.fraction < 1) {
path.fraction += dt * path.speed[path.origin]

//if fraction goes over 1 push it back to 1?

transform.position = Vector3.Lerp(
path.path[path.origin],
path.path[path.target],
path.fraction
)
} else {
path.origin = path.target
path.target += 1
if (path.target >= path.path.length) {
if (path.loop) {
path.target = 0
} else {
npc.stopWalking()
if (path.onFinishCallback) {
path.onFinishCallback()
}
path.fraction = 1
return
}
} else if (path.onReachedPointCallback) {
path.onReachedPointCallback()
}
path.fraction = 0 //starts on this point
transform.lookAt(path.path[path.target])
}
}else{
//default follow, smooth but with low FPS could cut corners
//always increment fraction
path.fraction += dt * path.speed[path.origin]

if(path.fraction >= 1){
path.origin = path.target
const tartInc = Math.max(1,Math.floor( path.fraction ))
path.target += tartInc
if (path.target >= path.path.length) {
if (path.loop) {
path.target = 0
} else {
//path.target = path.path.length - 1
npc.stopWalking()
if (path.onFinishCallback) {
path.onFinishCallback()
}
path.fraction = 1
return
}
} else if (path.onReachedPointCallback) {
path.onReachedPointCallback()
}
path.fraction -= tartInc
//TODO consider lerping look at
if (path.target < path.path.length) {
transform.lookAt(path.path[path.target])
}
}
}
//if reached target
if (path.target < path.path.length) {
transform.position = Vector3.Lerp(
path.path[path.origin],
path.path[path.target],
path.fraction
)
} else {
path.origin = path.target
path.target += 1
if (path.target >= path.path.length) {
if (path.loop) {
path.target = 0
} else {
npc.stopWalking()
if (path.onFinishCallback) {
path.onFinishCallback()
}
path.fraction = 1
return
}
} else if (path.onReachedPointCallback) {
path.onReachedPointCallback()
}
path.fraction = 0
transform.lookAt(path.path[path.target])
}
}
/*}catch(e){
debugger
log("npc.utils.NPCWalkSystem throw error",e)
}*/
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/npc/npc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ export class NPC extends Entity {
this.dialog = new DialogWindow(
typeof data.portrait === `string` ? { path: data.portrait } : data.portrait,
data && data.darkUI ? data.darkUI : false,
data.dialogSound ? data.dialogSound : undefined
data.dialogSound ? data.dialogSound : undefined,
data && data.dialogCustomTheme ? data.dialogCustomTheme : undefined
)
} else {
this.dialog = new DialogWindow(
undefined,
data && data.darkUI ? data.darkUI : false,
data && data.dialogSound ? data.dialogSound : undefined
data && data.dialogSound ? data.dialogSound : undefined,
data && data.dialogCustomTheme ? data.dialogCustomTheme : undefined
)
}

Expand Down Expand Up @@ -194,7 +196,7 @@ export class NPC extends Entity {
}

if (data && data.path) {
this.addComponent(new NPCLerpData(data.path ? data.path : []))
this.addComponent(new NPCLerpData(data.path ? data.path : [],data.pathLerpType))
this.getComponent(NPCLerpData).loop = true

this.followPath()
Expand Down Expand Up @@ -355,7 +357,7 @@ export class NPC extends Entity {
if (!data) {
return
}
this.addComponent(new NPCLerpData(data.path ? data.path : []))
this.addComponent(new NPCLerpData(data.path ? data.path : [],data.pathLerpType))
}

if (this.faceUser) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ export type NPCData = {
coolDownDuration?: number
hoverText?: string
dialogSound?: string
dialogCustomTheme?: Texture
walkingAnim?: string
walkingSpeed?: number
path?: Vector3[]
pathLerpType?: NPCLerpType
textBubble?: boolean
bubbleHeight?: number
noUI?: boolean
Expand Down Expand Up @@ -165,6 +167,7 @@ export type FollowPathData = {
totalDuration?: number
speed?: number
path?: Vector3[]
pathLerpType?: NPCLerpType
onFinishCallback?: () => void
onReachedPointCallback?: () => void
}
Expand Down Expand Up @@ -213,3 +216,9 @@ export enum NPCState {
FOLLOWPATH = 'followPath'
//FOLLOWPLAYER = 'followPlayer'
}


export enum NPCLerpType {
SMOOTH_PATH = 'smooth', //will follow the path but can cut sharp corners
RIGID_PATH = 'rigid', //will ensure each corner is hit
}