Add усложненный generic

This commit is contained in:
ShabOl 2023-12-10 22:09:29 +04:00
parent 2f897b2079
commit f518b62cdf
3 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package AirBomber;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import AirBomber.Generics.EntitiesGenericCollection;
import AirBomber.MovementStrategy.ObjectEntityRenderer;
import AirBomber.Rendering.BomberRendererBase;
public class FormArrays extends JFrame
{
}

View File

@ -0,0 +1,68 @@
package AirBomber.Generics;
import AirBomber.Entities.*;
import AirBomber.Rendering.*;
import java.util.ArrayList;
import java.util.Random;
public class ArraysGeneric <T extends BomberEntityBase, U extends IEngineRenderer>
{
private ArrayList<T> _entities;
private ArrayList<U> _renderers;
private int _entitiesCount = 0;
private int _renderersCount = 0;
private final int _entitiesMaxCount;
private final int _renderersMaxCount;
private final int _pictureWidth;
private final int _pictureHeight;
public ArraysGeneric(int EntitiesMaxCount, int RenderersMaxCount, int Width, int Height)
{
_entitiesMaxCount = EntitiesMaxCount;
_renderersMaxCount = RenderersMaxCount;
_entities = new ArrayList<T>(EntitiesMaxCount);
_renderers = new ArrayList<U>(RenderersMaxCount);
_pictureWidth = Width;
_pictureHeight = Height;
}
/** Полиморфные методы */
public boolean Add(T Entity)
{
if (Entity == null || _entitiesCount >= _entitiesMaxCount)
return false;
_entities.add(_entitiesCount++, Entity);
return true;
}
public boolean Add(U Renderer)
{
if (Renderer == null || _renderersCount >= _renderersMaxCount)
return false;
_renderers.add(_renderersCount++, Renderer);
return true;
}
public BomberRendererBase CreateObject()
{
if (_entitiesCount == 0 || _renderersCount == 0)
return null;
Random Random = new Random();
int EntityIndex = Random.nextInt(_entitiesCount);
int RendererIndex = Random.nextInt(_renderersCount);
T Entity = _entities.get(EntityIndex);
U Renderer = _renderers.get(RendererIndex);
return new BomberRendererBase(Entity, Renderer, _pictureWidth, _pictureHeight);
}
}

View File

@ -99,6 +99,21 @@ public class BomberRendererBase
EngineRenderer.SetAmount(Random.nextInt(1, 7));
}
public BomberRendererBase(BomberEntityBase Entity, IEngineRenderer EngineRenderer, int Width, int Height)
{
if (Width < _bomberWidth || Height < _bomberHeight)
return;
_startPosX = 0;
_startPosY = 0;
_pictureWidth = Width;
_pictureHeight = Height;
this.EntityBomber = Entity;
this.EngineRenderer = EngineRenderer;
}
public BomberEntityBase GetEntityBomber() { return EntityBomber; }
public void SetPosition(int x, int y)