mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 03:41:48 +01:00
Pre Point2f Nuke
Fixed drawing, Added general optimisations
This commit is contained in:
73
.idea/.idea.Motherload/.idea/workspace.xml
generated
73
.idea/.idea.Motherload/.idea/workspace.xml
generated
@@ -10,10 +10,20 @@
|
||||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="26a0623a-44d5-441c-8048-32ff1dab3479" name="Changes" comment="Fix precompiled headers Added edge detection (if it works :/)">
|
||||
<list default="true" id="26a0623a-44d5-441c-8048-32ff1dab3479" name="Changes" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Engine/Texture.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/Texture.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Engine/Texture.h" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/Texture.h" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Engine/utils.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/utils.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/Animations/Animation.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Animations/Animation.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/Animations/Animation.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Animations/Animation.h" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/Game.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Game.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/GridSystem/WorldTile.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldTile.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/GridSystem/WorldTile.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldTile.h" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/Gui/Screen.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Gui/Screen.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.h" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/Player.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Game/Player.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.h" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@@ -212,28 +222,28 @@
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"C++ Project.Game.executor": "Run",
|
||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"ignore.virus.scanning.warn.message": "true",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.detected.package.tslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
"node.js.selected.package.tslint": "(autodetect)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"settings.editor.selected.configurable": "editor.preferences.fonts.default",
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
<component name="PropertiesComponent">{
|
||||
"keyToString": {
|
||||
"C++ Project.Game.executor": "Run",
|
||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"ignore.virus.scanning.warn.message": "true",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.detected.package.tslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
"node.js.selected.package.tslint": "(autodetect)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"settings.editor.selected.configurable": "editor.preferences.fonts.default",
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
},
|
||||
"keyToStringList": {
|
||||
"rider.external.source.directories": [
|
||||
"C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache",
|
||||
"C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache",
|
||||
"C:\\Users\\Bram\\AppData\\Local\\Symbols\\src"
|
||||
"keyToStringList": {
|
||||
"rider.external.source.directories": [
|
||||
"C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache",
|
||||
"C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache",
|
||||
"C:\\Users\\Bram\\AppData\\Local\\Symbols\\src"
|
||||
]
|
||||
}
|
||||
}]]></component>
|
||||
}</component>
|
||||
<component name="RunManager">
|
||||
<configuration name="Game" type="CppProject" factoryName="C++ Project">
|
||||
<configuration_1>
|
||||
@@ -324,7 +334,13 @@
|
||||
<workItem from="1712269212956" duration="5658000" />
|
||||
<workItem from="1712313346695" duration="4041000" />
|
||||
<workItem from="1712322453730" duration="533000" />
|
||||
<workItem from="1712328371740" duration="12603000" />
|
||||
<workItem from="1712328371740" duration="13968000" />
|
||||
<workItem from="1712404469282" duration="714000" />
|
||||
<workItem from="1712414027596" duration="1111000" />
|
||||
<workItem from="1712830657879" duration="548000" />
|
||||
<workItem from="1713168521010" duration="1430000" />
|
||||
<workItem from="1713257719220" duration="5429000" />
|
||||
<workItem from="1713268116257" duration="1163000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="Rework Tile detection system">
|
||||
<option name="closed" value="true" />
|
||||
@@ -366,7 +382,15 @@
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1712359406452</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="6" />
|
||||
<task id="LOCAL-00006" summary="Fixed sidePieces (Need to add more cases tho)">
|
||||
<option name="closed" value="true" />
|
||||
<created>1712360855725</created>
|
||||
<option name="number" value="00006" />
|
||||
<option name="presentableId" value="LOCAL-00006" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1712360855725</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="7" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
@@ -380,7 +404,8 @@
|
||||
<MESSAGE value="Basic screen system" />
|
||||
<MESSAGE value="Update Screen system Added FuelScreen (Working buttons) Added SellScreen (Nothing working)" />
|
||||
<MESSAGE value="Fix precompiled headers Added edge detection (if it works :/)" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Fix precompiled headers Added edge detection (if it works :/)" />
|
||||
<MESSAGE value="Fixed sidePieces (Need to add more cases tho)" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Fixed sidePieces (Need to add more cases tho)" />
|
||||
</component>
|
||||
<component name="XDebuggerManager">
|
||||
<watches-manager>
|
||||
|
||||
@@ -238,7 +238,7 @@ void Texture::Draw( const Point2f& dstBottomLeft, const Rectf& srcRect, bool fli
|
||||
dstRect.width = m_Width;
|
||||
dstRect.height = m_Height;
|
||||
}
|
||||
Draw( dstRect, srcRect );
|
||||
Draw( dstRect, srcRect, flip );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,6 +341,9 @@ bool Texture::IsCreationOk( ) const
|
||||
{
|
||||
return m_CreationOk;
|
||||
}
|
||||
Rectf Texture::getSrcRect() {
|
||||
return Rectf{0, 0, m_Width, m_Height};
|
||||
}
|
||||
|
||||
void Texture::DrawFilledRect(const Rectf& rect) const
|
||||
{
|
||||
|
||||
@@ -20,6 +20,8 @@ public:
|
||||
float GetHeight() const;
|
||||
bool IsCreationOk( ) const;
|
||||
|
||||
Rectf getSrcRect();
|
||||
|
||||
static int m_TextureCounter;
|
||||
|
||||
private:
|
||||
|
||||
@@ -724,3 +724,6 @@ bool utils::isMouseDown(int button) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//utils::getScrollMovement()
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "pch.h"
|
||||
#include "Animation.h"
|
||||
|
||||
#include "utils.h"
|
||||
Animation::Animation(Texture* pTexture, int frames, float frameDuration, Rectf srcRect): m_pTexture(pTexture), m_SrcRect(srcRect), m_Frames(frames) {
|
||||
|
||||
}
|
||||
@@ -17,10 +19,13 @@ void Animation::Update(float elapsedSec) {
|
||||
}
|
||||
}
|
||||
}
|
||||
void Animation::Draw(const Point2f& pos, float angle) const {
|
||||
void Animation::Draw(const Point2f& pos) const {
|
||||
Draw(pos, Rectf{ pos.x, pos.y, m_SrcRect.width, m_SrcRect.height });
|
||||
}
|
||||
|
||||
void Animation::Draw(const Point2f& pos, const Rectf& dst) const {
|
||||
Rectf src = m_SrcRect;
|
||||
src.left += m_CurrentFrame * src.width;
|
||||
auto dst = Rectf { pos.x, pos.y, src.width, src.height };
|
||||
|
||||
|
||||
m_pTexture->Draw(dst, src, m_isFlipped);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ public:
|
||||
~Animation();
|
||||
|
||||
void Update(float elapsedSec);
|
||||
void Draw(const Point2f& pos, float angle = 0.0f) const;
|
||||
void Draw(const Point2f& pos) const;
|
||||
void Draw(const Point2f& pos, const Rectf& dst) const;
|
||||
|
||||
void SetPlaying(bool isPlaying) {
|
||||
m_isPlaying = isPlaying;
|
||||
|
||||
@@ -58,6 +58,8 @@ void Game::ProcessKeyUpEvent(const SDL_KeyboardEvent& e) {
|
||||
void Game::ProcessMouseMotionEvent(const SDL_MouseMotionEvent& e) {
|
||||
m_MousePos = Point2f { float(e.x), float(e.y) };
|
||||
m_pCurrentLevel->MouseMove(Point2f { float(e.x), float(e.y) });
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Game::ProcessMouseDownEvent(const SDL_MouseButtonEvent& e) {
|
||||
|
||||
@@ -192,7 +192,7 @@ void WorldTile::Update(Camera* camera) {
|
||||
Point2f mousePos = camera->TransformMouse(Point2f{utils::GetMousePos().x, 500 - utils::GetMousePos().y});
|
||||
m_Hightlight = utils::IsPointInRect(mousePos, Rectf{GetCollisionRect().pos, GetCollisionRect().size});
|
||||
if(CurrentIndex.x == 1 && CurrentIndex.y == 1) {
|
||||
std::cout << "Hey" << std::endl;
|
||||
// std::cout << "Hey" << std::endl;
|
||||
}
|
||||
}
|
||||
Collision::TileCollisionRect WorldTile::GetCollisionRect() {
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
#include "pch.h"
|
||||
#include "Screen.h"
|
||||
|
||||
Screen::Screen(const std::string& filePath, Point2f pos, Point2f size,TextureManager* manager): m_Position(pos), m_Size(size)
|
||||
{
|
||||
Screen::Screen(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager): m_Position(pos), m_Size(size) {
|
||||
m_Background = manager->GetTexture(filePath);
|
||||
}
|
||||
Screen::~Screen() {
|
||||
}
|
||||
void Screen::Update(float elapsedSecs) {
|
||||
for(Button* b : m_Buttons) {
|
||||
for (Button* b : m_Buttons) {
|
||||
b->Update(elapsedSecs);
|
||||
}
|
||||
}
|
||||
void Screen::Draw() const {
|
||||
Rectf dest = Rectf(m_Position, m_Size);
|
||||
Rectf src = Rectf(0,0, m_Background->GetWidth(), m_Background->GetHeight());
|
||||
Rectf src = Rectf(0, 0, m_Background->GetWidth(), m_Background->GetHeight());
|
||||
m_Background->Draw(dest, src, false);
|
||||
|
||||
for(Button* b: m_Buttons) {
|
||||
for (Button* b : m_Buttons) {
|
||||
b->Draw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
|
||||
// The grid is 34 x 50 big, the top center is 0,0 in world coords
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
int actualX = x - WORLD_WIDTH / 2;
|
||||
auto pos = Point2f { float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT };
|
||||
const int actualX = x - WORLD_WIDTH / 2;
|
||||
Point2f pos = Point2f { float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT };
|
||||
GroundTileType* type = Tiles::AIR;
|
||||
switch (utils::randRange(0, 2)) {
|
||||
case 0:
|
||||
@@ -40,13 +40,12 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
|
||||
std::cout << "??" << '\n';
|
||||
}
|
||||
|
||||
m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, Tiles::DIRT, TextureManager::GetInstance(), &m_gridManager});
|
||||
m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, type, TextureManager::GetInstance(), &m_gridManager});
|
||||
}
|
||||
}
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
m_gridManager.GetTileAtIndex(x, 0)->SetTileType(Tiles::AIR);
|
||||
}
|
||||
|
||||
}
|
||||
WorldLevel::~WorldLevel() {
|
||||
//delete m_pTextTexture;
|
||||
@@ -57,8 +56,6 @@ void WorldLevel::Update(float elapsedSec) {
|
||||
SDL_GetMouseState(&mouseX, &mouseY);
|
||||
m_mousePos = Point2f { float(mouseX), float(mouseY) };
|
||||
m_mousePos = m_pCamera->TransformMouse(m_mousePos);
|
||||
//m_player.Update(elapsedSec, *this);
|
||||
|
||||
|
||||
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
@@ -85,7 +82,7 @@ void WorldLevel::Update(float elapsedSec) {
|
||||
if(m_pSelectedTile != nullptr) {
|
||||
surroundingTiles surroundingTiles = m_gridManager.GetSurroundingTiles(m_pSelectedTile);
|
||||
TileDirection direction = TileDirection::TopMiddle;
|
||||
std::array<TileDirection, 8> directions = {TileDirection::TopLeft, TileDirection::TopMiddle, TileDirection::TopRight, TileDirection::MiddleLeft, TileDirection::MiddleRight, TileDirection::BottomLeft, TileDirection::BottomMiddle, TileDirection::BottomRight};
|
||||
const std::array<TileDirection, 8> directions = {TileDirection::TopLeft, TileDirection::TopMiddle, TileDirection::TopRight, TileDirection::MiddleLeft, TileDirection::MiddleRight, TileDirection::BottomLeft, TileDirection::BottomMiddle, TileDirection::BottomRight};
|
||||
|
||||
for(int i = 0; i < 8; i++) {
|
||||
direction = directions[i];
|
||||
@@ -96,9 +93,8 @@ void WorldLevel::Update(float elapsedSec) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//m_player.Update(elapsedSec, *this);
|
||||
m_player.Update(elapsedSec, *this);
|
||||
|
||||
Screen* screen = m_screenManager->GetCurrentScreen();
|
||||
if (screen != nullptr) {
|
||||
@@ -129,17 +125,6 @@ void WorldLevel::Draw() const {
|
||||
}
|
||||
}
|
||||
|
||||
//loop over worldtiles
|
||||
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
if (*m_gridManager.GetTileAtIndex(x, y)->GetTileType() != Tiles::AIR) {
|
||||
Collision::CollisionRect rect = m_gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect();
|
||||
utils::SetColor(Colors::GREEN);
|
||||
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
utils::SetColor(Colors::MAGENTA);
|
||||
utils::FillEllipse(0, 0, 5, 5);
|
||||
|
||||
@@ -190,8 +175,8 @@ void WorldLevel::ProcessImGui() {
|
||||
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
Point2f screenPos = utils::GetMousePos();
|
||||
std::string mousePos = "Mouse Pos: (" + std::to_string(screenPos.x) + ", " + std::to_string(screenPos.y) + ")";
|
||||
const Point2f screenPos = utils::GetMousePos();
|
||||
const std::string mousePos = "Mouse Pos: (" + std::to_string(screenPos.x) + ", " + std::to_string(screenPos.y) + ")";
|
||||
if(ImGui::BeginMenu(mousePos.c_str())) {
|
||||
|
||||
ImGui::EndMenu();
|
||||
|
||||
@@ -10,7 +10,7 @@ class WorldLevel : public Level
|
||||
{
|
||||
public:
|
||||
WorldLevel(Camera* camera, Rectf viewport);
|
||||
~WorldLevel() override;
|
||||
virtual ~WorldLevel() override;
|
||||
|
||||
WorldLevel(const WorldLevel& other) = default;
|
||||
WorldLevel(WorldLevel&& other) = default;
|
||||
@@ -39,7 +39,7 @@ private:
|
||||
WorldTile* m_pSelectedTile { nullptr };
|
||||
|
||||
// ImGui Vars
|
||||
bool m_ShowTextureManagerWindow { true };
|
||||
bool m_ShowTextureManagerWindow { false };
|
||||
bool m_ShowCameraWindow { false };
|
||||
bool m_ShowPlayerInfo { false };
|
||||
bool m_ShowPlayerInfo { true };
|
||||
};
|
||||
|
||||
258
Game/Player.cpp
258
Game/Player.cpp
@@ -16,7 +16,9 @@ Player::Player(const Point2f& Position, TextureManager* manager) : m_Position(Po
|
||||
m_ContactMap[Collision::CollisionDirection::Left] = nullptr;
|
||||
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
|
||||
|
||||
m_walkAnimation = new Animation(manager->GetTexture("animations/player/player_walk.png"), 8, 0.1f, Rectf { 0, 0, 70, 70 });
|
||||
m_walkAnimation = new Animation(
|
||||
manager->GetTexture("animations/player/player_walk.png"),
|
||||
8, 0.1f, Rectf { 0, 0, 70, 70 });
|
||||
}
|
||||
Collision::CollisionRect Player::GetCollisionRect() const {
|
||||
Collision::CollisionRect rect = { m_Position, m_Size, m_Vel };
|
||||
@@ -33,18 +35,29 @@ void Player::Draw() const {
|
||||
const int frameWidth = 70; //TODO: fix this
|
||||
int halfFrameWidth = frameWidth / 2;
|
||||
|
||||
auto drawPos = Point2f { center.x - halfFrameWidth - 10, center.y - halfFrameWidth + 9 };
|
||||
Point2f drawPos = Point2f { center.x - halfFrameWidth, center.y - halfFrameWidth + 9 };
|
||||
|
||||
m_walkAnimation->Draw(drawPos, 0);
|
||||
m_walkAnimation->Draw(drawPos, Rectf { drawPos.x, drawPos.y, frameWidth, frameWidth });
|
||||
}
|
||||
void Player::ProcessImGui() {
|
||||
ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
|
||||
ImGui::Text("is Grounded: %s", m_Grounded ? "true" : "false");
|
||||
ImGui::Text("Did just dig right: %s", m_DidJustDigRight ? "true" : "false");
|
||||
bool test = !utils::isKeyPressed(SDL_SCANCODE_H);
|
||||
ImGui::Text("Is Key Up H: %s", test ? "true" : "false");
|
||||
ImGui::Checkbox("Draw Collision Rect", &m_DrawCollisionRect);
|
||||
|
||||
std::string currentState{};
|
||||
switch (m_State) {
|
||||
case PlayerState::Idle:
|
||||
currentState = "Idle";
|
||||
break;
|
||||
case PlayerState::Digging:
|
||||
currentState = "Digging";
|
||||
break;
|
||||
case PlayerState::Walking:
|
||||
currentState = "Walking";
|
||||
break;
|
||||
}
|
||||
ImGui::Text("Player State %s", currentState.c_str());
|
||||
|
||||
|
||||
//ContactMap
|
||||
ImGui::Text("ContactMap:");
|
||||
ImGui::Text("Top: %s", m_ContactMap[Collision::CollisionDirection::Top] != nullptr ? "true" : "false");
|
||||
@@ -56,132 +69,139 @@ void Player::ProcessImGui() {
|
||||
|
||||
void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
|
||||
m_Vel = Point2f { 0, -100 };
|
||||
|
||||
//check for keys
|
||||
if (utils::isKeyDown(SDL_SCANCODE_W)) {
|
||||
m_Vel.y = 100;
|
||||
if(m_State != PlayerState::Digging) {
|
||||
m_Vel = Point2f { 0, -100 };
|
||||
if (utils::isKeyDown(SDL_SCANCODE_W)) {
|
||||
m_Vel.y = 100;
|
||||
m_Grounded = false;
|
||||
}
|
||||
if (utils::isKeyPressed(SDL_SCANCODE_S)) {
|
||||
m_Vel.y = -100;
|
||||
if (m_Grounded) {
|
||||
if (m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
|
||||
//Do the digging
|
||||
m_State = PlayerState::Digging;
|
||||
//Set the digging location in the center of the destination tile;
|
||||
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom];
|
||||
m_DigDestination = tile->GetPosition() + tile->GetSize() / 2;
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom]->SetTileType(Tiles::AIR);
|
||||
//center of tile
|
||||
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
||||
m_Position = Point2f { tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2 + 5 };
|
||||
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (utils::isKeyDown(SDL_SCANCODE_A)) {
|
||||
m_walkAnimation->SetFlipped(false);
|
||||
m_Vel.x = -100;
|
||||
}
|
||||
if (utils::isKeyDown(SDL_SCANCODE_D)) {
|
||||
m_Vel.x = 100;
|
||||
m_walkAnimation->SetFlipped(true);
|
||||
if (m_Grounded && !m_DidJustDigRight) {
|
||||
//Check if the player doesnt come from digging a tile
|
||||
if (m_ContactMap[Collision::CollisionDirection::Right] != nullptr) {
|
||||
m_ContactMap[Collision::CollisionDirection::Right]->SetTileType(Tiles::AIR);
|
||||
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Right];
|
||||
//center of tile
|
||||
const Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
||||
m_Position = Point2f { tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2 + 5 };
|
||||
|
||||
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
|
||||
m_DidJustDigRight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_DidJustDigRight) {
|
||||
if (!utils::isKeyDown(SDL_SCANCODE_D)) {
|
||||
m_DidJustDigRight = false;
|
||||
}
|
||||
}
|
||||
|
||||
m_walkAnimation->Update(elapsedTime);
|
||||
|
||||
|
||||
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
|
||||
m_ContactMap[Collision::CollisionDirection::Left] = nullptr;
|
||||
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
|
||||
m_Grounded = false;
|
||||
}
|
||||
if (utils::isKeyPressed(SDL_SCANCODE_S)) {
|
||||
m_Vel.y = -100;
|
||||
if (m_Grounded) {
|
||||
if (m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom]->SetTileType(Tiles::AIR);
|
||||
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom];
|
||||
//center of tile
|
||||
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
||||
m_Position = Point2f { tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2 + 5 };
|
||||
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (utils::isKeyDown(SDL_SCANCODE_A)) {
|
||||
m_walkAnimation->SetFlipped(false);
|
||||
m_Vel.x = -100;
|
||||
}
|
||||
if (utils::isKeyDown(SDL_SCANCODE_D)) {
|
||||
m_Vel.x = 100;
|
||||
m_walkAnimation->SetFlipped(true);
|
||||
if (m_Grounded && !m_DidJustDigRight) {
|
||||
//Check if the player doesnt come from digging a tile
|
||||
if (m_ContactMap[Collision::CollisionDirection::Right] != nullptr) {
|
||||
m_ContactMap[Collision::CollisionDirection::Right]->SetTileType(Tiles::AIR);
|
||||
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Right];
|
||||
//center of tile
|
||||
const Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
||||
m_Position = Point2f { tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2 + 5 };
|
||||
|
||||
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
|
||||
m_DidJustDigRight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_DidJustDigRight) {
|
||||
if (!utils::isKeyDown(SDL_SCANCODE_D)) {
|
||||
m_DidJustDigRight = false;
|
||||
}
|
||||
}
|
||||
|
||||
m_walkAnimation->Update(elapsedTime);
|
||||
|
||||
|
||||
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
|
||||
m_ContactMap[Collision::CollisionDirection::Left] = nullptr;
|
||||
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
|
||||
m_Grounded = false;
|
||||
float t = 0, min_t = INFINITY;
|
||||
Point2f intersectionPoint, normal;
|
||||
|
||||
std::vector<std::pair<int, float>> contactTimes {};
|
||||
|
||||
float t = 0, min_t = INFINITY;
|
||||
Point2f intersectionPoint, normal;
|
||||
const WorldGridManager& gridManager = level.GetGridManager();
|
||||
|
||||
std::vector<std::pair<int, float>> contactTimes {};
|
||||
|
||||
const WorldGridManager& gridManager = level.GetGridManager();
|
||||
|
||||
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
WorldTile* tile = gridManager.GetTileAtIndex(x, y);
|
||||
if (*tile->GetTileType() != Tiles::AIR) {
|
||||
tile->m_Hightlight = false;
|
||||
if (Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, tile->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||
contactTimes.emplace_back(std::pair<int, float> { x + y * WORLD_WIDTH, t });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b)
|
||||
{
|
||||
return a.second < b.second;
|
||||
});
|
||||
|
||||
for (std::pair<int, float> contact_time : contactTimes) {
|
||||
int x = contact_time.first % WORLD_WIDTH;
|
||||
int y = contact_time.first / WORLD_WIDTH;
|
||||
WorldTile* world_tile = gridManager.GetTileAtIndex(x, y);
|
||||
|
||||
const Point2f WorldTilePos = world_tile->GetCollisionRect().getCollisionRect().pos;
|
||||
const Point2f WorldTileSize = world_tile->GetCollisionRect().getCollisionRect().size;
|
||||
|
||||
if (WorldTilePos.y + WorldTileSize.y > m_Position.y) {
|
||||
if (WorldTilePos.x + WorldTileSize.x > m_Position.x) {
|
||||
if (WorldTilePos.y + WorldTileSize.y / 2 > m_Position.y && m_Position.y + m_Size.y / 2 > WorldTilePos.y) {
|
||||
//Right of player
|
||||
m_ContactMap[Collision::CollisionDirection::Right] = world_tile;
|
||||
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
WorldTile* tile = gridManager.GetTileAtIndex(x, y);
|
||||
if (*tile->GetTileType() != Tiles::AIR) {
|
||||
tile->m_Hightlight = false;
|
||||
if (Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, tile->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||
contactTimes.emplace_back(std::pair<int, float> { x + y * WORLD_WIDTH, t });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (WorldTilePos.y + WorldTileSize.y > m_Position.y) {
|
||||
if (WorldTilePos.x < m_Position.x + m_Size.x) {
|
||||
if (WorldTilePos.y + WorldTileSize.y / 2 > m_Position.y && m_Position.y + m_Size.y / 2 > WorldTilePos.y) {
|
||||
//Left of player
|
||||
m_ContactMap[Collision::CollisionDirection::Left] = world_tile;
|
||||
std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b)
|
||||
{
|
||||
return a.second < b.second;
|
||||
});
|
||||
|
||||
for (std::pair<int, float> contact_time : contactTimes) {
|
||||
int x = contact_time.first % WORLD_WIDTH;
|
||||
int y = contact_time.first / WORLD_WIDTH;
|
||||
WorldTile* world_tile = gridManager.GetTileAtIndex(x, y);
|
||||
|
||||
const Point2f WorldTilePos = world_tile->GetCollisionRect().getCollisionRect().pos;
|
||||
const Point2f WorldTileSize = world_tile->GetCollisionRect().getCollisionRect().size;
|
||||
|
||||
if (WorldTilePos.y + WorldTileSize.y > m_Position.y) {
|
||||
if (WorldTilePos.x + WorldTileSize.x > m_Position.x) {
|
||||
if (WorldTilePos.y + WorldTileSize.y / 2 > m_Position.y && m_Position.y + m_Size.y / 2 > WorldTilePos.y) {
|
||||
//Right of player
|
||||
m_ContactMap[Collision::CollisionDirection::Right] = world_tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Below the player
|
||||
if (WorldTilePos.y + WorldTileSize.y <= m_Position.y) {
|
||||
if (WorldTilePos.x + WorldTileSize.x / 2 > m_Position.x && m_Position.x + m_Size.x / 2 > WorldTilePos.x) {
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||
m_Grounded = true;
|
||||
world_tile->m_Hightlight = true;
|
||||
if (WorldTilePos.y + WorldTileSize.y > m_Position.y) {
|
||||
if (WorldTilePos.x < m_Position.x + m_Size.x) {
|
||||
if (WorldTilePos.y + WorldTileSize.y / 2 > m_Position.y && m_Position.y + m_Size.y / 2 > WorldTilePos.y) {
|
||||
//Left of player
|
||||
m_ContactMap[Collision::CollisionDirection::Left] = world_tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Below the player
|
||||
if (WorldTilePos.y + WorldTileSize.y <= m_Position.y) {
|
||||
if (WorldTilePos.x + WorldTileSize.x / 2 > m_Position.x && m_Position.x + m_Size.x / 2 > WorldTilePos.x) {
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||
m_Grounded = true;
|
||||
world_tile->m_Hightlight = true;
|
||||
}
|
||||
}
|
||||
|
||||
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
|
||||
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);
|
||||
}
|
||||
if(m_State != PlayerState::Digging) { //Fix for when the state is JUST set to digging
|
||||
if (m_Vel.x != 0.0f) {
|
||||
m_State = PlayerState::Walking;
|
||||
}
|
||||
else {
|
||||
m_State = PlayerState::Idle;
|
||||
}
|
||||
}
|
||||
|
||||
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
|
||||
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);
|
||||
}
|
||||
|
||||
if (m_Vel.x != 0.0f) {
|
||||
m_State = PlayerState::Walking;
|
||||
}
|
||||
else {
|
||||
m_State = PlayerState::Idle;
|
||||
}
|
||||
|
||||
switch (m_State) {
|
||||
@@ -191,10 +211,14 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
case PlayerState::Walking:
|
||||
m_walkAnimation->SetPlaying(true);
|
||||
break;
|
||||
case PlayerState::Digging:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
m_Position = m_Position + m_Vel * elapsedTime;
|
||||
if(m_State != PlayerState::Digging) {
|
||||
m_Position = m_Position + m_Vel * elapsedTime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ private:
|
||||
Point2f m_Acc;
|
||||
Point2f m_Gravity { 0, -9.81f };
|
||||
|
||||
Point2f m_DigDestination{};
|
||||
|
||||
bool m_Grounded { false };
|
||||
|
||||
bool m_DidJustDigRight { false };
|
||||
|
||||
Reference in New Issue
Block a user