PIbd-23_Panina_A.D.Cruiser..../GenericCreate.java

67 lines
2.4 KiB
Java
Raw Normal View History

2023-12-09 17:25:15 +04:00
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.ArrayList;
public class GenericCreate<T extends EntityCruiser, U extends IDop> {
2023-12-09 21:01:39 +04:00
ArrayList<T> _EntityCruiser;
2023-12-09 17:25:15 +04:00
ArrayList<U> _Wheels;
2023-12-09 21:01:39 +04:00
int pointerCruiser = 0;
2023-12-09 17:25:15 +04:00
int pointerWheels = 0;
public ArrayList<T> getEntities(){
2023-12-09 21:01:39 +04:00
return _EntityCruiser;
2023-12-09 17:25:15 +04:00
}
public ArrayList<U> getWheels(){
return _Wheels;
}
public GenericCreate(int countEntities,int countWheels){
2023-12-09 21:01:39 +04:00
_EntityCruiser=new ArrayList<>(countEntities);
2023-12-09 17:25:15 +04:00
_Wheels=new ArrayList<>(countWheels);
}
2023-12-09 21:01:39 +04:00
public int Add(T cruiser){
if(pointerCruiser <= _EntityCruiser.size()){
_EntityCruiser.add(cruiser);
pointerCruiser++;
return pointerCruiser;
2023-12-09 17:25:15 +04:00
}
return -1;
}
public int Add(U wheels){
if(pointerWheels <= _Wheels.size()){
_Wheels.add(wheels);
pointerWheels++;
return pointerWheels;
}
return -1;
}
2023-12-09 21:01:39 +04:00
public DrawingCruiser DrawingGeneratedCruiser()
2023-12-09 17:25:15 +04:00
{
Random rand=new Random();
2023-12-09 21:01:39 +04:00
if (_EntityCruiser.size() == 0 || _Wheels.size() == 0){
2023-12-09 17:25:15 +04:00
return null;
}
2023-12-09 21:01:39 +04:00
T entity = (_EntityCruiser.get(rand.nextInt(pointerCruiser)));
2023-12-09 17:25:15 +04:00
U wheel = (_Wheels.get(rand.nextInt(pointerWheels)));
if(entity instanceof EntityAdvancedCruiser){
2023-12-09 21:01:39 +04:00
return new DrawingAdvancedCruiser((EntityAdvancedCruiser) entity, wheel);
2023-12-09 17:25:15 +04:00
}
return new DrawingCruiser(entity, wheel);
}
2023-12-09 21:01:39 +04:00
public DrawingCruiser[] DrawEntitiesCruiser(Graphics g, int startX, int startY) {
DrawingCruiser[] drawingCruisers = new DrawingCruiser[_EntityCruiser.size()];
2023-12-09 17:25:15 +04:00
int height = 70;
2023-12-09 21:01:39 +04:00
DrawingCruiser cruiser;
DrawingAdvancedCruiser advancedcruiser;
for (int i = 0; i < _EntityCruiser.size(); i++) {
if (_EntityCruiser.get(i) instanceof EntityAdvancedCruiser) {
advancedcruiser = new DrawingAdvancedCruiser((EntityAdvancedCruiser) _EntityCruiser.get(i), null);
advancedcruiser.SetPosition(startX , startY + i * height);
advancedcruiser.DrawTransport(g);
2023-12-09 17:25:15 +04:00
} else {
2023-12-09 21:01:39 +04:00
cruiser = new DrawingCruiser(_EntityCruiser.get(i), null);
cruiser.SetPosition(startX , startY + i * height);
cruiser.DrawTransport(g);
2023-12-09 17:25:15 +04:00
}
}
2023-12-09 21:01:39 +04:00
return drawingCruisers;
2023-12-09 17:25:15 +04:00
}
}