1#include <Entities/Player.hpp>
2#include <Entities/Board.hpp>
3#include <Config/Globals.hpp>
5Player::Player(
int x,
int y):
7 currentDirection(
Player::RIGHT),
8 nextDirection(
Player::RIGHT)
11 this->body.push_back(
Body(x, y));
15 this->body.push_back(
Body(x - 1, y));
16 this->body.push_back(
Body(x - 1, y));
24 return (
int)(this->body.size());
28 return (this->body.front().x);
32 return (this->body.front().y);
34void Player::moveTo(
int x,
int y)
36 this->body.front().x = x;
37 this->body.front().y = y;
39void Player::move(Direction dir)
41 this->nextDirection = dir;
47void Player::update(
Board* board)
51 switch(this->nextDirection)
54 if (this->currentDirection != Player::LEFT)
55 this->currentDirection = this->nextDirection;
59 if (this->currentDirection != Player::RIGHT)
60 this->currentDirection = this->nextDirection;
64 if (this->currentDirection != Player::DOWN)
65 this->currentDirection = this->nextDirection;
69 if (this->currentDirection != Player::UP)
70 this->currentDirection = this->nextDirection;
75 for (
unsigned int i = (this->body.size() - 1); i > 0; i--)
77 this->body[i].x = this->body[i - 1].x;
78 this->body[i].y = this->body[i - 1].y;
82 switch(this->currentDirection)
85 this->body.front().x++;
89 this->body.front().x--;
92 this->body.front().y--;
96 this->body.front().y++;
103 if (board->isBorder(this->body.front().x,
104 this->body.front().y))
106 if (board->
style == Board::TELEPORT)
112 int headx = this->body.front().x;
113 int heady = this->body.front().y;
116 if (this->
bodyHit(headx, heady,
true))
120 if (board->
isWall(headx, heady))
123void Player::draw(Window* win)
126 for (
unsigned int i = 1; i < (this->body.size()); i++)
130 Globals::Theme::player_body);
133 win->printChar(((this->alive) ?
136 this->body.front().x,
137 this->body.front().y,
140 Globals::Theme::player_head_dead));
142bool Player::headHit(
int x,
int y)
144 return ((this->body.front().x == x) &&
145 (this->body.front().y == y));
150 if (isCheckingHead) initial = 3;
152 for (
unsigned int i = initial; i < (this->body.size()); i++)
153 if ((this->body[i].x == x) &&
154 (this->body[i].y == y))
159void Player::increase()
161 int lastx = this->body.back().x;
162 int lasty = this->body.back().y;
164 this->body.push_back(
Body(lastx, lasty));
A level where the snake runs and eats fruits.
Style style
Tells if the player will teleport when reaching the Board's limits or not.
bool isWall(int x, int y)
Tells if there's a wall at #x #y.
void teleport(Player *player)
Makes the Player teleport if it's on a border.
int getX()
Returns the head's x position.
int getY()
Returns the head's y position.
bool bodyHit(int x, int y, bool isCheckingHead=false)
Tells if something at #x and #y collides with any part of the snake.
Container for global settings on the game.