Puddisnky commited on
Commit
bd9f242
·
verified ·
1 Parent(s): c68eef3

The player must select a direction of movement. Then end the turn for the character to move

Browse files
Files changed (2) hide show
  1. script.js +59 -14
  2. style.css +7 -1
script.js CHANGED
@@ -3,6 +3,7 @@ document.addEventListener('DOMContentLoaded', () => {
3
  const gameState = {
4
  playerTurn: true,
5
  playerPosition: { x: 100, y: 100 },
 
6
  enemies: [
7
  { x: 300, y: 100, speed: 2, direction: 'right' },
8
  { x: 200, y: 200, speed: 1, direction: 'down' }
@@ -10,8 +11,9 @@ document.addEventListener('DOMContentLoaded', () => {
10
  swordAngle: 0,
11
  swordSpinning: false,
12
  swordSpeed: 0,
13
- gameLog: []
14
- };
 
15
 
16
  // DOM elements
17
  const gameCanvas = document.getElementById('gameCanvas');
@@ -62,15 +64,59 @@ document.addEventListener('DOMContentLoaded', () => {
62
  gameCanvas.appendChild(enemyElement);
63
  });
64
  }
65
-
66
  function setupControls() {
67
- moveUp.addEventListener('click', () => movePlayer(0, -30));
68
- moveDown.addEventListener('click', () => movePlayer(0, 30));
69
- moveLeft.addEventListener('click', () => movePlayer(-30, 0));
70
- moveRight.addEventListener('click', () => movePlayer(30, 0));
71
- spinBlade.addEventListener('click', startSpin);
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  }
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  function movePlayer(dx, dy) {
75
  if (!gameState.playerTurn) return;
76
 
@@ -159,32 +205,31 @@ document.addEventListener('DOMContentLoaded', () => {
159
  }
160
  });
161
  }
162
-
163
  function endTurn() {
164
  gameState.playerTurn = false;
 
 
165
  turnIndicator.textContent = "Enemy Turn";
166
  turnIndicator.className = "px-3 py-1 bg-red-600 rounded-full";
167
-
168
- setTimeout(() => {
169
  // Enemy moves
170
  moveEnemies();
171
  renderGame();
172
 
173
  // Blade keeps spinning if it was activated
174
  updateSpinning();
175
-
176
  // Return to player turn
177
  setTimeout(() => {
178
  if (!gameState.swordSpinning) {
179
  gameState.playerTurn = true;
180
  turnIndicator.textContent = "Player Turn";
181
  turnIndicator.className = "px-3 py-1 bg-blue-600 rounded-full";
182
- logMessage("Your turn!");
183
  } else {
184
  // If blade is still spinning, enemies get another turn
185
  endTurn();
186
  }
187
- }, 500);
188
  }, 1000);
189
  }
190
 
 
3
  const gameState = {
4
  playerTurn: true,
5
  playerPosition: { x: 100, y: 100 },
6
+ selectedMove: null,
7
  enemies: [
8
  { x: 300, y: 100, speed: 2, direction: 'right' },
9
  { x: 200, y: 200, speed: 1, direction: 'down' }
 
11
  swordAngle: 0,
12
  swordSpinning: false,
13
  swordSpeed: 0,
14
+ gameLog: [],
15
+ moveConfirmed: false
16
+ };
17
 
18
  // DOM elements
19
  const gameCanvas = document.getElementById('gameCanvas');
 
64
  gameCanvas.appendChild(enemyElement);
65
  });
66
  }
 
67
  function setupControls() {
68
+ moveUp.addEventListener('click', () => selectMove(0, -30));
69
+ moveDown.addEventListener('click', () => selectMove(0, 30));
70
+ moveLeft.addEventListener('click', () => selectMove(-30, 0));
71
+ moveRight.addEventListener('click', () => selectMove(30, 0));
72
+ spinBlade.addEventListener('click', () => {
73
+ if (!gameState.moveConfirmed) {
74
+ selectMove(0, 0, true);
75
+ }
76
+ });
77
+
78
+ // Add confirm button
79
+ const confirmButton = document.createElement('button');
80
+ confirmButton.id = 'confirmMove';
81
+ confirmButton.className = 'btn-action bg-green-600 hover:bg-green-700 col-span-3';
82
+ confirmButton.innerHTML = '<i data-feather="check"></i> Confirm Move';
83
+ document.querySelector('.grid.grid-cols-3').appendChild(confirmButton);
84
+ confirmButton.addEventListener('click', confirmMove);
85
+ feather.replace();
86
  }
87
 
88
+ function selectMove(dx, dy, isSpin = false) {
89
+ if (!gameState.playerTurn || gameState.moveConfirmed) return;
90
+
91
+ const newX = gameState.playerPosition.x + dx;
92
+ const newY = gameState.playerPosition.y + dy;
93
+
94
+ // Boundary check for moves (not needed for spin)
95
+ if (!isSpin && (newX < 0 || newX > gameCanvas.offsetWidth - 30 ||
96
+ newY < 0 || newY > gameCanvas.offsetHeight - 30)) {
97
+ logMessage("Can't move there - out of bounds!");
98
+ return;
99
+ }
100
+
101
+ gameState.selectedMove = { dx, dy, isSpin };
102
+ logMessage(isSpin ? "Spin blade selected!" : `Move to (${newX}, ${newY}) selected`);
103
+ }
104
+
105
+ function confirmMove() {
106
+ if (!gameState.playerTurn || !gameState.selectedMove || gameState.moveConfirmed) return;
107
+
108
+ gameState.moveConfirmed = true;
109
+
110
+ if (gameState.selectedMove.isSpin) {
111
+ startSpin();
112
+ } else {
113
+ gameState.playerPosition.x += gameState.selectedMove.dx;
114
+ gameState.playerPosition.y += gameState.selectedMove.dy;
115
+ renderGame();
116
+ endTurn();
117
+ }
118
+ }
119
+
120
  function movePlayer(dx, dy) {
121
  if (!gameState.playerTurn) return;
122
 
 
205
  }
206
  });
207
  }
 
208
  function endTurn() {
209
  gameState.playerTurn = false;
210
+ gameState.selectedMove = null;
211
+ gameState.moveConfirmed = false;
212
  turnIndicator.textContent = "Enemy Turn";
213
  turnIndicator.className = "px-3 py-1 bg-red-600 rounded-full";
214
+ setTimeout(() => {
 
215
  // Enemy moves
216
  moveEnemies();
217
  renderGame();
218
 
219
  // Blade keeps spinning if it was activated
220
  updateSpinning();
 
221
  // Return to player turn
222
  setTimeout(() => {
223
  if (!gameState.swordSpinning) {
224
  gameState.playerTurn = true;
225
  turnIndicator.textContent = "Player Turn";
226
  turnIndicator.className = "px-3 py-1 bg-blue-600 rounded-full";
227
+ logMessage("Your turn! Select a move.");
228
  } else {
229
  // If blade is still spinning, enemies get another turn
230
  endTurn();
231
  }
232
+ }, 500);
233
  }, 1000);
234
  }
235
 
style.css CHANGED
@@ -5,11 +5,17 @@ body {
5
  display: flex;
6
  flex-direction: column;
7
  }
8
-
9
  .btn-action {
10
  @apply bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded transition-all flex items-center justify-center gap-2;
11
  }
12
 
 
 
 
 
 
 
 
13
  #gameCanvas {
14
  border: 2px solid #4a5568;
15
  position: relative;
 
5
  display: flex;
6
  flex-direction: column;
7
  }
 
8
  .btn-action {
9
  @apply bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded transition-all flex items-center justify-center gap-2;
10
  }
11
 
12
+ .btn-action.selected {
13
+ @apply bg-purple-600 hover:bg-purple-700;
14
+ }
15
+
16
+ #confirmMove:disabled {
17
+ @apply bg-gray-500 cursor-not-allowed;
18
+ }
19
  #gameCanvas {
20
  border: 2px solid #4a5568;
21
  position: relative;