-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathField.php
More file actions
110 lines (98 loc) · 2.02 KB
/
Field.php
File metadata and controls
110 lines (98 loc) · 2.02 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace DesignPatterns\Behavioral\Command;
use DesignPatterns\Behavioral\Command\Command\BottomCommand;
use DesignPatterns\Behavioral\Command\Command\LeftCommand;
use DesignPatterns\Behavioral\Command\Command\RightCommand;
use DesignPatterns\Behavioral\Command\Command\TopCommand;
/**
* Field holds the current state of a game.
* When player wants to move it must call one of these operations:
* - toLeft
* - toRight
* - toTop
* - toBottom
* Note that the `Receiver` is not coupled with the `Field` and can not call its methods directly.
*
* Corresponds to `Receiver` in the Command pattern.
*
* @author Vlad Riabchenko <contact@vria.eu>
*/
class Field
{
/**
* @var Point
*/
private $player;
/**
* @var Point
*/
private $goal;
/**
* Constructor
*
* @param Point $player
* @param Point $goal
*/
public function __construct(Point $player, Point $goal)
{
$this->player = $player;
$this->goal = $goal;
}
/**
* @return Point
*/
public function getGoal()
{
return $this->goal;
}
/**
* @return Point
*/
public function getPlayer()
{
return $this->player;
}
/**
* @return bool
*/
public function checkGoal()
{
return $this->player->equalTo($this->goal);
}
/**
* Move left
* @see LeftCommand::move()
* @see RightCommand::moveBack()
*/
public function toLeft()
{
$this->player->x--;
}
/**
* Move right
* @see RightCommand::move()
* @see LeftCommand::moveBack()
*/
public function toRight()
{
$this->player->x++;
}
/**
* Move top
* @see TopCommand::move()
* @see BottomCommand::moveBack()
*/
public function toTop()
{
$this->player->y--;
}
/**
* Move bottom
* @see TopCommand::move()
* @see BottomCommand::moveBack()
*/
public function toBottom()
{
$this->player->y++;
}
}