Pibd-23_Lisov_N.A._AirFight.../Classes/MapWithSetAircraftsGeneric.java

157 lines
4.6 KiB
Java
Raw Normal View History

2022-11-22 03:36:42 +04:00
package Classes;
import Classes.Maps.AbstractMap;
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetAircraftsGeneric <T extends IDrawingObject,U extends AbstractMap>
{
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 145;
private final int _placeSizeHeight = 145;
private final SetAircraftsGeneric<T> _setAircrafts;
private final U _map;
public MapWithSetAircraftsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setAircrafts = new SetAircraftsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addAircraft(MapWithSetAircraftsGeneric<T, U> map, T aircraft)
{
return map._setAircrafts.Insert(aircraft);
}
public T removeAircraft(MapWithSetAircraftsGeneric<T, U> map, int position)
{
return map._setAircrafts.Remove(position);
}
2022-12-05 18:41:18 +04:00
public T getAircraft(MapWithSetAircraftsGeneric<T, U> map, int position)
{
return map._setAircrafts.get(position);
}
2022-11-22 03:36:42 +04:00
public Image showSet()
{
BufferedImage img = new BufferedImage(_pictureWidth,_pictureHeight,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) img.getGraphics();
DrawBackground(g2d);
DrawAircrafts(g2d);
return img;
}
public Image ShowOnMap()
{
Shaking();
for(int i = 0;i< _setAircrafts.getCount();i++)
{
if(_setAircrafts.get(i) != null)
{
return _map.CreateMap(_pictureWidth,_pictureHeight,_setAircrafts.get(i));
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
public Image MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
private void Shaking()
{
int j = _setAircrafts.getCount() - 1;
for (int i = 0; i < _setAircrafts.getCount(); i++)
{
if (_setAircrafts.get(i) == null)
{
for (; j > i; j--)
{
var aircraft = _setAircrafts.get(j);
if (aircraft != null)
{
_setAircrafts.Insert(aircraft, i);
_setAircrafts.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
g.setColor(Color.black);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g2d.setStroke(new BasicStroke(2));
for (int i = 0; i < _pictureWidth / _placeSizeWidth + 1; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
{
g2d.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 135, _placeSizeWidth - 80, j * _placeSizeHeight + 135);
if(i * _placeSizeWidth + _placeSizeWidth + 20 < _pictureWidth - 10)
{
g2d.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight + 45);
}
g2d.drawLine(i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight + 45, i * _placeSizeWidth + _placeSizeWidth + 20 , j * _placeSizeHeight + 60);
g2d.drawLine( i * _placeSizeWidth + 20, j * _placeSizeHeight + 135, i * _placeSizeWidth + 20, j * _placeSizeHeight + 60);
}
}
}
private void DrawAircrafts(Graphics g)
{
int curX = _pictureWidth / _placeSizeWidth - 1;
int curY = _pictureHeight / _placeSizeHeight - 1;
for(int i = 0; i < _setAircrafts.getCount();i++)
{
if(_setAircrafts.get(i) != null)
{
_setAircrafts.get(i).SetObject(curX * _placeSizeWidth + 30, curY * _placeSizeHeight + 40, _pictureWidth, _pictureHeight);
_setAircrafts.get(i).DrawingObject(g);
}
if (curX <= 0)
{
curX = _pictureWidth / _placeSizeWidth - 1;
curY--;
}
else
{
curX--;
}
}
}
}