-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
113 lines (108 loc) · 3 KB
/
Player.cpp
File metadata and controls
113 lines (108 loc) · 3 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
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Player.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hasmith <hasmith@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/26 17:23:42 by lhernand #+# #+# */
/* Updated: 2019/01/27 23:39:07 by hasmith ### ########.fr */
/* */
/* ************************************************************************** */
#include "Game.hpp"
#include "Player.hpp"
# include <ncurses.h>
# include <curses.h>
# include <iostream>
# include <thread>
# include <chrono>
Player::Player(void) :
numberOfMissiles(100)
{
// std::cout << "You have: " << numberOfMissiles << " missiles" << std::endl;
this->missile = new Missile[100];
this->currMissile = 0;
return ;
}
Player::~Player(void)
{
std::cout << "Player Destroyed" << std::endl;
}
Player::Player(Player const & src)
{
*this = src;
}
Player &Player::operator=(Player const & rhs)
{
if (this == &rhs)
return (*this);
this->x = rhs.getX();
this->y = rhs.getY();
return (*this);
}
void Player::moveRight(void)
{
mvaddch(getY(), getX(), ' ');
mvaddch(getY() + 1, getX() + 1, ' ');
mvaddch(getY() + 1, getX() - 1, ' ');
this->setX(x + 2);
}
void Player::moveLeft(void)
{
mvaddch(getY(), getX(), ' ');
mvaddch(getY() + 1, getX() + 1, ' ');
mvaddch(getY() + 1, getX() - 1, ' ');
this->setX(x - 2);
}
void Player::drawPlayer(void)
{
attron(COLOR_PAIR(1));
mvaddch(this->getY(), this->getX(), '^');
mvaddch(this->getY() + 1, getX() + 1, '^');
mvaddch(this->getY() + 1, getX() - 1, '^');
attroff(COLOR_PAIR(1));
box(stdscr, 0, 0);
refresh();
}
int Player::missilesCollisions(int ex, int ey)
{
for (int i = 0; i < this->numberOfMissiles; i++)
{
if (this->missile[i].missileCollision(ex, ey))
return 1;
}
return 0;
}
void Player::setGame(Game *game)
{
this->game = game;
this->setX(this->game->getMapX()/2);
this->setY(this->game->getMapY()-4);
}
void Player::drawMissiles(void)
{
for (int i = 0; i < this->numberOfMissiles; i++){
if (this->missile[i].getN() > 0)
{
this->missile[i].drawMissile(this->game);
}
}
}
void Player::getInput(char c, Game *game)
{
if (c == 27)
exit(0);
if ((c == '4' || c == KEY_LEFT) && (this->getX() > 2))
moveLeft();
else if ((c == '6' || c == KEY_RIGHT) && (this->getX() < game->getMapX() - 4))
moveRight();
else if (c == ' ')
{
this->missile[currMissile].setX(this->getX());
this->missile[currMissile].setY(this->getY());
this->missile[currMissile].setN(1);//set health
currMissile++;
currMissile %= this->numberOfMissiles;
}
drawPlayer();
}