Skip to content

Commit

Permalink
3D drawing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
IvoryDuke committed Jan 23, 2021
1 parent 3b78730 commit 5f4b902
Show file tree
Hide file tree
Showing 16 changed files with 920 additions and 272 deletions.
1 change: 1 addition & 0 deletions include/SSVOpenHexagon/Components/CCustomWall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CCustomWall

void update(HexagonGame& mHexagonGame, ssvu::FT mFT);
void draw(HexagonGame& mHexagonGame);
void draw3D(HexagonGame& mHexagonGame, const sf::Color& mColor);

[[gnu::always_inline, nodiscard]] bool isOverlapping(
const sf::Vector2f& mPoint) const noexcept
Expand Down
1 change: 1 addition & 0 deletions include/SSVOpenHexagon/Components/CCustomWallManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CCustomWallManager
void cleanup();
void clear();
void draw(HexagonGame& hexagonGame);
void draw3D(HexagonGame& hexagonGame, const sf::Color& mColor);

template <typename F>
void forCustomWalls(F&& f)
Expand Down
51 changes: 42 additions & 9 deletions include/SSVOpenHexagon/Components/CPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class CPlayer
sf::Vector2f startPos;
sf::Vector2f pos;
sf::Vector2f lastPos;
std::array<sf::Vector2f, 3> vertexPositions;
std::vector<sf::Vector2f> pivotVertexes;

float hue;
float angle;
Expand All @@ -39,36 +41,67 @@ class CPlayer
Ticker swapBlinkTimer;
Ticker deadEffectTimer;

float pivotRadius;
static constexpr float pivotBorderThickness{5.f};

void drawCommon(HexagonGame& mHexagonGame);
void drawPivot(HexagonGame& mHexagonGame, const sf::Color& mCapColor);
void drawPivot3D(HexagonGame& mHexagonGame, const sf::Color& mCapColor);
void drawDeathEffect(HexagonGame& mHexagonGame);
void drawDeathEffect3D(HexagonGame& mHexagonGame);

public:

CPlayer(const sf::Vector2f& mPos, const float swapCooldown) noexcept;

[[gnu::always_inline, nodiscard]] const sf::Vector2f&
getPosition() const noexcept
[[gnu::always_inline, nodiscard]]
const sf::Vector2f& getPosition() const noexcept
{
return pos;
}

[[nodiscard]] float getPlayerAngle() const noexcept;

[[gnu::always_inline, nodiscard]]
const std::array<sf::Vector2f, 3>& getVertexes() const noexcept
{
return vertexPositions;
}

[[gnu::always_inline, nodiscard]]
const std::vector<sf::Vector2f>& getPivotVertexes() const noexcept
{
return pivotVertexes;
}

[[gnu::always_inline, nodiscard]] float getPivotRadius() const noexcept
{
return pivotRadius + pivotBorderThickness;
}

void setPlayerAngle(const float newAng) noexcept;
void playerSwap(HexagonGame& mHexagonGame, bool mPlaySound);

void kill(HexagonGame& mHexagonGame);

void update(HexagonGame& mHexagonGame, ssvu::FT mFT);
void updateInput(HexagonGame& mHexagonGame, ssvu::FT mFT);
void updatePosition(HexagonGame& mHexagonGame, ssvu::FT mFT);
void update(HexagonGame& mHexagonGame, const ssvu::FT mFT);
void updateInput(HexagonGame& mHexagonGame, const ssvu::FT mFT);
void updatePosition(const HexagonGame& mHexagonGame, const ssvu::FT mFT);

void draw(HexagonGame& mHexagonGame, const sf::Color& mCapColor);
void draw3D(HexagonGame& mHexagonGame, const sf::Color& mPlayerColor,
const sf::Color& mCapColor);

void setSides(unsigned int mSideNumber) noexcept
{
pivotVertexes.resize(mSideNumber);
}

[[nodiscard]] bool push(
HexagonGame& mHexagonGame, const hg::CWall& wall, ssvu::FT mFT);
[[nodiscard]] bool push(const HexagonGame& mHexagonGame,
const CWall& wall, const sf::Vector2f& mCenterPos, ssvu::FT mFT);

[[nodiscard]] bool push(
HexagonGame& mHexagonGame, const hg::CCustomWall& wall, ssvu::FT mFT);
[[nodiscard]] bool push(const HexagonGame& mHexagonGame,
const hg::CCustomWall& wall, ssvu::FT mFT);

[[nodiscard]] bool getJustSwapped() const noexcept;
};
Expand Down
22 changes: 16 additions & 6 deletions include/SSVOpenHexagon/Components/CWall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "SSVOpenHexagon/Components/SpeedData.hpp"
#include "SSVOpenHexagon/Utils/PointInPolygon.hpp"

#include <SSVStart/Utils/Vector2.hpp>

#include <SFML/System/Vector2.hpp>

#include <array>
Expand All @@ -32,18 +34,26 @@ class CWall
float mThickness, float mDistance, const SpeedData& mSpeed,
const SpeedData& mCurve);

void update(HexagonGame& mHexagonGame, ssvu::FT mFT);
void update(const HexagonGame& mHexagonGame, const ssvu::FT mFT);

void moveTowardsCenter(HexagonGame& mHexagonGame,
const sf::Vector2f& mCenterPos, ssvu::FT mFT);
const sf::Vector2f& mCenterPos, const ssvu::FT mFT);

void moveCurve(HexagonGame& mHexagonGame, const sf::Vector2f& mCenterPos,
ssvu::FT mFT);
void moveCurve(const HexagonGame& mHexagonGame, const sf::Vector2f& mCenterPos,
const ssvu::FT mFT);

void draw(HexagonGame& mHexagonGame);
void drawToIntersections(HexagonGame& mHexagonGame,
const sf::Vector2f& mIntersectionOne, const sf::Vector2f& mIntersectionTwo);

void setHueMod(float mHueMod) noexcept;

[[gnu::always_inline, nodiscard]]
const std::array<sf::Vector2f, 4>& getVertexes() const noexcept
{
return vertexPositions;
}

[[gnu::always_inline, nodiscard]] const SpeedData& getSpeed() const noexcept
{
return speed;
Expand All @@ -54,8 +64,8 @@ class CWall
return curve;
}

[[gnu::always_inline, nodiscard]] bool isOverlapping(
const sf::Vector2f& mPoint) const noexcept
[[gnu::always_inline, nodiscard]]
bool isOverlapping(const sf::Vector2f& mPoint) const noexcept
{
return Utils::pointInPolygon(vertexPositions, mPoint.x, mPoint.y);
}
Expand Down
79 changes: 66 additions & 13 deletions include/SSVOpenHexagon/Core/HexagonGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,78 @@ class HexagonGame
void updateKeyIcons();

// Draw methods
void draw();
public:
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> playerTris;
Utils::FastVertexVector<sf::PrimitiveType::Quads> capQuads;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> capTris;
Utils::FastVertexVector<sf::PrimitiveType::Quads> capQuads3D;
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads3DFront;
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads3DBack;
Utils::FastVertexVector<sf::PrimitiveType::Quads> deathQuads;
Utils::FastVertexVector<sf::PrimitiveType::Quads> deathQuads3D;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> playerTris3D;
sf::Vector2f offset3D;

// Gameplay methods
void incrementDifficulty();
void sideChange(unsigned int mSideNumber);
[[nodiscard]] const sf::Vector2f& get3DOffset() noexcept
{
return offset3D;
}

// Draw methods
private:
static constexpr float piAndHalf{ssvu::piHalf * 3.f};

void drawSetup();
void draw2D();
void drawProjections();
void draw3D();
void drawWrapup();
void draw();
void drawText_TimeAndStatus(const sf::Color& offsetColor);
void drawText_Message(const sf::Color& offsetColor);
void drawText();
void drawKeyIcons();

std::function<void()> drawFunc;
void setDrawFunc()
{
if(!Config::get3D())
{
drawFunc = [this] { draw2D(); };
return;
}

if(levelData->_3DDrawingMode)
{
drawFunc = [this] { draw3D(); };
}
else
{
drawFunc = [this] { drawProjections(); };
}
}

unsigned int getVertexScreenPortion(const sf::Vector2f& mVertex);
void calcIntersectionPoint(sf::Vector2f& intersection,
const sf::Vector2f& wallVertexOne, const sf::Vector2f& wallVertexTwo);
[[nodiscard]] float getVertexRadAngle(const sf::Vector2f& mVertex, const float& mRotation)
{
return ssvu::getWrapRad(ssvs::getRad(mVertex, centerPos) - mRotation);
}

void drawWall3DSideBack(const std::array<sf::Vector2f, 4>& mVertexes,
const sf::Color& mColor, const unsigned int mIndex);
void drawWall3DSideFront(const std::array<sf::Vector2f, 4>& mVertexes,
const sf::Color& mColor, const unsigned int mIndex);
void drawPlayer3DSides(const std::array<sf::Vector2f, 3>& mVertexes,
const sf::Color& mColor);
void draw3DWall(CWall& mWall, const sf::Color& mColor,
const float& mRotation, const float& mRadius);

// Gameplay methods
void incrementDifficulty();
void sideChange(unsigned int mSideNumber);

// Data-related methods
void setLevelData(const LevelData& mLevelData, bool mMusicFirstPlay);
void playLevelMusic();
Expand Down Expand Up @@ -322,7 +382,6 @@ class HexagonGame

void invalidateScore(const std::string& mReason);


template <typename F>
Utils::LuaMetadataProxy addLuaFn(const std::string& name, F&& f)
{
Expand All @@ -341,8 +400,7 @@ class HexagonGame
const std::string& args, const std::string& docs) {
std::cout << "* **`" << ret << " " << name << "(" << args
<< ")`**: " << docs << "\n\n";
},
i);
}, i);
}
}

Expand All @@ -366,11 +424,6 @@ class HexagonGame
}

public:
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> playerTris;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> capTris;
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads3D;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> playerTris3D;

MenuGame* mgPtr;

Expand Down
1 change: 1 addition & 0 deletions include/SSVOpenHexagon/Data/LevelData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class LevelData
packPath + ssvuj::getExtr<std::string>(root, "luaFile", "nullLuaPath")};
std::vector<float> difficultyMults{
ssvuj::getExtr<std::vector<float>>(root, "difficultyMults", {})};
bool _3DDrawingMode{ssvuj::getExtr<bool>(root, "3DMode", false)};

LevelData(const ssvuj::Obj& mRoot, const ssvufs::Path& mPackPath,
const std::string& mPackId)
Expand Down
11 changes: 9 additions & 2 deletions include/SSVOpenHexagon/Utils/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,26 @@ inline void uppercasify(std::string& s)
return s;
}

inline float getFontHeight(sf::Text& font)
[[nodiscard, gnu::pure]] inline float getFontHeight(sf::Text& font)
{
font.setString("A");
return ssvs::getGlobalHeight(font);
}

inline float getFontHeight(sf::Text& font, const unsigned int charSize)
[[nodiscard, gnu::pure]] inline float getFontHeight(sf::Text& font, const unsigned int charSize)
{
font.setCharacterSize(charSize);
font.setString("A");
return ssvs::getGlobalHeight(font);
}

[[nodiscard, gnu::pure]] inline float fastSqrt(const float &n)
{
static union{int i; float f;} u;
u.i = 0x5F375A86 - (*(int*)&n >> 1);
return (int(3) - n * u.f * u.f) * n * u.f * 0.5f;
}

[[nodiscard, gnu::pure]] inline float getSaturated(float mValue)
{
return std::max(0.f, std::min(1.f, mValue));
Expand Down
30 changes: 30 additions & 0 deletions src/SSVOpenHexagon/Components/CCustomWall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ void CCustomWall::draw(HexagonGame& mHexagonGame)
vertexPositions[3], vertexColors[3]);
}

void CCustomWall::draw3D(HexagonGame& mHexagonGame, const sf::Color& mColor)
{
draw(mHexagonGame);

const sf::Vector2f& offset3D{mHexagonGame.get3DOffset()};
mHexagonGame.wallQuads3DBack.reserve_more(4 * 4);

unsigned int next;
for(unsigned int i{0}; i < 4; ++i)
{
next = (i + 1) % 4;

mHexagonGame.wallQuads3DBack.unsafe_emplace_back(
vertexPositions[i], mColor);

mHexagonGame.wallQuads3DBack.unsafe_emplace_back(
vertexPositions[next], mColor);

mHexagonGame.wallQuads3DBack.unsafe_emplace_back(
sf::Vector2f{vertexPositions[next].x + offset3D.x,
vertexPositions[next].y + offset3D.y},
mColor);

mHexagonGame.wallQuads3DBack.unsafe_emplace_back(
sf::Vector2f{vertexPositions[i].x + offset3D.x,
vertexPositions[i].y + offset3D.y},
mColor);
}
}

void CCustomWall::update(HexagonGame& mHexagonGame, ssvu::FT mFT)
{
(void)mHexagonGame;
Expand Down
Loading

0 comments on commit 5f4b902

Please sign in to comment.