-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
52 lines (48 loc) · 1.04 KB
/
player.cpp
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
#include "field.hpp"
#include "player.hpp"
void Player::set(int num){
playernum_ = num;
if (num == 1){
movement_ = UP;
direction_ = UP;
x_ = Field::WIDTH*3/4;
y_ = Field::HEIGHT*3/4;
} else {
movement_ = DOWN;
direction_ = DOWN;
x_ = Field::WIDTH*1/4;
y_ = Field::HEIGHT*1/4;
}
}
bool Player::tick(Field &f){
movement_ = direction_;
switch (movement_){
case LEFT:
x_ -= 1;
break;
case RIGHT:
x_ += 1;
break;
case UP:
y_ -= 1;
break;
case DOWN:
y_ += 1;
break;
}
if (f.block(x_, y_) != Field::Type::EMPTY){
return false;
}
if (x_ >= Field::WIDTH || y_ >= Field::HEIGHT || x_ < 0 || y_ < 0) {
return false;
}
f.setBlock(playernum_ == 1 ? Field::Type::PLAYER1 : Field::Type::PLAYER2, x_, y_);
return true;
}
void Player::keyEvent(Direction d){
if ((d == UP && direction_ == DOWN) || (d == LEFT && direction_ == RIGHT) ||
(d == DOWN && direction_ == UP) || (d == RIGHT && direction_ == LEFT)) {
return;
}
direction_ = d;
}