Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Quentin DRUAULT-AUBIN
lo23-project
Commits
b35cad66
Commit
b35cad66
authored
Dec 06, 2017
by
Quentin DRUAULT-AUBIN
Browse files
[Ship] Attack destroyed ship
parent
e374b140
Changes
1
Show whitespace changes
Inline
Side-by-side
Battleship/src/main/java/com/utclo23/ihmtable/controller/InGameGUIController.java
View file @
b35cad66
...
...
@@ -447,9 +447,11 @@ public class InGameGUIController {
}
/**
* Function to place a ship on the grid with its corresponding image
* @param ship
* @param ship : the ship to place
* @param grid : the grid to place the ship on
* @return : the imageview of the ship placed
*/
void
putShipOnBoard
(
Ship
ship
)
private
ImageView
putShipOnBoard
(
Ship
ship
,
GridPane
grid
)
{
// We assume the ship has the right coordinates.
// Moreover, this function won't update the shipboard!
...
...
@@ -460,26 +462,27 @@ public class InGameGUIController {
if
(
ship
.
getListCoord
().
get
(
0
).
getY
()
==
ship
.
getListCoord
().
get
(
1
).
getY
())
{
// Horizontal.
// Set the size.
shipOnTheGrid
.
setFitWidth
(
playerG
rid
.
getWidth
()/
10.0
*
ship
.
getSize
());
shipOnTheGrid
.
setFitHeight
(
playerG
rid
.
getHeight
()/
10.0
);
shipOnTheGrid
.
setFitWidth
(
g
rid
.
getWidth
()/
10.0
*
ship
.
getSize
());
shipOnTheGrid
.
setFitHeight
(
g
rid
.
getHeight
()/
10.0
);
// Place on the grid.
playerG
rid
.
add
(
shipOnTheGrid
,
Math
.
min
(
ship
.
getListCoord
().
get
(
0
).
getX
(),
ship
.
getListCoord
().
get
(
1
).
getX
()),
ship
.
getListCoord
().
get
(
0
).
getY
(),
ship
.
getSize
(),
1
);
g
rid
.
add
(
shipOnTheGrid
,
Math
.
min
(
ship
.
getListCoord
().
get
(
0
).
getX
(),
ship
.
getListCoord
().
get
(
1
).
getX
()),
ship
.
getListCoord
().
get
(
0
).
getY
(),
ship
.
getSize
(),
1
);
}
else
{
// Vertical
// Set the size.
shipOnTheGrid
.
setFitHeight
(
playerG
rid
.
getWidth
()/
10.0
);
shipOnTheGrid
.
setFitWidth
(
playerG
rid
.
getHeight
()/
10.0
*
ship
.
getSize
());
shipOnTheGrid
.
setFitHeight
(
g
rid
.
getWidth
()/
10.0
);
shipOnTheGrid
.
setFitWidth
(
g
rid
.
getHeight
()/
10.0
*
ship
.
getSize
());
// Rotate the image.
shipOnTheGrid
.
setRotate
(
90
);
// Place on the grid.
playerG
rid
.
add
(
shipOnTheGrid
,
ship
.
getListCoord
().
get
(
0
).
getX
(),
Math
.
min
(
ship
.
getListCoord
().
get
(
0
).
getY
(),
ship
.
getListCoord
().
get
(
1
).
getY
()),
1
,
ship
.
getSize
());
g
rid
.
add
(
shipOnTheGrid
,
ship
.
getListCoord
().
get
(
0
).
getX
(),
Math
.
min
(
ship
.
getListCoord
().
get
(
0
).
getY
(),
ship
.
getListCoord
().
get
(
1
).
getY
()),
1
,
ship
.
getSize
());
}
return
shipOnTheGrid
;
}
catch
(
Exception
ex
)
{
Logger
.
getLogger
(
InGameGUIController
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
return
null
;
}
}
/**
* Update the label on the ship button by adding the specified value
...
...
@@ -600,45 +603,49 @@ public class InGameGUIController {
/**
* Generic method for placing a mine on the grid just with the coordinates
* @param c
* @param p
* @param c
oord : where to place the mine
* @param p
layer : who places the mine
*/
private
void
placeMine
(
Coordinate
c
,
Player
p
)
private
void
placeMine
(
Coordinate
c
oord
,
Player
p
layer
)
{
// Select the right grid which depends on the player (TODO spectateur?)
GridPane
grid
;
if
(
p
.
getLightPublicUser
().
getId
().
equals
(
facade
.
getFacadeData
().
getMyPublicUserProfile
().
getId
()))
{
if
(
p
layer
.
getLightPublicUser
().
getId
().
equals
(
facade
.
getFacadeData
().
getMyPublicUserProfile
().
getId
()))
{
grid
=
playerGrid
;
}
else
{
grid
=
opponentGrid
;
}
//Select the node in the grid and disable it when we lanch the attack
Node
n
=
getNodeByRowColumnIndex
(
c
.
getX
(),
c
.
getY
(),
grid
);
n
.
setDisable
(
true
);
//
Select the node in the grid and disable it when we lanch the attack
Node
hitCell
=
getNodeByRowColumnIndex
(
c
oord
.
getX
(),
c
oord
.
getY
(),
grid
);
hitCell
.
setDisable
(
true
);
Pair
<
Integer
,
Ship
>
attack_result
=
facade
.
getFacadeData
().
attack
(
c
,
false
);
//TODO: Voir si il faut demander à data une méthode "attack" neutralisée,
Pair
<
Integer
,
Ship
>
attack_result
=
facade
.
getFacadeData
().
attack
(
c
oord
,
false
);
//
TODO: Voir si il faut demander à data une méthode "attack" neutralisée,
// on a besoin de pouvoir tester si une mine placée à un endroit provoque
// une explosion sans aucun autre effet (ou dire à data de tester si le joueur
// en local est le currentPlayer)
if
(
attack_result
.
getKey
()
==
1
)
{
// Ship Touched!
n
.
getStyleClass
().
add
(
"inGameGUI_touched_cell"
);
// TODO: check if the ship is destroyed.
if
(
attack_result
.
getValue
()
!=
null
)
//ship destroyed here
{
//change opacity of the ship
hitCell
.
getStyleClass
().
add
(
"inGameGUI_touched_cell"
);
// Check if the ship is destroyed.
Ship
destroyedShip
=
attack_result
.
getValue
();
ImageView
img
=
listOfShipsOnTheGrid
.
get
(
destroyedShip
);
img
.
setOpacity
(
0.5
);
if
(
destroyedShip
!=
null
)
{
if
(
grid
==
opponentGrid
)
{
// Add ship picture on the opponent grid.
ImageView
destroyedShipImage
=
putShipOnBoard
(
destroyedShip
,
opponentGrid
);
listOfShipsOnTheGrid
.
put
(
destroyedShip
,
destroyedShipImage
);
}
// Change the CSS class of the cells.
destroyShip
(
destroyedShip
,
opponentGrid
);
}
// Reset the number of turns passed.
nbPassedTurns
=
0
;
}
else
{
// Ship missed!
n
.
getStyleClass
().
add
(
"inGameGUI_missed_cell"
);
hitCell
.
getStyleClass
().
add
(
"inGameGUI_missed_cell"
);
}
}
...
...
@@ -746,7 +753,7 @@ public class InGameGUIController {
facade
.
getFacadeData
().
leaveGame
();
}
else
{
// Fake an attack.
placeMine
(
new
Coordinate
(-
1
,
-
1
),
currentPlayer
);
facade
.
getFacadeData
().
attack
(
new
Coordinate
(-
1
,
-
1
),
true
);
// Increase the number of turns passed.
nbPassedTurns
++;
...
...
@@ -965,7 +972,7 @@ public class InGameGUIController {
listOfShipsOnTheGrid
.
put
(
ship
,
shipOnTheGrid
);
// Put the image on the board
putShipOnBoard
(
ship
);
putShipOnBoard
(
ship
,
playerGrid
);
// ATTENTION! Grid size is out of control!
// setShip didn't return any exception so the ship is correctly placed -> Update the label on the left panel
...
...
@@ -1038,14 +1045,26 @@ public class InGameGUIController {
// Ship destroyed.
if
(
destroyedShip
!=
null
)
{
// Change the CSS class of the cells.
destroyShip
(
destroyedShip
,
playerGrid
);
}
}
/**
* Update the CSS of the cell where the destroyed ship is.
* @param ship : the destroyed ship
* @param grid : the player or opponent grid
*/
private
void
destroyShip
(
Ship
ship
,
GridPane
grid
)
{
// Change the opacity.
listOfShipsOnTheGrid
.
get
(
destroyedShip
).
setOpacity
(
0.5
);
listOfShipsOnTheGrid
.
get
(
ship
).
setOpacity
(
0.5
);
// Change the CSS class of the cells.
for
(
Coordinate
coordinate
:
destroyedS
hip
.
getListCoord
())
{
Node
node
=
getNodeByRowColumnIndex
(
coordinate
.
getY
(),
coordinate
.
getX
(),
playerG
rid
);
for
(
Coordinate
coordinate
:
s
hip
.
getListCoord
())
{
Node
node
=
getNodeByRowColumnIndex
(
coordinate
.
getY
(),
coordinate
.
getX
(),
g
rid
);
node
.
getStyleClass
().
removeAll
(
"inGameGUI_touched_cell"
);
node
.
getStyleClass
().
add
(
"inGameGUI_destroyed_cell"
);
}
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment