diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..cc121e9 --- /dev/null +++ b/Direction.java @@ -0,0 +1,3 @@ +public enum Direction { + Up, Down, Left, Right +} diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java new file mode 100644 index 0000000..cf537eb --- /dev/null +++ b/DrawningLocomotive.java @@ -0,0 +1,106 @@ +import java.awt.*; + +class DrawningLocomotive { + public EntityLocomotive Locomotive; + /// Левая координата отрисовки локомотива + private float _startPosX; + /// Верхняя координата отрисовки локомотива + private float _startPosY; + /// Ширина окна отрисовки + private Integer _pictureWidth = null; + /// Высота окна отрисовки + private Integer _pictureHeight = null; + /// Ширина отрисовки локомотива + private final int _locomotiveWidth = 110; + /// Высота отрисовки локомотива + private final int _locomotiveHeight = 50; + /// Инициализация свойств + public void Init(int speed, float weight, Color bodyColor, EntityLocomotive entity) + { + Locomotive = entity; + Locomotive.Init(speed, weight, bodyColor); + } + /// Установка позиции локомотива + public void SetPosition(int x, int y, int width, int height) + { + if (x < 0 || x + _locomotiveWidth >= width) + { + return; + } + if (y < 0 || y + _locomotiveHeight >= 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 + _locomotiveWidth + Locomotive.Step() < _pictureWidth) + { + _startPosX += Locomotive.Step(); + } + else _startPosX = _pictureWidth - _locomotiveWidth; + break; + //влево + case Left: + if (_startPosX - Locomotive.Step() >= 0) + { + _startPosX -= Locomotive.Step(); + } + else _startPosX = 0; + break; + //вверх + case Up: + if (_startPosY - Locomotive.Step() >= 0) + { + _startPosY -= Locomotive.Step(); + } + else _startPosY = 0; + break; + //вниз + case Down: + if (_startPosY + _locomotiveHeight + Locomotive.Step() < _pictureHeight) + { + _startPosY += Locomotive.Step(); + } + else _startPosY = _pictureHeight - _locomotiveHeight; + break; + } + } + + public void DrawTransport() { + //TODO: do! + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _locomotiveWidth || _pictureHeight <= _locomotiveHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _locomotiveWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _locomotiveWidth; + } + if (_startPosY + _locomotiveHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _locomotiveHeight; + } + } +} diff --git a/EntityLocomotive.java b/EntityLocomotive.java index 88bcfdf..7d58451 100644 --- a/EntityLocomotive.java +++ b/EntityLocomotive.java @@ -1,8 +1,46 @@ import java.awt.*; +import java.util.Random; public class EntityLocomotive { - public int Speed; - public int Weight; - public Color BodyColor; + private int Speed; + public int getSpeed() { + return Speed; + } + private float Weight; + public float getWeight() { + return Weight; + } + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + + public float Step () { + return Speed * 100 / Weight; + } + + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + if (speed <= 0) { + Speed = 50 + rnd.nextInt(100); + } + else { + Speed = speed; + } + + if (weight <= 0) { + Weight = 40 + rnd.nextInt(30); + } + else { + Weight = weight; + } + BodyColor = bodyColor; + } + + + + + }