PIbd-23_Panina_A.D.Cruiser..../GenericCreate.java
2023-12-09 21:01:39 +04:00

67 lines
2.4 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> _EntityCruiser;
ArrayList<U> _Wheels;
int pointerCruiser = 0;
int pointerWheels = 0;
public ArrayList<T> getEntities(){
return _EntityCruiser;
}
public ArrayList<U> getWheels(){
return _Wheels;
}
public GenericCreate(int countEntities,int countWheels){
_EntityCruiser=new ArrayList<>(countEntities);
_Wheels=new ArrayList<>(countWheels);
}
public int Add(T cruiser){
if(pointerCruiser <= _EntityCruiser.size()){
_EntityCruiser.add(cruiser);
pointerCruiser++;
return pointerCruiser;
}
return -1;
}
public int Add(U wheels){
if(pointerWheels <= _Wheels.size()){
_Wheels.add(wheels);
pointerWheels++;
return pointerWheels;
}
return -1;
}
public DrawingCruiser DrawingGeneratedCruiser()
{
Random rand=new Random();
if (_EntityCruiser.size() == 0 || _Wheels.size() == 0){
return null;
}
T entity = (_EntityCruiser.get(rand.nextInt(pointerCruiser)));
U wheel = (_Wheels.get(rand.nextInt(pointerWheels)));
if(entity instanceof EntityAdvancedCruiser){
return new DrawingAdvancedCruiser((EntityAdvancedCruiser) entity, wheel);
}
return new DrawingCruiser(entity, wheel);
}
public DrawingCruiser[] DrawEntitiesCruiser(Graphics g, int startX, int startY) {
DrawingCruiser[] drawingCruisers = new DrawingCruiser[_EntityCruiser.size()];
int height = 70;
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);
} else {
cruiser = new DrawingCruiser(_EntityCruiser.get(i), null);
cruiser.SetPosition(startX , startY + i * height);
cruiser.DrawTransport(g);
}
}
return drawingCruisers;
}
}