-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rename Orientation to SurfaceOrientationType * change types * orientation state variable * Orientation type * orientation reducer * Rename surface orientation to Orientability * fix instance of checking pentomino object equality
- Loading branch information
1 parent
ade2b3f
commit 3431567
Showing
14 changed files
with
264 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/components/GameStateProvider/currentPentominoReducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { produce } from "immer"; | ||
import { Orientation } from "../../constants"; | ||
import { Reducer } from "react"; | ||
|
||
export enum RotationDirection { | ||
Left, | ||
Right, | ||
} | ||
|
||
export enum ReflectionDirection { | ||
X, | ||
Y, | ||
} | ||
|
||
export enum OrientationActionType { | ||
rotate = "ROTATE", | ||
reflect = "REFLECT", | ||
replace = "REPLACE", | ||
} | ||
|
||
export interface OrientationAction { | ||
type: OrientationActionType; | ||
direction?: RotationDirection | ReflectionDirection; | ||
newOrientation?: Orientation; | ||
} | ||
|
||
export const orientationReducer: Reducer<Orientation, OrientationAction> = ( | ||
currentOrientation: Orientation, | ||
action: OrientationAction | ||
) => { | ||
switch (action.type) { | ||
case OrientationActionType.rotate: | ||
return produce(currentOrientation, (o: Orientation) => { | ||
const direction = action.direction === RotationDirection.Left ? -1 : 1; | ||
o.rotation = (4 + o.rotation + direction) % 4; | ||
}); | ||
case OrientationActionType.reflect: | ||
return produce(currentOrientation, (o: Orientation) => { | ||
o.reflection = (o.reflection + 1) % 2; | ||
if (o.rotation % 2 === 1) o.rotation = (o.rotation + 2) % 4; | ||
}); | ||
case OrientationActionType.replace: | ||
return action.newOrientation ? { ...action.newOrientation } : { rotation: 0, reflection: 0 }; | ||
} | ||
}; |
Oops, something went wrong.