PIbd-21_Belianin_N.N._Tank..../Tank/src/DrawingArmoVehicle.java

132 lines
4.0 KiB
Java
Raw Normal View History

2023-10-24 01:01:37 +04:00
import java.awt.*;
import java.util.Random;
public class DrawingArmoVehicle {
protected IOrnamentForm OrnamentsForm;
public EntityArmoVehicle ArmoVehicle;
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected int _TankWidth = 160;
protected int _TankHeight = 55;
2023-11-12 23:54:15 +04:00
public IMoveableObject GetMoveableObject() {
return new DrawingObjectTank(this);
}
2023-10-24 01:01:37 +04:00
public DrawingArmoVehicle(int speed, double weight, Color bodyColor, int _numWheel, int width, int height) {
_pictureWidth = width;
_pictureHeight = height;
if (_pictureHeight < _TankHeight || _pictureWidth < _TankWidth)
return;
ArmoVehicle = new EntityArmoVehicle(speed, weight, bodyColor, _numWheel);
Random random = new Random();
switch(random.nextInt(0, 3)) {
2023-10-24 01:01:37 +04:00
case 0:
OrnamentsForm = new DrawingWheelsCombination();
break;
case 1:
2023-10-29 18:57:32 +04:00
OrnamentsForm = new DrawingStarOrnament();
2023-10-24 01:01:37 +04:00
break;
case 2:
OrnamentsForm = new DrawingSuspensionOrnament();
break;
default:
OrnamentsForm = new DrawingWheelsCombination();
break;
}
OrnamentsForm.setDigit(_numWheel);
}
protected DrawingArmoVehicle(EntityArmoVehicle vehicle, IOrnamentForm ornamentsForm, int width, int height) {
if (height < _pictureHeight || width < _pictureWidth)
return;
_pictureWidth = width;
_pictureHeight = height;
ArmoVehicle = vehicle;
OrnamentsForm = ornamentsForm;
OrnamentsForm.setDigit(ArmoVehicle.numWheel);
}
2023-10-24 01:01:37 +04:00
public void SetPosition(int x, int y) {
_startPosX = Math.min(x, _pictureWidth - _TankWidth);
_startPosY = Math.min(y, _pictureHeight - _TankHeight);
}
public int GetPosX () {
2023-11-12 23:54:15 +04:00
return _startPosX;
}
public int GetPosY () {
2023-11-12 23:54:15 +04:00
return _startPosY;
}
public int GetWidth () {
2023-11-12 23:54:15 +04:00
return _TankWidth;
}
public int GetHeight () {
2023-11-12 23:54:15 +04:00
return _TankHeight;
}
2023-10-24 01:01:37 +04:00
public boolean CanMove(Direction direction) {
if (ArmoVehicle == null) {
return false;
}
switch (direction) {
case Left:
return _startPosX - ArmoVehicle.Step > 0;
case Right:
return _startPosX + _TankWidth + ArmoVehicle.Step < _pictureWidth;
case Up:
return _startPosY - ArmoVehicle.Step > 0;
case Down:
return _startPosY + _TankHeight + ArmoVehicle.Step < _pictureHeight;
default:
return false;
}
}
public void MoveTransport(Direction direction) {
if (!CanMove(direction) || ArmoVehicle == null) {
return;
}
switch (direction) {
//влево
case Left:
_startPosX -= (int)ArmoVehicle.Step;
2023-10-24 01:01:37 +04:00
break;
//вверх
case Up:
_startPosY -= (int)ArmoVehicle.Step;
2023-10-24 01:01:37 +04:00
break;
// вправо
case Right:
_startPosX += (int)ArmoVehicle.Step;
2023-10-24 01:01:37 +04:00
break;
//вниз
case Down:
_startPosY += (int)ArmoVehicle.Step;
2023-10-24 01:01:37 +04:00
break;
}
}
// Прорисовка объекта
2023-10-24 01:01:37 +04:00
public void DrawTransport(Graphics2D g) {
if (ArmoVehicle == null) {
return;
}
// body
2023-10-29 18:57:32 +04:00
g.setColor(ArmoVehicle.BodyColor);
2023-11-12 23:54:15 +04:00
int[] xPoints = {_startPosX + 5, _startPosX + 140, _startPosX + 130, _startPosX + 12};
2023-10-24 01:01:37 +04:00
int[] yPoints = {_startPosY + 30, _startPosY + 30, _startPosY + 42, _startPosY + 42};
int nPoints = xPoints.length;
2023-11-12 23:54:15 +04:00
g.drawPolygon(xPoints, yPoints, nPoints);
g.fillPolygon(xPoints, yPoints, nPoints);
2023-10-24 01:01:37 +04:00
//wheels
OrnamentsForm.Draw(g, _startPosX, _startPosY);
}
2023-10-29 18:57:32 +04:00
}