-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cpp
94 lines (82 loc) · 3.21 KB
/
Enemy.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
//
// Created by Goodecheeseburgers on 20/04/2020.
// attack-of-the-squares. A simple C++ / SFML game about squares.
// Copyright (C) 2020 - [email protected]
//
// https://github.com/Cheeseborgers/attack-of-the-squares
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#include "Enemy.h"
// Constructor
Enemy::Enemy(sf::RenderTarget &target, int type) {
this->color = {};
this->type = type;
this->velocityY = {};
this->initAttributes();
// Set the enemy position
this->shape.setPosition(
static_cast<float>(rand() % static_cast<int>(target.getSize().x - shape.getSize().x)),
0.F
);
}
// ---------------------------------------------------------------------------------------------------------------------
Enemy::~Enemy() = default;
// Methods -------------------------------------------------------------------------------------------------------------
void Enemy::initAttributes() {
switch (this->type) {
case 0:
this->color = sf::Color::Cyan;
this->size = sf::Vector2f(100.f, 100.f);
this->velocityY = 5.F;
break;
case 1:
this->color = sf::Color::Green;
this->size = sf::Vector2f(80.f, 80.f);
this->velocityY = 5.F;
break;
case 2:
this->color = sf::Color::Red;
this->size = sf::Vector2f(70.f, 70.f);
this->velocityY = 4.7F;
break;
case 3:
this->color = sf::Color::Yellow;
this->size = sf::Vector2f(50.f, 50.f);
this->velocityY = 4.5F;
break;
case 4:
this->color = sf::Color::Magenta;
this->size = sf::Vector2f(30.f, 30.f);
this->velocityY = 4.F;
break;
default:
std::cout << "ERROR::ENEMY::INITATTRIBUTES: No such type\n";
break;
}
// Set initial size
this->shape.setSize(this->size);
// Set initial fill colour
this->shape.setFillColor(this->color);
}
// ---------------------------------------------------------------------------------------------------------------------
void Enemy::update() {
this->posY -= this->velocityY;
this->shape.move(0.F, this->velocityY);
}
// ---------------------------------------------------------------------------------------------------------------------
void Enemy::render(sf::RenderTarget &target) const {
target.draw(this->shape);
}
// --------------------------------------------------------------------------------------------------------------------