Skip to content

Commit c6bc82f

Browse files
committed
feat(animation): add custom animation support
- Extend AnimationSet type to include custom actions - Implement setCustomAnimation function for invoking custom animations
1 parent 51937ab commit c6bc82f

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

readme.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,36 @@
1313

1414
## New Features
1515

16+
### (2024-04-23) Custom AnimationSet actions:
17+
18+
- Introduces support for defining custom actions in the animations set and invoking them using the setCustomAnimation function.
19+
20+
#### Usage Example:
21+
22+
```js
23+
import { useGame } from "ecctrl";
24+
25+
const animationSet = {
26+
idle: "Idle",
27+
walk: "Walk",
28+
run: "Run",
29+
.....
30+
// Custom action
31+
custom: {
32+
fly: "Fly"
33+
}
34+
};
35+
36+
// ...
37+
const setCustomAnimation = useGame((state) => state.setCustomAnimation);
38+
// ...
39+
setCustomAnimation("fly");
40+
```
41+
42+
#### Note:
43+
44+
Ensure to update your `animationSet` accordingly to include any custom animations you wish to use in your game.
45+
1646
### (2024-1-1) EcctrlMode:
1747

1848
- Now you can seamlessly switch between different modes by adding "mode" inside Ecctrl.
@@ -170,8 +200,8 @@ EcctrlProps: {
170200
autoBalance: true, // Enable auto-balance
171201
autoBalanceSpringK: 0.3, // Auto-balance spring constant
172202
autoBalanceDampingC: 0.03, // Auto-balance damping coefficient
173-
autoBalanceSpringOnY: 0.5, // Auto-balance spring on Y-axis
174-
autoBalanceDampingOnY: 0.015, // Auto-balance damping on Y-axis
203+
autoBalanceSpringOnY: 0.5, // Auto-balance spring on Y-axis
204+
autoBalanceDampingOnY: 0.015, // Auto-balance damping on Y-axis
175205
// Animation temporary setups
176206
animated: false, // Enable animation
177207
// Mode setups
@@ -425,7 +455,7 @@ pressButton1();
425455

426456
### Ecctrl Mode
427457

428-
Activate different modes in Ecctrl by including the desired mode inside Ecctrl component:
458+
Activate different modes in Ecctrl by including the desired mode inside Ecctrl component:
429459
`<Ecctrl mode="PointToMove">`.
430460

431461
#### 1. "PointToMove" Mode ([CodeSandbox Demo](https://codesandbox.io/p/sandbox/ecctrl-pointtomove-m9z6xh?file=%2Fsrc%2FMap.js%3A46%2C19))

src/stores/useGame.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ export const useGame = /* @__PURE__ */ create(
4545
state.curAnimation !== state.animationSet.action1 &&
4646
state.curAnimation !== state.animationSet.action2 &&
4747
state.curAnimation !== state.animationSet.action3 &&
48-
state.curAnimation !== state.animationSet.action4
48+
state.curAnimation !== state.animationSet.action4 &&
49+
!Object.values(state.animationSet?.custom).includes(
50+
state.curAnimation
51+
)
4952
) {
5053
return { curAnimation: state.animationSet.idle };
5154
}
@@ -144,11 +147,14 @@ export const useGame = /* @__PURE__ */ create(
144147
/**
145148
* Additional animations
146149
*/
147-
// triggerFunction: ()=>{
148-
// set((state) => {
149-
// return { curAnimation: state.animationSet.additionalAnimation };
150-
// });
151-
// }
150+
setCustomAnimation: (animation: string) => {
151+
set((state) => {
152+
if (state.animationSet.custom[animation]) {
153+
return { curAnimation: state.animationSet.custom[animation] };
154+
}
155+
return {};
156+
});
157+
},
152158

153159
/**
154160
* Set/get point to move point
@@ -196,6 +202,8 @@ export type AnimationSet = {
196202
action2?: string;
197203
action3?: string;
198204
action4?: string;
205+
// Custom actions
206+
custom?: Record<string, string>;
199207
};
200208

201209
type State = {
@@ -208,11 +216,12 @@ type State = {
208216
setMoveToPoint: (point: THREE.Vector3) => void;
209217
getMoveToPoint: () => {
210218
moveToPoint: THREE.Vector3;
211-
}
219+
};
212220
setCameraBased: (isCameraBased: boolean) => void;
213221
getCameraBased: () => {
214222
isCameraBased: boolean;
215-
}
223+
};
224+
setCustomAnimation: (animation: string) => void;
216225
} & {
217-
[key in keyof AnimationSet]: () => void;
226+
[key in keyof Omit<AnimationSet, "custom">]: () => void;
218227
};

0 commit comments

Comments
 (0)