PIbd-23_Polevoy_S.V._SelfPr.../DrawingArtillery.java

122 lines
3.9 KiB
Java
Raw Normal View History

2022-09-25 19:20:16 +04:00
import java.awt.*;
public class DrawingArtillery {
protected EntityArtillery artillery;
2022-10-08 19:19:17 +04:00
protected IDrawingRollers drawingRollers;
protected float _startPosX;
protected float _startPosY;
2022-09-25 19:20:16 +04:00
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
protected int _artilleryWidth = 80;
protected int _artilleryHeight = 50;
2022-09-25 19:20:16 +04:00
public EntityArtillery getArtillery() {
return artillery;
}
2022-10-08 14:43:17 +04:00
public DrawingArtillery(int speed, float weight, Color bodyColor, int rollersCount) {
artillery = new EntityArtillery(speed, weight, bodyColor);
2022-10-08 23:03:27 +04:00
drawingRollers = RollersType.random(rollersCount, bodyColor);
2022-09-25 19:20:16 +04:00
}
protected DrawingArtillery(int speed, float weight, Color bodyColor, int rollersCount, int artilleryWidth, int artilleryHeight) {
this(speed, weight, bodyColor, rollersCount);
_artilleryWidth = artilleryWidth;
_artilleryHeight = artilleryHeight;
}
2022-10-08 15:25:07 +04:00
public void setPosition(int x, int y, int width, int height) {
2022-09-25 19:20:16 +04:00
if (x < 0 || x + _artilleryWidth >= width)
{
return;
}
if (y < 0 || y + _artilleryHeight >= 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 + _artilleryWidth + artillery.getStep() < _pictureWidth)
{
_startPosX += artillery.getStep();
}
break;
case Left:
if (_startPosX - artillery.getStep() >= 0)
{
_startPosX -= artillery.getStep();
}
break;
case Up:
if (_startPosY - artillery.getStep() >= 0)
{
_startPosY -= artillery.getStep();
}
break;
case Down:
if (_startPosY + _artilleryHeight + artillery.getStep() < _pictureHeight)
{
_startPosY += artillery.getStep();
}
break;
}
}
public void drawTransport(Graphics2D g) {
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
{
return;
}
g.setColor(artillery.getBodyColor() != null ? artillery.getBodyColor() : Color.BLACK);
g.fillRect((int) (_startPosX + _artilleryWidth / 8 * 2), (int) _startPosY, _artilleryWidth / 8 * 4, _artilleryHeight / 5);
2022-09-25 19:20:16 +04:00
g.fillRect((int) _startPosX, (int) (_startPosY + _artilleryHeight / 5), _artilleryWidth, _artilleryHeight / 3);
g.setColor(Color.GRAY);
g.fillOval((int) _startPosX, (int) (_startPosY + _artilleryHeight * 2 / 5), _artilleryWidth, _artilleryHeight * 2 / 5);
drawingRollers.draw(g, (int) _startPosX, (int) _startPosY, _artilleryWidth, _artilleryHeight);
}
public void changeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _artilleryWidth || _pictureHeight <= _artilleryHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _artilleryWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _artilleryWidth;
}
if (_startPosY + _artilleryHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _artilleryHeight;
}
}
public float[] getCurrentPosition() {
return new float[] { _startPosX, _startPosX + _artilleryWidth - 1, _startPosY, _startPosY + _artilleryHeight -1 };
}
2022-09-25 19:20:16 +04:00
}