PIbd-23_Minhasapov_R.H._Exc.../EntityWithRollers.java

49 lines
1.4 KiB
Java
Raw Normal View History

2022-11-27 15:08:50 +04:00
import java.util.ArrayList;
import java.util.Random;
public class EntityWithRollers<T extends EntityTracktor, U extends IDrawningRollers> {
static Random rnd = new Random();
2022-11-27 15:08:50 +04:00
private ArrayList<T> entities;
private ArrayList<U> rollers;
public int maxEntitiesCount = 0;
public int maxRollersCount = 0;
public EntityWithRollers(int count) {
2022-11-27 15:08:50 +04:00
entities = new ArrayList<T>();
rollers = new ArrayList<U>();
maxEntitiesCount = count;
maxRollersCount = count;
}
public boolean add(T entity) {
2022-11-27 15:08:50 +04:00
if (entities.size() >= maxEntitiesCount){
2022-11-22 16:01:36 +04:00
return false;
}
2022-11-27 15:08:50 +04:00
entities.add(entity);
return true;
}
public boolean add(U roller) {
2022-11-27 15:08:50 +04:00
if (rollers.size() >= maxRollersCount){
2022-11-22 16:01:36 +04:00
return false;
}
2022-11-27 15:08:50 +04:00
rollers.add(roller);
return true;
}
2022-11-22 16:01:36 +04:00
public IDrawningObject constructTracktor() {
2022-11-27 15:08:50 +04:00
if (entities.size() == 0 || rollers.size() == 0) {
return null;
}
2022-11-27 15:08:50 +04:00
EntityTracktor entity = (EntityTracktor) entities.get(rnd.nextInt(0, entities.size()));
IDrawningRollers roller = (IDrawningRollers) rollers.get(rnd.nextInt(0, rollers.size()));
if (entity instanceof EntityTrackedVehicle advancedEntity) {
return new DrawningObjectExcavator(new DrawningTrackedVehicle(advancedEntity, roller));
}
return new DrawningObjectExcavator(new DrawningTracktor(entity, roller));
}
}