-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemies.cpp
133 lines (120 loc) · 6.72 KB
/
Enemies.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
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
#include "Entity.h"
#include "Enemies.h"
#include "Player.h"
using namespace std;
Goomba::Goomba(const string& texture_file, double x_pos, double y_pos, const double &sprite_width, const double &sprite_height, const int &map_width, const int &map_height, const int &tile_width, const int &tile_height, const double &map_scale) {
alive = true;
x = sprite_width;
y = sprite_height;
m_texture.loadFromFile(texture_file);
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(sprite_width * sprite_height * 4);
m_vertices[0].position = sf::Vector2f(x_pos, y_pos);
m_vertices[1].position = sf::Vector2f(x_pos + sprite_width, y_pos);
m_vertices[2].position = sf::Vector2f(x_pos + sprite_width, y_pos + sprite_height);
m_vertices[3].position = sf::Vector2f(x_pos, y_pos + sprite_height);
m_vertices[0].texCoords = sf::Vector2f(0, 0);
m_vertices[1].texCoords = sf::Vector2f(sprite_width, 0);
m_vertices[2].texCoords = sf::Vector2f(sprite_width, sprite_height);
m_vertices[3].texCoords = sf::Vector2f(0, sprite_height);
right = true;
x_vel = 0.1;
}
void Goomba::update(const double &delta_time, const int &map_rows, const int &map_columns, Player * player, Chork * chork, vector<vector<int>> &level) {
if (alive) {
int y_midpoint = player->m_vertices[0].position.y + (player->sprite_height / 2);
//int x_midpoint = player->m_vertices[0].position.x + (player->sprite_width / 2);
on_ground = (bool)level[(int)(m_vertices[3].position.y / 64)][(int)(m_vertices[3].position.x / 64)];
player_left = player->m_vertices[0].position.x;
player_right = player->m_vertices[2].position.x;
player_up = player->m_vertices[0].position.y;
player_down = player->m_vertices[2].position.y;
enemy_left = m_vertices[0].position.x;
enemy_right = m_vertices[2].position.x;
enemy_up = m_vertices[0].position.y;
enemy_down = m_vertices[2].position.y;
chork_left = chork->m_vertices[0].position.x;
chork_right = chork->m_vertices[2].position.x;
chork_up = chork->m_vertices[0].position.y;
chork_down = chork->m_vertices[2].position.y;
if (chork_left - enemy_right < 3 && chork_right - enemy_left > -3 && player_down - enemy_up > -3 && chork_up - enemy_down < 3) {
if (player->facing_right && (chork_right - enemy_left > 3)) { // && ((player_up > enemy_up && player_up < enemy_down) || (player_down > enemy_up && player_down < enemy_down))) {
alive = false;
m_vertices[0].position = sf::Vector2f(0, 0);
m_vertices[1].position = sf::Vector2f(0, 0);
m_vertices[2].position = sf::Vector2f(0, 0);
m_vertices[3].position = sf::Vector2f(0, 0);
} else if (!player->facing_right && (chork_left - enemy_right < 3)) { // && ((player_up > enemy_up && player_up < enemy_down) || (player_down > enemy_up && player_down < enemy_down))) {
alive = false;
m_vertices[0].position = sf::Vector2f(0, 0);
m_vertices[1].position = sf::Vector2f(0, 0);
m_vertices[2].position = sf::Vector2f(0, 0);
m_vertices[3].position = sf::Vector2f(0, 0);
}
}
//if there is a collision
if (player_left - enemy_right < 3 && player_right - enemy_left > -3 && player_down - enemy_up > -3 && player_up - enemy_down < 3) {
//if the player collides with either of the sides of the enemy
if (((double)player_left - enemy_right > -3 || (double)player_right - enemy_left < 3) && ((player_up > enemy_up && player_up < enemy_down) || (player_down > enemy_up && player_down < enemy_down) || (y_midpoint > enemy_up && y_midpoint < enemy_down))) {
if (player->get_invincible()) {
alive = false;
m_vertices[0].position = sf::Vector2f(0, 0);
m_vertices[1].position = sf::Vector2f(0, 0);
m_vertices[2].position = sf::Vector2f(0, 0);
m_vertices[3].position = sf::Vector2f(0, 0);
} else {
player->m_vertices[0].position = sf::Vector2f(0, 0);
player->m_vertices[1].position = sf::Vector2f(player->sprite_width, 0);
player->m_vertices[2].position = sf::Vector2f(player->sprite_width, player->sprite_height);
player->m_vertices[3].position = sf::Vector2f(0, player->sprite_height);
if (player->get_big()) {
player->set_big(0);
} else {
player->set_alive(false);
}
player->ow.play();
}
}
//if the player hits the top of the enemy
if (((double)player_down - enemy_up > -3 && player_up < enemy_up) && ((player_left > enemy_left && player_left < enemy_right) || (player_right > enemy_left && player_right < enemy_right))) {
player->yahoo_ee.play();
alive = false;
m_vertices[0].position = sf::Vector2f(0, 0);
m_vertices[1].position = sf::Vector2f(0, 0);
m_vertices[2].position = sf::Vector2f(0, 0);
m_vertices[3].position = sf::Vector2f(0, 0);
}
}
if (alive) {
y_vel = 1;
if (!on_ground) {
m_vertices[0].position = sf::Vector2f(m_vertices[0].position.x, m_vertices[0].position.y + (y_vel * delta_time));
m_vertices[1].position = sf::Vector2f(m_vertices[1].position.x, m_vertices[1].position.y + (y_vel * delta_time));
m_vertices[2].position = sf::Vector2f(m_vertices[2].position.x, m_vertices[2].position.y + (y_vel * delta_time));
m_vertices[3].position = sf::Vector2f(m_vertices[3].position.x, m_vertices[3].position.y + (y_vel * delta_time));
}
if (right && on_ground) {
if (!level[(int)(m_vertices[2].position.y / 64) - 1][(int)(m_vertices[2].position.x / 64)] && level[(int)(m_vertices[2].position.y / 64)][(int)(m_vertices[2].position.x / 64)]) {
x_vel = 0.3;
} else {
right = false;
x_vel = 0;
}
}
if (!right && on_ground) {
if (level[(int)(m_vertices[3].position.y / 64)][(int)(m_vertices[3].position.x / 64) - 1] && !level[(int)(m_vertices[3].position.y / 64) - 1][(int)(m_vertices[3].position.x / 64)]) {
x_vel = -.3;
} else {
right = true;
}
}
m_vertices[0].position = sf::Vector2f(m_vertices[0].position.x + (x_vel * delta_time), m_vertices[0].position.y);
m_vertices[1].position = sf::Vector2f(m_vertices[1].position.x + (x_vel * delta_time), m_vertices[1].position.y);
m_vertices[2].position = sf::Vector2f(m_vertices[2].position.x + (x_vel * delta_time), m_vertices[2].position.y);
m_vertices[3].position = sf::Vector2f(m_vertices[3].position.x + (x_vel * delta_time), m_vertices[3].position.y);
}
}
}