diff options
author | elioat <elioat@tilde.institute> | 2025-02-17 09:20:00 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-02-17 09:20:00 -0500 |
commit | 7d91eff5fc4245429e60c55b016fe1abeb21c6ac (patch) | |
tree | 0308e19c1a4823bf2df10a84d7ec0fa758daecfe /html/tower/docs/uiHandlers.js.html | |
parent | 86dc7f62ba51d1c9a6a8d943d188c2ddb6efa148 (diff) | |
download | tour-7d91eff5fc4245429e60c55b016fe1abeb21c6ac.tar.gz |
*
Diffstat (limited to 'html/tower/docs/uiHandlers.js.html')
-rw-r--r-- | html/tower/docs/uiHandlers.js.html | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/html/tower/docs/uiHandlers.js.html b/html/tower/docs/uiHandlers.js.html new file mode 100644 index 0000000..35f3867 --- /dev/null +++ b/html/tower/docs/uiHandlers.js.html @@ -0,0 +1,146 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <title>JSDoc: Source: uiHandlers.js</title> + + <script src="scripts/prettify/prettify.js"> </script> + <script src="scripts/prettify/lang-css.js"> </script> + <!--[if lt IE 9]> + <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> + <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> +</head> + +<body> + +<div id="main"> + + <h1 class="page-title">Source: uiHandlers.js</h1> + + + + + + + <section> + <article> + <pre class="prettyprint source linenums"><code>/** + * UI Handlers Module + * + * This module manages user interactions and UI state. + * Implements: + * 1. Drag and Drop system + * 2. Event handling + * 3. UI state management + * 4. Input validation + * + * @module uiHandlers + */ + +/** + * Initializes drag and drop functionality for tower placement + * Implements HTML5 Drag and Drop API + * + * @param {HTMLCanvasElement} canvas - Game canvas element + * @param {Object} gameState - Current game state + * @returns {Object} Drag handlers and state information + */ +function initializeDragAndDrop(canvas, gameState) { + let draggedTowerType = null; + let hoverCell = null; + + const dragHandlers = { + /** + * Handles start of tower drag operation + * Sets up drag data and visual feedback + */ + onDragStart: (e) => { + draggedTowerType = e.target.dataset.towerType; + e.dataTransfer.setData('text/plain', ''); + }, + + /** + * Handles end of drag operation + * Cleans up drag state + */ + onDragEnd: () => { + draggedTowerType = null; + hoverCell = null; + }, + + /** + * Handles drag over canvas + * Updates hover position and preview + */ + onDragOver: (e) => { + e.preventDefault(); + const rect = canvas.getBoundingClientRect(); + const x = Math.floor((e.clientX - rect.left) / (canvas.width / 20)); + const y = Math.floor((e.clientY - rect.top) / (canvas.height / 20)); + + hoverCell = (x >= 0 && x < 20 && y >= 0 && y < 20) ? { x, y } : null; + }, + + /** + * Handles tower placement on drop + * Validates placement and updates game state + */ + onDrop: (e) => { + e.preventDefault(); + if (!draggedTowerType || !hoverCell) return; + + placeTower(gameState, draggedTowerType, hoverCell); + draggedTowerType = null; + hoverCell = null; + } + }; + + return { + dragHandlers, + getHoverInfo: () => ({ draggedTowerType, hoverCell }) + }; +} + +/** + * Places a tower in the game grid + * Implements tower placement validation and state updates + * + * @param {Object} gameState - Current game state + * @param {string} towerType - Type of tower to place + * @param {Object} position - Grid position for placement + */ +function placeTower(gameState, towerType, position) { + const tower = TowerTypes[towerType]; + if ( + gameState.grid[position.y][position.x] === 'empty' && + gameState.currency >= tower.cost + ) { + gameState.grid[position.y][position.x] = 'tower'; + gameState.towers.push(createTower(towerType, { ...position })); + gameState.currency -= tower.cost; + } +} </code></pre> + </article> + </section> + + + + +</div> + +<nav> + <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-game.html">game</a></li><li><a href="module-gameState.html">gameState</a></li><li><a href="module-mechanics.html">mechanics</a></li><li><a href="module-path.html">path</a></li><li><a href="module-renderer.html">renderer</a></li><li><a href="module-uiHandlers.html">uiHandlers</a></li></ul> +</nav> + +<br class="clear"> + +<footer> + Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.3</a> on Mon Feb 17 2025 09:19:19 GMT-0500 (Eastern Standard Time) +</footer> + +<script> prettyPrint(); </script> +<script src="scripts/linenumber.js"> </script> +</body> +</html> |