-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit.cs
More file actions
60 lines (50 loc) · 1.45 KB
/
Unit.cs
File metadata and controls
60 lines (50 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Maze1
{
public abstract class Unit
{
public int X { get; private set; }
public int Y { get; private set; }
private char _symbol;
private ConsoleRenderer _renderer;
public Unit(int startX, int startY, char symbol, ConsoleRenderer renderer)
{
X = startX;
Y = startY;
_symbol = symbol;
_renderer = renderer;
_renderer.SetPixel(X, Y, _symbol);
}
public virtual bool TryMoveLeft(char[,] map)
{
return TryChangePosition(X + 1, Y, map);
}
public virtual bool TryMoveRight(char[,] map)
{
return TryChangePosition(X - 1, Y, map);
}
public virtual bool TryMoveUp(char[,] map)
{
return TryChangePosition(X, Y - 1, map);
}
public virtual bool TryMoveDown(char[,] map)
{
return TryChangePosition(X, Y + 1, map);
}
protected virtual bool TryChangePosition(int newX, int newY, char[,] map)
{
if (map[newX, newY] == '#')
return false;
_renderer.SetPixel(X, Y, ' ');
X = newX;
Y = newY;
_renderer.SetPixel(X, Y, _symbol);
return true;
}
public abstract void Update();
}
}