Skip to content

Commit 91390c0

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 91390c0

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-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: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,20 @@ export const useGame = /* @__PURE__ */ create(
3939

4040
idle: () => {
4141
set((state) => {
42+
const customAnimationActive = state.animationSet?.custom
43+
? Object.values(state.animationSet.custom).includes(
44+
state.curAnimation
45+
)
46+
: false;
47+
4248
if (state.curAnimation === state.animationSet.jumpIdle) {
4349
return { curAnimation: state.animationSet.jumpLand };
4450
} else if (
4551
state.curAnimation !== state.animationSet.action1 &&
4652
state.curAnimation !== state.animationSet.action2 &&
4753
state.curAnimation !== state.animationSet.action3 &&
48-
state.curAnimation !== state.animationSet.action4
54+
state.curAnimation !== state.animationSet.action4 &&
55+
!customAnimationActive
4956
) {
5057
return { curAnimation: state.animationSet.idle };
5158
}
@@ -144,11 +151,14 @@ export const useGame = /* @__PURE__ */ create(
144151
/**
145152
* Additional animations
146153
*/
147-
// triggerFunction: ()=>{
148-
// set((state) => {
149-
// return { curAnimation: state.animationSet.additionalAnimation };
150-
// });
151-
// }
154+
setCustomAnimation: (animation: string) => {
155+
set((state) => {
156+
if (state.animationSet.custom[animation]) {
157+
return { curAnimation: state.animationSet.custom[animation] };
158+
}
159+
return {};
160+
});
161+
},
152162

153163
/**
154164
* Set/get point to move point
@@ -196,6 +206,8 @@ export type AnimationSet = {
196206
action2?: string;
197207
action3?: string;
198208
action4?: string;
209+
// Custom actions
210+
custom?: Record<string, string>;
199211
};
200212

201213
type State = {
@@ -208,11 +220,12 @@ type State = {
208220
setMoveToPoint: (point: THREE.Vector3) => void;
209221
getMoveToPoint: () => {
210222
moveToPoint: THREE.Vector3;
211-
}
223+
};
212224
setCameraBased: (isCameraBased: boolean) => void;
213225
getCameraBased: () => {
214226
isCameraBased: boolean;
215-
}
227+
};
228+
setCustomAnimation: (animation: string) => void;
216229
} & {
217-
[key in keyof AnimationSet]: () => void;
230+
[key in keyof Omit<AnimationSet, "custom">]: () => void;
218231
};

0 commit comments

Comments
 (0)