158 lines
5.2 KiB
Java
158 lines
5.2 KiB
Java
package Drawnings;
|
|
|
|
import Entities.*;
|
|
|
|
import java.awt.*;
|
|
import java.util.Random;
|
|
|
|
public class DrawningTruck {
|
|
public EntityTruck EntityTruck;
|
|
public EntityTruck getEntityTruck() {
|
|
return EntityTruck;
|
|
}
|
|
private Integer _pictureWidth;
|
|
private Integer _pictureHeight;
|
|
protected Integer _startPosX;
|
|
protected Integer _startPosY;
|
|
private Integer _drawningCarWidth = 138;
|
|
private Integer _drawningCarHeight = 68;
|
|
public IDrawningWheels drawningWheels;
|
|
|
|
public int GetPosX(){return _startPosX;}
|
|
public int GetPosY(){return _startPosY;}
|
|
public int GetWidth(){return _drawningCarWidth;}
|
|
public int GetHeight(){return _drawningCarHeight;}
|
|
protected DrawningTruck() {
|
|
_pictureWidth = null;
|
|
_pictureHeight = null;
|
|
_startPosX = null;
|
|
_startPosY = null;
|
|
}
|
|
|
|
public DrawningTruck(EntityTruck truck, IDrawningWheels drawningWheels) {
|
|
EntityTruck = truck;
|
|
this.drawningWheels = drawningWheels;
|
|
}
|
|
protected void SetWheelsType() {
|
|
Random random = new Random();
|
|
switch (random.nextInt(1, 4)) {
|
|
case 1:
|
|
drawningWheels = new DrawningCircleWheels();
|
|
break;
|
|
case 2:
|
|
drawningWheels = new DrawningRhombWheels();
|
|
break;
|
|
case 3:
|
|
drawningWheels = new DrawningTriangleWheels();
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
drawningWheels.setCountWheels(random.nextInt(1, 4));
|
|
}
|
|
|
|
public DrawningTruck(int speed, double weight, Color bodyColor) {
|
|
this();
|
|
EntityTruck = new EntityTruck(speed, weight, bodyColor);
|
|
// для дополнительных элементов
|
|
SetWheelsType();
|
|
Random random = new Random();
|
|
drawningWheels.setCountWheels(random.nextInt(1, 4));
|
|
}
|
|
protected DrawningTruck(Integer drawningCarWidth, Integer drawningCarHeight) {
|
|
this();
|
|
_drawningCarWidth = drawningCarWidth;
|
|
_drawningCarHeight = drawningCarHeight;
|
|
}
|
|
public boolean SetPictureSize(int width, int height) {
|
|
if (width < _drawningCarWidth || height < _drawningCarHeight) {return false;}
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
if (_startPosX != null && _startPosY != null)
|
|
{
|
|
if (_startPosX + _drawningCarWidth > _pictureWidth)
|
|
{
|
|
_startPosX = -_drawningCarWidth + _pictureWidth;
|
|
}
|
|
else if (_startPosX < 0)
|
|
{
|
|
_startPosX = 0;
|
|
}
|
|
if (_startPosY + _drawningCarHeight > _pictureHeight)
|
|
{
|
|
_startPosY = -_drawningCarHeight + _pictureHeight;
|
|
}
|
|
else if (_startPosY < 0)
|
|
{
|
|
_startPosY = 0;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
public void SetPosition(int x, int y) {
|
|
if (_pictureHeight == null || _pictureWidth == null) return;
|
|
if (x + _drawningCarWidth > _pictureWidth) {
|
|
_startPosX = _pictureWidth - _drawningCarWidth;
|
|
} else if (x < 0) {
|
|
_startPosX = 0;
|
|
}
|
|
else {
|
|
_startPosX = x;
|
|
}
|
|
if (y + _drawningCarHeight > _pictureHeight) {
|
|
_startPosY = _pictureHeight - _drawningCarHeight;
|
|
} else if (y < 0) {
|
|
_startPosY = 0;
|
|
} else {
|
|
_startPosY = y;
|
|
}
|
|
}
|
|
public boolean MoveTransport(DirectionType directionType) {
|
|
if (EntityTruck == null || _startPosX == null || _startPosY == null) {
|
|
return false;
|
|
}
|
|
switch (directionType) {
|
|
case Left:
|
|
if (_startPosX - EntityTruck.Step() > 0) {
|
|
_startPosX -= (int) EntityTruck.Step();
|
|
}
|
|
return true;
|
|
case Right:
|
|
if (_startPosX + _drawningCarWidth + EntityTruck.Step() < _pictureWidth) {
|
|
_startPosX += (int) EntityTruck.Step();
|
|
}
|
|
return true;
|
|
case Up:
|
|
if (_startPosY - EntityTruck.Step() > 0) {
|
|
_startPosY -= (int) EntityTruck.Step();
|
|
}
|
|
return true;
|
|
case Down:
|
|
if (_startPosY + _drawningCarHeight + EntityTruck.Step() < _pictureHeight) {
|
|
_startPosY += (int) EntityTruck.Step();
|
|
}
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
public void DrawTransport(Graphics g) {
|
|
if (EntityTruck == null || _startPosX == null || _startPosY == null) {
|
|
return;
|
|
}
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
g2d.setColor(EntityTruck.getBodyColor());
|
|
g2d.fillRect(_startPosX, _startPosY + 40, 122, 10);
|
|
g2d.fillRect(_startPosX + 102, _startPosY + 10, 20, 30);
|
|
|
|
drawningWheels.DrawCleaningCarWheels(g, EntityTruck.getBodyColor(), Color.BLACK, _startPosX, _startPosY);
|
|
g2d.setColor(Color.black);
|
|
|
|
g2d.drawRect(_startPosX, _startPosY + 40, 122, 10);
|
|
g2d.drawRect(_startPosX + 102, _startPosY + 10, 20, 30);
|
|
|
|
g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12);
|
|
}
|
|
}
|