PIbd-22. Shabunov O.A. Lab work 02 (Hard) #2
@ -1,38 +0,0 @@
|
||||
package AirBomber;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public class BomberEntity
|
||||
{
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
private Color AdditionalColor;
|
||||
private boolean Bombs;
|
||||
private boolean FuelTanks;
|
||||
public double Step;
|
||||
|
||||
public void Init(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs)
|
||||
{
|
||||
this.Speed = Speed;
|
||||
this.Weight = Weight;
|
||||
this.BodyColor = BodyColor;
|
||||
this.AdditionalColor = AdditionalColor;
|
||||
this.FuelTanks = FuelTanks;
|
||||
this.Bombs = Bombs;
|
||||
|
||||
this.Step = (double)Speed * 100 / Weight * 5 / 2;
|
||||
}
|
||||
|
||||
public int GetSpeed() { return Speed; }
|
||||
|
||||
public double GetWeight() { return Weight; }
|
||||
|
||||
public Color GetBodyColor() { return BodyColor; }
|
||||
|
||||
public Color GetAdditionalColor() { return AdditionalColor; }
|
||||
|
||||
public boolean GetBombs() { return Bombs; }
|
||||
|
||||
public boolean GetFuelTanks() { return FuelTanks; }
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
package AirBomber;
|
||||
|
||||
import AirBomber.MovementStrategy.*;
|
||||
import AirBomber.Rendering.*;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
@ -9,7 +10,8 @@ import java.util.Random;
|
||||
|
||||
public class BomberForm extends JFrame
|
||||
{
|
||||
private BomberRenderer _bomberRenderer;
|
||||
private BomberRendererBase _bomberRenderer;
|
||||
private AbstractStrategy _strategy;
|
||||
|
||||
public BomberForm()
|
||||
{
|
||||
@ -32,12 +34,11 @@ public class BomberForm extends JFrame
|
||||
BomberPictureBox.setIcon(new ImageIcon(bmp));
|
||||
}
|
||||
|
||||
private void ButtonCreate_Click(ActionEvent e)
|
||||
private void ButtonCreateBomber_Click(ActionEvent e)
|
||||
{
|
||||
Random random = new Random();
|
||||
_bomberRenderer = new BomberRenderer();
|
||||
|
||||
_bomberRenderer.Init(
|
||||
_bomberRenderer = new BomberRenderer(
|
||||
random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
@ -47,11 +48,75 @@ public class BomberForm extends JFrame
|
||||
BomberPictureBox.getWidth(),
|
||||
BomberPictureBox.getHeight()
|
||||
);
|
||||
_bomberRenderer.SetPosition(random.nextInt(20, 100), random.nextInt(20, 100));
|
||||
_bomberRenderer.SetPosition(random.nextInt(10, 60), random.nextInt(10, 60));
|
||||
|
||||
MovementStrategyComboBox.setEnabled(true);
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonCreateBomberBase_Click(ActionEvent e)
|
||||
{
|
||||
Random random = new Random();
|
||||
|
||||
_bomberRenderer = new BomberRendererBase(
|
||||
random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
BomberPictureBox.getWidth(),
|
||||
BomberPictureBox.getHeight()
|
||||
);
|
||||
_bomberRenderer.SetPosition(random.nextInt(10, 60), random.nextInt(10, 60));
|
||||
|
||||
MovementStrategyComboBox.setEnabled(true);
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonPerformStep_Click(ActionEvent e)
|
||||
{
|
||||
if (_bomberRenderer == null)
|
||||
return;
|
||||
|
||||
if (MovementStrategyComboBox.isEnabled())
|
||||
{
|
||||
switch (MovementStrategyComboBox.getSelectedIndex())
|
||||
{
|
||||
case 0:
|
||||
_strategy = new MoveToCenterStrategy();
|
||||
break;
|
||||
|
||||
case 1:
|
||||
_strategy = new MoveToDownRightStrategy();
|
||||
break;
|
||||
|
||||
default:
|
||||
_strategy = null;
|
||||
}
|
||||
|
||||
if (_strategy == null)
|
||||
return;
|
||||
|
||||
_strategy.SetData(
|
||||
new ObjectEntityRenderer(_bomberRenderer),
|
||||
BomberPictureBox.getWidth(),
|
||||
BomberPictureBox.getHeight()
|
||||
);
|
||||
|
||||
MovementStrategyComboBox.setEnabled(false);
|
||||
}
|
||||
|
||||
if (_strategy == null)
|
||||
return;
|
||||
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
|
||||
if (_strategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
MovementStrategyComboBox.setEnabled(true);
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonMove_Click(ActionEvent e)
|
||||
{
|
||||
if (_bomberRenderer == null)
|
||||
@ -84,25 +149,28 @@ public class BomberForm extends JFrame
|
||||
private void InitializeComponent()
|
||||
{
|
||||
BomberPictureBox = new JLabel();
|
||||
CreateButton = new JButton();
|
||||
ButtonCreateBomberBase = new JButton();
|
||||
ButtonRight = new JButton();
|
||||
ButtonDown = new JButton();
|
||||
ButtonLeft = new JButton();
|
||||
ButtonUp = new JButton();
|
||||
MovementStrategyComboBox = new JComboBox<String>();
|
||||
ButtonCreateBomber = new JButton();
|
||||
ButtonPerformMove = new JButton();
|
||||
//
|
||||
// BomberPictureBox
|
||||
//
|
||||
BomberPictureBox.setBounds(0, 0, 884, 461);
|
||||
//
|
||||
// CreateButton
|
||||
// ButtonCreateBomberBase
|
||||
//
|
||||
CreateButton.setName("CreateButton");
|
||||
CreateButton.setBounds(12, 419, 80, 30);
|
||||
CreateButton.setText("Создать");
|
||||
CreateButton.setBackground(new Color(225, 225, 225));
|
||||
CreateButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
|
||||
CreateButton.setFocusable(false);
|
||||
CreateButton.addActionListener(e -> ButtonCreate_Click(e));
|
||||
ButtonCreateBomberBase.setName("ButtonCreateBomberBase");
|
||||
ButtonCreateBomberBase.setBounds(12, 383, 260, 30);
|
||||
ButtonCreateBomberBase.setText("Создать бомбардировщик");
|
||||
ButtonCreateBomberBase.setBackground(new Color(225, 225, 225));
|
||||
ButtonCreateBomberBase.setFont(new Font("Segoe UI", Font.PLAIN, 11));
|
||||
ButtonCreateBomberBase.setFocusable(false);
|
||||
ButtonCreateBomberBase.addActionListener(e -> ButtonCreateBomberBase_Click(e));
|
||||
//
|
||||
// ButtonRight
|
||||
//
|
||||
@ -144,6 +212,32 @@ public class BomberForm extends JFrame
|
||||
ButtonUp.setIcon(new ImageIcon("src/AirBomber/Resources/ArrowUp.png"));
|
||||
ButtonUp.addActionListener(e -> ButtonMove_Click(e));
|
||||
//
|
||||
// MovementStrategyComboBox
|
||||
//
|
||||
MovementStrategyComboBox.setBounds(706, 12, 166, 23);
|
||||
MovementStrategyComboBox.addItem("Перемещать в центр");
|
||||
MovementStrategyComboBox.addItem("Перемещать вправо вниз");
|
||||
//
|
||||
// ButtonCreateBomber
|
||||
//
|
||||
ButtonCreateBomber.setName("ButtonCreateBomber");
|
||||
ButtonCreateBomber.setBounds(12, 419, 260, 30);
|
||||
ButtonCreateBomber.setText("Создать продвинутый бомбардировщик");
|
||||
ButtonCreateBomber.setBackground(new Color(225, 225, 225));
|
||||
ButtonCreateBomber.setFont(new Font("Segoe UI", Font.PLAIN, 11));
|
||||
ButtonCreateBomber.setFocusable(false);
|
||||
ButtonCreateBomber.addActionListener(e -> ButtonCreateBomber_Click(e));
|
||||
//
|
||||
// ButtonPerformMove
|
||||
//
|
||||
ButtonPerformMove.setName("ButtonPerformMove");
|
||||
ButtonPerformMove.setBounds(706, 50, 166, 31);
|
||||
ButtonPerformMove.setText("Сделать шаг");
|
||||
ButtonPerformMove.setBackground(new Color(225, 225, 225));
|
||||
ButtonPerformMove.setFont(new Font("Segoe UI", Font.PLAIN, 11));
|
||||
ButtonPerformMove.setFocusable(false);
|
||||
ButtonPerformMove.addActionListener(e -> ButtonPerformStep_Click(e));
|
||||
//
|
||||
// BomberForm
|
||||
//
|
||||
setTitle("Бомбардировщик");
|
||||
@ -152,18 +246,24 @@ public class BomberForm extends JFrame
|
||||
setLayout(null);
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
add(ButtonPerformMove);
|
||||
add(ButtonCreateBomber);
|
||||
add(MovementStrategyComboBox);
|
||||
add(ButtonUp);
|
||||
add(ButtonLeft);
|
||||
add(ButtonDown);
|
||||
add(ButtonRight);
|
||||
add(CreateButton);
|
||||
add(ButtonCreateBomberBase);
|
||||
add(BomberPictureBox);
|
||||
}
|
||||
|
||||
private JLabel BomberPictureBox;
|
||||
private JButton CreateButton;
|
||||
private JButton ButtonCreateBomberBase;
|
||||
private JButton ButtonRight;
|
||||
private JButton ButtonDown;
|
||||
private JButton ButtonLeft;
|
||||
private JButton ButtonUp;
|
||||
private JComboBox<String> MovementStrategyComboBox;
|
||||
private JButton ButtonCreateBomber;
|
||||
private JButton ButtonPerformMove;
|
||||
}
|
||||
|
25
src/AirBomber/Entities/BomberEntity.java
Normal file
25
src/AirBomber/Entities/BomberEntity.java
Normal file
@ -0,0 +1,25 @@
|
||||
package AirBomber.Entities;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public class BomberEntity extends BomberEntityBase
|
||||
{
|
||||
private Color AdditionalColor;
|
||||
private boolean Bombs;
|
||||
private boolean FuelTanks;
|
||||
|
||||
public BomberEntity(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs)
|
||||
{
|
||||
super(Speed, Weight, BodyColor);
|
||||
|
||||
this.AdditionalColor = AdditionalColor;
|
||||
this.FuelTanks = FuelTanks;
|
||||
this.Bombs = Bombs;
|
||||
}
|
||||
|
||||
public Color GetAdditionalColor() { return AdditionalColor; }
|
||||
|
||||
public boolean GetBombs() { return Bombs; }
|
||||
|
||||
public boolean GetFuelTanks() { return FuelTanks; }
|
||||
}
|
26
src/AirBomber/Entities/BomberEntityBase.java
Normal file
26
src/AirBomber/Entities/BomberEntityBase.java
Normal file
@ -0,0 +1,26 @@
|
||||
package AirBomber.Entities;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public class BomberEntityBase
|
||||
{
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
public double Step;
|
||||
|
||||
public BomberEntityBase(int Speed, double Weight, Color BodyColor)
|
||||
{
|
||||
this.Speed = Speed;
|
||||
this.Weight = Weight;
|
||||
this.BodyColor = BodyColor;
|
||||
|
||||
this.Step = (double)Speed * 100 / Weight * 5 / 2;
|
||||
}
|
||||
|
||||
public int GetSpeed() { return Speed; }
|
||||
|
||||
public double GetWeight() { return Weight; }
|
||||
|
||||
public Color GetBodyColor() { return BodyColor; }
|
||||
}
|
81
src/AirBomber/MovementStrategy/AbstractStrategy.java
Normal file
81
src/AirBomber/MovementStrategy/AbstractStrategy.java
Normal file
@ -0,0 +1,81 @@
|
||||
package AirBomber.MovementStrategy;
|
||||
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
private IMovableObject _movableObject;
|
||||
|
||||
private Status _state = Status.NotInit;
|
||||
|
||||
protected int FieldWidth;
|
||||
protected int FieldHeight;
|
||||
|
||||
public int GetFieldWidth() { return FieldWidth; }
|
||||
public int GetFieldHeight() { return FieldHeight; }
|
||||
|
||||
public Status GetStatus() { return _state; }
|
||||
|
||||
public void SetData(IMovableObject MovableObject, int Width, int Height)
|
||||
{
|
||||
if (MovableObject == null)
|
||||
{
|
||||
_state = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
|
||||
_state = Status.InProgress;
|
||||
_movableObject = MovableObject;
|
||||
|
||||
FieldWidth = Width;
|
||||
FieldHeight = Height;
|
||||
}
|
||||
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
return;
|
||||
|
||||
if (IsTargetDestination())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
protected boolean MoveLeft() { return MoveTo(DirectionType.Left); }
|
||||
protected boolean MoveRight() { return MoveTo(DirectionType.Right); }
|
||||
protected boolean MoveUp() { return MoveTo(DirectionType.Up); }
|
||||
protected boolean MoveDown() { return MoveTo(DirectionType.Down); }
|
||||
|
||||
protected ObjectParameters GetObjectParameters()
|
||||
{
|
||||
return _movableObject != null ? _movableObject.GetObjectPosition() : null;
|
||||
}
|
||||
|
||||
protected Integer GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
return null;
|
||||
|
||||
return _movableObject != null ? _movableObject.GetStep() : null;
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract boolean IsTargetDestination();
|
||||
|
||||
private boolean MoveTo(DirectionType DirectionType)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
return false;
|
||||
|
||||
if (_movableObject != null && _movableObject.CanMove(DirectionType))
|
||||
{
|
||||
_movableObject.MoveObject(DirectionType);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package AirBomber;
|
||||
package AirBomber.MovementStrategy;
|
||||
|
||||
public enum DirectionType
|
||||
{
|
12
src/AirBomber/MovementStrategy/IMovableObject.java
Normal file
12
src/AirBomber/MovementStrategy/IMovableObject.java
Normal file
@ -0,0 +1,12 @@
|
||||
package AirBomber.MovementStrategy;
|
||||
|
||||
public interface IMovableObject
|
||||
{
|
||||
ObjectParameters GetObjectPosition();
|
||||
|
||||
int GetStep();
|
||||
|
||||
boolean CanMove(DirectionType Direction);
|
||||
|
||||
void MoveObject(DirectionType Direction);
|
||||
}
|
45
src/AirBomber/MovementStrategy/MoveToCenterStrategy.java
Normal file
45
src/AirBomber/MovementStrategy/MoveToCenterStrategy.java
Normal file
@ -0,0 +1,45 @@
|
||||
package AirBomber.MovementStrategy;
|
||||
|
||||
public class MoveToCenterStrategy extends AbstractStrategy
|
||||
{
|
||||
@Override
|
||||
protected boolean IsTargetDestination()
|
||||
{
|
||||
ObjectParameters ObjParams = GetObjectParameters();
|
||||
if (ObjParams == null)
|
||||
return false;
|
||||
|
||||
float FieldMiddleHorizontal = FieldWidth / 2;
|
||||
float FieldMiddleVertical = FieldHeight / 2;
|
||||
|
||||
return
|
||||
Math.abs(ObjParams.ObjectMiddleHorizontal() - FieldMiddleHorizontal) < GetStep() &&
|
||||
Math.abs(ObjParams.ObjectMiddleVertical() - FieldMiddleVertical) < GetStep();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
ObjectParameters ObjParams = GetObjectParameters();
|
||||
if (ObjParams == null)
|
||||
return;
|
||||
|
||||
float diffX = ObjParams.ObjectMiddleHorizontal() - FieldWidth / 2;
|
||||
if (Math.abs(diffX) >= GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
MoveLeft();
|
||||
else
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
float diffY = ObjParams.ObjectMiddleVertical() - FieldHeight / 2;
|
||||
if (Math.abs(diffY) >= GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
MoveUp();
|
||||
else
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
44
src/AirBomber/MovementStrategy/MoveToDownRightStrategy.java
Normal file
44
src/AirBomber/MovementStrategy/MoveToDownRightStrategy.java
Normal file
@ -0,0 +1,44 @@
|
||||
package AirBomber.MovementStrategy;
|
||||
|
||||
public class MoveToDownRightStrategy extends AbstractStrategy
|
||||
{
|
||||
@Override
|
||||
protected boolean IsTargetDestination()
|
||||
{
|
||||
ObjectParameters ObjParams = GetObjectParameters();
|
||||
if (ObjParams == null)
|
||||
return false;
|
||||
|
||||
return
|
||||
ObjParams.RightBorder() <= FieldWidth &&
|
||||
ObjParams.RightBorder() + GetStep() >= FieldWidth &&
|
||||
ObjParams.BottomBorder() <= FieldHeight &&
|
||||
ObjParams.BottomBorder() + GetStep() >= FieldHeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
ObjectParameters ObjParams = GetObjectParameters();
|
||||
if (ObjParams == null)
|
||||
return;
|
||||
|
||||
float diffX = ObjParams.RightBorder() - FieldWidth;
|
||||
if (Math.abs(diffX) >= GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
MoveLeft();
|
||||
else
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
float diffY = ObjParams.BottomBorder() - FieldHeight;
|
||||
if (Math.abs(diffY) >= GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
MoveUp();
|
||||
else
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
48
src/AirBomber/MovementStrategy/ObjectEntityRenderer.java
Normal file
48
src/AirBomber/MovementStrategy/ObjectEntityRenderer.java
Normal file
@ -0,0 +1,48 @@
|
||||
package AirBomber.MovementStrategy;
|
||||
|
||||
import AirBomber.Rendering.*;
|
||||
|
||||
public class ObjectEntityRenderer implements IMovableObject
|
||||
{
|
||||
private final BomberRendererBase _entityRenderer;
|
||||
|
||||
public ObjectEntityRenderer(BomberRendererBase EntityRenderer)
|
||||
{
|
||||
_entityRenderer = EntityRenderer;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectPosition()
|
||||
{
|
||||
if (_entityRenderer == null || _entityRenderer.GetEntityBomber() == null)
|
||||
return null;
|
||||
|
||||
return new ObjectParameters(
|
||||
_entityRenderer.PosX(),
|
||||
_entityRenderer.PosY(),
|
||||
_entityRenderer.EntityWidth(),
|
||||
_entityRenderer.EntityHeight()
|
||||
);
|
||||
}
|
||||
|
||||
public int GetStep()
|
||||
{
|
||||
if (_entityRenderer == null || _entityRenderer.GetEntityBomber() == null)
|
||||
return 0;
|
||||
|
||||
return (int)(_entityRenderer.GetEntityBomber().Step);
|
||||
}
|
||||
|
||||
public boolean CanMove(DirectionType Direction)
|
||||
{
|
||||
if (_entityRenderer == null)
|
||||
return false;
|
||||
|
||||
return _entityRenderer.CanMove(Direction);
|
||||
}
|
||||
|
||||
public void MoveObject(DirectionType Direction)
|
||||
{
|
||||
if (_entityRenderer != null)
|
||||
_entityRenderer.MoveEntity(Direction);
|
||||
}
|
||||
}
|
26
src/AirBomber/MovementStrategy/ObjectParameters.java
Normal file
26
src/AirBomber/MovementStrategy/ObjectParameters.java
Normal file
@ -0,0 +1,26 @@
|
||||
package AirBomber.MovementStrategy;
|
||||
|
||||
public class ObjectParameters
|
||||
{
|
||||
private final int _x;
|
||||
private final int _y;
|
||||
|
||||
private final int _width;
|
||||
private final int _height;
|
||||
|
||||
public int LeftBorder() { return _x; }
|
||||
public int TopBorder() { return _y; }
|
||||
public int RightBorder() { return _x + _width; }
|
||||
public int BottomBorder() { return _y + _height; }
|
||||
|
||||
public int ObjectMiddleHorizontal() { return _x + _width / 2; }
|
||||
public int ObjectMiddleVertical() { return _y + _height / 2; }
|
||||
|
||||
public ObjectParameters(int x, int y, int Width, int Height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = Width;
|
||||
_height = Height;
|
||||
}
|
||||
}
|
8
src/AirBomber/MovementStrategy/Status.java
Normal file
8
src/AirBomber/MovementStrategy/Status.java
Normal file
@ -0,0 +1,8 @@
|
||||
package AirBomber.MovementStrategy;
|
||||
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
64
src/AirBomber/Rendering/BomberRenderer.java
Normal file
64
src/AirBomber/Rendering/BomberRenderer.java
Normal file
@ -0,0 +1,64 @@
|
||||
package AirBomber.Rendering;
|
||||
|
||||
import AirBomber.Entities.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class BomberRenderer extends BomberRendererBase
|
||||
{
|
||||
public BomberRenderer(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs, int Width, int Height)
|
||||
{
|
||||
super(Speed, Weight, BodyColor, Width, Height, 200, 200);
|
||||
|
||||
if (EntityBomber != null)
|
||||
EntityBomber = new BomberEntity(Speed, Weight, BodyColor, AdditionalColor, FuelTanks, Bombs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawEntity(Graphics g)
|
||||
{
|
||||
if ( !(EntityBomber instanceof BomberEntity DerivedEntityBomber) )
|
||||
return;
|
||||
|
||||
super.DrawEntity(g);
|
||||
|
||||
/** Отрисовка дополнительных элементов */
|
||||
g.setColor(DerivedEntityBomber.GetAdditionalColor());
|
||||
|
||||
if (DerivedEntityBomber.GetFuelTanks())
|
||||
{
|
||||
int[] LeftGasTankX = {
|
||||
_startPosX + 50,
|
||||
_startPosX + 75,
|
||||
_startPosX + 75,
|
||||
_startPosX + 50,
|
||||
};
|
||||
int[] LeftGasTankY = {
|
||||
_startPosY + 85,
|
||||
_startPosY + 85,
|
||||
_startPosY + 70,
|
||||
_startPosY + 70,
|
||||
};
|
||||
g.fillPolygon(LeftGasTankX, LeftGasTankY, 4);
|
||||
|
||||
int[] RightGasTankX = {
|
||||
_startPosX + 50,
|
||||
_startPosX + 75,
|
||||
_startPosX + 75,
|
||||
_startPosX + 50,
|
||||
};
|
||||
int[] RightGasTankY = {
|
||||
_startPosY + 115,
|
||||
_startPosY + 115,
|
||||
_startPosY + 130,
|
||||
_startPosY + 130,
|
||||
};
|
||||
g.fillPolygon(RightGasTankX, RightGasTankY, 4);
|
||||
}
|
||||
|
||||
if (DerivedEntityBomber.GetBombs())
|
||||
{
|
||||
g.fillOval(_startPosX + 110, _startPosY + 115, 50, 25);
|
||||
g.fillOval(_startPosX + 110, _startPosY + 60, 50, 25);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,43 +1,103 @@
|
||||
package AirBomber;
|
||||
package AirBomber.Rendering;
|
||||
|
||||
import AirBomber.Entities.*;
|
||||
import AirBomber.MovementStrategy.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class BomberRenderer
|
||||
public class BomberRendererBase
|
||||
{
|
||||
private BomberEntity EntityBomber;
|
||||
protected BomberEntityBase EntityBomber;
|
||||
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
|
||||
private int _bomberWidth = 200;
|
||||
private int _bomberHeight = 200;
|
||||
protected int _bomberWidth = 200;
|
||||
protected int _bomberHeight = 200;
|
||||
|
||||
public EngineRenderer EngineRenderer;
|
||||
public int PosX() { return _startPosX; }
|
||||
public int PosY() { return _startPosY; }
|
||||
|
||||
public boolean Init(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs, int Width, int Height)
|
||||
public IEngineRenderer EngineRenderer;
|
||||
|
||||
public int EntityWidth() { return _bomberWidth; }
|
||||
public int EntityHeight() { return _bomberHeight; }
|
||||
|
||||
public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height)
|
||||
{
|
||||
if (Width < _bomberWidth || Height < _bomberHeight)
|
||||
return false;
|
||||
return;
|
||||
|
||||
_pictureWidth = Width;
|
||||
_pictureHeight = Height;
|
||||
|
||||
EntityBomber = new BomberEntity();
|
||||
EntityBomber.Init(Speed, Weight, BodyColor, AdditionalColor, FuelTanks, Bombs);
|
||||
|
||||
EngineRenderer = new EngineRenderer();
|
||||
EntityBomber = new BomberEntityBase(Speed, Weight, BodyColor);
|
||||
|
||||
Random Random = new Random();
|
||||
EngineRenderer.SetAmount(Random.nextInt(1, 7));
|
||||
|
||||
return true;
|
||||
/** Создание прорисовщика дополнительных элементов */
|
||||
int EngineRendererVariant = Random.nextInt(1, 4);
|
||||
switch (EngineRendererVariant) {
|
||||
case 1:
|
||||
EngineRenderer = new EngineRenderer();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
EngineRenderer = new RectangleEngineRenderer();
|
||||
break;
|
||||
|
||||
case 3:
|
||||
EngineRenderer = new TriangleEngineRenderer();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
EngineRenderer.SetAmount(Random.nextInt(1, 7));
|
||||
}
|
||||
|
||||
public BomberEntity GetEntityBomber() { return EntityBomber; }
|
||||
public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height, int EntityWidth, int EntityHeight)
|
||||
{
|
||||
if (Width < _bomberWidth || Height < _bomberHeight)
|
||||
return;
|
||||
|
||||
_pictureWidth = Width;
|
||||
_pictureHeight = Height;
|
||||
|
||||
_bomberWidth = EntityWidth;
|
||||
_bomberHeight = EntityHeight;
|
||||
|
||||
EntityBomber = new BomberEntityBase(Speed, Weight, BodyColor);
|
||||
|
||||
Random Random = new Random();
|
||||
|
||||
/** Создание прорисовщика дополнительных элементов */
|
||||
int EngineRendererVariant = Random.nextInt(1, 4);
|
||||
switch (EngineRendererVariant) {
|
||||
case 1:
|
||||
EngineRenderer = new EngineRenderer();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
EngineRenderer = new RectangleEngineRenderer();
|
||||
break;
|
||||
|
||||
case 3:
|
||||
EngineRenderer = new TriangleEngineRenderer();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
EngineRenderer.SetAmount(Random.nextInt(1, 7));
|
||||
}
|
||||
|
||||
public BomberEntityBase GetEntityBomber() { return EntityBomber; }
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
@ -59,35 +119,50 @@ public class BomberRenderer
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveEntity(DirectionType Direction)
|
||||
public boolean CanMove(DirectionType Direction)
|
||||
{
|
||||
if (EntityBomber == null)
|
||||
return false;
|
||||
|
||||
switch (Direction) {
|
||||
case Left:
|
||||
return _startPosX - EntityBomber.Step > 0;
|
||||
|
||||
case Up:
|
||||
return _startPosY - EntityBomber.Step > 0;
|
||||
|
||||
case Right:
|
||||
return _startPosX + _bomberWidth + EntityBomber.Step <= _pictureWidth;
|
||||
|
||||
case Down:
|
||||
return _startPosY + _bomberHeight + EntityBomber.Step <= _pictureHeight;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveEntity(DirectionType Direction)
|
||||
{
|
||||
if (EntityBomber == null || !CanMove(Direction))
|
||||
return;
|
||||
|
||||
switch (Direction)
|
||||
{
|
||||
case Up:
|
||||
if (_startPosY - EntityBomber.Step > 0)
|
||||
_startPosY -= (int)EntityBomber.Step;
|
||||
|
||||
_startPosY -= (int)EntityBomber.Step;
|
||||
break;
|
||||
|
||||
case Down:
|
||||
if (_startPosY + _bomberHeight + EntityBomber.Step <= _pictureHeight)
|
||||
_startPosY += (int)EntityBomber.Step;
|
||||
|
||||
_startPosY += (int)EntityBomber.Step;
|
||||
break;
|
||||
|
||||
case Left:
|
||||
if (_startPosX - EntityBomber.Step > 0)
|
||||
_startPosX -= (int)EntityBomber.Step;
|
||||
|
||||
_startPosX -= (int)EntityBomber.Step;
|
||||
break;
|
||||
|
||||
case Right:
|
||||
if (_startPosX + _bomberWidth + EntityBomber.Step <= _pictureWidth)
|
||||
_startPosX += (int)EntityBomber.Step;
|
||||
|
||||
_startPosX += (int)EntityBomber.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -183,46 +258,6 @@ public class BomberRenderer
|
||||
};
|
||||
g.drawPolygon(BackRightWingX, BackRightWingY, 4);
|
||||
|
||||
/** Отрисовка дополнительных элементов */
|
||||
g.setColor(EntityBomber.GetAdditionalColor());
|
||||
|
||||
if (EntityBomber.GetFuelTanks())
|
||||
{
|
||||
int[] LeftGasTankX = {
|
||||
_startPosX + 50,
|
||||
_startPosX + 75,
|
||||
_startPosX + 75,
|
||||
_startPosX + 50,
|
||||
};
|
||||
int[] LeftGasTankY = {
|
||||
_startPosY + 85,
|
||||
_startPosY + 85,
|
||||
_startPosY + 70,
|
||||
_startPosY + 70,
|
||||
};
|
||||
g.fillPolygon(LeftGasTankX, LeftGasTankY, 4);
|
||||
|
||||
int[] RightGasTankX = {
|
||||
_startPosX + 50,
|
||||
_startPosX + 75,
|
||||
_startPosX + 75,
|
||||
_startPosX + 50,
|
||||
};
|
||||
int[] RightGasTankY = {
|
||||
_startPosY + 115,
|
||||
_startPosY + 115,
|
||||
_startPosY + 130,
|
||||
_startPosY + 130,
|
||||
};
|
||||
g.fillPolygon(RightGasTankX, RightGasTankY, 4);
|
||||
}
|
||||
|
||||
if (EntityBomber.GetBombs())
|
||||
{
|
||||
g.fillOval(_startPosX + 110, _startPosY + 115, 50, 25);
|
||||
g.fillOval(_startPosX + 110, _startPosY + 60, 50, 25);
|
||||
}
|
||||
|
||||
EngineRenderer.DrawEngines(g, EntityBomber.GetBodyColor(), _startPosX, _startPosY);
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package AirBomber;
|
||||
package AirBomber.Rendering;
|
||||
|
||||
import java.awt.*;
|
||||
import AirBomber.EnginesAmount;
|
||||
|
||||
public class EngineRenderer
|
||||
public class EngineRenderer implements IEngineRenderer
|
||||
{
|
||||
private EnginesAmount Amount;
|
||||
|
10
src/AirBomber/Rendering/IEngineRenderer.java
Normal file
10
src/AirBomber/Rendering/IEngineRenderer.java
Normal file
@ -0,0 +1,10 @@
|
||||
package AirBomber.Rendering;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IEngineRenderer
|
||||
{
|
||||
public void SetAmount(int Amount);
|
||||
|
||||
public void DrawEngines(Graphics g, Color BodyColor, int StartPosX, int StartPosY);
|
||||
}
|
41
src/AirBomber/Rendering/RectangleEngineRenderer.java
Normal file
41
src/AirBomber/Rendering/RectangleEngineRenderer.java
Normal file
@ -0,0 +1,41 @@
|
||||
package AirBomber.Rendering;
|
||||
|
||||
import java.awt.*;
|
||||
import AirBomber.EnginesAmount;
|
||||
|
||||
public class RectangleEngineRenderer implements IEngineRenderer
|
||||
{
|
||||
private EnginesAmount Amount;
|
||||
|
||||
public void SetAmount(int Amount)
|
||||
{
|
||||
if (Amount <= 2)
|
||||
this.Amount = EnginesAmount.Two;
|
||||
|
||||
else if (Amount > 2 && Amount <= 4)
|
||||
this.Amount = EnginesAmount.Four;
|
||||
|
||||
else
|
||||
this.Amount = EnginesAmount.Six;
|
||||
}
|
||||
|
||||
public void DrawEngines(Graphics g, Color BodyColor, int _startPosX, int _startPosY)
|
||||
{
|
||||
g.setColor(BodyColor);
|
||||
|
||||
g.fillRect(_startPosX + 83, _startPosY + 50, 30, 15);
|
||||
g.fillRect(_startPosX + 83, _startPosY + 135, 30, 15);
|
||||
|
||||
if (Amount == EnginesAmount.Two)
|
||||
return;
|
||||
|
||||
g.fillRect(_startPosX + 83, _startPosY + 30, 30, 15);
|
||||
g.fillRect(_startPosX + 83, _startPosY + 155, 30, 15);
|
||||
|
||||
if (Amount == EnginesAmount.Four)
|
||||
return;
|
||||
|
||||
g.fillRect(_startPosX + 83, _startPosY + 10, 30, 15);
|
||||
g.fillRect(_startPosX + 83, _startPosY + 175, 30, 15);
|
||||
}
|
||||
}
|
56
src/AirBomber/Rendering/TriangleEngineRenderer.java
Normal file
56
src/AirBomber/Rendering/TriangleEngineRenderer.java
Normal file
@ -0,0 +1,56 @@
|
||||
package AirBomber.Rendering;
|
||||
|
||||
import java.awt.*;
|
||||
import AirBomber.EnginesAmount;
|
||||
|
||||
public class TriangleEngineRenderer implements IEngineRenderer
|
||||
{
|
||||
private EnginesAmount Amount;
|
||||
|
||||
public void SetAmount(int Amount)
|
||||
{
|
||||
if (Amount <= 2)
|
||||
this.Amount = EnginesAmount.Two;
|
||||
|
||||
else if (Amount > 2 && Amount <= 4)
|
||||
this.Amount = EnginesAmount.Four;
|
||||
|
||||
else
|
||||
this.Amount = EnginesAmount.Six;
|
||||
}
|
||||
|
||||
public void DrawEngines(Graphics g, Color BodyColor, int _startPosX, int _startPosY)
|
||||
{
|
||||
g.setColor(BodyColor);
|
||||
|
||||
int[] FirstTopX = { _startPosX + 83, _startPosX + 113, _startPosX + 83 };
|
||||
int[] FirstTopY = { _startPosY + 50, _startPosY + 57, _startPosY + 65 };
|
||||
g.fillPolygon(FirstTopX, FirstTopY, 3);
|
||||
|
||||
int[] FirstBottomX = { _startPosX + 83, _startPosX + 113, _startPosX + 83 };
|
||||
int[] FirstBottomY = { _startPosY + 135, _startPosY + 142, _startPosY + 150 };
|
||||
g.fillPolygon(FirstBottomX, FirstBottomY, 3);
|
||||
|
||||
if (Amount == EnginesAmount.Two)
|
||||
return;
|
||||
|
||||
int[] SecondTopX = { _startPosX + 83, _startPosX + 113, _startPosX + 83 };
|
||||
int[] SecondTopY = { _startPosY + 30, _startPosY + 37, _startPosY + 45 };
|
||||
g.fillPolygon(SecondTopX, SecondTopY, 3);
|
||||
|
||||
int[] SecondBottomX = { _startPosX + 83, _startPosX + 113, _startPosX + 83 };
|
||||
int[] SecondBottomY = { _startPosY + 155, _startPosY + 162, _startPosY + 170 };
|
||||
g.fillPolygon(SecondBottomX, SecondBottomY, 3);
|
||||
|
||||
if (Amount == EnginesAmount.Four)
|
||||
return;
|
||||
|
||||
int[] ThirdTopX = { _startPosX + 83, _startPosX + 113, _startPosX + 83 };
|
||||
int[] ThirdTopY = { _startPosY + 10, _startPosY + 17, _startPosY + 25 };
|
||||
g.fillPolygon(ThirdTopX, ThirdTopY, 3);
|
||||
|
||||
int[] ThirdBottomX = { _startPosX + 83, _startPosX + 113, _startPosX + 83 };
|
||||
int[] ThirdBottomY = { _startPosY + 175, _startPosY + 182, _startPosY + 190 };
|
||||
g.fillPolygon(ThirdBottomX, ThirdBottomY, 3);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user