react-native-games 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -368
- package/lib/module/games/balloon-blaster/BalloonBlaster.js +1 -167
- package/lib/module/games/balloon-blaster/BalloonBlaster.js.map +1 -1
- package/lib/module/games/balloon-blaster/BalloonBlasterConstants.js +1 -182
- package/lib/module/games/balloon-blaster/BalloonBlasterConstants.js.map +1 -1
- package/lib/module/games/balloon-blaster/BalloonBlasterService.js +1 -318
- package/lib/module/games/balloon-blaster/BalloonBlasterStore.js +1 -183
- package/lib/module/games/balloon-blaster/components/BalloonComponent.js +1 -237
- package/lib/module/games/balloon-blaster/components/GameArea.js +1 -156
- package/lib/module/games/balloon-blaster/components/GameBackground.js +1 -476
- package/lib/module/games/balloon-blaster/components/ScoreBoard.js +1 -112
- package/lib/module/games/balloon-blaster/components/ScoreBoard.js.map +1 -1
- package/lib/module/games/balloon-blaster/components/index.js +1 -7
- package/lib/module/games/candy-crush/CandyCrush.js +1 -131
- package/lib/module/games/candy-crush/CandyCrush.js.map +1 -1
- package/lib/module/games/candy-crush/CandyCrushConstants.js +1 -125
- package/lib/module/games/candy-crush/CandyCrushConstants.js.map +1 -1
- package/lib/module/games/candy-crush/CandyCrushService.js +1 -370
- package/lib/module/games/candy-crush/CandyCrushStore.js +1 -303
- package/lib/module/games/candy-crush/components/CandyItem.js +1 -191
- package/lib/module/games/candy-crush/components/GameBackground.js +1 -85
- package/lib/module/games/candy-crush/components/GameGrid.js +1 -314
- package/lib/module/games/candy-crush/components/ScoreBoard.js +1 -79
- package/lib/module/games/candy-crush/components/index.js +1 -7
- package/lib/module/games/candy-crush/index.js +1 -6
- package/lib/module/games/colors-sort/ColorsSort.js +1 -143
- package/lib/module/games/colors-sort/ColorsSort.js.map +1 -1
- package/lib/module/games/colors-sort/ColorsSortConstants.js +1 -72
- package/lib/module/games/colors-sort/ColorsSortConstants.js.map +1 -1
- package/lib/module/games/colors-sort/ColorsSortService.js +1 -255
- package/lib/module/games/colors-sort/ColorsSortStore.js +1 -257
- package/lib/module/games/colors-sort/ColorsSortStore.js.map +1 -1
- package/lib/module/games/colors-sort/components/ColorContainer.js +1 -140
- package/lib/module/games/colors-sort/components/GameBackground.js +1 -135
- package/lib/module/games/colors-sort/components/ScoreBoard.js +1 -70
- package/lib/module/games/colors-sort/components/index.js +1 -6
- package/lib/module/games/dino-jump/DinoJump.js +1 -209
- package/lib/module/games/dino-jump/DinoJump.js.map +1 -1
- package/lib/module/games/dino-jump/DinoJumpConstants.js +1 -189
- package/lib/module/games/dino-jump/DinoJumpConstants.js.map +1 -1
- package/lib/module/games/dino-jump/DinoJumpService.js +1 -270
- package/lib/module/games/dino-jump/DinoJumpStore.js +1 -381
- package/lib/module/games/dino-jump/components/DinoSprite.js +1 -418
- package/lib/module/games/dino-jump/components/DinoSprite.js.map +1 -1
- package/lib/module/games/dino-jump/components/GameArea.js +1 -68
- package/lib/module/games/dino-jump/components/GameBackground.js +1 -444
- package/lib/module/games/dino-jump/components/ObstacleSprite.js +1 -306
- package/lib/module/games/dino-jump/components/ScoreBoard.js +1 -105
- package/lib/module/games/dino-jump/components/ScoreBoard.js.map +1 -1
- package/lib/module/games/dino-jump/components/StarSprite.js +1 -45
- package/lib/module/games/dino-jump/components/index.js +1 -9
- package/lib/module/games/flappy-bird/FlappyBird.js +1 -126
- package/lib/module/games/flappy-bird/FlappyBird.js.map +1 -1
- package/lib/module/games/flappy-bird/FlappyBirdConstants.js +1 -90
- package/lib/module/games/flappy-bird/FlappyBirdConstants.js.map +1 -1
- package/lib/module/games/flappy-bird/FlappyBirdStore.js +1 -300
- package/lib/module/games/flappy-bird/components/Bird.js +1 -87
- package/lib/module/games/flappy-bird/components/GameArea.js +1 -87
- package/lib/module/games/flappy-bird/components/GameBackground.js +1 -79
- package/lib/module/games/flappy-bird/components/Pipes.js +1 -172
- package/lib/module/games/flappy-bird/components/ScoreBoard.js +1 -73
- package/lib/module/games/flappy-bird/components/index.js +1 -8
- package/lib/module/games/fruit-merger/FruitMerger.js +1 -120
- package/lib/module/games/fruit-merger/FruitMerger.js.map +1 -1
- package/lib/module/games/fruit-merger/FruitMergerConstants.js +1 -119
- package/lib/module/games/fruit-merger/FruitMergerConstants.js.map +1 -1
- package/lib/module/games/fruit-merger/FruitMergerService.js +1 -13
- package/lib/module/games/fruit-merger/FruitMergerStore.js +1 -315
- package/lib/module/games/fruit-merger/components/FruitItem.js +1 -102
- package/lib/module/games/fruit-merger/components/GameArea.js +1 -103
- package/lib/module/games/fruit-merger/components/GameBackground.js +1 -498
- package/lib/module/games/fruit-merger/components/ScoreBoard.js +1 -58
- package/lib/module/games/fruit-merger/components/index.js +1 -7
- package/lib/module/games/fruit-ninja/FruitNinja.js +1 -134
- package/lib/module/games/fruit-ninja/FruitNinja.js.map +1 -1
- package/lib/module/games/fruit-ninja/FruitNinjaConstants.js +1 -148
- package/lib/module/games/fruit-ninja/FruitNinjaConstants.js.map +1 -1
- package/lib/module/games/fruit-ninja/FruitNinjaService.js +1 -311
- package/lib/module/games/fruit-ninja/FruitNinjaStore.js +1 -191
- package/lib/module/games/fruit-ninja/FruitNinjaStore.js.map +1 -1
- package/lib/module/games/fruit-ninja/components/FruitComponent.js +1 -99
- package/lib/module/games/fruit-ninja/components/GameArea.js +1 -215
- package/lib/module/games/fruit-ninja/components/GameBackground.js +1 -1267
- package/lib/module/games/fruit-ninja/components/ScoreBoard.js +1 -92
- package/lib/module/games/fruit-ninja/components/ScoreBoard.js.map +1 -1
- package/lib/module/games/fruit-ninja/components/index.js +1 -7
- package/lib/module/games/game-2048/Game2048.js +1 -149
- package/lib/module/games/game-2048/Game2048.js.map +1 -1
- package/lib/module/games/game-2048/Game2048Constants.js +1 -263
- package/lib/module/games/game-2048/Game2048Constants.js.map +1 -1
- package/lib/module/games/game-2048/Game2048Service.js +1 -457
- package/lib/module/games/game-2048/Game2048Store.js +1 -236
- package/lib/module/games/game-2048/components/GameBackground.js +1 -247
- package/lib/module/games/game-2048/components/GameGrid.js +1 -139
- package/lib/module/games/game-2048/components/GameTile.js +1 -72
- package/lib/module/games/game-2048/components/ScoreBoard.js +1 -52
- package/lib/module/games/game-2048/components/index.js +1 -7
- package/lib/module/games/maze-runner/MazeRunner.js +1 -267
- package/lib/module/games/maze-runner/MazeRunner.js.map +1 -1
- package/lib/module/games/maze-runner/MazeRunnerConstants.js +1 -100
- package/lib/module/games/maze-runner/MazeRunnerConstants.js.map +1 -1
- package/lib/module/games/maze-runner/MazeRunnerService.js +1 -586
- package/lib/module/games/maze-runner/components/EnhancedBallComponent.js +1 -150
- package/lib/module/games/maze-runner/components/EnhancedGameArea.js +1 -370
- package/lib/module/games/maze-runner/components/GameBackground.js +1 -175
- package/lib/module/games/maze-runner/components/ScoreBoard.js +1 -61
- package/lib/module/games/maze-runner/components/SkiaPipeComponent.js +1 -209
- package/lib/module/games/maze-runner/components/StaticGameBackground.js +1 -169
- package/lib/module/games/maze-runner/components/WallComponent.js +1 -91
- package/lib/module/games/maze-runner/components/index.js +1 -8
- package/lib/module/games/popit-fidget/PopitFidget.js +1 -285
- package/lib/module/games/popit-fidget/PopitFidget.js.map +1 -1
- package/lib/module/games/popit-fidget/PopitFidgetConstants.js +1 -113
- package/lib/module/games/popit-fidget/PopitFidgetConstants.js.map +1 -1
- package/lib/module/games/popit-fidget/PopitFidgetService.js +1 -132
- package/lib/module/games/popit-fidget/PopitFidgetStore.js +1 -125
- package/lib/module/games/popit-fidget/components/BubbleComponent.js +1 -198
- package/lib/module/games/popit-fidget/components/FidgetGrid.js +1 -165
- package/lib/module/games/popit-fidget/components/GameBackground.js +1 -177
- package/lib/module/games/popit-fidget/components/ScoreBoard.js +1 -61
- package/lib/module/games/popit-fidget/components/index.js +1 -7
- package/lib/module/games/sliding-numbers/SlidingNumbers.js +1 -159
- package/lib/module/games/sliding-numbers/SlidingNumbers.js.map +1 -1
- package/lib/module/games/sliding-numbers/SlidingNumbersConstants.js +1 -207
- package/lib/module/games/sliding-numbers/SlidingNumbersConstants.js.map +1 -1
- package/lib/module/games/sliding-numbers/SlidingNumbersService.js +1 -248
- package/lib/module/games/sliding-numbers/SlidingNumbersStore.js +1 -274
- package/lib/module/games/sliding-numbers/components/GameBackground.js +1 -259
- package/lib/module/games/sliding-numbers/components/NumbersGrid.js +1 -174
- package/lib/module/games/sliding-numbers/components/NumbersTile.js +1 -116
- package/lib/module/games/sliding-numbers/components/ScoreBoard.js +1 -64
- package/lib/module/games/sliding-numbers/components/index.js +1 -7
- package/lib/module/games/snake/Snake.js +1 -189
- package/lib/module/games/snake/Snake.js.map +1 -1
- package/lib/module/games/snake/SnakeConstants.js +1 -138
- package/lib/module/games/snake/SnakeConstants.js.map +1 -1
- package/lib/module/games/snake/SnakeService.js +1 -148
- package/lib/module/games/snake/SnakeStore.js +1 -182
- package/lib/module/games/snake/components/GameBackground.js +1 -221
- package/lib/module/games/snake/components/GameGrid.js +1 -153
- package/lib/module/games/snake/components/ScoreBoard.js +1 -51
- package/lib/module/games/snake/components/index.js +1 -6
- package/lib/module/games/snake/index.js +1 -6
- package/lib/module/games/space-fighter/SpaceFighter.js +1 -165
- package/lib/module/games/space-fighter/SpaceFighter.js.map +1 -1
- package/lib/module/games/space-fighter/SpaceFighterConstants.js +1 -108
- package/lib/module/games/space-fighter/SpaceFighterConstants.js.map +1 -1
- package/lib/module/games/space-fighter/SpaceFighterService.js +1 -326
- package/lib/module/games/space-fighter/SpaceFighterStore.js +1 -209
- package/lib/module/games/space-fighter/components/AsteroidComponent.js +1 -113
- package/lib/module/games/space-fighter/components/GameArea.js +1 -289
- package/lib/module/games/space-fighter/components/GameBackground.js +1 -239
- package/lib/module/games/space-fighter/components/ScoreBoard.js +1 -136
- package/lib/module/games/space-fighter/components/Spacecraft3D.js +1 -202
- package/lib/module/games/space-fighter/components/SpacecraftPath.js +1 -52
- package/lib/module/games/space-fighter/components/index.js +1 -9
- package/lib/module/games/whack-a-mole/WhackAMole.js +1 -270
- package/lib/module/games/whack-a-mole/WhackAMole.js.map +1 -1
- package/lib/module/games/whack-a-mole/WhackAMoleConstants.js +1 -115
- package/lib/module/games/whack-a-mole/WhackAMoleConstants.js.map +1 -1
- package/lib/module/games/whack-a-mole/WhackAMoleService.js +1 -120
- package/lib/module/games/whack-a-mole/WhackAMoleStore.js +1 -172
- package/lib/module/games/whack-a-mole/components/GameBackground.js +1 -477
- package/lib/module/games/whack-a-mole/components/GameGrid.js +1 -97
- package/lib/module/games/whack-a-mole/components/GameHole.js +1 -196
- package/lib/module/games/whack-a-mole/components/MoleCharacter.js +1 -241
- package/lib/module/games/whack-a-mole/components/ScoreBoard.js +1 -67
- package/lib/module/games/whack-a-mole/components/ScoreBoard.js.map +1 -1
- package/lib/module/games/whack-a-mole/components/index.js +1 -8
- package/lib/module/helpers/AnimationFrame.js +1 -120
- package/lib/module/helpers/AnimationTracker.js +1 -89
- package/lib/module/helpers/ErrorHandler.js +1 -269
- package/lib/module/helpers/GameControlButton.js +1 -219
- package/lib/module/helpers/GameOverModal.js +1 -144
- package/lib/module/helpers/GameOverModal.js.map +1 -1
- package/lib/module/helpers/GameSettingsModal.js +1 -287
- package/lib/module/helpers/ParticleBlast.js +1 -134
- package/lib/module/helpers/ScoreBoardContainer.js +1 -34
- package/lib/module/helpers/index.js +1 -12
- package/lib/module/index.js +1 -22
- package/lib/module/services/GamesConstants.js +1 -178
- package/lib/module/services/GamesService.js +1 -112
- package/lib/module/services/GamesService.js.map +1 -1
- package/lib/module/services/HapticsService.js +1 -77
- package/lib/module/services/SoundsService.js +1 -302
- package/lib/module/services/UtilsService.js +1 -32
- package/lib/typescript/src/games/balloon-blaster/BalloonBlaster.d.ts.map +1 -1
- package/lib/typescript/src/games/balloon-blaster/BalloonBlasterConstants.d.ts +1 -1
- package/lib/typescript/src/games/balloon-blaster/components/ScoreBoard.d.ts.map +1 -1
- package/lib/typescript/src/games/candy-crush/CandyCrushConstants.d.ts +7 -7
- package/lib/typescript/src/games/colors-sort/ColorsSort.d.ts.map +1 -1
- package/lib/typescript/src/games/colors-sort/ColorsSortStore.d.ts.map +1 -1
- package/lib/typescript/src/games/dino-jump/DinoJump.d.ts.map +1 -1
- package/lib/typescript/src/games/dino-jump/components/DinoSprite.d.ts.map +1 -1
- package/lib/typescript/src/games/flappy-bird/FlappyBird.d.ts.map +1 -1
- package/lib/typescript/src/games/flappy-bird/FlappyBirdConstants.d.ts.map +1 -1
- package/lib/typescript/src/games/fruit-merger/FruitMerger.d.ts.map +1 -1
- package/lib/typescript/src/games/fruit-merger/FruitMergerConstants.d.ts.map +1 -1
- package/lib/typescript/src/games/fruit-ninja/FruitNinja.d.ts.map +1 -1
- package/lib/typescript/src/games/fruit-ninja/components/ScoreBoard.d.ts.map +1 -1
- package/lib/typescript/src/games/game-2048/Game2048.d.ts.map +1 -1
- package/lib/typescript/src/games/maze-runner/MazeRunner.d.ts.map +1 -1
- package/lib/typescript/src/games/popit-fidget/PopitFidget.d.ts.map +1 -1
- package/lib/typescript/src/games/sliding-numbers/SlidingNumbers.d.ts.map +1 -1
- package/lib/typescript/src/games/space-fighter/SpaceFighter.d.ts.map +1 -1
- package/lib/typescript/src/games/whack-a-mole/WhackAMole.d.ts.map +1 -1
- package/lib/typescript/src/games/whack-a-mole/WhackAMoleConstants.d.ts +1 -1
- package/lib/typescript/src/games/whack-a-mole/components/ScoreBoard.d.ts.map +1 -1
- package/lib/typescript/src/helpers/GameOverModal.d.ts +3 -0
- package/lib/typescript/src/helpers/GameOverModal.d.ts.map +1 -1
- package/lib/typescript/src/services/GamesConstants.d.ts +7 -7
- package/package.json +2 -2
|
@@ -1,189 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import React from 'react';
|
|
4
|
-
import { View, StyleSheet } from 'react-native';
|
|
5
|
-
import { GestureHandlerRootView, Gesture, GestureDetector } from 'react-native-gesture-handler';
|
|
6
|
-
import { runOnJS } from 'react-native-reanimated';
|
|
7
|
-
import { GameControlButton, GameOverModal, GameSettingsModal } from "../../helpers/index.js";
|
|
8
|
-
import { DEFAULT_GAME_SETTINGS, GAME_IDS } from "../../services/UtilsService.js";
|
|
9
|
-
import { GameBackground, GameGrid, ScoreBoard } from "./components/index.js";
|
|
10
|
-
import { useSnakeStore, useScore, useIsPlaying, useIsGameOver } from "./SnakeStore.js";
|
|
11
|
-
import { SNAKE_COLORS } from "./SnakeConstants.js";
|
|
12
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
|
-
export const Snake = /*#__PURE__*/React.memo(({
|
|
14
|
-
settings,
|
|
15
|
-
onSettingsChange
|
|
16
|
-
}) => {
|
|
17
|
-
// Zustand store selectors for optimal re-renders
|
|
18
|
-
const score = useScore();
|
|
19
|
-
const isPlaying = useIsPlaying();
|
|
20
|
-
const isGameOver = useIsGameOver();
|
|
21
|
-
|
|
22
|
-
// Store actions - use stable references to prevent re-renders
|
|
23
|
-
const startGame = useSnakeStore(state => state.startGame);
|
|
24
|
-
const stopGame = useSnakeStore(state => state.stopGame);
|
|
25
|
-
const resetGame = useSnakeStore(state => state.resetGame);
|
|
26
|
-
const initializeGame = useSnakeStore(state => state.initializeGame);
|
|
27
|
-
const updateSettings = useSnakeStore(state => state.updateSettings);
|
|
28
|
-
const changeDirection = useSnakeStore(state => state.changeDirection);
|
|
29
|
-
|
|
30
|
-
// Track previous values to prevent unnecessary updates
|
|
31
|
-
const prevSettingsRef = React.useRef({
|
|
32
|
-
enableSounds: true,
|
|
33
|
-
enableHaptics: true
|
|
34
|
-
});
|
|
35
|
-
const prevDifficultyRef = React.useRef('easy');
|
|
36
|
-
|
|
37
|
-
// Update settings when they change
|
|
38
|
-
React.useEffect(() => {
|
|
39
|
-
const enableSounds = settings?.enableSounds ?? true;
|
|
40
|
-
const enableHaptics = settings?.enableHaptics ?? true;
|
|
41
|
-
|
|
42
|
-
// Only update if values actually changed
|
|
43
|
-
if (prevSettingsRef.current.enableSounds !== enableSounds || prevSettingsRef.current.enableHaptics !== enableHaptics) {
|
|
44
|
-
prevSettingsRef.current = {
|
|
45
|
-
enableSounds,
|
|
46
|
-
enableHaptics
|
|
47
|
-
};
|
|
48
|
-
updateSettings(enableSounds, enableHaptics);
|
|
49
|
-
}
|
|
50
|
-
}, [updateSettings, settings?.enableSounds, settings?.enableHaptics]);
|
|
51
|
-
|
|
52
|
-
// Initialize on mount and when difficulty changes
|
|
53
|
-
React.useEffect(() => {
|
|
54
|
-
const difficulty = settings?.difficulty || 'easy';
|
|
55
|
-
|
|
56
|
-
// Only initialize if difficulty actually changed
|
|
57
|
-
if (prevDifficultyRef.current !== difficulty) {
|
|
58
|
-
prevDifficultyRef.current = difficulty;
|
|
59
|
-
initializeGame(difficulty);
|
|
60
|
-
}
|
|
61
|
-
}, [initializeGame, settings?.difficulty]);
|
|
62
|
-
|
|
63
|
-
// Game control handlers
|
|
64
|
-
const handleStartGame = React.useCallback(() => {
|
|
65
|
-
startGame();
|
|
66
|
-
}, [startGame]);
|
|
67
|
-
const handleResetGame = React.useCallback(() => {
|
|
68
|
-
resetGame();
|
|
69
|
-
}, [resetGame]);
|
|
70
|
-
const handleStopGame = React.useCallback(() => {
|
|
71
|
-
stopGame();
|
|
72
|
-
}, [stopGame]);
|
|
73
|
-
|
|
74
|
-
// Gesture handler for swipe controls with safe state updates
|
|
75
|
-
const handleSwipe = React.useCallback(direction => {
|
|
76
|
-
try {
|
|
77
|
-
if (!isPlaying) return;
|
|
78
|
-
|
|
79
|
-
// Use requestAnimationFrame for safer state updates
|
|
80
|
-
requestAnimationFrame(() => {
|
|
81
|
-
try {
|
|
82
|
-
changeDirection(direction);
|
|
83
|
-
} catch (error) {
|
|
84
|
-
console.error('Direction change error:', error);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
} catch (error) {
|
|
88
|
-
console.error('Swipe handler error:', error);
|
|
89
|
-
}
|
|
90
|
-
}, [isPlaying, changeDirection]);
|
|
91
|
-
|
|
92
|
-
// Pan gesture for swipe detection
|
|
93
|
-
const panGesture = React.useMemo(() => Gesture.Pan().onEnd(event => {
|
|
94
|
-
const threshold = 30; // Minimum swipe distance
|
|
95
|
-
const absX = Math.abs(event.translationX);
|
|
96
|
-
const absY = Math.abs(event.translationY);
|
|
97
|
-
if (absX > threshold || absY > threshold) {
|
|
98
|
-
if (absX > absY) {
|
|
99
|
-
// Horizontal swipe
|
|
100
|
-
if (event.translationX > 0) {
|
|
101
|
-
runOnJS(handleSwipe)('RIGHT');
|
|
102
|
-
} else {
|
|
103
|
-
runOnJS(handleSwipe)('LEFT');
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
// Vertical swipe
|
|
107
|
-
if (event.translationY > 0) {
|
|
108
|
-
runOnJS(handleSwipe)('DOWN');
|
|
109
|
-
} else {
|
|
110
|
-
runOnJS(handleSwipe)('UP');
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}), [handleSwipe]);
|
|
115
|
-
|
|
116
|
-
// Memoized GameControlButton props
|
|
117
|
-
const gameControlButtonProps = React.useMemo(() => ({
|
|
118
|
-
isPlaying,
|
|
119
|
-
gameOver: isGameOver,
|
|
120
|
-
onStartGame: handleStartGame,
|
|
121
|
-
onStopGame: handleStopGame,
|
|
122
|
-
onResetGame: handleResetGame,
|
|
123
|
-
startButtonText: 'Start Slithering!',
|
|
124
|
-
startButtonSubtext: 'Swipe to move or turn.',
|
|
125
|
-
startButtonColor: SNAKE_COLORS.BUTTON_PRIMARY,
|
|
126
|
-
startButtonBorderColor: SNAKE_COLORS.BUTTON_SECONDARY
|
|
127
|
-
}), [isPlaying, isGameOver, handleStartGame, handleStopGame, handleResetGame]);
|
|
128
|
-
|
|
129
|
-
// Memoized GameOverModal props
|
|
130
|
-
const gameOverModalProps = React.useMemo(() => ({
|
|
131
|
-
isVisible: isGameOver,
|
|
132
|
-
score,
|
|
133
|
-
onPlayAgain: handleResetGame,
|
|
134
|
-
buttonText: 'Play Again!',
|
|
135
|
-
primaryColor: SNAKE_COLORS.MODAL_PRIMARY,
|
|
136
|
-
borderColor: SNAKE_COLORS.MODAL_BORDER,
|
|
137
|
-
buttonColor: SNAKE_COLORS.MODAL_BUTTON,
|
|
138
|
-
buttonBorderColor: SNAKE_COLORS.MODAL_BUTTON_BORDER,
|
|
139
|
-
scoreFormatter: s => `${s}`,
|
|
140
|
-
additionalInfo: 'Great slithering!'
|
|
141
|
-
}), [isGameOver, score, handleResetGame]);
|
|
142
|
-
|
|
143
|
-
// Memoized GameSettingsModal props
|
|
144
|
-
const gameSettingsModalProps = React.useMemo(() => ({
|
|
145
|
-
gameId: GAME_IDS.SNAKE,
|
|
146
|
-
settings: settings || DEFAULT_GAME_SETTINGS,
|
|
147
|
-
onSettingsChange
|
|
148
|
-
}), [settings, onSettingsChange]);
|
|
149
|
-
|
|
150
|
-
// Get offset from settings
|
|
151
|
-
const offset = settings?.offset ?? 0;
|
|
152
|
-
return /*#__PURE__*/_jsx(GameBackground, {
|
|
153
|
-
children: /*#__PURE__*/_jsxs(GestureHandlerRootView, {
|
|
154
|
-
style: styles.container,
|
|
155
|
-
children: [/*#__PURE__*/_jsx(GestureDetector, {
|
|
156
|
-
gesture: panGesture,
|
|
157
|
-
children: /*#__PURE__*/_jsx(View, {
|
|
158
|
-
style: styles.container,
|
|
159
|
-
children: /*#__PURE__*/_jsx(View, {
|
|
160
|
-
style: [styles.gridContainer, {
|
|
161
|
-
paddingTop: offset
|
|
162
|
-
}],
|
|
163
|
-
children: /*#__PURE__*/_jsx(GameGrid, {})
|
|
164
|
-
})
|
|
165
|
-
})
|
|
166
|
-
}), /*#__PURE__*/_jsx(ScoreBoard, {
|
|
167
|
-
offset: offset
|
|
168
|
-
}), /*#__PURE__*/_jsx(GameControlButton, {
|
|
169
|
-
...gameControlButtonProps
|
|
170
|
-
}), /*#__PURE__*/_jsx(GameOverModal, {
|
|
171
|
-
...gameOverModalProps
|
|
172
|
-
}), /*#__PURE__*/_jsx(GameSettingsModal, {
|
|
173
|
-
...gameSettingsModalProps
|
|
174
|
-
})]
|
|
175
|
-
})
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
Snake.displayName = 'Snake';
|
|
179
|
-
const styles = StyleSheet.create({
|
|
180
|
-
container: {
|
|
181
|
-
flex: 1
|
|
182
|
-
},
|
|
183
|
-
gridContainer: {
|
|
184
|
-
flex: 1,
|
|
185
|
-
justifyContent: 'center',
|
|
186
|
-
alignItems: 'center'
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
//# sourceMappingURL=Snake.js.map
|
|
1
|
+
"use strict";import React from 'react';import{View,StyleSheet}from 'react-native';import{GestureHandlerRootView,Gesture,GestureDetector}from 'react-native-gesture-handler';import{runOnJS}from 'react-native-reanimated';import{GameControlButton,GameOverModal,GameSettingsModal}from "../../helpers/index.js";import{DEFAULT_GAME_SETTINGS,GAME_IDS}from "../../services/UtilsService.js";import{GameBackground,GameGrid,ScoreBoard}from "./components/index.js";import{useSnakeStore,useScore,useIsPlaying,useIsGameOver}from "./SnakeStore.js";import{SNAKE_COLORS}from "./SnakeConstants.js";import{jsx as _jsx,jsxs as _jsxs}from "react/jsx-runtime";export const Snake = React.memo(({settings,onSettingsChange})=>{const score = useScore();const isPlaying = useIsPlaying();const isGameOver = useIsGameOver();const startGame = useSnakeStore(state => state.startGame);const stopGame = useSnakeStore(state => state.stopGame);const resetGame = useSnakeStore(state => state.resetGame);const initializeGame = useSnakeStore(state => state.initializeGame);const updateSettings = useSnakeStore(state => state.updateSettings);const changeDirection = useSnakeStore(state => state.changeDirection);const prevSettingsRef = React.useRef({enableSounds:true,enableHaptics:true});const prevDifficultyRef = React.useRef('easy');React.useEffect(()=>{const enableSounds = settings?.enableSounds ?? true;const enableHaptics = settings?.enableHaptics ?? true;if(prevSettingsRef.current.enableSounds !== enableSounds || prevSettingsRef.current.enableHaptics !== enableHaptics){prevSettingsRef.current ={enableSounds,enableHaptics};updateSettings(enableSounds,enableHaptics);}},[updateSettings,settings?.enableSounds,settings?.enableHaptics]);React.useEffect(()=>{const difficulty = settings?.difficulty || 'easy';if(prevDifficultyRef.current !== difficulty){prevDifficultyRef.current = difficulty;initializeGame(difficulty);}},[initializeGame,settings?.difficulty]);const handleStartGame = React.useCallback(()=>{startGame();},[startGame]);const handleResetGame = React.useCallback(()=>{resetGame();},[resetGame]);const handleStopGame = React.useCallback(()=>{stopGame();},[stopGame]);const handleSwipe = React.useCallback(direction =>{try{if(!isPlaying)return;requestAnimationFrame(()=>{try{changeDirection(direction);}catch(error){console.error('Direction change error:',error);}});}catch(error){console.error('Swipe handler error:',error);}},[isPlaying,changeDirection]);const panGesture = React.useMemo(()=> Gesture.Pan().onEnd(event =>{const threshold = 30;const absX = Math.abs(event.translationX);const absY = Math.abs(event.translationY);if(absX > threshold || absY > threshold){if(absX > absY){if(event.translationX > 0){runOnJS(handleSwipe)('RIGHT');}else{runOnJS(handleSwipe)('LEFT');}}else{if(event.translationY > 0){runOnJS(handleSwipe)('DOWN');}else{runOnJS(handleSwipe)('UP');}}}}),[handleSwipe]);const gameControlButtonProps = React.useMemo(()=>({isPlaying,gameOver:isGameOver,onStartGame:handleStartGame,onStopGame:handleStopGame,onResetGame:handleResetGame,startButtonText:'Start Slithering!',startButtonSubtext:'Swipe to move or turn.',startButtonColor:SNAKE_COLORS.BUTTON_PRIMARY,startButtonBorderColor:SNAKE_COLORS.BUTTON_SECONDARY}),[isPlaying,isGameOver,handleStartGame,handleStopGame,handleResetGame]);const gameOverModalProps = React.useMemo(()=>({isVisible:isGameOver,score,onPlayAgain:handleResetGame,buttonText:'Play Again!',primaryColor:"rgba(0,204,106,0.5)",borderColor:"rgba(0,204,106,0.5)",buttonColor:"#ffffff",buttonBorderColor:"#ffffff",buttonTextColor:"#00cc6a",scoreFormatter:s => `${s}`}),[isGameOver,score,handleResetGame]);const gameSettingsModalProps = React.useMemo(()=>({gameId:GAME_IDS.SNAKE,settings:settings || DEFAULT_GAME_SETTINGS,onSettingsChange}),[settings,onSettingsChange]);const offset = settings?.offset ?? 0;return _jsx(GameBackground,{children:_jsxs(GestureHandlerRootView,{style:styles.container,children:[_jsx(GestureDetector,{gesture:panGesture,children:_jsx(View,{style:styles.container,children:_jsx(View,{style:[styles.gridContainer,{paddingTop:offset}],children:_jsx(GameGrid,{})})})}),_jsx(ScoreBoard,{offset:offset}),_jsx(GameControlButton,{...gameControlButtonProps}),_jsx(GameOverModal,{...gameOverModalProps}),_jsx(GameSettingsModal,{...gameSettingsModalProps})]})});});Snake.displayName = 'Snake';const styles = StyleSheet.create({container:{flex:1},gridContainer:{flex:1,justifyContent:'center',alignItems:'center'}});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["React","View","StyleSheet","GestureHandlerRootView","Gesture","GestureDetector","runOnJS","GameControlButton","GameOverModal","GameSettingsModal","DEFAULT_GAME_SETTINGS","GAME_IDS","GameBackground","GameGrid","ScoreBoard","useSnakeStore","useScore","useIsPlaying","useIsGameOver","SNAKE_COLORS","jsx","_jsx","jsxs","_jsxs","Snake","memo","settings","onSettingsChange","score","isPlaying","isGameOver","startGame","state","stopGame","resetGame","initializeGame","updateSettings","changeDirection","prevSettingsRef","useRef","enableSounds","enableHaptics","prevDifficultyRef","useEffect","current","difficulty","handleStartGame","useCallback","handleResetGame","handleStopGame","handleSwipe","direction","requestAnimationFrame","error","console","panGesture","useMemo","Pan","onEnd","event","threshold","absX","Math","abs","translationX","absY","translationY","gameControlButtonProps","gameOver","onStartGame","onStopGame","onResetGame","startButtonText","startButtonSubtext","startButtonColor","BUTTON_PRIMARY","startButtonBorderColor","BUTTON_SECONDARY","gameOverModalProps","isVisible","onPlayAgain","buttonText","primaryColor","
|
|
1
|
+
{"version":3,"names":["React","View","StyleSheet","GestureHandlerRootView","Gesture","GestureDetector","runOnJS","GameControlButton","GameOverModal","GameSettingsModal","DEFAULT_GAME_SETTINGS","GAME_IDS","GameBackground","GameGrid","ScoreBoard","useSnakeStore","useScore","useIsPlaying","useIsGameOver","SNAKE_COLORS","jsx","_jsx","jsxs","_jsxs","Snake","memo","settings","onSettingsChange","score","isPlaying","isGameOver","startGame","state","stopGame","resetGame","initializeGame","updateSettings","changeDirection","prevSettingsRef","useRef","enableSounds","enableHaptics","prevDifficultyRef","useEffect","current","difficulty","handleStartGame","useCallback","handleResetGame","handleStopGame","handleSwipe","direction","requestAnimationFrame","error","console","panGesture","useMemo","Pan","onEnd","event","threshold","absX","Math","abs","translationX","absY","translationY","gameControlButtonProps","gameOver","onStartGame","onStopGame","onResetGame","startButtonText","startButtonSubtext","startButtonColor","BUTTON_PRIMARY","startButtonBorderColor","BUTTON_SECONDARY","gameOverModalProps","isVisible","onPlayAgain","buttonText","primaryColor","borderColor","buttonColor","buttonBorderColor","buttonTextColor","scoreFormatter","s","gameSettingsModalProps","gameId","SNAKE","offset","children","style","styles","container","gesture","gridContainer","paddingTop","displayName","create","flex","justifyContent","alignItems"],"sourceRoot":"../../../../src","sources":["games/snake/Snake.tsx"],"mappings":";;AAAA,OAAOA,KAAK,MAAM,OAAO;AACzB,SAASC,IAAI,EAAEC,UAAU,QAAQ,cAAc;AAC/C,SAASC,sBAAsB,EAAEC,OAAO,EAAEC,eAAe,QAAQ,8BAA8B;AAC/F,SAASC,OAAO,QAAQ,yBAAyB;AACjD,SAASC,iBAAiB,EAAEC,aAAa,EAAEC,iBAAiB,QAAQ,wBAAe;AACnF,SAAyBC,qBAAqB,EAAEC,QAAQ,QAAQ,gCAA6B;AAC7F,SAASC,cAAc,EAAEC,QAAQ,EAAEC,UAAU,QAAQ,uBAAc;AACnE,SACEC,aAAa,EACbC,QAAQ,EACRC,YAAY,EACZC,aAAa,QACR,iBAAc;AACrB,SAASC,YAAY,QAAwB,qBAAkB;AAAC,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAGhE,OAAO,MAAMC,KAA0B,gBAAGxB,KAAK,CAACyB,IAAI,CAAC,CAAC;EAAEC,QAAQ;EAAEC;AAAiB,CAAC,KAAK;EACvF;EAC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Aa,EAAE;cAAEC,UAAU,EAAEP;YAAO,CAAC,CAAE;YAAAC,QAAA,eAC1DxE,IAAA,CAACR,QAAQ,IAAE;UAAC,CACR;QAAC,CACH;MAAC,CACQ,CAAC,eAGlBQ,IAAA,CAACP,UAAU;QAAC8E,MAAM,EAAEA;MAAO,CAAE,CAAC,eAG9BvE,IAAA,CAACd,iBAAiB;QAAA,GAAK4D;MAAsB,CAAG,CAAC,eAGjD9C,IAAA,CAACb,aAAa;QAAA,GAAKsE;MAAkB,CAAG,CAAC,eAGzCzD,IAAA,CAACZ,iBAAiB;QAAA,GAAKgF;MAAsB,CAAG,CAAC;IAAA,CAC3B;EAAC,CACX,CAAC;AAErB,CAAC,CAAC;AAEFjE,KAAK,CAAC4E,WAAW,GAAG,OAAO;AAE3B,MAAML,MAAM,GAAG7F,UAAU,CAACmG,MAAM,CAAC;EAC/BL,SAAS,EAAE;IACTM,IAAI,EAAE;EACR,CAAC;EACDJ,aAAa,EAAE;IACbI,IAAI,EAAE,CAAC;IACPC,cAAc,EAAE,QAAQ;IACxBC,UAAU,EAAE;EACd;AACF,CAAC,CAAC","ignoreList":[]}
|
|
@@ -1,138 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import { Dimensions } from 'react-native';
|
|
4
|
-
const {
|
|
5
|
-
width
|
|
6
|
-
} = Dimensions.get('window');
|
|
7
|
-
|
|
8
|
-
// Snake Game Constants
|
|
9
|
-
export const SNAKE_GAME_CONFIG = {
|
|
10
|
-
// Grid configuration
|
|
11
|
-
GRID_SIZE: 20,
|
|
12
|
-
// 20x20 grid
|
|
13
|
-
CELL_SIZE: Math.floor((width - 40) / 20),
|
|
14
|
-
// Calculate cell size based on screen width
|
|
15
|
-
|
|
16
|
-
// Game timing - Lower value = faster snake
|
|
17
|
-
GAME_SPEED: {
|
|
18
|
-
easy: 400,
|
|
19
|
-
// milliseconds per move - Slow
|
|
20
|
-
medium: 300,
|
|
21
|
-
// Medium speed
|
|
22
|
-
hard: 200 // Fast speed
|
|
23
|
-
},
|
|
24
|
-
// Scoring - multiplied by difficulty
|
|
25
|
-
POINTS_PER_FOOD: 10,
|
|
26
|
-
POINTS_PER_SPECIAL_FOOD: 25,
|
|
27
|
-
DIFFICULTY_MULTIPLIER: {
|
|
28
|
-
easy: 1,
|
|
29
|
-
medium: 2,
|
|
30
|
-
hard: 3
|
|
31
|
-
},
|
|
32
|
-
// Special food spawn chance
|
|
33
|
-
SPECIAL_FOOD_CHANCE: 0.15,
|
|
34
|
-
// 15% chance
|
|
35
|
-
SPECIAL_FOOD_DURATION: 5000,
|
|
36
|
-
// 5 seconds
|
|
37
|
-
|
|
38
|
-
// Initial snake length
|
|
39
|
-
INITIAL_SNAKE_LENGTH: 3
|
|
40
|
-
};
|
|
41
|
-
export const SNAKE_COLORS = {
|
|
42
|
-
// Background
|
|
43
|
-
BACKGROUND: '#252541',
|
|
44
|
-
GRID_LINE: 'rgba(255, 255, 255, 0.15)',
|
|
45
|
-
// Snake colors - vibrant gradient
|
|
46
|
-
SNAKE_HEAD: '#00ff88',
|
|
47
|
-
SNAKE_BODY: '#00cc6a',
|
|
48
|
-
SNAKE_TAIL: '#00994d',
|
|
49
|
-
SNAKE_OUTLINE: '#004d26',
|
|
50
|
-
// Food colors
|
|
51
|
-
FOOD_NORMAL: '#ff4757',
|
|
52
|
-
FOOD_SPECIAL: '#ffa502',
|
|
53
|
-
FOOD_GLOW: 'rgba(255, 71, 87, 0.5)',
|
|
54
|
-
// UI colors
|
|
55
|
-
SCORE_TEXT: '#ffffff',
|
|
56
|
-
SCORE_BACKGROUND: 'rgba(0, 255, 136, 0.2)',
|
|
57
|
-
BUTTON_PRIMARY: '#00cc6a',
|
|
58
|
-
BUTTON_SECONDARY: '#00994d',
|
|
59
|
-
MODAL_PRIMARY: 'rgba(0, 255, 136, 0.95)',
|
|
60
|
-
MODAL_BORDER: 'rgba(0, 204, 106, 0.9)',
|
|
61
|
-
MODAL_BUTTON: '#00ff88',
|
|
62
|
-
MODAL_BUTTON_BORDER: '#00cc6a',
|
|
63
|
-
// Game over
|
|
64
|
-
COLLISION_COLOR: '#ff4757'
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// Direction constants
|
|
68
|
-
export const DIRECTIONS = {
|
|
69
|
-
UP: {
|
|
70
|
-
x: 0,
|
|
71
|
-
y: -1
|
|
72
|
-
},
|
|
73
|
-
DOWN: {
|
|
74
|
-
x: 0,
|
|
75
|
-
y: 1
|
|
76
|
-
},
|
|
77
|
-
LEFT: {
|
|
78
|
-
x: -1,
|
|
79
|
-
y: 0
|
|
80
|
-
},
|
|
81
|
-
RIGHT: {
|
|
82
|
-
x: 1,
|
|
83
|
-
y: 0
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
// Snake Game Theme - Green/Neon theme
|
|
87
|
-
export const SNAKE_THEME = {
|
|
88
|
-
backgroundColor: '#1a1a2e',
|
|
89
|
-
headerBackgroundColor: '#00ff88',
|
|
90
|
-
headerTextColor: '#1a1a2e',
|
|
91
|
-
sectionBackgroundColor: 'rgba(0, 255, 136, 0.15)',
|
|
92
|
-
sectionTitleColor: '#00ff88',
|
|
93
|
-
buttonSelectedColor: '#00ff88',
|
|
94
|
-
buttonUnselectedColor: 'rgba(0, 255, 136, 0.2)',
|
|
95
|
-
buttonSelectedTextColor: '#1a1a2e',
|
|
96
|
-
buttonUnselectedTextColor: '#00ff88',
|
|
97
|
-
switchTrackColorFalse: 'rgba(0, 255, 136, 0.3)',
|
|
98
|
-
switchTrackColorTrue: '#00ff88',
|
|
99
|
-
switchThumbColor: '#ffffff',
|
|
100
|
-
infoTextColor: 'rgba(255, 255, 255, 0.7)'
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
// Snake Difficulty Descriptions
|
|
104
|
-
export const SNAKE_DIFFICULTY_DESCRIPTIONS = {
|
|
105
|
-
easy: 'Slow speed • 1x points • Perfect for beginners',
|
|
106
|
-
medium: 'Medium speed • 2x points • Balanced challenge',
|
|
107
|
-
hard: 'Fast speed • 3x points • Expert level'
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
// Snake sounds
|
|
111
|
-
export const SNAKE_SOUNDS = {
|
|
112
|
-
EAT: {
|
|
113
|
-
text: 'Nom!',
|
|
114
|
-
pitch: 1.2,
|
|
115
|
-
rate: 1.5
|
|
116
|
-
},
|
|
117
|
-
SPECIAL: {
|
|
118
|
-
text: 'Bonus!',
|
|
119
|
-
pitch: 1.5,
|
|
120
|
-
rate: 2.0
|
|
121
|
-
},
|
|
122
|
-
COLLISION: {
|
|
123
|
-
text: 'Ouch!',
|
|
124
|
-
pitch: 0.8,
|
|
125
|
-
rate: 1.2
|
|
126
|
-
},
|
|
127
|
-
WIN: {
|
|
128
|
-
text: 'Victory!',
|
|
129
|
-
pitch: 1.6,
|
|
130
|
-
rate: 1.8
|
|
131
|
-
},
|
|
132
|
-
LOSE: {
|
|
133
|
-
text: 'Game Over',
|
|
134
|
-
pitch: 0.8,
|
|
135
|
-
rate: 1.2
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
//# sourceMappingURL=SnakeConstants.js.map
|
|
1
|
+
"use strict";import{Dimensions}from 'react-native';const{width}= Dimensions.get('window');export const SNAKE_GAME_CONFIG ={GRID_SIZE:20,CELL_SIZE:Math.floor((width - 40)/ 20),GAME_SPEED:{easy:400,medium:300,hard:200},POINTS_PER_FOOD:10,POINTS_PER_SPECIAL_FOOD:25,DIFFICULTY_MULTIPLIER:{easy:1,medium:2,hard:3},SPECIAL_FOOD_CHANCE:0.15,SPECIAL_FOOD_DURATION:5000,INITIAL_SNAKE_LENGTH:3};export const SNAKE_COLORS ={BACKGROUND:'#252541',GRID_LINE:'rgba(255,255,255,0.15)',SNAKE_HEAD:'#00ff88',SNAKE_BODY:'#00cc6a',SNAKE_TAIL:'#00994d',SNAKE_OUTLINE:'#004d26',FOOD_NORMAL:'#ff4757',FOOD_SPECIAL:'#ffa502',FOOD_GLOW:'rgba(255,71,87,0.5)',SCORE_TEXT:'#ffffff',SCORE_BACKGROUND:'rgba(0,255,136,0.2)',BUTTON_PRIMARY:'#00cc6a',BUTTON_SECONDARY:'#00994d',MODAL_PRIMARY:'rgba(0,255,136,0.95)',MODAL_BORDER:'rgba(0,204,106,0.9)',MODAL_BUTTON:'#00ff88',MODAL_BUTTON_BORDER:'#00cc6a',COLLISION_COLOR:'#ff4757'};export const DIRECTIONS ={UP:{x:0,y:-1},DOWN:{x:0,y:1},LEFT:{x:-1,y:0},RIGHT:{x:1,y:0}};export const SNAKE_THEME ={backgroundColor:'rgba(0,0,0,0.7)',headerBackgroundColor:'#00cc6a',headerTextColor:'#ffffff',sectionBackgroundColor:'rgba(0,204,106,0.15)',sectionTitleColor:'#00cc6a',buttonSelectedColor:'#00cc6a',buttonUnselectedColor:'rgba(255,255,255,0.2)',buttonSelectedTextColor:'#ffffff',buttonUnselectedTextColor:'rgba(255,255,255,0.7)',switchTrackColorFalse:'rgba(0,204,106,0.3)',switchTrackColorTrue:'#00cc6a',switchThumbColor:'#ffffff',infoTextColor:'rgba(255,255,255,0.9)'};export const SNAKE_DIFFICULTY_DESCRIPTIONS ={easy:'Slow speed • 1x points • Perfect for beginners',medium:'Medium speed • 2x points • Balanced challenge',hard:'Fast speed • 3x points • Expert level'};export const SNAKE_SOUNDS ={EAT:{text:'Nom!',pitch:1.2,rate:1.5},SPECIAL:{text:'Bonus!',pitch:1.5,rate:2.0},COLLISION:{text:'Ouch!',pitch:0.8,rate:1.2},WIN:{text:'Victory!',pitch:1.6,rate:1.8},LOSE:{text:'Game Over',pitch:0.8,rate:1.2}};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Dimensions","width","get","SNAKE_GAME_CONFIG","GRID_SIZE","CELL_SIZE","Math","floor","GAME_SPEED","easy","medium","hard","POINTS_PER_FOOD","POINTS_PER_SPECIAL_FOOD","DIFFICULTY_MULTIPLIER","SPECIAL_FOOD_CHANCE","SPECIAL_FOOD_DURATION","INITIAL_SNAKE_LENGTH","SNAKE_COLORS","BACKGROUND","GRID_LINE","SNAKE_HEAD","SNAKE_BODY","SNAKE_TAIL","SNAKE_OUTLINE","FOOD_NORMAL","FOOD_SPECIAL","FOOD_GLOW","SCORE_TEXT","SCORE_BACKGROUND","BUTTON_PRIMARY","BUTTON_SECONDARY","MODAL_PRIMARY","MODAL_BORDER","MODAL_BUTTON","MODAL_BUTTON_BORDER","COLLISION_COLOR","DIRECTIONS","UP","x","y","DOWN","LEFT","RIGHT","SNAKE_THEME","backgroundColor","headerBackgroundColor","headerTextColor","sectionBackgroundColor","sectionTitleColor","buttonSelectedColor","buttonUnselectedColor","buttonSelectedTextColor","buttonUnselectedTextColor","switchTrackColorFalse","switchTrackColorTrue","switchThumbColor","infoTextColor","SNAKE_DIFFICULTY_DESCRIPTIONS","SNAKE_SOUNDS","EAT","text","pitch","rate","SPECIAL","COLLISION","WIN","LOSE"],"sourceRoot":"../../../../src","sources":["games/snake/SnakeConstants.ts"],"mappings":";;AAAA,SAASA,UAAU,QAAQ,cAAc;AAGzC,MAAM;EAAEC;AAAM,CAAC,GAAGD,UAAU,CAACE,GAAG,CAAC,QAAQ,CAAC;;AAE1C;AACA,OAAO,MAAMC,iBAAiB,GAAG;EAC/B;EACAC,SAAS,EAAE,EAAE;EAAE;EACfC,SAAS,EAAEC,IAAI,CAACC,KAAK,CAAC,CAACN,KAAK,GAAG,EAAE,IAAI,EAAE,CAAC;EAAE;;EAE1C;EACAO,UAAU,EAAE;IACVC,IAAI,EAAE,GAAG;IAAK;IACdC,MAAM,EAAE,GAAG;IAAG;IACdC,IAAI,EAAE,GAAG,CAAM;EACjB,CAAC;EAED;EACAC,eAAe,EAAE,EAAE;EACnBC,uBAAuB,EAAE,EAAE;EAC3BC,qBAAqB,EAAE;IACrBL,IAAI,EAAE,CAAC;IACPC,MAAM,EAAE,CAAC;IACTC,IAAI,EAAE;EACR,CAAC;EAED;EACAI,mBAAmB,EAAE,IAAI;EAAE;EAC3BC,qBAAqB,EAAE,IAAI;EAAE;;EAE7B;EACAC,oBAAoB,EAAE;AACxB,CAAU;AAEV,OAAO,MAAMC,YAAY,GAAG;EAC1B;EACAC,UAAU,EAAE,SAAS;EACrBC,SAAS,EAAE,2BAA2B;EAEtC;EACAC,UAAU,EAAE,SAAS;EACrBC,UAAU,EAAE,SAAS;EACrBC,UAAU,EAAE,SAAS;EACrBC,aAAa,EAAE,SAAS;EAExB;EACAC,WAAW,EAAE,SAAS;EACtBC,YAAY,EAAE,SAAS;EACvBC,SAAS,EAAE,wBAAwB;EAEnC;EACAC,UAAU,EAAE,SAAS;EACrBC,gBAAgB,EAAE,wBAAwB;EAC1CC,cAAc,EAAE,SAAS;EACzBC,gBAAgB,EAAE,SAAS;EAC3BC,aAAa,EAAE,yBAAyB;EACxCC,YAAY,EAAE,wBAAwB;EACtCC,YAAY,EAAE,SAAS;EACvBC,mBAAmB,EAAE,SAAS;EAE9B;EACAC,eAAe,EAAE;AACnB,CAAU;;AAEV;AACA,OAAO,MAAMC,UAAU,GAAG;EACxBC,EAAE,EAAE;IAAEC,CAAC,EAAE,CAAC;IAAEC,CAAC,EAAE,CAAC;EAAE,CAAC;EACnBC,IAAI,EAAE;IAAEF,CAAC,EAAE,CAAC;IAAEC,CAAC,EAAE;EAAE,CAAC;EACpBE,IAAI,EAAE;IAAEH,CAAC,EAAE,CAAC,CAAC;IAAEC,CAAC,EAAE;EAAE,CAAC;EACrBG,KAAK,EAAE;IAAEJ,CAAC,EAAE,CAAC;IAAEC,CAAC,EAAE;EAAE;AACtB,CAAU;AAIV;AACA,OAAO,MAAMI,WAA8B,GAAG;EAC5CC,eAAe,EAAE,
|
|
1
|
+
{"version":3,"names":["Dimensions","width","get","SNAKE_GAME_CONFIG","GRID_SIZE","CELL_SIZE","Math","floor","GAME_SPEED","easy","medium","hard","POINTS_PER_FOOD","POINTS_PER_SPECIAL_FOOD","DIFFICULTY_MULTIPLIER","SPECIAL_FOOD_CHANCE","SPECIAL_FOOD_DURATION","INITIAL_SNAKE_LENGTH","SNAKE_COLORS","BACKGROUND","GRID_LINE","SNAKE_HEAD","SNAKE_BODY","SNAKE_TAIL","SNAKE_OUTLINE","FOOD_NORMAL","FOOD_SPECIAL","FOOD_GLOW","SCORE_TEXT","SCORE_BACKGROUND","BUTTON_PRIMARY","BUTTON_SECONDARY","MODAL_PRIMARY","MODAL_BORDER","MODAL_BUTTON","MODAL_BUTTON_BORDER","COLLISION_COLOR","DIRECTIONS","UP","x","y","DOWN","LEFT","RIGHT","SNAKE_THEME","backgroundColor","headerBackgroundColor","headerTextColor","sectionBackgroundColor","sectionTitleColor","buttonSelectedColor","buttonUnselectedColor","buttonSelectedTextColor","buttonUnselectedTextColor","switchTrackColorFalse","switchTrackColorTrue","switchThumbColor","infoTextColor","SNAKE_DIFFICULTY_DESCRIPTIONS","SNAKE_SOUNDS","EAT","text","pitch","rate","SPECIAL","COLLISION","WIN","LOSE"],"sourceRoot":"../../../../src","sources":["games/snake/SnakeConstants.ts"],"mappings":";;AAAA,SAASA,UAAU,QAAQ,cAAc;AAGzC,MAAM;EAAEC;AAAM,CAAC,GAAGD,UAAU,CAACE,GAAG,CAAC,QAAQ,CAAC;;AAE1C;AACA,OAAO,MAAMC,iBAAiB,GAAG;EAC/B;EACAC,SAAS,EAAE,EAAE;EAAE;EACfC,SAAS,EAAEC,IAAI,CAACC,KAAK,CAAC,CAACN,KAAK,GAAG,EAAE,IAAI,EAAE,CAAC;EAAE;;EAE1C;EACAO,UAAU,EAAE;IACVC,IAAI,EAAE,GAAG;IAAK;IACdC,MAAM,EAAE,GAAG;IAAG;IACdC,IAAI,EAAE,GAAG,CAAM;EACjB,CAAC;EAED;EACAC,eAAe,EAAE,EAAE;EACnBC,uBAAuB,EAAE,EAAE;EAC3BC,qBAAqB,EAAE;IACrBL,IAAI,EAAE,CAAC;IACPC,MAAM,EAAE,CAAC;IACTC,IAAI,EAAE;EACR,CAAC;EAED;EACAI,mBAAmB,EAAE,IAAI;EAAE;EAC3BC,qB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B;EACjDC,uBAAuB,EAAE,SAAS;EAClCC,yBAAyB,EAAE,0BAA0B;EAAE;EACvDC,qBAAqB,EAAE,wBAAwB;EAC/CC,oBAAoB,EAAE,SAAS;EAAE;EACjCC,gBAAgB,EAAE,SAAS;EAC3BC,aAAa,EAAE,0BAA0B,CAAE;AAC7C,CAAC;;AAED;AACA,OAAO,MAAMC,6BAA6B,GAAG;EAC3CjD,IAAI,EAAE,gDAAgD;EACtDC,MAAM,EAAE,+CAA+C;EACvDC,IAAI,EAAE;AACR,CAAU;;AAEV;AACA,OAAO,MAAMgD,YAAY,GAAG;EAC1BC,GAAG,EAAE;IAAEC,IAAI,EAAE,MAAM;IAAEC,KAAK,EAAE,GAAG;IAAEC,IAAI,EAAE;EAAI,CAAC;EAC5CC,OAAO,EAAE;IAAEH,IAAI,EAAE,QAAQ;IAAEC,KAAK,EAAE,GAAG;IAAEC,IAAI,EAAE;EAAI,CAAC;EAClDE,SAAS,EAAE;IAAEJ,IAAI,EAAE,OAAO;IAAEC,KAAK,EAAE,GAAG;IAAEC,IAAI,EAAE;EAAI,CAAC;EACnDG,GAAG,EAAE;IAAEL,IAAI,EAAE,UAAU;IAAEC,KAAK,EAAE,GAAG;IAAEC,IAAI,EAAE;EAAI,CAAC;EAChDI,IAAI,EAAE;IAAEN,IAAI,EAAE,WAAW;IAAEC,KAAK,EAAE,GAAG;IAAEC,IAAI,EAAE;EAAI;AACnD,CAAU","ignoreList":[]}
|
|
@@ -1,148 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import { SNAKE_GAME_CONFIG, DIRECTIONS } from "./SnakeConstants.js";
|
|
4
|
-
export class SnakeService {
|
|
5
|
-
nextId = 1;
|
|
6
|
-
|
|
7
|
-
// Initialize snake at center of grid
|
|
8
|
-
initializeSnake() {
|
|
9
|
-
const centerX = Math.floor(SNAKE_GAME_CONFIG.GRID_SIZE / 2);
|
|
10
|
-
const centerY = Math.floor(SNAKE_GAME_CONFIG.GRID_SIZE / 2);
|
|
11
|
-
const snake = [];
|
|
12
|
-
for (let i = 0; i < SNAKE_GAME_CONFIG.INITIAL_SNAKE_LENGTH; i++) {
|
|
13
|
-
snake.push({
|
|
14
|
-
id: this.nextId++,
|
|
15
|
-
x: centerX - i,
|
|
16
|
-
y: centerY
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
return snake;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Generate random food position (not on snake)
|
|
23
|
-
generateFood(snake, isSpecial = false) {
|
|
24
|
-
let position;
|
|
25
|
-
let attempts = 0;
|
|
26
|
-
const maxAttempts = 100;
|
|
27
|
-
do {
|
|
28
|
-
position = {
|
|
29
|
-
x: Math.floor(Math.random() * SNAKE_GAME_CONFIG.GRID_SIZE),
|
|
30
|
-
y: Math.floor(Math.random() * SNAKE_GAME_CONFIG.GRID_SIZE)
|
|
31
|
-
};
|
|
32
|
-
attempts++;
|
|
33
|
-
} while (attempts < maxAttempts && this.isPositionOnSnake(position, snake));
|
|
34
|
-
const food = {
|
|
35
|
-
id: this.nextId++,
|
|
36
|
-
x: position.x,
|
|
37
|
-
y: position.y,
|
|
38
|
-
isSpecial
|
|
39
|
-
};
|
|
40
|
-
if (isSpecial) {
|
|
41
|
-
food.expiresAt = Date.now() + SNAKE_GAME_CONFIG.SPECIAL_FOOD_DURATION;
|
|
42
|
-
}
|
|
43
|
-
return food;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Check if position is on snake
|
|
47
|
-
isPositionOnSnake(position, snake) {
|
|
48
|
-
return snake.some(segment => segment.x === position.x && segment.y === position.y);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Move snake in direction
|
|
52
|
-
moveSnake(snake, direction) {
|
|
53
|
-
const head = snake[0];
|
|
54
|
-
if (!head) return snake;
|
|
55
|
-
const directionVector = DIRECTIONS[direction];
|
|
56
|
-
const newHead = {
|
|
57
|
-
id: this.nextId++,
|
|
58
|
-
x: head.x + directionVector.x,
|
|
59
|
-
y: head.y + directionVector.y
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
// Add new head
|
|
63
|
-
const newSnake = [newHead, ...snake];
|
|
64
|
-
// Remove tail (will be added back if food is eaten)
|
|
65
|
-
newSnake.pop();
|
|
66
|
-
return newSnake;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Check collision with walls
|
|
70
|
-
checkWallCollision(head) {
|
|
71
|
-
return head.x < 0 || head.x >= SNAKE_GAME_CONFIG.GRID_SIZE || head.y < 0 || head.y >= SNAKE_GAME_CONFIG.GRID_SIZE;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Check collision with self
|
|
75
|
-
checkSelfCollision(snake) {
|
|
76
|
-
const head = snake[0];
|
|
77
|
-
if (!head) return false;
|
|
78
|
-
|
|
79
|
-
// Check if head collides with any body segment (skip head itself)
|
|
80
|
-
for (let i = 1; i < snake.length; i++) {
|
|
81
|
-
const segment = snake[i];
|
|
82
|
-
if (segment && head.x === segment.x && head.y === segment.y) {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Check if snake ate food
|
|
90
|
-
checkFoodCollision(head, food) {
|
|
91
|
-
return head.x === food.x && head.y === food.y;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Grow snake (add segment at tail)
|
|
95
|
-
growSnake(snake) {
|
|
96
|
-
const tail = snake[snake.length - 1];
|
|
97
|
-
if (!tail) return snake;
|
|
98
|
-
|
|
99
|
-
// Add new segment at tail position
|
|
100
|
-
const newSegment = {
|
|
101
|
-
id: this.nextId++,
|
|
102
|
-
x: tail.x,
|
|
103
|
-
y: tail.y
|
|
104
|
-
};
|
|
105
|
-
return [...snake, newSegment];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Check if direction change is valid (can't reverse)
|
|
109
|
-
isValidDirectionChange(currentDirection, newDirection) {
|
|
110
|
-
const opposites = {
|
|
111
|
-
UP: 'DOWN',
|
|
112
|
-
DOWN: 'UP',
|
|
113
|
-
LEFT: 'RIGHT',
|
|
114
|
-
RIGHT: 'LEFT'
|
|
115
|
-
};
|
|
116
|
-
return opposites[currentDirection] !== newDirection;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Get opposite direction
|
|
120
|
-
getOppositeDirection(direction) {
|
|
121
|
-
const opposites = {
|
|
122
|
-
UP: 'DOWN',
|
|
123
|
-
DOWN: 'UP',
|
|
124
|
-
LEFT: 'RIGHT',
|
|
125
|
-
RIGHT: 'LEFT'
|
|
126
|
-
};
|
|
127
|
-
return opposites[direction];
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Reset ID counter
|
|
131
|
-
resetIdCounter() {
|
|
132
|
-
this.nextId = 1;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Check if special food has expired
|
|
136
|
-
isSpecialFoodExpired(food) {
|
|
137
|
-
if (!food.isSpecial || !food.expiresAt) return false;
|
|
138
|
-
return Date.now() > food.expiresAt;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Calculate score based on food type and difficulty
|
|
142
|
-
calculateScore(isSpecialFood, difficulty) {
|
|
143
|
-
const baseScore = isSpecialFood ? SNAKE_GAME_CONFIG.POINTS_PER_SPECIAL_FOOD : SNAKE_GAME_CONFIG.POINTS_PER_FOOD;
|
|
144
|
-
const difficultyMultiplier = SNAKE_GAME_CONFIG.DIFFICULTY_MULTIPLIER[difficulty];
|
|
145
|
-
return baseScore * difficultyMultiplier;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
//# sourceMappingURL=SnakeService.js.map
|
|
1
|
+
"use strict";import{SNAKE_GAME_CONFIG,DIRECTIONS}from "./SnakeConstants.js";export class SnakeService{nextId = 1;initializeSnake(){const centerX = Math.floor(SNAKE_GAME_CONFIG.GRID_SIZE / 2);const centerY = Math.floor(SNAKE_GAME_CONFIG.GRID_SIZE / 2);const snake = [];for(let i = 0;i < SNAKE_GAME_CONFIG.INITIAL_SNAKE_LENGTH;i++){snake.push({id:this.nextId++,x:centerX - i,y:centerY});}return snake;}generateFood(snake,isSpecial = false){let position;let attempts = 0;const maxAttempts = 100;do{position ={x:Math.floor(Math.random()* SNAKE_GAME_CONFIG.GRID_SIZE),y:Math.floor(Math.random()* SNAKE_GAME_CONFIG.GRID_SIZE)};attempts++;}while(attempts < maxAttempts && this.isPositionOnSnake(position,snake));const food ={id:this.nextId++,x:position.x,y:position.y,isSpecial};if(isSpecial){food.expiresAt = Date.now()+ SNAKE_GAME_CONFIG.SPECIAL_FOOD_DURATION;}return food;}isPositionOnSnake(position,snake){return snake.some(segment => segment.x === position.x && segment.y === position.y);}moveSnake(snake,direction){const head = snake[0];if(!head)return snake;const directionVector = DIRECTIONS[direction];const newHead ={id:this.nextId++,x:head.x + directionVector.x,y:head.y + directionVector.y};const newSnake = [newHead,...snake];newSnake.pop();return newSnake;}checkWallCollision(head){return head.x < 0 || head.x >= SNAKE_GAME_CONFIG.GRID_SIZE || head.y < 0 || head.y >= SNAKE_GAME_CONFIG.GRID_SIZE;}checkSelfCollision(snake){const head = snake[0];if(!head)return false;for(let i = 1;i < snake.length;i++){const segment = snake[i];if(segment && head.x === segment.x && head.y === segment.y){return true;}}return false;}checkFoodCollision(head,food){return head.x === food.x && head.y === food.y;}growSnake(snake){const tail = snake[snake.length - 1];if(!tail)return snake;const newSegment ={id:this.nextId++,x:tail.x,y:tail.y};return [...snake,newSegment];}isValidDirectionChange(currentDirection,newDirection){const opposites ={UP:'DOWN',DOWN:'UP',LEFT:'RIGHT',RIGHT:'LEFT'};return opposites[currentDirection] !== newDirection;}getOppositeDirection(direction){const opposites ={UP:'DOWN',DOWN:'UP',LEFT:'RIGHT',RIGHT:'LEFT'};return opposites[direction];}resetIdCounter(){this.nextId = 1;}isSpecialFoodExpired(food){if(!food.isSpecial || !food.expiresAt)return false;return Date.now()> food.expiresAt;}calculateScore(isSpecialFood,difficulty){const baseScore = isSpecialFood ? SNAKE_GAME_CONFIG.POINTS_PER_SPECIAL_FOOD:SNAKE_GAME_CONFIG.POINTS_PER_FOOD;const difficultyMultiplier = SNAKE_GAME_CONFIG.DIFFICULTY_MULTIPLIER[difficulty];return baseScore * difficultyMultiplier;}}
|