aboutsummaryrefslogtreecommitdiff
path: root/game/coins/CoinSystem.cpp
blob: 4474195309151fcaf3add943f674905b1d9a0bfb (plain)
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
#include "CoinSystem.h"
#include "CoinSubScene.h"
#include "api/CircleCollider.h"
#include "api/Metadata.h"
#include "api/Script.h"
#include "api/Sprite.h"
#include "api/Transform.h"
#include "types.h"

using namespace crepe;
using namespace std;

std::vector<CoinData> CoinSystem::coin_locations;

void CoinSystem::create_coins(crepe::Scene & scn) {
	CoinSubScene coin;
	while(coin.create(scn) < this->MAXIMUM_AMOUNT);
}

void CoinSystem::add_location(const crepe::vec2& location){
	coin_locations.push_back(CoinData(location));
}

void CoinSystem::remove_location(const std::string& name){
	auto it = std::find_if(coin_locations.begin(), coin_locations.end(),
				[&name](const CoinData& data) {
					return data.name == name;
				});

	// If a match is found, erase
	if (it != coin_locations.end()) {
		coin_locations.erase(it);
	}
}


void CoinSystem::update(float position, RefVector<Sprite> coin_sprites, RefVector<CircleCollider> coin_colliders, RefVector<Metadata> coin_metadatas) {
	for (auto& coin : coin_locations) {
		if (!coin.active) {
			if (coin.start_location.x > SPAWN_DISTANCE + position && coin.start_location.x < SPAWN_AREA + SPAWN_DISTANCE + position) {
					
				// Local variables to hold the matching coin data
				Metadata* coin_metadata = nullptr;
				CircleCollider* coin_collider = nullptr;
				
				// Iterate over the sprites to find the matching one
				for (Sprite& coin_sprite : coin_sprites) {
					if (!coin_sprite.active) {
						// Find the matching Metadata
						for (Metadata& coin_metadata_unkown : coin_metadatas) {
							if (coin_metadata_unkown.game_object_id == coin_sprite.game_object_id) {
								coin_metadata = &coin_metadata_unkown;
								// No need to continue searching
								break; 
							}
						}
							
						// Find the matching CircleCollider
						for (CircleCollider& coin_collider_unkown : coin_colliders) {
							if (coin_collider_unkown.game_object_id == coin_sprite.game_object_id) {
								coin_collider = &coin_collider_unkown;
								// No need to continue searching
								break;
							}
						}

						// If we found both the metadata and collider, activate the coin
						if (coin_metadata && coin_collider) {
							coin.name = coin_metadata->name;
							coin.active = true;
							coin_sprite.active = true;
							coin_collider->active = true;
							break;
						}
					}
				}
			}
		}
	}
}