Skip to content

Commit

Permalink
more animation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nearnshaw committed Oct 3, 2023
1 parent 641e7ac commit d50cbeb
Show file tree
Hide file tree
Showing 11 changed files with 735 additions and 742 deletions.
133 changes: 65 additions & 68 deletions Hummingbirds/src/hummingBird.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,79 @@ import { Quaternion } from '@dcl/sdk/math'
import * as utils from '@dcl-sdk/utils'

export function createHummingBird() {
const bird = engine.addEntity()
Transform.create(bird, {
position: { x: 13, y: 3.5, z: 5 },
rotation: { x: 0, y: 0, z: 0, w: 1 },
scale: { x: 0.2, y: 0.2, z: 0.2 }
})
GltfContainer.create(bird, {
src: 'models/hummingbird.glb'
})
Animator.create(bird, {
states: [
{
clip: 'fly',
loop: true,
playing: true,
shouldReset: false,
speed: 2,
name: 'fly'
},
{
clip: 'look',
loop: false,
playing: false,
shouldReset: false,
name: 'look'
},
{
clip: 'shake',
loop: false,
playing: false,
shouldReset: false,
name: 'shake'
}
]
})
const bird = engine.addEntity()
Transform.create(bird, {
position: { x: 13, y: 3.5, z: 5 },
rotation: { x: 0, y: 0, z: 0, w: 1 },
scale: { x: 0.2, y: 0.2, z: 0.2 }
})
GltfContainer.create(bird, {
src: 'models/hummingbird.glb'
})
Animator.create(bird, {
states: [
{
clip: 'fly',
loop: true,
playing: true,
shouldReset: false,
speed: 2
},
{
clip: 'look',
loop: false,
playing: false,
shouldReset: false
},
{
clip: 'shake',
loop: false,
playing: false,
shouldReset: false
}
]
})

// fly pattern
utils.timers.setInterval(() => {
const birdTransform = Transform.getMutable(bird)
// fly pattern
utils.timers.setInterval(() => {
const birdTransform = Transform.getMutable(bird)

// next target
const nextPos = {
x: Math.random() * 12 + 2,
y: Math.random() * 3 + 1,
z: Math.random() * 12 + 2
}
// next target
const nextPos = {
x: Math.random() * 12 + 2,
y: Math.random() * 3 + 1,
z: Math.random() * 12 + 2
}

const nextRot = Quaternion.fromLookAt(birdTransform.position, nextPos)
const nextRot = Quaternion.fromLookAt(birdTransform.position, nextPos)

// face new pos
utils.tweens.startRotation(bird, birdTransform.rotation, nextRot, 0.3, utils.InterpolationType.EASEINSINE)
// face new pos
utils.tweens.startRotation(bird, birdTransform.rotation, nextRot, 0.3, utils.InterpolationType.EASEINSINE)

// move to next pos (after rotating)
utils.timers.setTimeout(
() => {
utils.tweens.startTranslation(bird, birdTransform.position, nextPos, 2, utils.InterpolationType.EASEINEXPO)
},
300 // after rotation is over
)
// move to next pos (after rotating)
utils.timers.setTimeout(
() => {
utils.tweens.startTranslation(bird, birdTransform.position, nextPos, 2, utils.InterpolationType.EASEINEXPO)
},
300 // after rotation is over
)

// randomly play head animation
utils.timers.setTimeout(
() => randomHeadMovement(bird),
2500 // after rotation and translation + pause
)
}, 4000) // loop every 4 seconds
// randomly play head animation
utils.timers.setTimeout(
() => randomHeadMovement(bird),
2500 // after rotation and translation + pause
)
}, 4000) // loop every 4 seconds
}

// Randomly determine if any head moving animations are played
export function randomHeadMovement(bird: Entity) {
const anim = Math.random()
if (anim < 0.2) {
const look = Animator.getClip(bird, 'look')
look.playing = true
} else if (anim > 0.8) {
const shake = Animator.getClip(bird, 'shake')
shake.playing = true
}
const anim = Math.random()
if (anim < 0.2) {
const look = Animator.getClip(bird, 'look')
look.playing = true
} else if (anim > 0.8) {
const shake = Animator.getClip(bird, 'shake')
shake.playing = true
}
}
119 changes: 59 additions & 60 deletions Hummingbirds/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,69 @@
import {
Animator,
AudioSource,
ColliderLayer,
engine,
GltfContainer,
InputAction,
pointerEventsSystem,
Transform
Animator,
AudioSource,
ColliderLayer,
engine,
GltfContainer,
InputAction,
pointerEventsSystem,
Transform
} from '@dcl/sdk/ecs'
import { createHummingBird } from './hummingBird'

export function main() {
const ground = engine.addEntity()
Transform.create(ground, {
position: { x: 8, y: 0, z: 8 },
rotation: { x: 0, y: 0, z: 0, w: 0 },
scale: { x: 1.6, y: 1.6, z: 1.6 }
})
GltfContainer.create(ground, {
src: 'models/Ground.gltf'
})
const ground = engine.addEntity()
Transform.create(ground, {
position: { x: 8, y: 0, z: 8 },
rotation: { x: 0, y: 0, z: 0, w: 0 },
scale: { x: 1.6, y: 1.6, z: 1.6 }
})
GltfContainer.create(ground, {
src: 'models/Ground.gltf'
})

const tree = engine.addEntity()
Transform.create(tree, {
position: { x: 8, y: 0, z: 8 },
rotation: { x: 0, y: 0, z: 0, w: 0 },
scale: { x: 1.6, y: 1.6, z: 1.6 }
})
GltfContainer.create(tree, {
src: 'models/Tree.gltf',
visibleMeshesCollisionMask: ColliderLayer.CL_POINTER,
invisibleMeshesCollisionMask: undefined
})
const tree = engine.addEntity()
Transform.create(tree, {
position: { x: 8, y: 0, z: 8 },
rotation: { x: 0, y: 0, z: 0, w: 0 },
scale: { x: 1.6, y: 1.6, z: 1.6 }
})
GltfContainer.create(tree, {
src: 'models/Tree.gltf',
visibleMeshesCollisionMask: ColliderLayer.CL_POINTER,
invisibleMeshesCollisionMask: undefined
})

AudioSource.create(tree, {
audioClipUrl: 'sounds/pickUp.mp3',
loop: false,
playing: false
})
AudioSource.create(tree, {
audioClipUrl: 'sounds/pickUp.mp3',
loop: false,
playing: false
})

Animator.create(tree, {
states: [
{
clip: 'Tree_Action',
loop: false,
playing: false,
shouldReset: true,
name: 'Tree_Action'
}
]
})
Animator.create(tree, {
states: [
{
clip: 'Tree_Action',
loop: false,
playing: false,
shouldReset: true
}
]
})

pointerEventsSystem.onPointerDown(
{
entity: tree,
opts: {
button: InputAction.IA_PRIMARY,
hoverText: 'Shake'
}
},
function () {
createHummingBird()
const anim = Animator.getMutable(tree)
anim.states[0].playing = true
const audioSource = AudioSource.getMutable(tree)
audioSource.playing = true
}
)
pointerEventsSystem.onPointerDown(
{
entity: tree,
opts: {
button: InputAction.IA_PRIMARY,
hoverText: 'Shake'
}
},
function () {
createHummingBird()
const anim = Animator.getMutable(tree)
anim.states[0].playing = true
const audioSource = AudioSource.getMutable(tree)
audioSource.playing = true
}
)
}
108 changes: 53 additions & 55 deletions Shark-animation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@
import {
Animator,
ColliderLayer,
engine,
GltfContainer,
InputAction,
pointerEventsSystem,
Transform
Animator,
ColliderLayer,
engine,
GltfContainer,
InputAction,
pointerEventsSystem,
Transform
} from '@dcl/sdk/ecs'
import { Vector3 } from '@dcl/sdk/math'

export function main() {
const seaBed = engine.addEntity()
const seaBed = engine.addEntity()

Transform.create(seaBed, {
position: Vector3.create(8, 0, 8),
scale: Vector3.create(0.8, 0.8, 0.8)
})
Transform.create(seaBed, {
position: Vector3.create(8, 0, 8),
scale: Vector3.create(0.8, 0.8, 0.8)
})

GltfContainer.create(seaBed, {
src: 'models/Underwater.gltf'
})
GltfContainer.create(seaBed, {
src: 'models/Underwater.gltf'
})

const shark = engine.addEntity()
const shark = engine.addEntity()

Transform.create(shark, {
position: Vector3.create(8, 3, 8)
})
Transform.create(shark, {
position: Vector3.create(8, 3, 8)
})

GltfContainer.create(shark, {
src: 'models/shark.glb',
visibleMeshesCollisionMask: ColliderLayer.CL_POINTER,
invisibleMeshesCollisionMask: undefined
})
GltfContainer.create(shark, {
src: 'models/shark.glb',
visibleMeshesCollisionMask: ColliderLayer.CL_POINTER,
invisibleMeshesCollisionMask: undefined
})

Animator.create(shark, {
states: [
{
clip: 'swim',
name: 'swim',
playing: true,
loop: true
},
{
clip: 'bite',
name: 'bite',
playing: false,
loop: false,
shouldReset: true
}
]
})
Animator.create(shark, {
states: [
{
clip: 'swim',
playing: true,
loop: true
},
{
clip: 'bite',
playing: false,
loop: false,
shouldReset: true
}
]
})

pointerEventsSystem.onPointerDown(
{
entity: shark,
opts: {
button: InputAction.IA_POINTER,
hoverText: 'Bite'
}
},
() => {
// TODO use Animator.getClip()
const mutableAnimator = Animator.getMutable(shark)
mutableAnimator.states[1].playing = true
}
)
pointerEventsSystem.onPointerDown(
{
entity: shark,
opts: {
button: InputAction.IA_POINTER,
hoverText: 'Bite'
}
},
() => {
// TODO use Animator.getClip()
const mutableAnimator = Animator.getMutable(shark)
mutableAnimator.states[1].playing = true
}
)
}
Loading

0 comments on commit d50cbeb

Please sign in to comment.