-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwner.java
186 lines (164 loc) · 4.68 KB
/
Owner.java
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
/*******************************************************************************
* Copyright (C) 2019-2020 ROMAINPC
* Copyright (C) 2019-2020 Picachoc
*
* This file is part of PICROM-Wars
*
* PICROM-Wars is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PICROM-Wars is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package picrom.owner;
import java.util.LinkedList;
import java.util.List;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import picrom.entity.castle.Castle;
import picrom.utils.Kingdom;
/**
* An Owner is a player (AI or human). It has a its own identity (name and
* color) and it owns Casltes.
*
* This class is also a JavaFX component, a small bar with some informations
* about the owner. Show it for instance in a VBox.
*/
public abstract class Owner extends StackPane {
private Color color;
private String name; // not use as ID, prefer object reference
private String ownerType;
// castles owned
private List<Castle> castles;
private Label numberL;
private Line crossed;
/**
* Constructor to create an Owner from a Kingdom identity.
*
* @param kingdom Identity
* @param ownerType String printed in UI: type of the owner(human, ai, ...)
* @see picrom.utils.Kingdom
*/
public Owner(Kingdom kingdom, String ownerType) {
this(kingdom.getColor(), kingdom.getName(), ownerType);
}
/**
* Constructor to create an Owner.
*
* @param color Color
* @param name Name
* @param ownerType String printed in UI: type of the owner(human, ai, ...)
*/
public Owner(Color color, String name, String ownerType) {
this.color = color;
this.name = name;
this.ownerType = ownerType;
castles = new LinkedList<Castle>();
setUI();
}
/**
* Constructor to create an Owner with random identity (name and color).
*
* @param ownerType String printed in UI: type of the owner(human, ai, ...)
*/
public Owner(String ownerType) {
this(Kingdom.randomKingdom(), ownerType);
}
/**
* Just setup JavaFX part of the object.
*/
private void setUI() {
HBox content = new HBox();
content.setAlignment(Pos.CENTER_LEFT);
Label type = new Label(ownerType);
type.setPrefWidth(25);
Label nameL = new Label(" " + name);
Rectangle rec = new Rectangle(30, 15, color);
numberL = new Label(String.valueOf(castles.size()));
numberL.setTextFill(color.getBrightness() < 0.5 ? Color.WHITE : Color.BLACK);
StackPane castlesOwned = new StackPane();
castlesOwned.getChildren().addAll(rec, numberL);
content.getChildren().addAll(type, castlesOwned, nameL);
crossed = new Line(0, 0, 100, 0);
crossed.setStrokeWidth(3);
crossed.setTranslateX(20);
this.getChildren().addAll(content, crossed);
}
/**
* @return Owner color
*/
public Color getColor() {
return color;
}
/**
* Set owner color
*
* @param color new Color
*/
public void setColor(Color color) {
this.color = color;
}
/**
* @return Owner name
*/
public String getName() {
return name;
}
/**
* Set owner name
*
* @param name new name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return List of all Castles owned by the Owner.
*/
public List<Castle> getCastles() {
return castles;
}
/**
* Give a Castle to the Owner.
*
* @param castle the Castle
*/
public void addCastle(Castle castle) {
castles.add(castle);
castle.setOwner(this);
numberL.setText(String.valueOf(castles.size()));
crossed.setVisible(castles.size() < 1);
}
/**
* To remove a Castle, the Owner will not owns this Castle.
*
* @param castle the Castle
*/
public void removeCastle(Castle castle) {
castles.remove(castle);
numberL.setText(String.valueOf(castles.size()));
crossed.setVisible(castles.size() < 1);
}
/**
* @return true if this Owner still have at least one Castle.
*/
public boolean isInGame() {
return castles.size() > 0;
}
@Override
public String toString() {
return name + " ( " + ownerType + ", " + color + "), Castles: " + castles;
}
}