/
Robot.cpp
143 lines (113 loc) · 4.23 KB
/
Robot.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* File: Robot.cpp
* Author: lucasvg
*
* Created on November 11, 2013, 3:59 PM
*/
#include <iostream>
#include "Timer.h"
#include "Robot.h"
#include "ShotsOnTheWorld.h"
#include "Timer.h"
Robot::Robot(const int ROBOT_WIDTH, const int ROBOT_HEIGHT, const int ROBOT_START_AMOUNT_OF_SHOTS, int robot_gun_position,
const int ROBOT_SPEED, SDL_Surface *robot_surface, const int SCREEN_PLAYABLE_WIDTH, const int SCREEN_HEIGHT,
int shot_width, int shot_height, int shot_velx, int shot_vely, SDL_Surface *shot_surface)
: ROBOT_GUN_POSITION(robot_gun_position) {
setX((SCREEN_PLAYABLE_WIDTH - ROBOT_WIDTH) / 2);
box.y = SCREEN_HEIGHT - ROBOT_HEIGHT;
box.w = ROBOT_WIDTH;
box.h = ROBOT_HEIGHT;
speed = ROBOT_SPEED;
this->shot_width = shot_width;
this->shot_height = shot_height;
this->shot_velx = shot_velx;
this->shot_vely = shot_vely;
this->shot_surface = shot_surface;
score = 0;
setAmountOfShots(ROBOT_START_AMOUNT_OF_SHOTS);
surface = robot_surface;
};
void Robot::setX(int x) {
box.x = x;
};
void Robot::setAmountOfShots(const int ROBOT_START_AMOUNT_OF_SHOTS) {
amountOfShots = ROBOT_START_AMOUNT_OF_SHOTS;
};
void Robot::move(int x, int SCREEN_PLAYABLE_WIDTH, Piece mainPiece) {
if (x == 0)
return;
// if the robot collides with the borders
if ((box.x + x < 0) or ((box.x + x + box.w) > SCREEN_PLAYABLE_WIDTH))
return;
//if the robot collides with the mainPiece
for (int i = 0; i < mainPiece.size(); i++)
if (x > 0) {
if (isCollidingRight(mainPiece[i])) {
if (mainPiece.moveHorizontal(x, SCREEN_PLAYABLE_WIDTH))
return move(-(x-1), SCREEN_PLAYABLE_WIDTH, mainPiece);
}
} else {
if (isCollidingLeft(mainPiece[i])) {
if (mainPiece.moveHorizontal(x, SCREEN_PLAYABLE_WIDTH))
return move((x-1), SCREEN_PLAYABLE_WIDTH, mainPiece);
}
}
box.x += x;
};
bool Robot::isCollidingLeft(Square square) {
// if the square is on hight possible to be collided with the robot
if (box.y <= square.gety())
// is trying to move to left
if (((box.x) == (square.getx() + square.getw())))
return true;
return false;
}
bool Robot::isCollidingRight(Square square) {
// if the square is on hight possible to be collided with the robot
if (box.y <= square.gety())
// is trying to move to left
if ((box.x + box.w) == (square.getx()))
return true;
return false;
}
bool Robot::isColliding(Piece piece) {
for (int i = 0; i < piece.size(); i++)
if (isColliding(piece[i]))
return true;
return false;
}
bool Robot::isColliding(Square square) {
if ((square.getx() >= box.x) and square.getx() <= box.x + box.w)
if (square.gety() + square.geth() >= box.y)
return true;
if (square.gety() + square.geth() >= box.y)
if (((square.getx() > box.x) and (square.getx() < box.x + box.w)) or
((square.getx() + square.getw() > box.x) and (square.getx() + square.getw() < box.x + box.w)))
return true;
return false;
}
void Robot::show(SDL_Surface *screen) {
apply_surface(box.x, box.y, surface, screen);
};
void Robot::handleEvents(SDL_Event event, int SCREEN_PLAYABLE_WIDTH, Piece mainPiece, ShotsOnTheWorld & shotsOnTheWorld,
Timer & delta_robot, const int ROBOT_SHOOT_DELAY, const int ROBOT_MOVE_DELAY) {
Uint8 *keystates = SDL_GetKeyState(NULL);
if (delta_robot.get_ticks() > ROBOT_MOVE_DELAY) {
if (keystates[ SDLK_LEFT ])
move(speed * (-1), SCREEN_PLAYABLE_WIDTH, mainPiece);
if (keystates[ SDLK_RIGHT ])
move(speed, SCREEN_PLAYABLE_WIDTH, mainPiece);
}
if (delta_robot.get_ticks() > ROBOT_SHOOT_DELAY) {
if (keystates[ SDLK_SPACE ])
shotsOnTheWorld.newShot(Shot(box.x + ROBOT_GUN_POSITION, box.y, shot_width, shot_height, shot_velx, shot_vely, shot_surface, this));
}
if ((delta_robot.get_ticks() > ROBOT_MOVE_DELAY) and (delta_robot.get_ticks() > ROBOT_SHOOT_DELAY))
delta_robot.start();
}
int Robot::getScore() {
return score;
}
void Robot::setScore(int score) {
this->score = score;
}