PIbd-23_Polevoy_S.V._SelfPr.../EntityWithRollers.java

44 lines
1.4 KiB
Java
Raw Normal View History

2022-10-25 16:53:45 +04:00
import java.util.Random;
public class EntityWithRollers<T extends EntityArtillery, U extends IDrawingRollers> {
static Random rnd = new Random();
private final Object[] entities;
public int entitiesCount = 0;
private final Object[] rollers;
public int rollersCount = 0;
public EntityWithRollers(int count) {
entities = new Object[count];
rollers = new Object[count];
}
public boolean add(EntityArtillery entity) {
if (entitiesCount >= entities.length) {
return false;
}
entities[entitiesCount++] = entity;
return true;
}
public boolean add(IDrawingRollers roller) {
if (rollersCount >= rollers.length) {
return false;
}
rollers[rollersCount++] = roller;
return true;
}
public IDrawingObject constructArtillery() {
if (entitiesCount == 0 || rollersCount == 0) {
return null;
}
EntityArtillery entity = (EntityArtillery) entities[rnd.nextInt(0, entitiesCount)];
IDrawingRollers roller = (IDrawingRollers) rollers[rnd.nextInt(0, rollersCount)];
if (entity instanceof EntityAdvancedArtillery advancedEntity) {
return new DrawingObjectArtillery(new DrawingAdvancedArtillery(advancedEntity, roller));
}
return new DrawingObjectArtillery(new DrawingArtillery(entity, roller));
}
}