-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
85 lines (73 loc) · 1.48 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
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
#include "player.h"
#include <iostream>
using namespace std;
player::player(game_board* board)
{
x = 1;
y = 1;
startX = 1;
startY = 1;
character = '@';
this->board = board;
}
player::~player()
{
}
void player::move( Qt::Key direction, game_board& board )
{
int newX = 0, newY = 0;
switch (direction) {
case Qt::Key_A:
if (board.get_char(x - 1, y ) != '#')
--newX;
break;
case Qt::Key_D:
if (board.get_char(x + 1, y ) != '#')
++newX;
break;
case Qt::Key_W:
if (board.get_char(x , y - 1) != '#')
--newY;
break;
case Qt::Key_S:
if (board.get_char(x , y + 1) != '#')
++newY;
break;
default:
break;
}
board.set_char(x, y, '.');
QChar newChar = board.get_char(x + newX, y + newY);
// If it's not one of these two, then notify the board
// Board will take care of what happens when you collide with
// that object
if(newChar != '.' && newChar != '@')
{
board.player_collided(newChar, board);
return;
}
board.set_char(x + newX, y + newY, character);
x += newX;
y += newY;
}
void player::set_position(int x, int y)
{
this->x = x;
this->y = y;
}
void player::set_start_position(int x, int y)
{
this->startX = x;
this->startY = y;
}
void player::goto_start()
{
board->set_char(x, y, '.');
x = startX;
y = startY;
board->set_char(startX, startY, character);
}
bool player::update(game_board& board, int timestep)
{
return true;
}