Карты + расположение обьекта

This commit is contained in:
Данила 2022-12-04 14:27:23 +04:00
parent 729f5b1460
commit 70a62e0085
3 changed files with 126 additions and 86 deletions

View File

@ -46,14 +46,10 @@ namespace Locomative
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
for(int i =0; i < _setLoco.Count; i++) foreach (var loco in _setLoco.GetLoco())
{
var loco = _setLoco.Get(i);
if(loco != null)
{ {
return _map.CreateMap(_pictureWidth, _pictureHeight, loco); return _map.CreateMap(_pictureWidth, _pictureHeight, loco);
} }
}
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
@ -69,11 +65,11 @@ namespace Locomative
int j = _setLoco.Count - 1; int j = _setLoco.Count - 1;
for(int i = 0; i < _setLoco.Count; i++) for(int i = 0; i < _setLoco.Count; i++)
{ {
if(_setLoco.Get(i) == null) if (_setLoco[j] == null)
{ {
for(; j>1; j--) for(; j>1; j--)
{ {
var car = _setLoco.Get(j); var car = _setLoco[j];
if (car != null) if (car != null)
{ {
_setLoco.Insert(car, i); _setLoco.Insert(car, i);
@ -107,40 +103,31 @@ namespace Locomative
int CountWidth = _pictureWidth / _placeSizeWidth; int CountWidth = _pictureWidth / _placeSizeWidth;
int x = 10; int x = 10;
int y = _pictureHeight - _placeSizeHeight + 5; int y = _pictureHeight - _placeSizeHeight;
int k = 0;
for (int k = 0; k < _setLoco.Count; k++) foreach (var plain in _setLoco.GetLoco())
{
if (_setLoco.Get(k) != null)
{
if ((k - 1) % CountWidth != 0 || k == 0)
{
_setLoco.Get(k)?.SetObject(x, y, _pictureWidth, _pictureHeight);
_setLoco.Get(k)?.DrawningObject(g);
x += _placeSizeWidth;
}
else
{ {
_setLoco.Get(k)?.SetObject(x, y, _pictureWidth, _pictureHeight);
_setLoco.Get(k)?.DrawningObject(g);
x = 10;
y -= _placeSizeHeight;
}
}
if (_setLoco.Get(k) == null)
{
if ((k + 1) % CountWidth != 0 || k == 0) if ((k + 1) % CountWidth != 0 || k == 0)
{
plain?.SetObject(x, y, _pictureWidth, _pictureHeight);
plain?.DrawningObject(g);
x += _placeSizeWidth; x += _placeSizeWidth;
}
else else
{ {
plain?.SetObject(x, y, _pictureWidth, _pictureHeight);
plain?.DrawningObject(g);
x = 10; x = 10;
y -= _placeSizeHeight; y -= _placeSizeHeight;
} }
k++;
} }
} }
} }
} }
}

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetLocoGeneric<DrawningObjectLoco,AbstractMap>> _mapStorages;
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string,MapWithSetLocoGeneric<DrawningObjectLoco, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(string name, AbstractMap map)
{
if (_mapStorages.ContainsKey(name))
return;
Keys.Add(name);
_mapStorages.Add(name, new MapWithSetLocoGeneric<DrawningObjectLoco, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
public void DelMap(string name)
{
if (!_mapStorages.ContainsKey(name))
return;
Keys.Remove(name);
_mapStorages.Remove(name);
}
public MapWithSetLocoGeneric<DrawningObjectLoco, AbstractMap> this[string ind]
{
get
{
if (!_mapStorages.ContainsKey(ind))
return null;
else
return _mapStorages[ind];
}
}
}
}

View File

@ -9,73 +9,75 @@ namespace Locomative
internal class SetLocoGeneric<T> internal class SetLocoGeneric<T>
where T : class where T : class
{ {
private readonly T[] _places; private readonly List<T> _places;
public int Count => _places.Length; public int Count => _places.Count;
private readonly int _maxCount;
public SetLocoGeneric(int count) public SetLocoGeneric(int count)
{ {
_places = new T[count]; _maxCount = count;
_places = new List<T>();
} }
public int Insert(T locomative) public int Insert(T plain)
{ {
int i;
for (i = 0; i < Count; i++)
{
if (_places[i] == null)
{
for (int j = i; j >= 1; j--)
{
_places[j] = _places[j - 1];
}
_places[0] = locomative;
return i;
}
}
return -1; Insert(plain, 0);
return _places.Count;
} }
public int Insert(T locomative, int position) public int Insert(T loco, int position)
{ {
if (position > Count || position < 0) if (position < 0 || _places.Count < position || position > _maxCount || _places.Count == _maxCount)
return -1; return -1;
if (_places[position] == null) else if (loco == null)
{ return -1;
_places[position] = locomative; _places.Insert(position, loco);
return position; return position;
}
else
{
for (int i = position; i < Count; i++)
{
if (_places[i] == null)
{
for (int j = i; j >= position + 1; j--)
{
_places[j] = _places[j - 1];
}
_places[position] = locomative;
return position;
}
}
}
return -1;
} }
public T Remove(int position) public T Remove(int position)
{ {
T mid; T mid;
if (_places[position] != null && position < _places.Length) if (position < 0 || _places.Count < position || position > _maxCount)
return null;
else if (_places[position] == null)
return null;
else
{ {
mid = _places[position]; mid = _places[position];
_places[position] = null; _places.RemoveAt(position);
}
return mid; return mid;
} }
else public T this[int position]
return null;
}
public T Get(int position)
{ {
get
{
if (position < 0 || _places.Count < position || position > _maxCount)
return null;
else if (_places[position] == null)
return null;
else
return _places[position]; return _places[position];
} }
set
{
if (position < 0 || _places.Count < position || position > _maxCount)
return;
else if (_places.Count == _maxCount)
return;
else
_places[position] = value;
}
}
public IEnumerable<T> GetLoco()
{
foreach (var loco in _places)
{
if (loco != null)
yield return loco;
else
yield break;
}
}
} }
} }