using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace App
{
public class World
{
public List<IActor> Actors { get; } = new List<IActor>();
private readonly int fieldH;
private readonly int fieldW;
private readonly Point _drawingPoint;
public World(int h, int w, Point drawingPoint)
{
fieldH = h;
fieldW = w;
_drawingPoint = drawingPoint;
}
public void DrawField()
{
char[] c = new char[fieldW];
Array.Fill(c, '.');
for (int i = 0; i < fieldH; i++)
{
Console.SetCursorPosition(_drawingPoint.X, _drawingPoint.Y + i);
Console.WriteLine(c);
}
foreach (var actor in Actors)
{
DrawActor(actor);
}
Console.ResetColor();
Console.SetCursorPosition(0, _drawingPoint.Y + fieldH + 1);
}
public void Tick()
{
foreach (var actor in Actors)
{
actor.TickAction();
}
}
private void DrawActor(IActor actor)//Чтобы наш эктор в рамках поля рисовался
{
Console.SetCursorPosition((actor.PositionInsideField.X + _drawingPoint.X), (actor.PositionInsideField.Y + _drawingPoint.Y));
Console.ForegroundColor = actor.Color;
Console.Write(actor.Icon);
}
}
public struct Point
{
public int X, Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
}
public interface IActor
{
ConsoleColor Color { get; }
char Icon { get; }
Point PositionInsideField { get; }
void TickAction();
}
public class ActorA : IActor
{
public ConsoleColor Color => ConsoleColor.Red;
public char Icon => '@';
private Point pos = new Point(2, 2);
public Point PositionInsideField => pos;
private bool moveRight = false;
public void TickAction()
{
if (moveRight)
pos.X++;
else
pos.X--;
if (pos.X > 10 || pos.X < 1)
moveRight = !moveRight;
}
}
public class ActorB : IActor
{
public ConsoleColor Color => ConsoleColor.Blue;
public char Icon => '*';
private Point pos = new Point(9, 9);
public Point PositionInsideField => pos;
private bool moveUp = false;
public void TickAction()
{
if (moveUp)
pos.Y++;
else
pos.Y--;
if (pos.Y > 10 || pos.Y < 1)
moveUp = !moveUp;
}
}
class Program
{
public static int ReadInt => int.Parse(Console.ReadLine());
public static async Task Main()
{
World f = new World(20, 20, new Point(5, 5));
f.Actors.Add(new ActorA());
f.Actors.Add(new ActorB());
while (true)
{
f.Tick();
f.DrawField();
await Task.Delay(50);
}
}
}
}