forked from zhusim222/Bee-game
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.cpp
56 lines (44 loc) · 1.37 KB
/
player.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
#include "player.h"
#include <splashkit.h>
#include "globals.h"
#include <iostream>
#include <algorithm>
#include <memory>
#include <vector>
Player::Player(float x, float y, float speed) {
this->x = x;
this->y = y;
this->speed = speed;
this->width = bitmap_width(bee)*BEE_SCALE; // Assuming 'bee' is the bitmap for the player
this->height = bitmap_height(bee)*BEE_SCALE;
}
int Player::HP = 3; // Initialize the static HP variable
void Player::move_right() {
x += speed;
}
void Player::move_left() {
x -= speed;
}
void Player::attach(Observer* observer) {
observers.push_back(observer);
}
void Player::detach(Observer* observer) {
auto it = std::remove(observers.begin(), observers.end(), observer);
if (it != observers.end()) {
std::cout << "Detaching observer" << std::endl;
observers.erase(it, observers.end());
}
}
void Player::notify(Observer* observer, bool is_collision) {
observer->CollisionUpdate(is_collision); // Call onCollision on the observer, passing this obstacle
}
void Player::notify_all_observers() {
std::cout << "Notifying all observers..." << std::endl;
for (Observer* observer : observers) {
if (observer == nullptr) {
std::cout << "Observer is null!" << std::endl;
continue; // Skip null observers
}
observer->deceaseSpeed(1);
}
}