-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.java
More file actions
782 lines (683 loc) · 37.1 KB
/
controller.java
File metadata and controls
782 lines (683 loc) · 37.1 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
package lordOfTheRings;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import keyboard.utilesTeclado;
import lordOfTheRings.misExcepciones.CharacterExistsException;
import lordOfTheRings.misUtiles.util;
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
public class controller implements Initializable {
//OBJETO JUEGO A TRAVÉS DEL CUAL REALIZARÉ LAS ACCIONES PRINCIPALES DEL JUEGO
juego game = new juego();
public void setGame(juego game) {
this.game = game;
}
/**
* Objetos de la interfaz gráfica . . .
*/
/**
* Parte de creación de personajes
*/
@FXML
private ComboBox<String> tipo_heroe_combo;
@FXML
private ComboBox<String> tipo_bestia_combo;
@FXML
private TextField nombre_heroe_field;
@FXML
private TextField nombre_bestia_field;
@FXML
private TextField vida_heroe_field;
@FXML
private TextField vida_bestia_field;
@FXML
private TextField armadura_heroe_field;
@FXML
private TextField armadura_bestia_field;
@FXML
private Label error_heroe_label;
@FXML
private Label error_bestia_label;
@FXML
private Label error_vida_heroe_label;
@FXML
private Label error_vida_bestia_label;
@FXML
private Label error_armadura_heroe_label;
@FXML
private Label error_armadura_bestia_label;
@FXML
private Button anadir_heroe;
@FXML
private Button anadir_bestia;
/**
* Parte de la edición de listas
*/
@FXML
private ListView<heroe> lista_heroes_field;
@FXML
private ListView<bestia> lista_bestias_field;
@FXML
private Button subir_heroe = new Button();
@FXML
private Button bajar_heroe = new Button();
@FXML
private Button eliminar_heroe = new Button();
@FXML
private Button editar_heroe = new Button();
@FXML
private Button subir_bestia = new Button();
@FXML
private Button bajar_bestia = new Button();
@FXML
private Button eliminar_bestia = new Button();
@FXML
private Button editar_bestia = new Button();
/**
* Parte de la lucha principal
*/
@FXML
private Button lucha;
@FXML
private Button empezar_batalla;
@FXML
private Button load;
@FXML
private Label error_botones;
@FXML
private Pane pane_lucha;
@FXML
private Button delete_saves;
@FXML
private TextArea fight_area;
//BORDES EMPLEADOS
Border bordeGenerico;
BorderStroke bordeStrokeError = new BorderStroke(Paint.valueOf("red"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT);
Border bordeError = new Border(bordeStrokeError);
//VARIABLES EMPLEADAS EN LA INTRODUCCIÓN DE DATOS
boolean vidaHeroeCorrecta = false;
boolean armaduraHeroeCorrecta = false;
boolean vidaBestiaCorrecta = false;
boolean armaduraBestiaCorrecta = false;
public TextArea getFight_area() {
return fight_area;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
combo_heroes(); //INICIAR COMBO DE HEROES
combo_bestias(); //INICIAR COMBO DE BESTIAS
bordeGenerico = vida_heroe_field.getBorder(); //CAPTURA DEL BORDE GENÉRICO
game.setEmpezarLucha(false);
game.setCarga(true);
fight_area.appendText("CARGAR// Con la partida vacía, carga la partida anterior . . .");
fight_area.appendText("\nEMPEZAR BATALLA// Con al menos un ejercito creado, borra todos los datos . . .");
fight_area.appendText("\nBORRAR AUTOGUARDADO// Borra el archivo que almacena los datos guardados . . .");
}
/**
* Cuando pulsemos sobre los combo, y se seleccione una opción!!! Estos adoptaran el Borde genérico . . .
* En caso de estar vacío, al pulsar en una opción desaparecerá el borde de error.
*
* @param event Evento de 'click'
*/
@FXML
void comboAction(ActionEvent event) {
ComboBox c = (ComboBox) event.getSource();
switch (c.getId()) {
case "tipo_heroe_combo":
if (tipo_heroe_combo.getSelectionModel().isEmpty()) tipo_heroe_combo.setBorder(bordeError);
else tipo_heroe_combo.setBorder(bordeGenerico);
break;
case "tipo_bestia_combo":
if (tipo_bestia_combo.getSelectionModel().isEmpty()) tipo_bestia_combo.setBorder(bordeError);
else tipo_bestia_combo.setBorder(bordeGenerico);
break;
}
}
/**
* Todas las acciones realizadas mediante botones, e identificadas mediante el FXID de cada botón
*
* @param event Evento 'click'
*/
@FXML
void action(ActionEvent event) {
Button b = (Button) event.getSource();
switch (b.getId()) {
case "anadir_heroe":
if (!game.isBotones_off()) {
pintar_campos_vacios(tipo_heroe_combo, nombre_heroe_field, vida_heroe_field, armadura_heroe_field); //SI AL PULSAR ESTAN VACÍOS SE PINTAN DE ROJO
Boolean nombreHeroeCorrecto = !verificar_campo_vacio(nombre_heroe_field); // . . . Y SE CAPTURA UNA BOOLEAN SOBRE EL NOMBRE
campo_numerico_correcto(vida_heroe_field); //SE VERIFICA QUE SEAN NUMÉRICOS
campo_numerico_correcto(armadura_heroe_field);
if (vidaHeroeCorrecta && armaduraHeroeCorrecta && nombreHeroeCorrecto &&
tipo_heroe_combo.getSelectionModel().getSelectedItem() != null) { //SI TODOS LOS DATOS SON CORRECTOS . . .
String nombre = nombre_heroe_field.getText(); //CAPTURA DEL NOMBRE
int vida = Integer.parseInt(vida_heroe_field.getText()); //CAPTURA DE LA VIDA
int armadura = Integer.parseInt(armadura_heroe_field.getText()); //CAPTURA DE LA ARMADURA
tipo tipoHeroe = tipo.valueOf(tipo_heroe_combo.getSelectionModel().getSelectedItem()); //CAPTURA DEL TIPO
try {
game.addCharacter(nombre, vida, armadura, tipoHeroe); //SE LLAMA A LA FUNCION AÑADIR PEROSNAJE (HEROE), PASANDOLE LOS DATOS INTRODUCIDOS
} catch (CharacterExistsException e) {
error_heroe_label.setText(e.getLocalizedMessage()); //SE RECOGE EXCEPCION DE PERSONAJE YA EXISTE - SE PINTA ETIQUETA CON ERROR
}
lista_heroes_field.setItems(FXCollections.observableArrayList(game.getListaHeroes())); //SE PINTA EN LA LISTVIEW DE HEROES, LA LISTA DE HEROES
//TODOS LOS CAMPOS SE REINICIAN
nombre_heroe_field.setText("");
tipo_heroe_combo.getSelectionModel().clearSelection();
tipo_heroe_combo.setBorder(bordeGenerico);
vida_heroe_field.setText("");
armadura_heroe_field.setText("");
game.setEmpezarLucha(true);
game.setCarga(false);
autoguardado();
} else if (verificar_campo_vacio(nombre_heroe_field) || verificar_campo_vacio(vida_heroe_field)
|| verificar_campo_vacio(armadura_heroe_field) ||
tipo_heroe_combo.getSelectionModel().getSelectedItem() == null) { //SI ALGUNO DE LOS DATOS NO HA SIDO INTRODUCIDO . . .
error_heroe_label.setText("Todos los campos son obligatorios . . ."); //SE PINTA ETIQUETA CON ERROR
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "anadir_bestia":
if (!game.isBotones_off()) {
pintar_campos_vacios(tipo_bestia_combo, nombre_bestia_field, vida_bestia_field, armadura_bestia_field); //SI AL PULSAR ESTAN VACÍOS SE PINTAN DE ROJO
Boolean nombreBestiaCorrecto = !verificar_campo_vacio(nombre_bestia_field);// . . . Y SE CAPTURA UNA BOOLEAN SOBRE EL NOMBRE
campo_numerico_correcto(vida_bestia_field); //SE VERIFICA QUE SEAN NUMÉRICOS
campo_numerico_correcto(armadura_bestia_field);
if (vidaBestiaCorrecta && armaduraBestiaCorrecta && nombreBestiaCorrecto &&
tipo_bestia_combo.getSelectionModel().getSelectedItem() != null) { //SI TODOS LOS DATOS SON CORRECTOS . . .
String nombre = nombre_bestia_field.getText(); //SE CAPTURA EL NOMBRE
int vida = Integer.parseInt(vida_bestia_field.getText()); //SE CAPTURA LA VIDA
int armadura = Integer.parseInt(armadura_bestia_field.getText()); //SE CAPTURA LA ARMADURA
tipo tipoBestia = tipo.valueOf(tipo_bestia_combo.getSelectionModel().getSelectedItem()); //SE CAPTURA EL TIPO
try {
game.addCharacter(nombre, vida, armadura, tipoBestia); //SE LLAMA A LA FUNCIÓN AÑADIR PERSONAJE (BESTIA), CON LOS DATOS INTRODUCIDOS
} catch (CharacterExistsException e) {
error_bestia_label.setText(e.getLocalizedMessage()); //SI YA EXISTE SE PINTA UNA ETIQUETA CON EL ERROR
}
lista_bestias_field.setItems(FXCollections.observableArrayList(game.getListaBestias())); //SE PINTA EN LA LISTVIEW DE HEROES, LA LISTA DE HEROES
//SE REINICIAN TODOS LOS CAMPOS
nombre_bestia_field.setText("");
tipo_bestia_combo.getSelectionModel().clearSelection();
tipo_bestia_combo.setBorder(bordeGenerico);
vida_bestia_field.setText("");
armadura_bestia_field.setText("");
game.setEmpezarLucha(true);
game.setCarga(false);
autoguardado();
} else if (vida_bestia_field.getText().isEmpty() || armadura_bestia_field.getText().isEmpty()
|| nombre_bestia_field.getText().isEmpty() ||
tipo_bestia_combo.getSelectionModel().getSelectedItem() == null) { //SI ALGUNO DE LOS DATOS NO HA SIDO INTRODUCIDO . . .
error_bestia_label.setText("Todos los campos son obligatorios . . ."); //SE PINTA ETIQUETA CON ERROR
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "subir_heroe":
if (!game.isBotones_off()) {
heroe auxSubirHeroe = lista_heroes_field.getSelectionModel().getSelectedItem(); //SE CAPTURA EL OBJETO SELECCIONADO
int subirHeroe = game.getListaHeroes().indexOf(auxSubirHeroe); //SE AVERIGUA LA POSICIÓN (X) DE LA LISTA EN LA QUE RESIDE ESTE OBJETO
if (subirHeroe > 0) {//SI ESTA POSICIÓN NO ES LA PRIMERA
heroe auxBajar = game.getListaHeroes().get(subirHeroe - 1); //SE OBTIENE EL OBJETO DE LA POSICIÓN ANTERIOR A X
game.getListaHeroes().set(subirHeroe - 1, auxSubirHeroe); //EL OBJETO AL QUE SE LE HA DICHO 'SUBIR', SUBE DE POSICIÓN EN LA LISTA
game.getListaHeroes().set(subirHeroe, auxBajar); //Y EL OBJETO AL QUE SE LE HA ROBADO EL SITIO, BAJA DE POSICIÓN EN LA LISTA
lista_heroes_field.setItems(FXCollections.observableArrayList(game.getListaHeroes())); //SE MUESTREA LA LISTA
autoguardado();
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "bajar_heroe":
if (!game.isBotones_off()) {
heroe auxBajarHeroe = lista_heroes_field.getSelectionModel().getSelectedItem(); //SE CAPTURA EL OBJETO SELECCIONADO
int bajarHeroe = game.getListaHeroes().indexOf(auxBajarHeroe);//SE AVERIGUA LA POSICION (X) DE LA LISTA EN LA QUE RESIDE ESTE OBJETO
if ((bajarHeroe >= 0) && (bajarHeroe < game.getListaHeroes().size() - 1)) { //SI ESTA POSICION NO ES LA ÚLTIMA . . . Y HAY UN OBJETO SELECCIONADO
heroe auxSubir = game.getListaHeroes().get(bajarHeroe + 1); //SE OBTIENE EL OBJETO DE LA POSICIÓN POSTERIOR A X
game.getListaHeroes().set(bajarHeroe + 1, auxBajarHeroe);//EL OBJETO SELECCIONADO, BAJA DE POSICIÓN EN LA LISTA
game.getListaHeroes().set(bajarHeroe, auxSubir);//Y EL OBJETO AL QUE SE LE HA ROBADO EL SITIO, SUBE DE POSICIÓN EN LA LISTA
lista_heroes_field.setItems(FXCollections.observableArrayList(game.getListaHeroes()));
autoguardado();
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "eliminar_heroe":
if (!game.isBotones_off()) {
heroe auxEliminarHeroe = lista_heroes_field.getSelectionModel().getSelectedItem(); //SE CAPTURA EL OBJETO SELECCIONADO
game.getListaHeroes().remove(auxEliminarHeroe); // . . . Y SE ELIMINA DE LA LISTA
lista_heroes_field.setItems(FXCollections.observableArrayList(game.getListaHeroes())); //SE MUESTREA LA LISTA
autoguardado();
if (game.getListaHeroes().isEmpty() && game.getListaBestias().isEmpty()) {
game.setCarga(true);
game.setEmpezarLucha(false);
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "editar_heroe":
if (!game.isBotones_off()) {
if (lista_heroes_field.getSelectionModel().getSelectedItem() != null) {
heroe modif = lista_heroes_field.getSelectionModel().getSelectedItem();
List<String> choices = new ArrayList<>();
choices.add("Nombre");
choices.add("Tipo");
choices.add("Vida");
choices.add("Armadura");
ChoiceDialog<String> opciones = new ChoiceDialog<>(null, choices);
opciones.setTitle("Editar ");
opciones.setHeaderText("Que opción deseas modificar");
opciones.setContentText("Elige una opción:");
while (true) {
Optional<String> result = opciones.showAndWait();
if (result.isEmpty()) break;
if (result.get().equalsIgnoreCase("nombre")) {
TextInputDialog opcionVida = new TextInputDialog("");
opcionVida.setTitle("Editar ");
opcionVida.setHeaderText("Editar nombre del heroe seleccionado");
opcionVida.setContentText("Introduce un nombre:");
Optional<String> nombre = opcionVida.showAndWait();
nombre.ifPresent(modif::setNombre);
lista_heroes_field.refresh();
autoguardado();
} else if (result.get().equalsIgnoreCase("tipo")) {
ChoiceDialog<tipo> opcionTipo = new ChoiceDialog<>(null, Arrays.stream(tipo.values()).filter(tipo -> tipo.getTipoPersonaje().equals("Heroe")).collect(Collectors.toList()));
opcionTipo.setTitle("Editar ");
opcionTipo.setHeaderText("Editar la raza del heroe seleccionado");
opcionTipo.setContentText("Elige una opción:");
Optional<tipo> tipo = opcionTipo.showAndWait();
tipo.ifPresent(modif::setType);
lista_heroes_field.refresh();
autoguardado();
} else if (result.get().equalsIgnoreCase("vida")) {
TextInputDialog opcionVida = new TextInputDialog("");
opcionVida.setTitle("Editar ");
opcionVida.setHeaderText("Editar vida del heroe seleccionado");
opcionVida.setContentText("Introduce un valor:");
Optional<String> vidaString = opcionVida.showAndWait();
if (vidaString.isPresent()) {
int vida = Integer.parseInt(vidaString.get());
modif.setVida(vida);
lista_heroes_field.refresh();
autoguardado();
}
} else if (result.get().equalsIgnoreCase("armadura")) {
TextInputDialog opcionArmadura = new TextInputDialog("");
opcionArmadura.setTitle("Editar ");
opcionArmadura.setHeaderText("Editar armadura del heroe seleccionado");
opcionArmadura.setContentText("Introduce un valor:");
Optional<String> armaduraString = opcionArmadura.showAndWait();
if (armaduraString.isPresent()) {
int armadura = Integer.parseInt(armaduraString.get());
modif.setArmadura(armadura);
lista_heroes_field.refresh();
autoguardado();
}
}
}
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "subir_bestia":
if (!game.isBotones_off()) {
bestia auxSubirBestia = lista_bestias_field.getSelectionModel().getSelectedItem();//SE CAPTURA EL OBJETO SELECCIONADO
int subirBestia = game.getListaBestias().indexOf(auxSubirBestia);//SE AVERIGUA LA POSICIÓN (X) DE LA LISTA EN LA QUE RESIDE ESTE OBJETO
if (subirBestia > 0) {//SI ESTA POSICIÓN NO ES LA PRIMERA
bestia auxBajar = game.getListaBestias().get(subirBestia - 1);//SE OBTIENE EL OBJETO DE LA POSICIÓN ANTERIOR A X
game.getListaBestias().set(subirBestia - 1, auxSubirBestia);//EL OBJETO AL QUE SE LE HA DICHO 'SUBIR', SUBE DE POSICIÓN EN LA LISTA
game.getListaBestias().set(subirBestia, auxBajar);//Y EL OBJETO AL QUE SE LE HA ROBADO EL SITIO, BAJA DE POSICIÓN EN LA LISTA
lista_bestias_field.setItems(FXCollections.observableArrayList(game.getListaBestias()));//SE MUESTREA LA LISTA
autoguardado();
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "bajar_bestia":
if (!game.isBotones_off()) {
bestia auxBajarBestia = lista_bestias_field.getSelectionModel().getSelectedItem();//SE CAPTURA EL OBJETO SELECCIONADO
int bajarBestia = game.getListaBestias().indexOf(auxBajarBestia); //SE AVERIGUA LA POSICION (X) DE LA LISTA EN LA QUE RESIDE ESTE OBJETO
if ((bajarBestia >= 0) && (bajarBestia < game.getListaBestias().size() - 1)) {//SI ESTA POSICION NO ES LA ÚLTIMA . . . Y HAY UN OBJETO SELECCIONADO
bestia auxSubir = game.getListaBestias().get(bajarBestia + 1); //SE OBTIENE EL OBJETO DE LA POSICIÓN POSTERIOR A X
game.getListaBestias().set(bajarBestia + 1, auxBajarBestia); //EL OBJETO SELECCIONADO, BAJA DE POSICIÓN EN LA LISTA
game.getListaBestias().set(bajarBestia, auxSubir); //Y EL OBJETO AL QUE SE LE HA ROBADO EL SITIO, SUBE DE POSICIÓN EN LA LISTA
lista_bestias_field.setItems(FXCollections.observableArrayList(game.getListaBestias())); //SE MUESTREA LA LISTA
autoguardado();
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "eliminar_bestia":
if (!game.isBotones_off()) {
bestia auxEliminarBestia = lista_bestias_field.getSelectionModel().getSelectedItem(); // SE CAPTURA EL OBJETO SELECCIONADO
game.getListaBestias().remove(auxEliminarBestia); // . . . Y SE ELIMINA DE LA LISTA
lista_bestias_field.setItems(FXCollections.observableArrayList(game.getListaBestias())); //SE MUESTREA LA LISTA
autoguardado();
if (game.getListaHeroes().isEmpty() && game.getListaBestias().isEmpty()) {
game.setCarga(true);
game.setEmpezarLucha(false);
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "editar_bestia":
if (!game.isBotones_off()) {
if (lista_bestias_field.getSelectionModel().getSelectedItem() != null) {
bestia modif = lista_bestias_field.getSelectionModel().getSelectedItem();
List<String> choices = new ArrayList<>();
choices.add("Nombre");
choices.add("Tipo");
choices.add("Vida");
choices.add("Armadura");
ChoiceDialog<String> opciones = new ChoiceDialog<>(null, choices);
opciones.setTitle("Editar ");
opciones.setHeaderText("Que opción deseas modificar");
opciones.setContentText("Elige una opción:");
while (true) {
Optional<String> result = opciones.showAndWait();
if (result.isEmpty()) break;
if (result.get().equalsIgnoreCase("nombre")) {
TextInputDialog opcionVida = new TextInputDialog("");
opcionVida.setTitle("Editar ");
opcionVida.setHeaderText("Editar nombre de la bestia seleccionada");
opcionVida.setContentText("Introduce un nombre:");
Optional<String> nombre = opcionVida.showAndWait();
nombre.ifPresent(modif::setNombre);
lista_bestias_field.refresh();
autoguardado();
} else if (result.get().equalsIgnoreCase("tipo")) {
ChoiceDialog<tipo> opcionTipo = new ChoiceDialog<>(null, Arrays.stream(tipo.values()).filter(tipo -> tipo.getTipoPersonaje().equals("Bestia")).collect(Collectors.toList()));
opcionTipo.setTitle("Editar ");
opcionTipo.setHeaderText("Editar la raza de la bestia seleccionada");
opcionTipo.setContentText("Elige una opción:");
Optional<tipo> tipo = opcionTipo.showAndWait();
tipo.ifPresent(modif::setType);
lista_bestias_field.refresh();
autoguardado();
} else if (result.get().equalsIgnoreCase("vida")) {
TextInputDialog opcionVida = new TextInputDialog("");
opcionVida.setTitle("Editar ");
opcionVida.setHeaderText("Editar vida de la bestia seleccionada");
opcionVida.setContentText("Introduce un valor:");
Optional<String> vidaString = opcionVida.showAndWait();
if (vidaString.isPresent()) {
int vida = Integer.parseInt(vidaString.get());
modif.setVida(vida);
lista_bestias_field.refresh();
autoguardado();
}
} else if (result.get().equalsIgnoreCase("armadura")) {
TextInputDialog opcionArmadura = new TextInputDialog("");
opcionArmadura.setTitle("Editar ");
opcionArmadura.setHeaderText("Editar armadura de la bestia seleccionada");
opcionArmadura.setContentText("Introduce un valor:");
Optional<String> armaduraString = opcionArmadura.showAndWait();
if (armaduraString.isPresent()) {
int armadura = Integer.parseInt(armaduraString.get());
modif.setArmadura(armadura);
lista_bestias_field.refresh();
autoguardado();
}
}
}
}
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "lucha":
if (!game.isBotones_off()) {
if (game.getListaHeroes().size() != 0 && game.getListaBestias().size() != 0) { //SI HAY SOLDADOS EN AMBOS EJERCITOS
fight_area.clear();
game.lucha(fight_area); //SE PINTA EN LA ARENA DE COMBATE EL PROCESO DE LUCHA Y SE DAN DOS OPCIONES
fight_area.appendText("\n\nPulsa 'Volver a Luchar' para jugar con el ejercito formado anteriormente . . .\nPulsa 'Empezar Batalla' para" + " volver a formar tu ejercito\n");
lista_heroes_field.refresh(); //REFRESCAR LISTAS
lista_bestias_field.refresh();
game.setEmpezarLucha(true);
game.setCarga(false);
game.setBotones_off(true);
} //else
//error_botones.setText("Debes completar ambos ejercitos para empezar a luchar"); //SI NO HAY SOLDADOS SE PINTA UN ERROR
} else {
error_botones.setText("Elija 'EMPEZAR BATALLA' o 'LUCHAR DE NUEVO . . .");
}
break;
case "empezar_batalla":
if (game.isEmpezarLucha()) { //SI LA OPCION SE HA ACTIVADO
game.reiniciarListas(); //SE BORRAN LAS LISTAS PRINCIPALES
lista_heroes_field.setItems(FXCollections.observableArrayList(game.getListaHeroes())); //SE MUESTREAN LAS LISTAS
lista_bestias_field.setItems(FXCollections.observableArrayList(game.getListaBestias()));
fight_area.clear(); //SE LIMPIA LA ARENA DEL COMBATE
fight_area.appendText("CARGAR// Con la partida vacía, carga la partida anterior . . .");
fight_area.appendText("\nEMPEZAR BATALLA// Con al menos un ejercito creado, borra todos los datos . . .");
fight_area.appendText("\nBORRAR AUTOGUARDADO// Borra el archivo que almacena los datos guardados . . .");
game.setBotones_off(false);
game.setEmpezarLucha(false); //AL EMPEZAR BATALLA, SE DESHABILITA EMPEZAR DE NUEVO Y LUCHAR DE NUEVO
game.setCarga(true);
error_botones.setText("EMPIEZA LA BATALLA!!!");
}
break;
case "load":
if (game.isCarga()) {
File savegame = new File(util.pathDir + util.pathFile);
if (savegame.exists()) {
cargarPartida(savegame);
} else {
error_botones.setText("No hay partida guardada . . .");
}
}
break;
case "delete_saves":
File archivo = new File(util.pathDir + util.pathFile);
if (archivo.exists()) {
if (archivo.delete()) {
error_botones.setText("PARTIDA ELIMINADA EXITOSAMENTE!!!");
} else {
error_botones.setText("No se ha podido eliminar la partida!");
}
} else {
error_botones.setText("No hay partida guardada. . .");
}
break;
}
}
/**
* Al clicar en alguno de los campos de añadir heroe o bestia, los errores mostrados desaparecerá, dando una nueva oportunidad
*
* @param event
*/
@FXML
void resetErrorHeroeLabel(MouseEvent event) {
error_heroe_label.setText("");
}
@FXML
void resetErrorBestiaLabel(MouseEvent event) {
error_bestia_label.setText("");
}
/**
* Al introducir valores en los campos numéricos . . . se verificará que sean numéricos
*
* @param event
*/
@FXML
void errorCampo(KeyEvent event) {
TextField campo = (TextField) event.getSource();
switch (campo.getId()) {
case "vida_heroe_field":
utilesTeclado u = new utilesTeclado();
if (!utilesTeclado.esUnNumero(vida_heroe_field.getText())) {
vida_heroe_field.setBorder(new Border(bordeStrokeError));
error_vida_heroe_label.setText("Error: Este campo debe ser numérico");
vidaHeroeCorrecta = false;
} else if (vida_heroe_field.getText().equalsIgnoreCase("")) {
error_vida_heroe_label.setText("");
vidaHeroeCorrecta = false;
} else {
error_vida_heroe_label.setText("");
vidaHeroeCorrecta = true;
}
break;
case "armadura_heroe_field":
if (!utilesTeclado.esUnNumero(armadura_heroe_field.getText())) {
armadura_heroe_field.setBorder(new Border(bordeStrokeError));
error_armadura_heroe_label.setText("Error: Este campo debe ser numérico");
armaduraHeroeCorrecta = false;
} else if (armadura_heroe_field.getText().equals("")) {
error_armadura_heroe_label.setText("");
armaduraHeroeCorrecta = false;
} else {
error_armadura_heroe_label.setText("");
armaduraHeroeCorrecta = true;
}
break;
case "vida_bestia_field":
if (!utilesTeclado.esUnNumero(vida_bestia_field.getText())) {
vida_bestia_field.setBorder(new Border(bordeStrokeError));
error_vida_bestia_label.setText("Error: Este campo debe ser numérico");
vidaBestiaCorrecta = false;
} else if (vida_bestia_field.getText().equals("")) {
error_vida_bestia_label.setText("");
vidaBestiaCorrecta = false;
} else {
error_vida_bestia_label.setText("");
vidaBestiaCorrecta = true;
}
break;
case "armadura_bestia_field":
if (!utilesTeclado.esUnNumero(armadura_bestia_field.getText())) {
armadura_bestia_field.setBorder(new Border(bordeStrokeError));
error_armadura_bestia_label.setText("Error: Este campo debe ser numérico");
armaduraBestiaCorrecta = false;
} else if (armadura_bestia_field.getText().equals("")) {
error_armadura_bestia_label.setText("");
armaduraBestiaCorrecta = false;
} else {
error_armadura_bestia_label.setText("");
armaduraBestiaCorrecta = true;
}
break;
}
}
/**
* Si se introduce texto en los campos, el borde volvera a ser el genérico, retirando así el error
*
* @param event
*/
@FXML
void introducirTexto(KeyEvent event) {
TextField aux = (TextField) event.getSource();
aux.setBorder(bordeGenerico);
}
@FXML
void reset_errores_botones(MouseEvent event) {
error_botones.setText("");
}
/**
* Se le pasará un TextField para verificar si hay algo escrito
*
* @param t TextField
* @return Devolverá si está vacío o no
*/
private boolean verificar_campo_vacio(TextField t) {
return t.getText().isEmpty();
}
/**
* Se le pasará un ComboBox y varios TextField para verificar si hay algo selecconado o escrito, y en caso contrario, se pintarán de rojo
* marcando así un error
*
* @param t
*/
private void pintar_campos_vacios(ComboBox combo, TextField... t) {
for (int i = 0; i < t.length; i++) {
if (t[i].getText().isEmpty()) {
t[i].setBorder(bordeError);
} else {
t[i].setBorder(bordeGenerico);
}
}
if (combo.getSelectionModel().isEmpty()) combo.setBorder(bordeError);
else combo.setBorder(bordeGenerico);
}
/**
* Verificará que los campos numéricos solo procesen números y que no esten vacíos
*
* @param text
* @return
*/
private boolean campo_numerico_correcto(TextField text) {
if (!utilesTeclado.esUnNumero(text.getText())) {
text.setBorder(bordeError);
return false;
} else if (text.getText().equalsIgnoreCase("")) {
return false;
}
return true;
}
/**
* Rellenar los combos
*/
private void combo_heroes() {
List<tipo> listaHeroes = Arrays.asList(tipo.values());
List<String> listaHeroesCombo = listaHeroes.stream().filter(tipo -> tipo.getTipoPersonaje().equalsIgnoreCase("heroe")).map(tipo -> tipo.name()).collect(Collectors.toList());
tipo_heroe_combo.setItems(FXCollections.observableArrayList(listaHeroesCombo));
}
private void combo_bestias() {
List<tipo> listaBestias = Arrays.asList(tipo.values());
List<String> listaBestiasCombo = listaBestias.stream().filter(tipo -> tipo.getTipoPersonaje().equalsIgnoreCase("bestia")).map(tipo -> tipo.name()).collect(Collectors.toList());
tipo_bestia_combo.setItems(FXCollections.observableArrayList(listaBestiasCombo));
}
private void cargarPartida(File f) {
ObjectInputStream flujoEntrada = null;
try {
flujoEntrada = new ObjectInputStream(new FileInputStream(f));
juego gameLoad = (juego) flujoEntrada.readObject();
setGame(gameLoad);
lista_bestias_field.setItems(FXCollections.observableArrayList(gameLoad.getListaBestias()));
lista_heroes_field.setItems(FXCollections.observableArrayList(gameLoad.getListaHeroes()));
game.setEmpezarLucha(true);
game.setCarga(false);
error_botones.setText("PARTIDA CARGADA EXITOSAMENTE!!!");
flujoEntrada.close();
} catch (IOException | ClassNotFoundException e) {
error_botones.setText("No se ha podido cargar la partida!");
}
}
private void autoguardado() {
File carpeta = new File(util.pathDir);
if (!carpeta.exists()) {
if (carpeta.mkdir()) {
guardarPartida(carpeta);
} else
error_botones.setText("No se ha podido crear el directorio de guardado");
} else {
guardarPartida(carpeta);
}
}
private void guardarPartida(File f) {
File savegame = new File(f + File.separator + util.pathFile);
try {
ObjectOutputStream flujoSalida = new ObjectOutputStream(new FileOutputStream(savegame));
flujoSalida.writeObject(game);
error_botones.setText("Autoguardado!!!");
flujoSalida.close();
} catch (IOException e) {
error_botones.setText("No se ha podido guardar la partida!");
}
}
}