/
CharacterAnimation.ts
179 lines (156 loc) · 4.87 KB
/
CharacterAnimation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { Direction } from "./../../Direction/Direction.js";
import { WalkingAnimationMapping } from "../../GridEngine.js";
import { Observable, Subject } from "rxjs";
export type CharacterIndex = number;
/** Frame numbers for one movement direction */
export interface FrameRow {
/** Frame number for animation frame with left foot in front */
leftFoot: number;
/** Frame number for animation frame standing (no foot in front) */
standing: number;
/** Frame number for animation frame with right foot in front */
rightFoot: number;
}
export class CharacterAnimation {
static readonly FRAMES_CHAR_ROW = 3;
private static readonly FRAMES_CHAR_COL = 4;
private lastFootLeft = false;
private directionToFrameRow: { [key in Direction]?: number } = {
[Direction.DOWN]: 0,
[Direction.DOWN_LEFT]: 1,
[Direction.DOWN_RIGHT]: 2,
[Direction.LEFT]: 1,
[Direction.RIGHT]: 2,
[Direction.UP]: 3,
[Direction.UP_LEFT]: 1,
[Direction.UP_RIGHT]: 2,
};
private _isEnabled = true;
private frameChange$ = new Subject<number>();
constructor(
private walkingAnimationMapping:
| WalkingAnimationMapping
| number
| undefined,
private charsInRow: number,
) {
this.setWalkingAnimationMapping(walkingAnimationMapping);
}
frameChange(): Observable<number> {
return this.frameChange$;
}
setIsEnabled(isEnabled: boolean): void {
this._isEnabled = isEnabled;
}
isEnabled(): boolean {
return this._isEnabled;
}
updateCharacterFrame(
movementDirection: Direction,
hasWalkedHalfATile: boolean,
currentFrame: number,
): void {
if (this._isEnabled) {
if (hasWalkedHalfATile) {
this.setStandingFrameDuringWalk(movementDirection, currentFrame);
} else {
this.setWalkingFrame(movementDirection);
}
}
}
setStandingFrame(direction: Direction): void {
if (this._isEnabled) {
this._setStandingFrame(direction);
}
}
setWalkingAnimationMapping(
walkingAnimationMapping?: WalkingAnimationMapping | number,
): void {
this.walkingAnimationMapping = walkingAnimationMapping;
this._isEnabled = this.walkingAnimationMapping !== undefined;
}
getWalkingAnimationMapping(): WalkingAnimationMapping | number | undefined {
return this.walkingAnimationMapping;
}
getCharsInRow(): number {
return this.charsInRow;
}
private setStandingFrameDuringWalk(
direction: Direction,
currentFrame: number,
): void {
if (!this.isCurrentFrameStanding(direction, currentFrame)) {
this.lastFootLeft = !this.lastFootLeft;
}
this._setStandingFrame(direction);
}
private setWalkingFrame(direction: Direction): void {
const frameRow = this.framesOfDirection(direction);
if (frameRow)
this.frameChange$.next(
this.lastFootLeft ? frameRow.rightFoot : frameRow.leftFoot,
);
}
private _setStandingFrame(direction: Direction): void {
const framesOfDirection = this.framesOfDirection(direction);
if (framesOfDirection) {
this.frameChange$.next(framesOfDirection.standing);
}
}
private isCurrentFrameStanding(
direction: Direction,
currentFrame: number,
): boolean {
return currentFrame === this.framesOfDirection(direction)?.standing;
}
private framesOfDirection(direction: Direction): FrameRow | undefined {
if (typeof this.walkingAnimationMapping === "number") {
return this.getFramesForCharIndex(
direction,
this.walkingAnimationMapping,
);
}
return this.getFramesForAnimationMapping(direction);
}
private getFramesForAnimationMapping(
direction: Direction,
): FrameRow | undefined {
if (!this.walkingAnimationMapping) return;
return (
this.walkingAnimationMapping[direction] ||
this.walkingAnimationMapping[this.fallbackDirection(direction)]
);
}
private fallbackDirection(direction: Direction): Direction {
switch (direction) {
case Direction.DOWN_LEFT:
return Direction.LEFT;
case Direction.DOWN_RIGHT:
return Direction.RIGHT;
case Direction.UP_LEFT:
return Direction.LEFT;
case Direction.UP_RIGHT:
return Direction.RIGHT;
}
return direction;
}
private getFramesForCharIndex(
direction: Direction,
characterIndex: number,
): FrameRow {
const playerCharRow = Math.floor(characterIndex / this.charsInRow);
const playerCharCol = characterIndex % this.charsInRow;
const framesInRow = this.charsInRow * CharacterAnimation.FRAMES_CHAR_ROW;
const framesInSameRowBefore =
CharacterAnimation.FRAMES_CHAR_ROW * playerCharCol;
const rows =
(this.directionToFrameRow[direction] ?? 0) +
playerCharRow * CharacterAnimation.FRAMES_CHAR_COL;
const startFrame = framesInSameRowBefore + rows * framesInRow;
return {
rightFoot: startFrame,
standing: startFrame + 1,
leftFoot: startFrame + 2,
};
}
}