111 lines
3.2 KiB
Java
111 lines
3.2 KiB
Java
import java.awt.*;
|
|
|
|
public class DrawingShip {
|
|
private EntityShip ship;
|
|
private DrawingDecks drawingDecks;
|
|
private float _startPosX;
|
|
private float _startPosY;
|
|
private Integer _pictureWidth = null;
|
|
private Integer _pictureHeight = null;
|
|
private final int _shipWidth = 80;
|
|
private final int _shipHeight = 30;
|
|
|
|
public EntityShip getShip() {
|
|
return ship;
|
|
}
|
|
|
|
public void Init(int speed, float weight, Color bodyColor, int decksCount) {
|
|
ship = new EntityShip();
|
|
ship.Init(speed, weight, bodyColor);
|
|
drawingDecks = new DrawingDecks();
|
|
drawingDecks.Init(decksCount, bodyColor);
|
|
}
|
|
|
|
public void SetPosition(int x, int y, int width, int height) {
|
|
if (x < 0 || x + _shipWidth >= width)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (y < 0 || y + _shipHeight >= height)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_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 + _shipWidth + ship.getStep() < _pictureWidth)
|
|
{
|
|
_startPosX += ship.getStep();
|
|
}
|
|
break;
|
|
|
|
case Left:
|
|
if (_startPosX - ship.getStep() >= 0)
|
|
{
|
|
_startPosX -= ship.getStep();
|
|
}
|
|
break;
|
|
|
|
case Up:
|
|
if (_startPosY - ship.getStep() >= 0)
|
|
{
|
|
_startPosY -= ship.getStep();
|
|
}
|
|
break;
|
|
|
|
case Down:
|
|
if (_startPosY + _shipHeight + ship.getStep() < _pictureHeight)
|
|
{
|
|
_startPosY += ship.getStep();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void drawTransport(Graphics2D g) {
|
|
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
|
{
|
|
return;
|
|
}
|
|
g.setColor(ship.getBodyColor() != null ? ship.getBodyColor() : Color.BLACK);
|
|
int xPoly[] = {(int)_startPosX, (int)_startPosX + 80, (int)_startPosX + 70, (int)_startPosX + 10};
|
|
int yPoly[] = {(int)_startPosY + 10, (int)_startPosY + 10, (int)_startPosY + 30, (int)_startPosY + 30};
|
|
g.fillPolygon(xPoly, yPoly, 4);
|
|
g.fillRect((int)_startPosX + 30, (int)_startPosY, 20, 10);
|
|
drawingDecks.draw(g, (int) _startPosX, (int) _startPosY, _shipWidth, _shipHeight);
|
|
}
|
|
|
|
public void changeBorders(int width, int height)
|
|
{
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight)
|
|
{
|
|
_pictureWidth = null;
|
|
_pictureHeight = null;
|
|
return;
|
|
}
|
|
if (_startPosX + _shipWidth > _pictureWidth)
|
|
{
|
|
_startPosX = _pictureWidth - _shipWidth;
|
|
}
|
|
if (_startPosY + _shipHeight > _pictureHeight)
|
|
{
|
|
_startPosY = _pictureHeight - _shipHeight;
|
|
}
|
|
}
|
|
} |