PIbd-23_Panina_A.D.Cruiser..../DrawingCruiser.java
2023-12-09 17:25:15 +04:00

147 lines
5.0 KiB
Java

import java.awt.*;
public class DrawingCruiser {
public EntityCruiser EntityCruiser;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private int _cruiserWidth = 110;
private int _cruiserHeight = 60;
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _cruiserWidth;
}
public int GetHeight() {
return _cruiserHeight;
}
public IDop wheels;
public DrawingCruiser(int speed, double weight, Color bodyColor, int width, int height) {
if (width < _cruiserWidth || height < _cruiserHeight) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
public EntityCruiser getEntity() {
return EntityCruiser;
}
protected DrawingCruiser(int speed, double weight, Color bodyColor, int
width, int height, int cruiserWidth, int cruiserHeight) {
if (width <= _cruiserWidth || height <= _cruiserHeight) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
_cruiserWidth = cruiserWidth;
_cruiserHeight = cruiserHeight;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
public DrawingCruiser(EntityCruiser entityCruiser, IDop wheels) {
EntityCruiser = entityCruiser;
this.wheels = wheels;
_pictureWidth = 1000;
_pictureHeight = 1000;
_cruiserWidth = 110;
_cruiserHeight = 60;
}
public void SetPosition(int x, int y) {
if (x < 0 || x + _cruiserWidth > _pictureWidth) {
x = Math.max(0, _pictureWidth - _cruiserWidth);
}
if (y < 0 || y + _cruiserHeight > _pictureHeight) {
y = Math.max(0, _pictureHeight - _cruiserHeight);
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction) {
if (!CanMove(direction) || EntityCruiser == null) {
return;
}
switch (direction) {
case Left:
if (_startPosX - EntityCruiser.Step() >= 0) {
_startPosX -= (int) EntityCruiser.Step();
}
break;
case Up:
if (_startPosY - EntityCruiser.Step() >= 0) {
_startPosY -= (int) EntityCruiser.Step();
}
break;
case Right:
if (_startPosX + EntityCruiser.Step() + _cruiserWidth <= _pictureWidth) {
_startPosX += (int) EntityCruiser.Step();
}
break;
case Down:
if (_startPosY + EntityCruiser.Step() + _cruiserHeight <= _pictureHeight) {
_startPosY += (int) EntityCruiser.Step();
}
break;
}
}
public boolean CanMove(Direction direction) {
if (EntityCruiser == null) {
return false;
}
switch (direction) {
case Left:
return (_startPosX - EntityCruiser.Step()) > 0;
case Up:
return _startPosY - EntityCruiser.Step() > 0;
case Right:
return _startPosX + EntityCruiser.Step() + _cruiserWidth < _pictureWidth;
case Down:
return _startPosY + EntityCruiser.Step() + _cruiserHeight < _pictureHeight;
default:
return false;
}
}
public void DrawTransport(Graphics g) {
if (EntityCruiser == null) {
return;
}
g.setColor(EntityCruiser.BodyColor);
g.drawRect(_startPosX + 9, _startPosY + 15, 10, 30);
g.drawRect(_startPosX + 90, _startPosY + 15, 10,
30);
g.drawRect(_startPosX + 20, _startPosY + 4, 70, 52);
g.fillRect(_startPosX + 10, _startPosY + 15, 10, 30);
g.fillRect(_startPosX + 90, _startPosY + 15, 10, 30);
g.fillRect(_startPosX + 20, _startPosY + 5, 70, 50);
g.fillRect(_startPosX + 10, _startPosY + 15, 10, 30);
g.fillRect(_startPosX + 90, _startPosY + 15, 10, 30);
g.fillRect(_startPosX + 20, _startPosY + 5, 70, 50);
Point[] points1 = new Point[3];
points1[0] = new Point(_startPosX + 100, _startPosY + 5);
points1[1] = new Point(_startPosX + 100, _startPosY + 55);
points1[2] = new Point(_startPosX + 150, _startPosY + 30);
g.fillPolygon(new int[]{points1[0].x, points1[1].x, points1[2].x},
new int[]{points1[0].y, points1[1].y, points1[2].y}, 3);
g.fillRect(_startPosX + 5, _startPosY + 15, 10, 10);
g.fillRect(_startPosX + 5, _startPosY + 35, 10, 10);
if (wheels == null) {
return;
}
wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor);
}
public IMoveableObject GetMoveableObject(){return new DrawingObjectCruiser(this);}
}