Skip to content

Commit f11a4d0

Browse files
committed
rotate triggers + a bit of optimization
1 parent dc08bdd commit f11a4d0

File tree

13 files changed

+110
-193
lines changed

13 files changed

+110
-193
lines changed

GD/EffectGameObject.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,15 @@ void EffectGameObject::moveAction()
143143

144144
void EffectGameObject::toggleAction()
145145
{
146-
for (auto pair : gameLayer->groups[targetGroupId]->objects)
146+
for (GameObject* obj : gameLayer->groups[targetGroupId]->objects)
147147
{
148-
for (auto pair2 : pair.second)
149-
{
150-
GameObject* obj = pair2.second;
151-
obj->enabled = activateGroup;
148+
obj->enabled = activateGroup;
152149

153-
if (!activateGroup)
154-
{
155-
obj->removeFromBatcher();
156-
for (std::shared_ptr<Sprite> sprite : obj->childSprites)
157-
sprite->removeFromBatcher();
158-
}
150+
if (!activateGroup && obj->currentBatcher)
151+
{
152+
obj->removeFromBatcher();
153+
for (std::shared_ptr<Sprite> sprite : obj->childSprites)
154+
sprite->removeFromBatcher();
159155
}
160156
}
161157
}
@@ -194,18 +190,14 @@ void EffectGameObject::rotateAction()
194190
void EffectGameObject::stopAction()
195191
{
196192
std::shared_ptr<Group> group = gameLayer->groups[targetGroupId];
197-
for (auto pair : group->objects)
193+
for (GameObject* obj : group->objects)
198194
{
199-
for (auto pair2 : pair.second)
195+
if (obj->isTrigger)
200196
{
201-
GameObject* obj = pair2.second;
202-
if (obj->isTrigger)
203-
{
204-
EffectGameObject* trigger = dynamic_cast<EffectGameObject*>(obj);
197+
EffectGameObject* trigger = dynamic_cast<EffectGameObject*>(obj);
205198

206-
if(trigger->triggerAction)
207-
trigger->triggerAction->stop();
208-
}
199+
if (trigger->triggerAction)
200+
trigger->triggerAction->stop();
209201
}
210202
}
211203
}

GD/GameLayer.cpp

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,14 @@ void GameLayer::updateLevelObjects()
329329
{
330330
for (int i = prevSection; i < nextSection + 1; i++)
331331
{
332-
if (!groups[group]->objects.contains(i))
333-
continue;
334332
std::vector<GameObject*> updateSection;
335-
auto map = &groups[group]->objects[i];
333+
auto map = &groups[group]->objectsInSections[i];
336334
updateSection.reserve(map->size());
337335
for (auto& pair : *map)
338336
{
339337
GameObject* obj = pair.second;
340-
if (obj)
341-
{
342-
obj->updatePosition();
343-
updateSection.push_back(obj);
344-
}
338+
obj->updatePosition(true);
339+
updateSection.push_back(obj);
345340
}
346341
for (GameObject* obj : updateSection)
347342
obj->tryUpdateSection();
@@ -623,7 +618,7 @@ void GameLayer::setupObjects(std::string_view levelString)
623618

624619
void GameLayer::updateTriggers()
625620
{
626-
auto camPos = camera.getCenter().x / Application::zoomModifier;
621+
float camPos = (camera.getCenter().x + 100.f) / Application::zoomModifier;
627622

628623
int curSection = Common::sectionForPos(camPos);
629624

@@ -676,21 +671,6 @@ void GameLayer::updateTriggers()
676671

677672
void GameLayer::updateVisibility()
678673
{
679-
/* for(int i : dirtySections)
680-
{
681-
auto& section = sectionObjects[i];
682-
for (GameObject* obj : section)
683-
{
684-
obj->removeFromBatcher();
685-
for (std::shared_ptr<Sprite> spr : obj->childSprites)
686-
spr->removeFromBatcher();
687-
}
688-
std::sort(sectionObjects[i].begin(), sectionObjects[i].end(), [](const auto& x, const auto& y) { return
689-
x->zOrder < y->zOrder; });
690-
}
691-
692-
dirtySections.clear(); */
693-
694674
auto& winSize = camera.getSize();
695675
auto camPos = camera.getCenter().x / Application::zoomModifier;
696676

@@ -736,16 +716,15 @@ void GameLayer::updateVisibility()
736716
if (!obj || obj->currentBatcher != nullptr || !obj->enabled)
737717
continue;
738718

739-
obj->updatePosition();
740-
741719
if (obj->isTrigger)
742720
continue;
743721

744-
obj->updateOpacity();
745-
746722
for (std::shared_ptr<Sprite>& sprite : obj->childSprites)
747723
layerObject(sprite.get());
748724
layerObject(obj);
725+
726+
obj->updatePosition(false);
727+
obj->updateOpacity();
749728
}
750729
}
751730
}

GD/GameLayer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ class GameLayer : public Layer
8080
std::unordered_set<int> dirtyChannels;
8181
std::unordered_set<int> dirtyGroups;
8282

83-
std::unordered_set<int> dirtySections;
84-
8583
std::array<std::shared_ptr<ColorChannel>, 1013> colorChannels;
8684
std::array<std::shared_ptr<Group>, 1013> groups;
8785

GD/GameObject.cpp

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,14 @@ std::shared_ptr<GameObject> GameObject::createFromString(std::string_view str)
260260

261261
ptr->groups.push_back(group);
262262

263-
if (!GameLayer::instance->groups[group]->objects.contains(ptr->section))
263+
GameLayer::instance->groups[group]->objects.push_back(ptr.get());
264+
265+
if (!GameLayer::instance->groups[group]->objectsInSections.contains(ptr->section))
264266
{
265267
std::unordered_map<int, GameObject*> map;
266-
GameLayer::instance->groups[group]->objects.insert({ptr->section, map});
268+
GameLayer::instance->groups[group]->objectsInSections.insert({ptr->section, map});
267269
}
268-
GameLayer::instance->groups[group]->objects[ptr->section].insert(
270+
GameLayer::instance->groups[group]->objectsInSections[ptr->section].insert(
269271
{static_cast<int>(ptr->uniqueID), ptr.get()});
270272
}
271273
break;
@@ -309,6 +311,14 @@ std::shared_ptr<GameObject> GameObject::createFromString(std::string_view str)
309311
}
310312
}
311313

314+
if (ptr->isTrigger && effectPtr->spawnTriggered && ptr->groups.size() > 0)
315+
{
316+
for (int i : ptr->groups)
317+
{
318+
GameLayer::instance->groups[i]->spawnTriggered.push_back(effectPtr.get());
319+
}
320+
}
321+
312322
if (objectEntry.contains("color_type"))
313323
{
314324
if (objectEntry["color_type"] == "Base" || objectEntry.contains("swap_base_detail") &&
@@ -339,7 +349,6 @@ std::shared_ptr<GameObject> GameObject::createFromString(std::string_view str)
339349
ptr->setupCustomObjects(objectEntry, ptr);
340350

341351
ptr->startPosition = ptr->getPosition();
342-
ptr->startRotation = ptr->getRotation();
343352

344353
return ptr;
345354
}
@@ -353,56 +362,21 @@ void GameObject::updateOpacity()
353362
opacityMultiplier *= GameLayer::instance->groups[i]->groupOpacity;
354363
}
355364

356-
setOpacity(opacity);
365+
setOpacityWithoutSend(opacity);
357366

358367
for (std::shared_ptr<Sprite>& sprite : childSprites)
359368
{
360369
sprite->opacityMultiplier = opacityMultiplier;
361-
sprite->setOpacity(sprite->opacity);
370+
sprite->setOpacityWithoutSend(sprite->opacity);
362371
}
363372
}
364373

365-
void GameObject::updatePosition()
374+
void GameObject::updatePosition(bool send)
366375
{
367-
if (groups.size() <= 0)
368-
return;
369-
370-
sf::Vector2f move = {0, 0};
371-
float rotate = 0;
372-
for (int i : groups)
373-
{
374-
move += GameLayer::instance->groups[i]->moveTotal;
375-
}
376-
377-
sf::Vector2f newPosition = startPosition + rotateOffsetMovement;
378-
379-
for (int i : groups)
380-
{
381-
std::shared_ptr<Group> group = GameLayer::instance->groups[i];
382-
rotate += group->rotateTotal;
383-
384-
if (group->rotateAround)
385-
{
386-
sf::Vector2f rotationPoint = group->rotateAround->getPosition();
387-
float rotationAngle = group->rotateTotalMovement;
388-
389-
sf::Transform transform;
390-
transform.rotate(rotationAngle, rotationPoint);
391-
392-
//newPosition += group->rotateAround->getPosition() - group->rotateAround->startPosition;
393-
newPosition = transform.transformPoint(newPosition);
394-
}
395-
}
396-
397-
rotate += rotateOffset;
398-
399-
setPosition(newPosition + move);
400-
setRotation(startRotation + rotate);
401-
402-
this->updateVerticesPosition();
376+
this->updateVerticesPosition(send);
403377

404378
for (std::shared_ptr<Sprite> child : childSprites)
405-
child->updateVerticesPosition();
379+
child->updateVerticesPosition(send);
406380
}
407381

408382
void GameObject::tryUpdateSection()
@@ -416,9 +390,7 @@ void GameObject::tryUpdateSection()
416390
{
417391
removeFromChannel();
418392
for (auto& spr : childSprites)
419-
{
420393
spr->removeFromChannel();
421-
}
422394

423395
auto&thisSectionMap = GameLayer::instance->sectionObjects[this->section];
424396

@@ -433,24 +405,21 @@ void GameObject::tryUpdateSection()
433405
}
434406
GameLayer::instance->sectionObjects[section].insert({this->uniqueID, this});
435407

436-
/* if (GameLayer::instance->dirtySections.find(section) == GameLayer::instance->dirtySections.end())
437-
GameLayer::instance->dirtySections.insert(section); */
438-
439408
this->section = section;
440409

441410
for (int i : groups)
442411
{
443412
std::shared_ptr<Group> group = GameLayer::instance->groups[i];
444-
auto groupMap = &group->objects[oldSection];
413+
auto groupMap = &group->objectsInSections[oldSection];
445414

446415
groupMap->erase(uniqueID);
447416

448-
if (!group->objects.contains(section))
417+
if (!group->objectsInSections.contains(section))
449418
{
450419
std::unordered_map<int, GameObject*> map;
451-
group->objects.insert({section, map});
420+
group->objectsInSections.insert({section, map});
452421
}
453-
group->objects[section].insert({static_cast<int>(uniqueID), this});
422+
group->objectsInSections[section].insert({static_cast<int>(uniqueID), this});
454423
}
455424

456425
removeFromBatcher();

GD/GameObject.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ class GameObject : public Sprite
2727
std::vector<int> groups;
2828

2929
sf::Vector2f startPosition;
30-
sf::Vector2f rotateOffsetMovement;
31-
32-
float rotateOffset = 0.f;
33-
float startRotation = 0.f;
3430

3531
public:
3632
GameObject()
@@ -42,7 +38,7 @@ class GameObject : public Sprite
4238
void setupCustomObjects(nlohmann::json& objectJson, std::shared_ptr<GameObject> parent);
4339

4440
void updateOpacity();
45-
void updatePosition();
41+
void updatePosition(bool send);
4642

4743
void tryUpdateSection();
4844
};

GD/Group.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
#pragma once
22

3-
#include "GameObject.h"
3+
#include "EffectGameObject.h"
44

55
class ActionInterval;
66

77
class Group
88
{
99
public:
10-
std::unordered_map<int, std::unordered_map<int, GameObject*>> objects;
10+
std::unordered_map<int, std::unordered_map<int, GameObject*>> objectsInSections;
11+
std::vector<GameObject*> objects;
1112
float groupOpacity = 1.0f;
1213

13-
sf::Vector2f moveTotal = { 0, 0 };
14-
float rotateTotalMovement = 0;
15-
float rotateTotal = 0;
14+
bool toggle = true;
1615

17-
GameObject* rotateAround = nullptr;
16+
std::vector<EffectGameObject*> spawnTriggered;
1817

1918
std::shared_ptr <ActionInterval> rotateAction = nullptr;
2019
public:

GD/MoveAction.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ void MoveAction::update(float time)
3434

3535
moveLast = moveCurrent;
3636

37-
targetGroup->moveTotal += deltaMovement;
37+
for (GameObject* obj : this->targetGroup->objects)
38+
{
39+
obj->move(deltaMovement);
40+
}
3841

3942
auto&dirty = GameLayer::instance->dirtyGroups;
4043
if (dirty.find(groupID) == dirty.end())

GD/OpacityAction.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ void OpacityAction::update(float time)
3030

3131
for (int i = GameLayer::instance->prevSection; i < GameLayer::instance->nextSection + 1; i++)
3232
{
33-
for (auto pair : groupPtr->objects[i])
33+
for (auto pair : groupPtr->objectsInSections[i])
3434
{
3535
GameObject* obj = pair.second;
3636
obj->updateOpacity();
37+
obj->sendVertices();
38+
for(auto spr : obj->childSprites)
39+
spr->sendVertices();
3740
}
3841
}
3942
}

GD/PulseAction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void PulseAction::update(float time)
9898

9999
for (int i = GameLayer::instance->prevSection; i < GameLayer::instance->nextSection + 1; i++)
100100
{
101-
for (auto pair : group->objects[i])
101+
for (auto pair : group->objectsInSections[i])
102102
{
103103
GameObject* obj = pair.second;
104104
applyToSpriteIn(obj, fadetime);
@@ -115,7 +115,7 @@ void PulseAction::update(float time)
115115
std::shared_ptr<Group> group = GameLayer::instance->groups[target];
116116
for (int i = GameLayer::instance->prevSection; i < GameLayer::instance->nextSection + 1; i++)
117117
{
118-
for (auto pair : group->objects[i])
118+
for (auto pair : group->objectsInSections[i])
119119
{
120120
GameObject* obj = pair.second;
121121
applyToSpriteOut(obj, fadetime);
@@ -129,7 +129,7 @@ void PulseAction::update(float time)
129129
std::shared_ptr<Group> group = GameLayer::instance->groups[target];
130130
for (int i = GameLayer::instance->prevSection; i < GameLayer::instance->nextSection + 1; i++)
131131
{
132-
for (auto pair : group->objects[i])
132+
for (auto pair : group->objectsInSections[i])
133133
{
134134
GameObject* obj = pair.second;
135135
applyToSpriteIn(obj, fadein + hold);

0 commit comments

Comments
 (0)