Nokia Snake Game - Component Method Signatures

Component API surface

Nokia Snake Game - Component Method Signatures

Game Engine Component Methods

Game Loop Methods

/**
 * Initialize the game engine with configuration
 * @param {Object} config - Game configuration
 * @returns {Promise<void>}
 */
async initialize(config)

/**
 * Start the main game loop
 * @returns {void}
 */
startGameLoop()

/**
 * Stop the game loop
 * @returns {void}
 */
stopGameLoop()

/**
 * Update game state based on time delta
 * @param {number} deltaTime - Time since last update in milliseconds
 * @returns {void}
 */
update(deltaTime)

/**
 * Fixed update for physics and game logic
 * @param {number} fixedDeltaTime - Fixed time step
 * @returns {void}
 */
fixedUpdate(fixedDeltaTime)

Collision Detection Methods

/**
 * Check for collisions between game objects
 * @param {Array} objects - Game objects to check
 * @returns {Array} Array of collision pairs
 */
checkCollisions(objects)

/**
 * Handle collision between two objects
 * @param {Object} obj1 - First object
 * @param {Object} obj2 - Second object
 * @returns {void}
 */
handleCollision(obj1, obj2)

/**
 * Check if snake collides with itself
 * @param {Array} snakeSegments - Snake body segments
 * @returns {boolean} True if collision detected
 */
checkSelfCollision(snakeSegments)

/**
 * Check if snake collides with walls
 * @param {Object} snakeHead - Snake head position
 * @param {Object} gameBounds - Game boundaries
 * @returns {boolean} True if collision detected
 */
checkWallCollision(snakeHead, gameBounds)

Game State Transition Methods

/**
 * Transition to game start state
 * @returns {void}
 */
transitionToStart()

/**
 * Transition to game playing state
 * @returns {void}
 */
transitionToPlaying()

/**
 * Transition to game paused state
 * @returns {void}
 */
transitionToPaused()

/**
 * Transition to game over state
 * @param {Object} gameOverData - Game over information
 * @returns {void}
 */
transitionToGameOver(gameOverData)

/**
 * Transition to game restart state
 * @returns {void}
 */
transitionToRestart()

Game Rule Enforcement Methods

/**
 * Apply game rules to current state
 * @param {Object} gameState - Current game state
 * @returns {Object} Updated game state
 */
applyGameRules(gameState)

/**
 * Calculate score based on game events
 * @param {Object} gameEvent - Game event
 * @returns {number} Score delta
 */
calculateScore(gameEvent)

/**
 * Apply power-up effects
 * @param {string} powerUpType - Type of power-up
 * @param {Object} gameState - Current game state
 * @returns {Object} Updated game state
 */
applyPowerUp(powerUpType, gameState)

/**
 * Generate new food at random position
 * @param {Object} gameState - Current game state
 * @returns {Object} New food object
 */
generateFood(gameState)

Rendering Component Methods

Canvas Rendering Methods

/**
 * Initialize WebGL 2D context
 * @param {HTMLCanvasElement} canvas - Canvas element
 * @returns {WebGLRenderingContext} WebGL context
 */
initializeWebGLContext(canvas)

/**
 * Render snake segments
 * @param {Array} snakeSegments - Snake body segments
 * @param {Object} style - Visual style
 * @returns {void}
 */
renderSnake(snakeSegments, style)

/**
 * Render food items
 * @param {Array} foodItems - Food objects
 * @param {Object} style - Visual style
 * @returns {void}
 */
renderFood(foodItems, style)

/**
 * Render obstacles/walls
 * @param {Array} obstacles - Obstacle objects
 * @param {Object} style - Visual style
 * @returns {void}
 */
renderObstacles(obstacles, style)

/**
 * Render power-up effects
 * @param {Array} powerUps - Power-up objects
 * @param {Object} style - Visual style
 * @returns {void}
 */
renderPowerUps(powerUps, style)

UI Element Rendering Methods

/**
 * Render score display
 * @param {number} score - Current score
 * @param {Object} position - Display position
 * @returns {void}
 */
renderScore(score, position)

/**
 * Render high score display
 * @param {number} highScore - High score
 * @param {Object} position - Display position
 * @returns {void}
 */
renderHighScore(highScore, position)

/**
 * Render game timer
 * @param {number} time - Current game time
 * @param {Object} position - Display position
 * @returns {void}
 */
renderTimer(time, position)

/**
 * Render game level display
 * @param {number} level - Current level
 * @param {Object} position - Display position
 * @returns {void}
 */
renderLevel(level, position)

Animation Methods

/**
 * Animate snake movement
 * @param {Array} snakeSegments - Snake segments
 * @param {Object} animationConfig - Animation configuration
 * @returns {Promise<void>}
 */
animateSnakeMovement(snakeSegments, animationConfig)

/**
 * Animate food collection
 * @param {Object} food - Food object
 * @returns {Promise<void>}
 */
animateFoodCollection(food)

/**
 * Animate power-up activation
 * @param {Object} powerUp - Power-up object
 * @returns {Promise<void>}
 */
animatePowerUpActivation(powerUp)

/**
 * Animate game over sequence
 * @param {Object} gameOverData - Game over information
 * @returns {Promise<void>}
 */
animateGameOver(gameOverData)

Screen Update Methods

/**
 * Clear the rendering canvas
 * @returns {void}
 */
clearCanvas()

/**
 * Update entire screen with current game state
 * @param {Object} gameState - Current game state
 * @returns {void}
 */
updateScreen(gameState)

/**
 * Handle canvas resize
 * @param {number} width - New width
 * @param {number} height - New height
 * @returns {void}
 */
handleResize(width, height)

/**
 * Apply visual theme
 * @param {Object} theme - Theme configuration
 * @returns {void}
 */
applyTheme(theme)

Input Handler Component Methods

Keyboard Input Handling

/**
 * Initialize keyboard event listeners
 * @returns {void}
 */
initializeKeyboardListeners()

/**
 * Handle keydown events
 * @param {KeyboardEvent} event - Keyboard event
 * @returns {void}
 */
handleKeyDown(event)

/**
 * Handle keyup events
 * @param {KeyboardEvent} event - Keyboard event
 * @returns {void}
 */
handleKeyUp(event)

/**
 * Get current input state
 * @returns {Object} Current input state
 */
getInputState()

/**
 * Reset input state
 * @returns {void}
 */
resetInputState()

Input Validation Methods

/**
 * Validate keyboard input
 * @param {KeyboardEvent} event - Keyboard event
 * @returns {boolean} True if valid input
 */
validateKeyboardInput(event)

/**
 * Sanitize input data
 * @param {Object} inputData - Raw input data
 * @returns {Object} Sanitized input data
 */
sanitizeInput(inputData)

/**
 * Check for input conflicts
 * @param {Object} currentInput - Current input state
 * @param {Object} newInput - New input
 * @returns {boolean} True if conflict detected
 */
checkInputConflict(currentInput, newInput)

Control Mapping Methods

/**
 * Map keyboard key to game action
 * @param {string} key - Keyboard key
 * @param {string} action - Game action
 * @returns {void}
 */
mapKeyToAction(key, action)

/**
 * Get action for keyboard key
 * @param {string} key - Keyboard key
 * @returns {string|null} Mapped action or null
 */
getActionForKey(key)

/**
 * Reset control mappings to defaults
 * @returns {void}
 */
resetControlMappings()

/**
 * Save custom control mappings
 * @param {Object} mappings - Control mappings
 * @returns {void}
 */
saveControlMappings(mappings)

State Management Component Methods

State Persistence Methods

/**
 * Save game state to localStorage
 * @param {Object} state - Game state to save
 * @returns {Promise<void>}
 */
async saveGameState(state)

/**
 * Load game state from localStorage
 * @returns {Promise<Object>} Loaded game state
 */
async loadGameState()

/**
 * Clear saved game state
 * @returns {Promise<void>}
 */
async clearSavedState()

/**
 * Check if saved state exists
 * @returns {Promise<boolean>} True if saved state exists
 */
async hasSavedState()

Score Tracking Methods

/**
 * Update current score
 * @param {number} delta - Score change
 * @returns {void}
 */
updateScore(delta)

/**
 * Get current score
 * @returns {number} Current score
 */
getCurrentScore()

/**
 * Update high score if beaten
 * @param {number} score - New score
 * @returns {boolean} True if high score beaten
 */
updateHighScore(score)

/**
 * Get high scores list
 * @returns {Array} Array of high scores
 */
getHighScores()

State Transition Methods

/**
 * Dispatch state change action
 * @param {Object} action - State action
 * @returns {void}
 */
dispatch(action)

/**
 * Subscribe to state changes
 * @param {Function} callback - Callback function
 * @returns {Function} Unsubscribe function
 */
subscribe(callback)

/**
 * Get current state
 * @returns {Object} Current game state
 */
getState()

/**
 * Reset state to initial values
 * @returns {void}
 */
resetState()

Note on Business Rules

Detailed business rules for each method will be defined in the Functional Design stage (per-unit, CONSTRUCTION phase). These method signatures provide the interface contracts that components will implement.