2022-11-21 21:34:45 +04:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
public class EntityWithRollers<T extends EntityTracktor, U extends IDrawningRollers> {
|
|
|
|
static Random rnd = new Random();
|
|
|
|
private Object[] entities;
|
|
|
|
public int entitiesCount = 0;
|
|
|
|
private Object[] rollers;
|
|
|
|
public int rollersCount = 0;
|
|
|
|
|
|
|
|
public EntityWithRollers(int count) {
|
|
|
|
//new T[count] не работает "Type parameter 'T' cannot be instantiated directly"
|
|
|
|
entities = new Object[count];
|
|
|
|
//new U[count] не работает "Type parameter 'U' cannot be instantiated directly"
|
|
|
|
rollers = new Object[count];
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean add(T entity) {
|
|
|
|
if (entitiesCount >= entities.length) {
|
2022-11-22 16:01:36 +04:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-21 21:34:45 +04:00
|
|
|
entities[entitiesCount++] = entity;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean add(U roller) {
|
|
|
|
if (rollersCount >= rollers.length) {
|
2022-11-22 16:01:36 +04:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-21 21:34:45 +04:00
|
|
|
rollers[rollersCount++] = roller;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-22 16:01:36 +04:00
|
|
|
public IDrawningObject constructTracktor() {
|
2022-11-21 21:34:45 +04:00
|
|
|
if (entitiesCount == 0 || rollersCount == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
EntityTracktor entity = (EntityTracktor) entities[rnd.nextInt(0, entitiesCount)];
|
|
|
|
IDrawningRollers roller = (IDrawningRollers) rollers[rnd.nextInt(0, rollersCount)];
|
|
|
|
|
|
|
|
if (entity instanceof EntityTrackedVehicle advancedEntity) {
|
|
|
|
return new DrawningObjectExcavator(new DrawningTrackedVehicle(advancedEntity, roller));
|
|
|
|
}
|
|
|
|
return new DrawningObjectExcavator(new DrawningTracktor(entity, roller));
|
|
|
|
}
|
|
|
|
}
|