This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
56 lines (48 loc) · 1.51 KB
/
object.cpp
File metadata and controls
56 lines (48 loc) · 1.51 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
#include "object.hpp"
#include <random>
#include <algorithm>
#include "sprite_factory.hpp"
object::object(sf::RenderWindow & window, sf::Vector2f position , sf::Vector2f size, object_states object_state , std::string spritename):
drawable(window, position, size),
spritename(spritename),
object_state(object_state)
{
texture.loadFromFile(sprite_factory::get_instance().filenames[spritename]);
sprite.setTexture(texture);
}
void object::draw() {
sprite.setPosition(position);
window.draw(sprite);
}
sf::FloatRect object::getbounds() {
return obs.getGlobalBounds();
}
void object::lower(){
position.y += 1;
}
moving_object::moving_object(sf::RenderWindow & window, sf::Vector2f position, sf::Vector2f size, object_states object_state, std::string filename, int change, bool direction, unsigned int speed):
object(window, position, size, object_state, filename), change(change), direction(direction), speed(speed)
{
if(position.x >= window.getSize().x/4 && position.x <= window.getSize().x/4*3) {
moving = true;
}
}
unsigned int moving_object::random_int_between_range(int min, int max) {
return rand() % (max+1);
}
void moving_object::update() {
if(moving == false) {
int tmp = random_int_between_range(0, change);
if(tmp == 0) {
moving = true;
}
}
else if(moving == true) {
if(direction == 1) {
position.x += speed;
}
else if(direction == 0) {
position.x -= speed;
}
}
}