Skip to content

Commit

Permalink
Merge pull request #60 from HJfod/startpos-switch-fix
Browse files Browse the repository at this point in the history
Startpos Switcher Rewrite
  • Loading branch information
Fleeym authored Oct 10, 2023
2 parents a36352a + c1ad19c commit 463bb25
Show file tree
Hide file tree
Showing 14 changed files with 881 additions and 439 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ file(GLOB SOURCES
src/features/MoreTabs/*.cpp
src/features/VisibilityToggle/*.cpp
src/features/BetterScaling/*.cpp
src/features/StartPosSwitcher/*.cpp
src/features/*.cpp
src/other/*.cpp
src/*.cpp
Expand Down
6 changes: 6 additions & 0 deletions api/include/MoreTabs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <Geode/modify/EditorUI.hpp>
#include <Geode/binding/EditButtonBar.hpp>
#include <Geode/utils/cocos.hpp>
#include <Geode/binding/CCMenuItemToggler.hpp>
#include <Geode/binding/EditButtonBar.hpp>
#include <Geode/binding/GameManager.hpp>
#include <Geode/binding/CCMenuItemSpriteExtra.hpp>
#include <Geode/modify/EditorUI.hpp>
#include <geode.custom-keybinds/include/Keybinds.hpp>

using namespace geode::prelude;

Expand Down
6 changes: 0 additions & 6 deletions api/src/MoreTabs.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
#include <MoreTabs.hpp>
#include <Geode/binding/CCMenuItemToggler.hpp>
#include <Geode/binding/EditButtonBar.hpp>
#include <Geode/binding/GameManager.hpp>
#include <Geode/binding/CCMenuItemSpriteExtra.hpp>
#include <Geode/modify/EditorUI.hpp>
#include <geode.custom-keybinds/include/Keybinds.hpp>

using namespace keybinds;
using namespace editor_api;
Expand Down
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"type": "bool",
"default": true,
"name": "Smart StartPos",
"description": "When enabled, all StartPoses in the editor will be automatically set for you"
"description": "When enabled, all StartPoses in the editor will be automatically set for you. <cr>Does not set gravity for ball and spider gamemodes.</c>"
},
"playtest-lag-fix": {
"type": "bool",
Expand Down
193 changes: 193 additions & 0 deletions src/Features/StartPosSwitcher/StartPosManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#include "StartPosManager.hpp"

StartPosManager* StartPosManager::get() {
if (!s_instance) {
auto instance = new StartPosManager;
if (instance) {
s_instance = instance;
return instance;
}

CC_SAFE_DELETE(instance);
return instance;
}
return s_instance;
}

void StartPosManager::setStartPositions(CCArray* objects) {
m_positions.clear();
m_positions.push_back(CCPointZero);
if (!objects) {
return;
}
for (auto object : CCArrayExt<GameObject*>(objects)) {
if (object->m_objectID == 31) {
auto skip = false;
for (auto position : m_positions) {
if (position == object->getPosition()) {
skip = true;
break;
}
}
if (skip) {
continue;
}
m_positions.push_back(object->getPosition());
auto editor = LevelEditorLayer::get();
if (m_active != CCPointZero && editor && !editor->m_editorInitialising) {
if (object->getPosition() == editor->m_editorUI->getGridSnappedPos(m_active)) {
m_active = object->getPosition();
}
}
}
}

this->sort();

if (m_positions.size() == 1) {
m_active = CCPointZero;
return;
}
if (m_initialized) {
return;
}

m_active = m_positions.at(m_positions.size() - 1);
m_initialized = true;
}

void StartPosManager::sort() {
std::sort(m_positions.begin(), m_positions.end(), [](CCPoint first, CCPoint second) {
return first.x < second.x;
});
}

void StartPosManager::setActive(CCPoint const& pos) {
bool set = false;
for (auto startPos : m_positions) {
if (startPos == pos) {
m_active = pos;
set = true;
break;
}
}

if (!set) {
m_active = m_positions.at(m_positions.size() - 1);
}
}

void StartPosManager::addStartPos(CCPoint const& position) {
for (auto startPosition : m_positions) {
if (startPosition == position) {
return;
}
}
m_positions.push_back(position);
this->sort();
}

void StartPosManager::replaceStartPos(CCPoint const& before, CCPoint const& after) {
if (!LevelEditorLayer::get() || LevelEditorLayer::get()->m_editorInitialising) {
return;
}

for (auto& position : m_positions) {
if (position == before) {
position = after;
break;
}
}
if (m_active == before) {
m_active = after;
}

this->sort();
}

StartPosObject* StartPosManager::getStartPosFromPoint(CCPoint const& point) {
if (LevelEditorLayer::get()) {
for (auto object : CCArrayExt<GameObject*>(LevelEditorLayer::get()->m_objects)) {
if (object->m_objectID == 31) {
if (object->getPosition() == point) {
return static_cast<StartPosObject*>(object);
}

if (!LevelEditorLayer::get()->m_editorInitialising && object->getPosition() == LevelEditorLayer::get()->m_editorUI->getGridSnappedPos(point)) {
this->replaceStartPos(point, LevelEditorLayer::get()->m_editorUI->getGridSnappedPos(point));
return static_cast<StartPosObject*>(object);
}
}
}

return nullptr;
}

if (PlayLayer::get()) {
for (auto object : CCArrayExt<GameObject*>(PlayLayer::get()->m_objects)) {
if (
object->m_objectID == 31 &&
object->getPosition() == point
) {
return static_cast<StartPosObject*>(object);
}
}
}
return nullptr;
}

void StartPosManager::next() {
for (auto pos : m_positions) {
if (pos.x > m_active.x) {
m_active = pos;
return;
}
}
}

void StartPosManager::previous() {
auto found = CCPointZero;
for (auto pos : m_positions) {
if (pos.x < m_active.x) {
found = pos;
}
}

m_active = found;
}

void StartPosManager::clear() {
m_active = CCPointZero;
m_positions.clear();
m_initialized = false;
}

bool StartPosManager::isDefault() {
if (m_positions.size() == 0) {
return true;
}
return m_active == m_positions.at(m_positions.size() - 1);
}

bool StartPosManager::isLevelStart() {
if (m_positions.size() == 0) {
return false;
}
return m_active == m_positions.at(0);
}

CCPoint StartPosManager::getActive() {
return m_active;
}

std::vector<CCPoint> StartPosManager::getPositions() {
return m_positions;
}

void StartPosManager::setDefault() {
m_active = m_positions.at(m_positions.size() - 1);
}

void StartPosManager::setFirst() {
m_active = m_positions.at(0);
}
33 changes: 33 additions & 0 deletions src/Features/StartPosSwitcher/StartPosManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <Geode/Geode.hpp>
#include <vector>
#include <algorithm>

using namespace geode::prelude;

class StartPosManager : public CCObject {
protected:
std::vector<CCPoint> m_positions = {};
CCPoint m_active = CCPointZero;
bool m_initialized = false;

inline static StartPosManager* s_instance;
void sort();
public:
static StartPosManager* get();
void setStartPositions(CCArray* objects);
void setActive(CCPoint const& pos);
void addStartPos(CCPoint const& pos);
void replaceStartPos(CCPoint const& before, CCPoint const& after);
bool isDefault();
bool isLevelStart();
StartPosObject* getStartPosFromPoint(CCPoint const& pos);
CCPoint getActive();
std::vector<CCPoint> getPositions();
void next();
void previous();
void clear();
void setDefault();
void setFirst();
};
13 changes: 11 additions & 2 deletions src/features/LDMObjectCount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ class $modify(LDMCountEditorPauseLayer, EditorPauseLayer) {
auto menu = static_cast<CCMenu*>(this->getChildByID("info-menu"));
auto objectCountLabel = static_cast<CCLabelBMFont*>(menu->getChildByID("object-count-label"));

auto objectCount = this->countValidObjects();

std::string objectCountText = objectCountLabel->getString();
std::stringstream ss;
ss << std::string(objectCountLabel->getString());
int ldmCount = this->countLDMObjects();
float percentage = static_cast<float>(ldmCount) / this->countValidObjects() * 100;
int ldmCount;
float percentage;
if (objectCount == 0) {
ldmCount = 0;
percentage = 0.f;
} else {
ldmCount = this->countLDMObjects();
percentage = static_cast<float>(ldmCount) / objectCount * 100;
}

ss << " | " << ldmCount << " LDM (" << std::fixed << std::setprecision(2) << percentage << "%)";
objectCountLabel->setString(ss.str().c_str());
Expand Down
42 changes: 24 additions & 18 deletions src/features/SmartStartPos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,7 @@ class $modify(LevelEditorLayer) {
}

void setupStartPos(StartPosObject* startPos) {
for (size_t i = 0; i < gravityPortals.size(); i++)
{
if (gravityPortals[i]->getPositionX() - 10 > startPos->getPositionX())
break;
if (gravityPortals[i]->getPositionX() - 10 < startPos->getPositionX())
startPos->m_levelSettings->m_isFlipped = gravityPortals[i]->m_objectID == 11;
}

startPos->m_levelSettings->m_startDual = LevelEditorLayer::get()->m_levelSettings->m_startDual;
for (size_t i = 0; i < dualPortals.size(); i++)
{
if (dualPortals[i]->getPositionX() - 10 > startPos->getPositionX())
break;
if (dualPortals[i]->getPositionX() - 10 < startPos->getPositionX())
startPos->m_levelSettings->m_startDual = dualPortals[i]->m_objectID == 286;
}
bool isBallOrSpider = false;
startPos->m_levelSettings->m_startMode = LevelEditorLayer::get()->m_levelSettings->m_startMode;
for (size_t i = 0; i < gamemodePortals.size(); i++)
{
Expand All @@ -51,6 +36,7 @@ class $modify(LevelEditorLayer) {
break;
case 47:
startPos->m_levelSettings->m_startMode = (int)IconType::Ball;
isBallOrSpider = true;
break;
case 111:
startPos->m_levelSettings->m_startMode = (int)IconType::Ufo;
Expand All @@ -63,10 +49,29 @@ class $modify(LevelEditorLayer) {
break;
case 1331:
startPos->m_levelSettings->m_startMode = (int)IconType::Spider;
isBallOrSpider = true;
break;
}
}
}
if (!isBallOrSpider) {
for (size_t i = 0; i < gravityPortals.size(); i++)
{
if (gravityPortals[i]->getPositionX() - 10 > startPos->getPositionX())
break;
if (gravityPortals[i]->getPositionX() - 10 < startPos->getPositionX())
startPos->m_levelSettings->m_isFlipped = gravityPortals[i]->m_objectID == 11;
}
}

startPos->m_levelSettings->m_startDual = LevelEditorLayer::get()->m_levelSettings->m_startDual;
for (size_t i = 0; i < dualPortals.size(); i++)
{
if (dualPortals[i]->getPositionX() - 10 > startPos->getPositionX())
break;
if (dualPortals[i]->getPositionX() - 10 < startPos->getPositionX())
startPos->m_levelSettings->m_startDual = dualPortals[i]->m_objectID == 286;
}

startPos->m_levelSettings->m_startMini = LevelEditorLayer::get()->m_levelSettings->m_startMini;
for (size_t i = 0; i < miniPortals.size(); i++)
Expand Down Expand Up @@ -215,7 +220,8 @@ class $modify(LevelEditorLayer) {
speedChanges.push_back(g);
break;
}

SSPsetup();
if (LevelEditorLayer::get() && !LevelEditorLayer::get()->m_editorInitialising && g->m_objectID == 31) {
this->setupStartPos(static_cast<StartPosObject*>(g));
}
}
};
Loading

0 comments on commit 463bb25

Please sign in to comment.