Zad2 final v2 - fix collision
This commit is contained in:
parent
4960cdb39b
commit
d2556a6579
@ -143,6 +143,7 @@
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>vcpkg_installed\x64-windows\x64-windows\include\SDL2</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
|
@ -135,7 +135,7 @@ namespace KapitanGame {
|
||||
switch (line[i])
|
||||
{
|
||||
case '#':
|
||||
Objects.emplace_back(std::make_shared<KWall>(position, Textures["wall.bmp"]));
|
||||
Objects.emplace(std::make_pair(static_cast<int>(i), y), std::make_shared<KWall>(position, Textures["wall.bmp"]));
|
||||
break;
|
||||
case ' ':
|
||||
FreePositions.emplace_back(position);
|
||||
@ -157,8 +157,7 @@ namespace KapitanGame {
|
||||
{
|
||||
Pawns.clear();
|
||||
std::shuffle(FreePositions.begin(), FreePositions.end(), Utils::GetRandomEngine());
|
||||
Objects.emplace_back(std::make_shared<KExit>(FreePositions.back(), Textures["exit.bmp"]));
|
||||
Exit = Objects.back();
|
||||
Exit = Objects.emplace(std::make_pair(static_cast<int>(FreePositions.back().X / Constants::TILE_WIDTH), static_cast<int>(FreePositions.back().Y / Constants::TILE_HEIGHT)), std::make_shared<KExit>(FreePositions.back(), Textures["exit.bmp"])).first->second;
|
||||
FreePositions.pop_back();
|
||||
|
||||
Pawns.emplace_back(std::make_shared<KCirclePawn>(FreePositions.back(), Textures["P1.bmp"], PlayerControllers[static_cast<int>(KPlayer::Player1)]));
|
||||
@ -383,7 +382,7 @@ namespace KapitanGame {
|
||||
|
||||
if (Playing) {
|
||||
for (const auto& object : Objects)
|
||||
object->Render(Renderer, cameraInUse);
|
||||
object.second->Render(Renderer, cameraInUse);
|
||||
for (const auto& pawn : Pawns) {
|
||||
pawn->Render(Renderer, cameraInUse);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#include "KFont.h"
|
||||
#include "KInput.h"
|
||||
#include "Utils.h"
|
||||
|
||||
|
||||
namespace KapitanGame {
|
||||
@ -29,7 +30,7 @@ namespace KapitanGame {
|
||||
|
||||
SDL_Renderer* Renderer = nullptr;
|
||||
|
||||
std::vector<std::shared_ptr<KObject>> Objects;
|
||||
std::unordered_map<std::pair<int, int>, std::shared_ptr<KObject>, Utils::PairHash> Objects;
|
||||
std::vector<std::shared_ptr<KPawn>> Pawns;
|
||||
std::vector<std::shared_ptr<KPlayerController>> PlayerControllers;
|
||||
std::unordered_map<std::string, KTexture> Textures;
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Constants.h"
|
||||
#include "KPlayerController.h"
|
||||
@ -9,10 +10,17 @@
|
||||
|
||||
namespace KapitanGame
|
||||
{
|
||||
void KPawn::CollisionDetectionStep(const std::vector<std::shared_ptr<KObject>>& objects)
|
||||
void KPawn::CollisionDetectionStep(const std::unordered_map<std::pair<int, int>, std::shared_ptr<KObject>, Utils::PairHash>& objects)
|
||||
{
|
||||
if (GetCollider() == nullptr) return;
|
||||
for (auto& other : objects) {
|
||||
|
||||
const auto xPos = static_cast<int>(GetPosition().X / Constants::TILE_WIDTH);
|
||||
const auto yPos = static_cast<int>(GetPosition().Y / Constants::TILE_HEIGHT);
|
||||
|
||||
for (const auto& [oX, oY] : std::initializer_list<std::pair<int, int>>{ {0, 0}, {-1,0}, {1,0}, {0,-1}, {0,1}, {-1,-1}, {1,1}, {1,-1}, {-1,1} })
|
||||
{
|
||||
try {
|
||||
const auto& other = objects.at({ xPos + oX, yPos + oY });
|
||||
if (other->GetCollider() == nullptr) continue;
|
||||
if (other->Id == Id) continue;
|
||||
if (GetCollider()->IsCollision(other->GetCollider())) {
|
||||
@ -26,6 +34,10 @@ namespace KapitanGame
|
||||
Position += separationVector;
|
||||
}
|
||||
}
|
||||
catch (std::out_of_range&)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void KPawn::MovementStep(const float& timeStep)
|
||||
|
@ -1,8 +1,9 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "KObject.h"
|
||||
#include "Utils.h"
|
||||
|
||||
namespace KapitanGame
|
||||
{
|
||||
@ -17,7 +18,7 @@ namespace KapitanGame
|
||||
PlayerController(playerController)
|
||||
{
|
||||
}
|
||||
void CollisionDetectionStep(const std::vector<std::shared_ptr<KObject>>& objects);
|
||||
void CollisionDetectionStep(const std::unordered_map<std::pair<int, int>, std::shared_ptr<KObject>, Utils::PairHash>& objects);
|
||||
void MovementStep(const float& timeStep);
|
||||
void CollisionDetectionWithMapStep(const SDL_FRect& map);
|
||||
void AddMovementInput(const KVector2D& input);
|
||||
|
@ -96,5 +96,14 @@ namespace KapitanGame {
|
||||
}
|
||||
|
||||
typedef Iterator<KPlayer, KPlayer::Player1, KPlayer::Player2> KPlayerIterator;
|
||||
|
||||
struct PairHash {
|
||||
template <class T1, class T2>
|
||||
std::size_t operator () (const std::pair<T1, T2>& p) const {
|
||||
auto h1 = std::hash<T1>{}(p.first);
|
||||
auto h2 = std::hash<T2>{}(p.second);
|
||||
return h1 ^ h2;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user