2023-10-20 12:17:34 +04:00
|
|
|
package ProjectStormtrooper;
|
|
|
|
|
|
|
|
import java.awt.*;
|
2023-12-17 17:41:47 +04:00
|
|
|
import java.util.ArrayList;
|
2023-11-07 12:57:17 +04:00
|
|
|
import java.util.Stack;
|
2023-10-20 12:17:34 +04:00
|
|
|
|
|
|
|
public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveableObject> {
|
|
|
|
private int _pictureWidth;
|
|
|
|
private int _pictureHeight;
|
|
|
|
private final int _placeSizeWidth = 160;
|
|
|
|
private final int _placeSizeHeight = 120;
|
|
|
|
private SetGeneric<T> _collection;
|
2023-12-17 17:41:47 +04:00
|
|
|
public ArrayList<T> GetPlanes() {
|
|
|
|
return _collection.GetEnumerator();
|
|
|
|
}
|
2023-10-20 12:17:34 +04:00
|
|
|
|
|
|
|
public PlanesGenericCollection(int picWidth, int picHeight) {
|
|
|
|
int horizontalObjectsCount = picWidth / _placeSizeWidth;
|
|
|
|
int verticalObjectsCount = picHeight / _placeSizeHeight;
|
|
|
|
_pictureWidth = picWidth;
|
|
|
|
_pictureHeight = picHeight;
|
|
|
|
_collection = new SetGeneric<T>(horizontalObjectsCount * verticalObjectsCount);
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:43:07 +04:00
|
|
|
public int Add(T obj) {
|
2023-10-20 12:17:34 +04:00
|
|
|
if (obj == null) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-10-20 22:43:07 +04:00
|
|
|
return _collection.Insert(obj);
|
2023-10-20 12:17:34 +04:00
|
|
|
}
|
|
|
|
|
2023-10-20 22:43:07 +04:00
|
|
|
public T Sub(int pos) {
|
2023-10-24 12:43:15 +04:00
|
|
|
T obj = _collection.Get(pos);
|
|
|
|
if (obj != null) {
|
|
|
|
_collection.Remove(pos);
|
|
|
|
}
|
|
|
|
return obj;
|
2023-10-20 12:17:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public U GetU(int pos) {
|
|
|
|
if (_collection.Get(pos) != null)
|
|
|
|
return (U) _collection.Get(pos).GetMoveableObject();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ShowPlanes(Graphics g) {
|
|
|
|
DrawBackground(g);
|
|
|
|
DrawObjects(g);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DrawBackground(Graphics g) {
|
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
g2d.setStroke(new BasicStroke(3));
|
|
|
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
|
|
|
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
|
|
|
|
// Линия разметки места
|
|
|
|
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
|
|
|
}
|
|
|
|
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DrawObjects(Graphics g) {
|
|
|
|
int placesColumnCount = _pictureHeight / _placeSizeHeight;
|
|
|
|
int placesRowCount = _pictureWidth / _placeSizeWidth;
|
2023-10-24 12:43:15 +04:00
|
|
|
int i = 0;
|
|
|
|
for (var obj :
|
|
|
|
_collection.GetEnumerator()) {
|
2023-10-20 12:17:34 +04:00
|
|
|
// установка позиции
|
2023-10-24 12:43:15 +04:00
|
|
|
if (obj != null) {
|
|
|
|
obj.SetPosition(
|
|
|
|
(placesRowCount - 1) * _placeSizeWidth - (i % placesColumnCount * _placeSizeWidth) + (_placeSizeWidth - obj.GetWidth()) / 2,
|
|
|
|
i / placesColumnCount * _placeSizeHeight + (_placeSizeHeight - obj.GetHeight()) / 2
|
|
|
|
);
|
|
|
|
// прорисовка объекта
|
|
|
|
obj.DrawTransport(g);
|
|
|
|
}
|
|
|
|
i++;
|
2023-10-20 12:17:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|