Skip to content

Commit 0bdb11d

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 0bdb11d

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
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: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ export const useGame = /* @__PURE__ */ create(
4747
state.curAnimation !== state.animationSet.action3 &&
4848
state.curAnimation !== state.animationSet.action4
4949
) {
50+
if (
51+
state.animationSet?.custom &&
52+
Object.values(state.animationSet?.custom).includes(
53+
state.curAnimation
54+
)
55+
) {
56+
return {};
57+
}
5058
return { curAnimation: state.animationSet.idle };
5159
}
5260
return {};
@@ -144,11 +152,17 @@ export const useGame = /* @__PURE__ */ create(
144152
/**
145153
* Additional animations
146154
*/
147-
// triggerFunction: ()=>{
148-
// set((state) => {
149-
// return { curAnimation: state.animationSet.additionalAnimation };
150-
// });
151-
// }
155+
setCustomAnimation: (animation: string) => {
156+
set((state) => {
157+
if (
158+
state.animationSet.custom[animation] &&
159+
state.curAnimation === state.animationSet.idle
160+
) {
161+
return { curAnimation: state.animationSet.custom[animation] };
162+
}
163+
return { curAnimation: state.animationSet.idle };
164+
});
165+
},
152166

153167
/**
154168
* Set/get point to move point
@@ -196,6 +210,8 @@ export type AnimationSet = {
196210
action2?: string;
197211
action3?: string;
198212
action4?: string;
213+
// Custom actions
214+
custom?: Record<string, string>;
199215
};
200216

201217
type State = {
@@ -208,11 +224,12 @@ type State = {
208224
setMoveToPoint: (point: THREE.Vector3) => void;
209225
getMoveToPoint: () => {
210226
moveToPoint: THREE.Vector3;
211-
}
227+
};
212228
setCameraBased: (isCameraBased: boolean) => void;
213229
getCameraBased: () => {
214230
isCameraBased: boolean;
215-
}
231+
};
232+
setCustomAnimation: (animation: string) => void;
216233
} & {
217-
[key in keyof AnimationSet]: () => void;
234+
[key in keyof Omit<AnimationSet, "custom">]: () => void;
218235
};

0 commit comments

Comments
 (0)