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

72 lines
2.3 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.ArrayList;
public class GenericCreate<T extends EntityCruiser, U extends IDop> {
ArrayList<T> _EntityCar;
ArrayList<U> _Wheels;
int pointerCars = 0;
int pointerWheels = 0;
public ArrayList<T> getEntities(){
return _EntityCar;
}
public ArrayList<U> getWheels(){
return _Wheels;
}
public GenericCreate(int countEntities,int countWheels){
_EntityCar=new ArrayList<>(countEntities);
_Wheels=new ArrayList<>(countWheels);
}
public int Add(T car){
if(pointerCars <= _EntityCar.size()){
_EntityCar.add(car);
pointerCars++;
return pointerCars;
}
return -1;
}
public int Add(U wheels){
if(pointerWheels <= _Wheels.size()){
_Wheels.add(wheels);
pointerWheels++;
return pointerWheels;
}
return -1;
}
public DrawingCruiser DrawingGeneratedCar()
{
Random rand=new Random();
if (_EntityCar.size() == 0 || _Wheels.size() == 0){
return null;
}
T entity = (_EntityCar.get(rand.nextInt(pointerCars)));
U wheel = (_Wheels.get(rand.nextInt(pointerWheels)));
if(entity instanceof EntityAdvancedCruiser){
return new DrawingAdvancedCruiser((EntityAdvancedCruiser)entity, wheel);
}
return new DrawingCruiser(entity, wheel);
}
public DrawingCruiser[] DrawEntitiesCars(Graphics g, int startX, int startY) {
DrawingCruiser[] drawingCars = new DrawingCruiser[_EntityCar.size()];
int height = 70;
DrawingCruiser car;
DrawingAdvancedCruiser truck;
for (int i = 0; i < _EntityCar.size(); i++) {
if (_EntityCar.get(i) instanceof EntityAdvancedCruiser) {
truck = new DrawingAdvancedCruiser((EntityAdvancedCruiser) _EntityCar.get(i), null);
truck.SetPosition(startX , startY + i * height);
truck.DrawTransport(g);
} else {
car = new DrawingCruiser(_EntityCar.get(i), null);
car.SetPosition(startX , startY + i * height);
car.DrawTransport(g);
}
}
return drawingCars;
}
}