File size: 9,395 Bytes
7428b13
 
 
a46ce65
7428b13
 
4c208f2
 
 
a46ce65
7428b13
 
 
 
 
c61508c
7428b13
4c208f2
 
 
 
 
 
7428b13
 
 
 
4c208f2
7428b13
 
 
4c208f2
7428b13
 
 
 
4c208f2
 
 
 
 
 
 
7428b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a46ce65
4c208f2
 
7428b13
 
 
4c208f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a46ce65
 
 
4c208f2
 
 
 
 
 
7428b13
4c208f2
 
 
a46ce65
4c208f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a46ce65
 
4c208f2
 
 
 
 
 
a46ce65
4c208f2
 
 
 
 
 
 
a46ce65
4c208f2
 
 
 
a46ce65
4c208f2
 
 
 
 
 
a46ce65
 
4c208f2
 
 
a46ce65
4c208f2
 
 
 
 
 
 
7428b13
 
 
4c208f2
 
7428b13
4c208f2
 
7428b13
4c208f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7428b13
 
 
 
 
 
 
 
90a88fc
7428b13
90a88fc
7428b13
 
 
 
 
 
 
 
4c208f2
 
7428b13
 
 
4c208f2
7428b13
 
 
 
 
 
 
 
4c208f2
 
c61508c
4c208f2
7428b13
 
 
 
 
 
 
 
 
 
90a88fc
 
 
7428b13
 
 
 
 
e95e2d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7428b13
 
 
afa3e65
7428b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e95e2d9
 
7428b13
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<script lang="ts">
  import { onMount } from 'svelte';
  import { fade } from 'svelte/transition';
  import type { PicletInstance, BattleMove } from '$lib/db/schema';
  import BattleField from '../Battle/BattleField.svelte';
  import BattleControls from '../Battle/BattleControls.svelte';
  import { BattleEngine } from '$lib/battle-engine/BattleEngine';
  import type { BattleState, MoveAction } from '$lib/battle-engine/types';
  import { picletInstanceToBattleDefinition, battlePicletToInstance } from '$lib/utils/battleConversion';
  import { getEffectivenessText, getEffectivenessColor } from '$lib/types/picletTypes';
  
  export let playerPiclet: PicletInstance;
  export let enemyPiclet: PicletInstance;
  export let isWildBattle: boolean = true;
  export let onBattleEnd: (result: any) => void = () => {};
  export let rosterPiclets: PicletInstance[] = []; // All roster piclets passed from parent
  
  // Initialize battle engine
  let battleEngine: BattleEngine;
  let battleState: BattleState;
  let currentPlayerPiclet = playerPiclet;
  let currentEnemyPiclet = enemyPiclet;
  
  // Battle state
  let currentMessage = isWildBattle 
    ? `A wild ${enemyPiclet.nickname} appeared!` 
    : `Trainer wants to battle!`;
  let battlePhase: 'intro' | 'main' | 'moveSelect' | 'picletSelect' | 'ended' = 'intro';  
  let processingTurn = false;
  let battleEnded = false;
  
  // HP animation states  
  let playerHpPercentage = playerPiclet.currentHp / playerPiclet.maxHp;
  let enemyHpPercentage = enemyPiclet.currentHp / enemyPiclet.maxHp;
  
  onMount(() => {
    // Initialize battle engine with converted piclet definitions
    const playerDefinition = picletInstanceToBattleDefinition(playerPiclet);
    const enemyDefinition = picletInstanceToBattleDefinition(enemyPiclet);
    
    battleEngine = new BattleEngine(playerDefinition, enemyDefinition, playerPiclet.level, enemyPiclet.level);
    battleState = battleEngine.getState();
    
    // Start intro sequence
    setTimeout(() => {
      currentMessage = `Go, ${playerPiclet.nickname}!`;
      setTimeout(() => {
        currentMessage = `What will ${playerPiclet.nickname} do?`;
        battlePhase = 'main';
      }, 1500);
    }, 2000);
  });
  
  function handleAction(action: string) {
    if (processingTurn || battleEnded) return;
    
    switch (action) {
      case 'catch':
        if (isWildBattle) {
          processingTurn = true;
          currentMessage = 'You threw a Piclet Ball!';
          setTimeout(() => {
            currentMessage = 'The wild piclet broke free!';
            processingTurn = false;
          }, 2000);
        }
        break;
      case 'run':
        if (isWildBattle) {
          currentMessage = 'Got away safely!';
          battleEnded = true;
          setTimeout(() => onBattleEnd(false), 1500);
        } else {
          currentMessage = "You can't run from a trainer battle!";
        }
        break;
    }
  }
  
  function handleMoveSelect(move: BattleMove) {
    if (!battleEngine) return;
    
    battlePhase = 'main';
    processingTurn = true;
    
    // Find the corresponding move in the battle engine
    const battleMove = battleState.playerPiclet.moves.find(m => m.move.name === move.name);
    if (!battleMove) return;
    
    const moveAction: MoveAction = {
      type: 'move',
      moveIndex: battleState.playerPiclet.moves.indexOf(battleMove)
    };
    
    try {
      // Choose random enemy move (could be improved with AI)
      const availableEnemyMoves = battleState.opponentPiclet.moves.filter(m => m.currentPP > 0);
      if (availableEnemyMoves.length === 0) {
        currentMessage = `${currentEnemyPiclet.nickname} has no moves left!`;
        processingTurn = false;
        return;
      }
      
      const randomEnemyMove = availableEnemyMoves[Math.floor(Math.random() * availableEnemyMoves.length)];
      const enemyMoveIndex = battleState.opponentPiclet.moves.indexOf(randomEnemyMove);
      const enemyAction: MoveAction = {
        type: 'move', 
        moveIndex: enemyMoveIndex
      };
      
      // Execute the turn - battle engine handles priority automatically
      const result = battleEngine.executeTurn(moveAction, enemyAction);
      battleState = battleEngine.getState();
      
      // Show battle messages with timing
      if (result.log && result.log.length > 0) {
        let messageIndex = 0;
        function showNextBattleMessage() {
          if (messageIndex < result.log.length) {
            currentMessage = result.log[messageIndex];
            messageIndex++;
            setTimeout(showNextBattleMessage, 1500);
          } else {
            // After all messages, check battle end or continue
            finalizeTurn();
          }
        }
        showNextBattleMessage();
      } else {
        finalizeTurn();
      }
      
      function finalizeTurn() {
        // Update UI state from battle engine
        updateUIFromBattleState();
        
        // Check for battle end
        if (battleState.winner) {
          battleEnded = true;
          const winMessage = battleState.winner === 'player' 
            ? `${currentEnemyPiclet.nickname} fainted! You won!`
            : `${currentPlayerPiclet.nickname} fainted! You lost!`;
          currentMessage = winMessage;
          setTimeout(() => {
            onBattleEnd(battleState.winner === 'player');
          }, 2000);
        } else {
          setTimeout(() => {
            currentMessage = `What will ${currentPlayerPiclet.nickname} do?`;
            processingTurn = false;
          }, 1000);
        }
      }
    } catch (error) {
      console.error('Battle engine error:', error);
      currentMessage = 'Something went wrong in battle!';
      processingTurn = false;
    }
  }
  
  
  function updateUIFromBattleState() {
    if (!battleState) return;
    
    // Update player piclet state
    currentPlayerPiclet = battlePicletToInstance(battleState.playerPiclet, currentPlayerPiclet);
    playerHpPercentage = battleState.playerPiclet.currentHp / battleState.playerPiclet.maxHp;
    
    // Update enemy piclet state  
    currentEnemyPiclet = battlePicletToInstance(battleState.opponentPiclet, currentEnemyPiclet);
    enemyHpPercentage = battleState.opponentPiclet.currentHp / battleState.opponentPiclet.maxHp;
  }
  
  function handlePicletSelect(piclet: PicletInstance) {
    if (!battleEngine) return;
    
    battlePhase = 'main';
    currentMessage = `Come back, ${currentPlayerPiclet.nickname}!`;
    
    setTimeout(() => {
      // Convert the selected piclet to battle definition and switch
      const newPicletDefinition = picletInstanceToBattleDefinition(piclet);
      
      try {
        // TODO: Implement switching in battle engine
        // For now, just update the UI
        currentPlayerPiclet = piclet;
        playerHpPercentage = piclet.currentHp / piclet.maxHp;
        currentMessage = `Go, ${piclet.nickname}!`;
        
        setTimeout(() => {
          currentMessage = `What will ${piclet.nickname} do?`;
        }, 1500);
      } catch (error) {
        console.error('Switch error:', error);
        currentMessage = 'Unable to switch Piclets!';
      }
    }, 1500);
  }
  
  function handleBack() {
    battlePhase = 'main';
  }
</script>

<div class="battle-page" transition:fade={{ duration: 300 }}>
  <nav class="battle-nav">
    <button class="back-button" on:click={() => onBattleEnd('cancelled')} style="display: none;">
      ← Back
    </button>
    <h1>{isWildBattle ? 'Wild Battle' : 'Battle'}</h1>
    <div class="nav-spacer"></div>
  </nav>
  
  <div class="battle-content">
    <BattleField 
      playerPiclet={currentPlayerPiclet}
      enemyPiclet={currentEnemyPiclet}
      {playerHpPercentage}
      {enemyHpPercentage}
      showIntro={battlePhase === 'intro'}
      {battleState}
    />
    
    <BattleControls
      {currentMessage}
      {battlePhase}
      {processingTurn}
      {battleEnded}
      {isWildBattle}
      playerPiclet={currentPlayerPiclet}
      enemyPiclet={currentEnemyPiclet}
      {rosterPiclets}
      {battleState}
      onAction={handleAction}
      onMoveSelect={handleMoveSelect}
      onPicletSelect={handlePicletSelect}
      onBack={handleBack}
    />
  </div>
</div>

<style>
  .battle-page {
    position: fixed;
    inset: 0;
    z-index: 1000;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: #f8f9fa;
    overflow: hidden;
    padding-top: env(safe-area-inset-top);
  }
  
  @media (max-width: 768px) {
    .battle-page {
      background: white;
    }
    
    .battle-page::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      height: env(safe-area-inset-top);
      background: white;
      z-index: 1;
    }
  }
  
  .battle-nav {
    display: none; /* Hide navigation in battle */
  }
  
  .back-button {
    background: none;
    border: none;
    color: #007bff;
    font-size: 1rem;
    cursor: pointer;
    padding: 0.5rem;
  }
  
  .battle-nav h1 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
    color: #1a1a1a;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }
  
  .nav-spacer {
    width: 60px;
  }
  
  .battle-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    background: #f8f9fa;
  }
</style>