-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionProcessor.cs
63 lines (56 loc) · 1.64 KB
/
ActionProcessor.cs
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
61
62
63
public class ActionProcessor
{
private readonly World World;
private readonly Map Map;
public ActionProcessor(World world, Map map)
{
Map = map;
World = world;
}
public void ProcessAction(Entity entity, Action action)
{
switch (action)
{
case Action.MoveLeft:
case Action.MoveRight:
case Action.MoveUp:
case Action.MoveDown:
{
Move(entity, action);
break;
}
case Action.EatGlob:
{
Glob glob;
if (Map.TryGetNeighbor(entity, out glob))
{
Map.Remove(glob);
World.Remove(glob);
}
break;
}
case Action.None:
default:
return;
}
}
private void Move(Entity entity, Action action)
{
var deltaX = 0;
var deltaY = 0;
if (action == Action.MoveDown && entity.Row < (Map.Size-1)) deltaY = 1;
if (action == Action.MoveUp && entity.Row > 0) deltaY = -1;
if (action == Action.MoveRight && entity.Column < (Map.Size-1)) deltaX = 1;
if (action == Action.MoveLeft && entity.Column > 0) deltaX = -1;
if(Map.IsEmpty(entity.Row + deltaY, entity.Column + deltaX) && (deltaX != 0 || deltaY != 0))
{
Move(entity, deltaX, deltaY);
}
}
private void Move(Entity entity, int deltaX, int deltaY)
{
Map.Remove(entity);
entity.Move(deltaX, deltaY);
Map.Add(entity);
}
}