Rework Tile detection system

This commit is contained in:
Bram Verhulst
2024-03-28 18:53:36 +01:00
parent d441222173
commit 3b9c96ea8d
15 changed files with 252 additions and 41 deletions

Binary file not shown.

6
.idea/.idea.Motherload/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

169
.idea/.idea.Motherload/.idea/workspace.xml generated Normal file
View File

@@ -0,0 +1,169 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoGeneratedRunConfigurationManager">
<projectFile>Game/Game.vcxproj</projectFile>
</component>
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="26a0623a-44d5-441c-8048-32ff1dab3479" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/.name" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Engine/utils.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/utils.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Game.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Game.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Game.vcxproj" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Game.vcxproj" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Player.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/TextureManager.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/TextureManager.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldGridManager.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/WorldGridManager.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldLevel.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/WorldLevel.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldTile.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/WorldTile.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldTile.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/WorldTile.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Prog2Engine.sln" beforeDir="false" afterPath="$PROJECT_DIR$/Motherload.sln" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="HighlightingSettingsPerFile">
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectColorInfo">{
&quot;customColor&quot;: &quot;&quot;,
&quot;associatedIndex&quot;: 1
}</component>
<component name="ProjectId" id="2eJKJUJtMhvjVjJ1bs1T5LdGwo0" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;C++ Project.Game.executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
},
&quot;keyToStringList&quot;: {
&quot;rider.external.source.directories&quot;: [
&quot;C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache&quot;,
&quot;C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache&quot;,
&quot;C:\\Users\\Bram\\AppData\\Local\\Symbols\\src&quot;
]
}
}</component>
<component name="RunManager">
<configuration name="Game" type="CppProject" factoryName="C++ Project">
<configuration_1>
<option name="CONFIGURATION" value="Debug" />
<option name="PLATFORM" value="x64" />
<option name="PROJECT_FILE_PATH" value="$PROJECT_DIR$/Game/Game.vcxproj" />
<option name="CURRENT_LAUNCH_PROFILE" value="Local" />
<option name="EXE_PATH" value="$(LocalDebuggerCommand)" />
<option name="PROGRAM_PARAMETERS" value="$(LocalDebuggerCommandArguments)" />
<option name="WORKING_DIRECTORY" value="$(LocalDebuggerWorkingDirectory)" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
</configuration_1>
<configuration_2>
<option name="CONFIGURATION" value="Debug" />
<option name="PLATFORM" value="x86" />
<option name="PROJECT_FILE_PATH" value="$PROJECT_DIR$/Game/Game.vcxproj" />
<option name="CURRENT_LAUNCH_PROFILE" value="Local" />
<option name="EXE_PATH" value="$(LocalDebuggerCommand)" />
<option name="PROGRAM_PARAMETERS" value="$(LocalDebuggerCommandArguments)" />
<option name="WORKING_DIRECTORY" value="$(LocalDebuggerWorkingDirectory)" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
</configuration_2>
<configuration_3>
<option name="CONFIGURATION" value="Release" />
<option name="PLATFORM" value="x64" />
<option name="PROJECT_FILE_PATH" value="$PROJECT_DIR$/Game/Game.vcxproj" />
<option name="CURRENT_LAUNCH_PROFILE" value="Local" />
<option name="EXE_PATH" value="$(LocalDebuggerCommand)" />
<option name="PROGRAM_PARAMETERS" value="$(LocalDebuggerCommandArguments)" />
<option name="WORKING_DIRECTORY" value="$(LocalDebuggerWorkingDirectory)" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
</configuration_3>
<configuration_4>
<option name="CONFIGURATION" value="Release" />
<option name="PLATFORM" value="x86" />
<option name="PROJECT_FILE_PATH" value="$PROJECT_DIR$/Game/Game.vcxproj" />
<option name="CURRENT_LAUNCH_PROFILE" value="Local" />
<option name="EXE_PATH" value="$(LocalDebuggerCommand)" />
<option name="PROGRAM_PARAMETERS" value="$(LocalDebuggerCommandArguments)" />
<option name="WORKING_DIRECTORY" value="$(LocalDebuggerWorkingDirectory)" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
</configuration_4>
<option name="DEFAULT_PROJECT_PATH" value="$PROJECT_DIR$/Game/Game.vcxproj" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="26a0623a-44d5-441c-8048-32ff1dab3479" name="Changes" comment="" />
<created>1711617171269</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1711617171269</updated>
<workItem from="1711617171845" duration="39000" />
<workItem from="1711626582968" duration="6190000" />
<workItem from="1711641119746" duration="813000" />
<workItem from="1711642495862" duration="258000" />
<workItem from="1711647566340" duration="378000" />
<workItem from="1711647993177" duration="357000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VcsManagerConfiguration">
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
</component>
</project>

BIN
Assets/PlayerWalk.aseprite Normal file

Binary file not shown.

BIN
Assets/PlayerWalk.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -757,11 +757,8 @@ int utils::randRange(int min, int max) {
}
bool utils::isKeyDown(int keycode) {
const Uint8* pStates = SDL_GetKeyboardState(nullptr);
if (pStates != nullptr)
{
if(pStates[keycode]) {
return true;
}
if (pStates != nullptr) {
return pStates[keycode];
}
return false;
}
@@ -771,8 +768,10 @@ bool utils::isKeyPressed(int keycode) {
if (pStates != nullptr) {
if(pStates[keycode] && !s_KeyStates[keycode]) {
s_KeyStates[keycode] = true;
return s_KeyStates[keycode];
}else {
s_KeyStates[keycode] = false;
}
return s_KeyStates[keycode];
}
return false;
}

View File

@@ -47,8 +47,6 @@ void Game::Update(float elapsedSec) {
void Game::Draw() const {
utils::ClearBackground(Color4f(0.0f, 0.0f, 0.3f, 1.0f));
m_WorldLevel.Draw();
TextureManager::GetInstance()->GetTexture("dingus.png")->Draw(Point2f{0,0}, {}, true);
TextureManager::GetInstance()->GetTexture("dingus.png")->Draw(Point2f{0,300}, {}, false);
}
void Game::ProcessKeyDownEvent(const SDL_KeyboardEvent& e) {
@@ -76,7 +74,7 @@ void Game::ProcessMouseUpEvent(const SDL_MouseButtonEvent& e) {
//case SDL_BUTTON_LEFT:
// std::cout << " left button " << std::endl;
// break;
//case SDL_BUTTON_RIGHT:
//case SDL_BUTTON_RIGHT:k
// std::cout << " right button " << std::endl;
// break;
//case SDL_BUTTON_MIDDLE:

View File

@@ -315,9 +315,6 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s</Command>
<ClInclude Include="WorldLevel.h" />
<ClInclude Include="WorldTile.h" />
</ItemGroup>
<ItemGroup>
<Folder Include="Level\" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@@ -67,7 +67,6 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
}
}
}
if(utils::isKeyDown(SDL_SCANCODE_A)) {
@@ -93,7 +92,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if(m_DidJustDigRight) {
if(!utils::isKeyDown(SDL_SCANCODE_D)) {
m_DidJustDigRight = false;
}
}
}
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
@@ -112,9 +111,10 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
for(int y { 0 }; y < WORLD_HEIGHT; ++y) {
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == Tiles::DIRT) {
gridManager.GetTileAtIndex(x,y)->m_Hightlight = false;
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
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.push_back(std::pair<int, float>{x + y * WORLD_WIDTH, t});
}
}

View File

@@ -15,6 +15,4 @@ Texture* TextureManager::GetTexture(const std::string& name) {
Texture* pTexture = new Texture(name);
m_Textures[name] = pTexture;
return pTexture;
}
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include <array>
#include <vector>
#include "structs.h"
@@ -11,6 +12,19 @@ static const int TILE_HEIGHT = 50;
class WorldTile;
struct surroundingTiles {
std::array<WorldTile*, 8> tiles;
//Center of the surrounding tiles is 0, 0
void SetTile(int x, int y, WorldTile* tile) {
tiles[x + y * 3] = tile;
}
WorldTile* GetTile(int x, int y) {
return tiles[x + y * 3];
}
};
class WorldGridManager
{
public:

View File

@@ -23,8 +23,23 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport):
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
int actualX = x - WORLD_WIDTH / 2;
Point2f pos = Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT};
GroundTileType type = rand() % 2 == 0 ? Tiles::DIRT : Tiles::DIRT;
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, Tiles::DIRT, TextureManager::GetInstance() });
GroundTileType* type = Tiles::AIR;
switch(utils::randRange(0,2)) {
case 0:
type = Tiles::DIRT;
break;
case 1:
type = Tiles::IRON;
break;
case 2:
//AIR
break;
default:
std::cout << "??" << std::endl;
}
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, type, TextureManager::GetInstance() });
}
}
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
@@ -92,7 +107,7 @@ 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::DIRT) {
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);

View File

@@ -6,16 +6,16 @@
#include "utils.h"
WorldTile::WorldTile(const Point2f& position, GroundTileType groundTileType, TextureManager* pTextureManager) : m_Position { position }, m_GroundTileType { groundTileType } {
WorldTile::WorldTile(const Point2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager) : m_Position { position }, m_GroundTileType { groundTileType } {
// const std::string dirtPath = + "tiles/dirt/dirt" + std::to_string(utils::randRange(1, 5)) + ".png";
// m_pTexture = new Texture(dirtPath);
m_pTexture = pTextureManager->GetTexture(groundTileType.getPath());
m_pTexture = pTextureManager->GetTexture(groundTileType->getPath());
}
WorldTile::~WorldTile() {
delete m_pTexture;
}
void WorldTile::Draw() const {
if (m_GroundTileType == Tiles::DIRT) {
if (*m_GroundTileType != Tiles::AIR) {
m_pTexture->Draw(m_Position);
if(m_Hightlight) {
utils::SetColor(Colors::GREEN);

View File

@@ -24,16 +24,20 @@ public:
return m_type != rhs.m_type;
}
bool operator== (const GroundTileType* rhs) const {
virtual bool operator== (const GroundTileType* rhs) const {
return rhs->m_type == m_type;
}
bool operator!= (const GroundTileType* rhs) const {
virtual bool operator!= (const GroundTileType* rhs) const {
return rhs->m_type != m_type;
}
virtual std::string getPath() {
virtual std::string getPath() const {
return m_filePath;
}
virtual GroundTileTypes getType() const {
return m_type;
}
protected:
std::string m_filePath;
private:
@@ -42,37 +46,48 @@ private:
class RandomGroundTile: public GroundTileType {
public:
RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(filePath, type), m_variant(utils::randRange(1, maxRandom)) {
RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(filePath, type), m_maxRandom(maxRandom) {
}
~RandomGroundTile() override = default;
std::string getPath() override {
bool operator== (const GroundTileType* rhs) const override{
return rhs->getType() == this->getType();
}
bool operator!= (const GroundTileType* rhs) const override{
return rhs->getType() != this->getType();
}
std::string getPath() const override {
int variant = utils::randRange(1, m_maxRandom);
std::string toReplace{ "[0]" };
std::string replacement = std::to_string(m_variant);
std::string replacement = std::to_string(utils::randRange(1, m_maxRandom));
size_t found = m_filePath.find(toReplace);
std::string newFilePath{ m_filePath };
std::string newFilePath{ m_filePath };
if(found != std::string::npos) {
newFilePath.replace(found, 3, replacement);
}
return newFilePath;
}
private:
int m_variant;
int m_maxRandom;
};
namespace Tiles
{
static GroundTileType AIR = GroundTileType("", GroundTileTypes::Air);
static GroundTileType DIRT = RandomGroundTile("tiles/dirt/dirt[0].png", GroundTileTypes::Dirt, 6);
static GroundTileType IRON = GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
static GroundTileType* AIR = new GroundTileType("", GroundTileTypes::Air);
static GroundTileType* DIRT = new RandomGroundTile("tiles/dirt/dirt[0].png", GroundTileTypes::Dirt, 5);
static GroundTileType* IRON = new GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
}
class WorldTile {
public:
WorldTile() = default;
WorldTile(const Point2f& position, GroundTileType groundTileType, TextureManager* pTextureManager);
WorldTile(const Point2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager);
~WorldTile();
void Draw() const;
@@ -82,8 +97,8 @@ public:
Point2f GetSize() const { return Point2f{ 50, 50 }; }
GroundTileType GetTileType() const { return m_GroundTileType; }
void SetTileType(GroundTileType type) { m_GroundTileType = type; }
GroundTileType* GetTileType() const { return m_GroundTileType; }
void SetTileType(GroundTileType* type) { m_GroundTileType = type; }
Collision::TileCollisionRect GetCollisionRect();
@@ -92,7 +107,7 @@ public:
private:
Point2f m_Position;
GroundTileType m_GroundTileType;
GroundTileType* m_GroundTileType;
Texture* m_pTexture;