Skip to content

Commit 07fc90e

Browse files
committed
🎨 Expand animation with looping
1 parent 7bc11bf commit 07fc90e

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

app/src/lib/gait.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ interface Animation {
523523
// options: Options = {};
524524
parameters: Parameters
525525
frames: Frame[]
526+
loop?: boolean
526527
}
527528

528529
const generateCircleAnimation = (
@@ -558,6 +559,7 @@ const generateCircleAnimation = (
558559
const kinematicShowCaseGen = generateCircleAnimation(0.5, 0.7, 4000, 32)
559560

560561
const kinematicShowCase: Animation = {
562+
loop: true,
561563
parameters: {
562564
speed: { min: 0.1, max: 2, default: 1 },
563565
x_offset: { min: -0.1, max: 0.1, default: 0 }
@@ -618,6 +620,7 @@ const kinematicShowCase: Animation = {
618620
}
619621

620622
const stretch: Animation = {
623+
loop: false,
621624
parameters: {
622625
speed: { min: 0.1, max: 2, default: 1 },
623626
x_offset: { min: -0.1, max: 0.1, default: 0 }
@@ -769,6 +772,8 @@ const stretch: Animation = {
769772
}
770773

771774
const pee: Animation = {
775+
loop: false,
776+
772777
parameters: {
773778
speed: { min: 0.1, max: 2, default: 1 },
774779
x_offset: { min: -0.1, max: 0.1, default: 0 }
@@ -857,29 +862,43 @@ const pee: Animation = {
857862
export class Animater extends GaitState {
858863
protected name = 'Bezier'
859864
time = 0
860-
animation = stretch //pee;
865+
animation = [stretch, pee, kinematicShowCase][0]
866+
speed = 1
867+
xOffset = 0
861868

862869
begin() {
863-
this.time = 0
870+
this.reset()
864871
super.begin()
865872
}
866873

867874
end() {
868-
this.time = 0
875+
this.reset()
869876
super.end()
870877
}
871878

879+
reset() {
880+
this.time = 0
881+
}
882+
872883
step(body_state: body_state_t, command: ControllerCommand, dt: number = 0.02) {
873884
return this.step_animation(body_state, dt)
874885
}
875886

887+
setAnimation(animation: Animation) {
888+
this.animation = animation
889+
this.reset()
890+
}
891+
876892
step_animation(body_state: body_state_t, dt: number = 0.02) {
877893
this.dt = dt / 1000
878-
this.time += dt
894+
const frames = this.animation.frames
895+
const duration = frames[frames.length - 1].time
879896

880-
const duration = this.animation.frames[this.animation.frames.length - 1].time
881-
if (this.time > duration) {
897+
this.time += dt * this.speed
898+
if (this.animation.loop !== false && this.time > duration) {
882899
this.time = this.time % duration
900+
} else if (this.time > duration) {
901+
this.time = duration
883902
}
884903

885904
const { prevFrame, nextFrame } = this.getBoundingFrames()

0 commit comments

Comments
 (0)