119 lines
3.6 KiB
Java
119 lines
3.6 KiB
Java
import java.awt.*;
|
|
|
|
public class DrawingShip {
|
|
protected EntityShip ship;
|
|
protected IDrawingDecks drawingDecks;
|
|
protected float _startPosX;
|
|
protected float _startPosY;
|
|
private Integer _pictureWidth = null;
|
|
private Integer _pictureHeight = null;
|
|
protected int _shipWidth = 80;
|
|
protected int _shipHeight = 30;
|
|
|
|
public EntityShip getShip() {
|
|
return ship;
|
|
}
|
|
|
|
public DrawingShip(int speed, float weight, Color bodyColor, int decksCount) {
|
|
ship = new EntityShip(speed, weight, bodyColor);
|
|
drawingDecks = DecksType.random(decksCount, bodyColor);
|
|
}
|
|
|
|
protected DrawingShip(int speed, float weight, Color bodyColor, int decksCount, int shipWidth, int shipHeight) {
|
|
this(speed, weight, bodyColor, decksCount);
|
|
_shipWidth = shipWidth;
|
|
_shipHeight = shipHeight;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
public float[] getCurrentPosition() {
|
|
return new float[] { _startPosX, _startPosX + _shipWidth - 1, _startPosY, _startPosY + _shipHeight -1 };
|
|
}
|
|
} |