-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStGameLogic.cpp
More file actions
64 lines (56 loc) · 2.63 KB
/
StGameLogic.cpp
File metadata and controls
64 lines (56 loc) · 2.63 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
60
61
62
63
64
#include "StGameLogic.h"
#include <vector>
void StGameLogic::Step(std::unique_ptr<stSharedRes> & gameUnits) {
for(int i = 0; i < gameUnits->coin.size(); i++)
gameUnits->coin[i].sprite.setPosition(gameUnits->coinBody[i]->GetPosition().x, gameUnits->coinBody[i]->GetPosition().y);
for(int i = 0; i < gameUnits->enemy.size(); i++)
gameUnits->enemy[i].sprite.setPosition(gameUnits->enemyBody[i]->GetPosition().x, gameUnits->enemyBody[i]->GetPosition().y);
gameUnits->player.sprite.setPosition(gameUnits->playerBody->GetPosition().x, gameUnits->playerBody->GetPosition().y);
}
void StGameLogic::HandleCollisions(std::unique_ptr<stSharedRes> & res) {
std::vector<int> coinBodyToRemove;
std::vector<int> enemyBodyToRemove;
for(b2ContactEdge* ce = res->playerBody->GetContactList(); ce; ce = ce->next)
{
b2Contact* c = ce->contact;
for(int i = 0; i < res->coinBody.size(); i++)
if(c->GetFixtureA() == res->coinBody[i]->GetFixtureList())
{
coinBodyToRemove.push_back(i);
}
for(int i = 0; i < res->enemyBody.size(); i++)
if(c->GetFixtureA() == res->enemyBody[i]->GetFixtureList())
{
if(res->playerBody->GetPosition().y < res->enemyBody[i]->GetPosition().y)
{
res->playerBody->SetLinearVelocity(b2Vec2(0.0f, -10.0f));
enemyBodyToRemove.push_back(i);
}
else
{
int tmp = (res->playerBody->GetPosition().x < res->enemyBody[i]->GetPosition().x) ? -1 : 1;
res->playerBody->SetLinearVelocity(b2Vec2(10.0f * tmp, 0.0f));
}
}
}
for (auto cbIndex = coinBodyToRemove.begin(); cbIndex != coinBodyToRemove.end(); cbIndex++)
{
res->coinBody[*cbIndex]->DestroyFixture(res->coinBody[*cbIndex]->GetFixtureList());
res->coin.erase(res->coin.begin() + *cbIndex);
res->coinBody.erase(res->coinBody.begin() + *cbIndex);
}
coinBodyToRemove.clear();
for (auto ebIndex = enemyBodyToRemove.begin(); ebIndex != enemyBodyToRemove.end(); ebIndex++)
{
res->enemyBody[*ebIndex]->DestroyFixture(res->enemyBody[*ebIndex]->GetFixtureList());
res->enemy.erase(res->enemy.begin() + *ebIndex);
res->enemyBody.erase(res->enemyBody.begin() + *ebIndex);
}
enemyBodyToRemove.clear();
for(int i = 0; i < res->enemyBody.size(); i++)
if(res->enemyBody[i]->GetLinearVelocity() == b2Vec2_zero)
{
int tmp = (rand() % 2 == 1) ? 1 : -1;
res->enemyBody[i]->SetLinearVelocity(b2Vec2(5.0f * tmp, 0.0f));
}
}