155 lines
4.8 KiB
Java
155 lines
4.8 KiB
Java
import java.awt.*;
|
|
import java.util.Random;
|
|
public class DrawingWarship {
|
|
protected EntityWarship Warship;
|
|
public EntityWarship GetWarship(){return Warship;}
|
|
public DrawingBlocks Blocks;
|
|
|
|
protected int _startPosX;
|
|
protected int _startPosY;
|
|
|
|
private Integer _pictureWidth = null;
|
|
private Integer _pictureHeight = null;
|
|
|
|
private int _warshipWidth = 114;
|
|
private int _warshipHeight = 40;
|
|
|
|
public DrawingWarship(int speed, float weight, Color bodyColor)
|
|
{
|
|
Warship = new EntityWarship(speed, weight, bodyColor);
|
|
Blocks = new DrawingBlocks();
|
|
Blocks.blockDirection = BlockRandom();
|
|
}
|
|
protected DrawingWarship(int speed, float weight, Color bodyColor, int warshipWidth, int warshipHeight)
|
|
{
|
|
this(speed, weight, bodyColor);
|
|
_warshipWidth = warshipWidth;
|
|
_warshipHeight = warshipHeight;
|
|
}
|
|
|
|
public void SetPosition(int x, int y, int width, int height)
|
|
{
|
|
if (width >= x + _warshipWidth && height >= y + _warshipHeight && x >= 0 && y >= 0)
|
|
{
|
|
_startPosX = x;
|
|
_startPosY = y;
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
}
|
|
}
|
|
|
|
public void MoveTransport(Direction direction)
|
|
{
|
|
if (_pictureWidth == null || _pictureHeight == null)
|
|
{
|
|
return;
|
|
}
|
|
switch (direction)
|
|
{
|
|
// вправо
|
|
case Right:
|
|
if (_startPosX + _warshipWidth + Warship.Step < _pictureWidth)
|
|
{
|
|
_startPosX += Warship.Step;
|
|
}
|
|
break;
|
|
//влево
|
|
case Left:
|
|
if(_startPosX - Warship.Step > 0)
|
|
{
|
|
_startPosX -= Warship.Step;
|
|
}
|
|
break;
|
|
//вверх
|
|
case Up:
|
|
if (_startPosY - Warship.Step > 0)
|
|
{
|
|
_startPosY -= Warship.Step;
|
|
}
|
|
break;
|
|
//вниз
|
|
case Down:
|
|
if (_startPosY + _warshipHeight + Warship.Step < _pictureHeight)
|
|
{
|
|
_startPosY += Warship.Step;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void DrawTransport(Graphics2D g2){
|
|
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
|
{
|
|
return;
|
|
}
|
|
//главная палуба
|
|
int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4};
|
|
int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40};
|
|
g2.setColor(Warship.GetBodyColor());
|
|
g2.fillPolygon(pointXWarship, pointYWarship, 5);
|
|
g2.setColor(Color.BLACK);
|
|
g2.drawPolygon(pointXWarship, pointYWarship, 5);
|
|
|
|
//мачта
|
|
g2.setColor(Color.WHITE);
|
|
g2.fillOval(_startPosX + 80, _startPosY + 13, 15, 15);
|
|
//границы мачты
|
|
g2.setColor(Color.BLACK);
|
|
g2.drawOval( _startPosX + 80, _startPosY + 13, 15, 15);
|
|
|
|
//палуба
|
|
g2.setColor(Color.WHITE);
|
|
g2.fillRect(_startPosX + 70, _startPosY + 10, 8, 18);
|
|
g2.fillRect(_startPosX + 55, _startPosY + 15, 15, 8);
|
|
//границы палубы
|
|
g2.setColor(Color.BLACK);
|
|
g2.drawRect(_startPosX + 70, _startPosY + 10, 8, 18);
|
|
g2.drawRect(_startPosX + 55, _startPosY + 15, 15, 8);
|
|
|
|
//двигатели
|
|
g2.fillRect(_startPosX, _startPosY + 5, 4, 10);
|
|
g2.fillRect(_startPosX, _startPosY + 23, 4, 10);
|
|
|
|
//блоки
|
|
Blocks.DrawBlocks(g2,_startPosX, _startPosY);
|
|
}
|
|
|
|
public BlockDirection BlockRandom(){
|
|
Random rand = new Random();
|
|
int resRand = rand.nextInt(3);
|
|
if(resRand == 0) return BlockDirection.TwoBlocks;
|
|
if(resRand == 1) return BlockDirection.FourBlocks;
|
|
if(resRand == 2) return BlockDirection.SixBlocks;
|
|
return null;
|
|
}
|
|
|
|
public void ChangeBorders(int width, int height)
|
|
{
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
if (_pictureWidth <= _warshipWidth || _pictureHeight <= _warshipHeight)
|
|
{
|
|
_pictureWidth = null;
|
|
_pictureHeight = null;
|
|
return;
|
|
}
|
|
if (_startPosX + _warshipWidth > _pictureWidth)
|
|
{
|
|
_startPosX = _pictureWidth - _warshipWidth;
|
|
}
|
|
if (_startPosY + _warshipHeight > _pictureHeight)
|
|
{
|
|
_startPosY = _pictureHeight - _warshipHeight;
|
|
}
|
|
}
|
|
|
|
public float[] GetCurrentPosition(){
|
|
float[] pos = new float[4];
|
|
pos[0] = _startPosX;
|
|
pos[1] =_startPosY;
|
|
pos[2] = _startPosX + _warshipWidth;
|
|
pos[3] = _startPosY + _warshipHeight;
|
|
return pos;
|
|
}
|
|
}
|