-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPool.cpp
More file actions
59 lines (53 loc) · 1.8 KB
/
Copy pathObjectPool.cpp
File metadata and controls
59 lines (53 loc) · 1.8 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
57
58
59
//
// Created by Louis-Philippe on 4/13/2026.
//
#include "ObjectPool.h"
BulletPool::BulletPool() {
all.reserve(POOL_SIZE);
available.reserve(POOL_SIZE);
// prealloue toutes les balles au debut
for (int i = 0; i < POOL_SIZE; i++) {
Bullet* bullet = new Bullet({0.f, 0.f}, {0.f, -1.f},{255, 255, 255, 255}, false, nullptr);
bullet->bIsDestroyed = true;
all.push_back(bullet);
available.push_back(bullet);
}
}
//pour les deletes a la fin
BulletPool::~BulletPool() {
for (auto* bullet : all) {
delete bullet;
}
}
Bullet* BulletPool::Get(SDL_FPoint spawn, SDL_FPoint dir, SDL_Color color, bool isRGB, SDL_Texture* texture) {
if (available.empty()) {
// alloue une balle supplementaire si on a tout utiliser les 200 balles
SDL_LogWarn(0, "BulletPool: pool épuisé, allocation dynamique");
Bullet* bullet = new Bullet(spawn, dir, color, isRGB, texture);
all.push_back(bullet); // ownership transférée au pool
return bullet;
}
Bullet* bullet = available.back();
available.pop_back();
// Normalise la direction
float length = std::sqrt(dir.x * dir.x + dir.y * dir.y);
if (length > 0.f) {
dir.x /= length;
dir.y /= length;
}
// Réinitialise la balle
bullet->movement.velocity = {700.f * dir.x, 700.f * dir.y};
bullet->render.color = color;
bullet->transform.position = spawn;
bullet->transform.size = {40.f, 40.f};
bullet->bIsRGB = isRGB;
bullet->textureBullet = texture;
bullet->bIsDestroyed = false;
bullet->entityType = EntityType::Bullet;
return bullet;
}
void BulletPool::Return(Bullet* bullet) {
bullet->bIsDestroyed = true;
bullet->textureBullet = nullptr;
available.push_back(bullet);
}