[Added] Start of player class, start of collision detection

This commit is contained in:
Ninjaws
2024-12-10 19:26:48 +01:00
parent 97d93ab113
commit 774dc3806b
18 changed files with 433 additions and 50 deletions
+79 -2
View File
@@ -4,11 +4,13 @@
#include <sstream>
#include <iostream>
#include "services/AssetService.h"
#include "entities/Map.h"
Map::Map()
{
loadTileset();
loadMap();
loadColMap();
}
void Map::loadMap()
@@ -78,11 +80,53 @@ void Map::loadMap()
}
}
void Map::loadColMap()
{
std::vector<int> tempColMap;
std::ifstream openfile(AssetService::getInstance().getMapUrl(EMap::COLLISION));
colMap.clear();
if (openfile.is_open())
{
while (!openfile.eof())
{
std::string str;
std::getline(openfile, str);
std::stringstream stream(str);
std::string value;
while (std::getline(stream, value, ' '))
{
if (value.length() > 0)
{
int tempValue = atoi(value.c_str());
tempColMap.push_back(tempValue);
}
}
colMap.push_back(tempColMap);
tempColMap.clear();
}
}
}
void Map::loadTileset()
{
this->tileset = AssetService::getInstance().loadTexture("Textures/tileset.png");
}
std::unordered_map<ECorner, Vector2> Map::cornersToTilePos(std::unordered_map<ECorner, Vector2> corners)
{
// std::cout << "Starting converting" << std::endl;
std::unordered_map<ECorner, Vector2> cornersTilePos;
for (const auto &pair : corners)
{
// std::cout << "Foreach pair" << std::endl;
Vector2 original = corners[pair.first];
cornersTilePos[pair.first] = {floorf(original.x / this->tileSize), floorf(original.y / this->tileSize)};
}
return cornersTilePos;
}
void Map::draw()
{
for (int i = 0; i < map.size(); i++)
@@ -92,8 +136,8 @@ void Map::draw()
if (map[i][j].x != -1 && map[i][j].y != -1)
{
// Rectangle source = {0,0,32,32};
Rectangle sourceRect = {map[i][j].x * tileSize, map[i][j].y * tileSize, tileSize, tileSize};
DrawTextureRec(tileset, sourceRect, {j * tileSize, i * tileSize}, WHITE);
Rectangle sourceRect = {map[i][j].x * tileSize, map[i][j].y * tileSize, (float)tileSize, (float)tileSize};
DrawTextureRec(tileset, sourceRect, {(float)j * tileSize, (float)i * tileSize}, WHITE);
// tileSprites.setPosition(j * tileSize, i * tileSize);
// tileSprites.setTextureRect(sf::IntRect(map[i][j].x * tileSize, map[i][j].y * tileSize, tileSize, tileSize));
// GetWindow().draw(tileSprites);
@@ -101,3 +145,36 @@ void Map::draw()
}
}
}
std::unordered_map<ECorner, bool> Map::checkForCollision(TextureWrapper texture)
{
// std::cout << "Converting.." << std::endl;
std::unordered_map<ECorner, Vector2> cornersTilePos = cornersToTilePos(texture.getCorners());
// std::cout << "Done with converting" << std::endl;
std::unordered_map<ECorner, bool> cornerCollisions;
for (const auto &pair : cornersTilePos)
{
// std::cout << "foreach collision check" << std::endl;
Vector2 pos = cornersTilePos[pair.first];
// std::cout << pos.x << " " << pos.y << std::endl;
if(colMap[pos.y][pos.x] == 1) {
cornerCollisions[pair.first] = true;
} else {
cornerCollisions[pair.first] = false;
}
}
// std::cout << "Finished collision checks" << std::endl;
return cornerCollisions;
// for (int i = 0; i < tiles.size(); i++)
// {
// if (colMap[tiles[i].y][tiles[i].x] == 1)
// {
// timesCollision.push_back(i);
// }
// }
}
+15
View File
@@ -28,6 +28,21 @@ void AssetService::setAssets()
maps[EMap::TEXTURE] = "Maps/map1.txt";
maps[EMap::COLLISION] = "Maps/colmap1.txt";
sprites[ESprite::PLAYER] = loadTexture("Textures/player.png");
sprites[ESprite::PLAYER_GUN_LEFT] = loadTexture("Textures/gunleft.png");
sprites[ESprite::PLAYER_GUN_RIGHT] = loadTexture("Textures/gunright.png");
sprites[ESprite::ENEMY] = loadTexture("Textures/enemy.png");
sprites[ESprite::ENEMY_GUN_LEFT] = loadTexture("Textures/gunleft_2.png");
sprites[ESprite::ENEMY_GUN_RIGHT] = loadTexture("Textures/gunright_2.png");
sprites[ESprite::CRATE] = loadTexture("Textures/woodencrate.png");
sprites[ESprite::JELLY] = loadTexture("Textures/happyjelly.png");
sprites[ESprite::HEALTHPACK] = loadTexture("Textures/FirstAidKit.png");
sprites[ESprite::AMMOPACK] = loadTexture("Textures/Bullets_small.png");
sprites[ESprite::POWERUP] = loadTexture("Textures/powerUpAnimation.png");
sprites[ESprite::HUD_HEART] = loadTexture("Textures/heart.png");
sprites[ESprite::HUD_HEART_EMPTY] = loadTexture("Textures/heart_empty.png");
sprites[ESprite::HUD_AMMO] = loadTexture("Textures/bullet_hud.png");
}
Font AssetService::getFont(EFont font, int fontSize)
-9
View File
@@ -89,13 +89,4 @@ void StateService::step()
handleMusic();
currentScreen->draw();
currentScreen->logic();
// if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
// {
// setScreen(EScreen::MAIN);
// }
// if(instance) {
// instance->handleMusic();
// instance->currentScreen->draw();
// instance->currentScreen->logic();
// }
}