[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
+17 -2
View File
@@ -5,6 +5,9 @@
#include "entities/Background.h"
#include "services/StateService.h"
#include "services/AudioService.h"
#include "services/MapService.h"
#include "services/InputService.h"
#include "entities/Player.h"
class Game
{
@@ -24,6 +27,7 @@ public:
wrappers.push_back(wrapper3);
this->background = Background(wrappers);
this->map = Map();
MapService::getInstance().setMap(this->map);
Vector2 screenSize = StateService::getInstance().getScreenSize();
Camera2D camera = {0};
@@ -33,6 +37,11 @@ public:
camera.zoom = 1.0f;
this->camera = camera;
this->player = Player();
this->player.getTexture().setPosition({200,350});
InputService::getInstance().setKeysToWatch({KEY_SPACE, KEY_A, KEY_D}, {MOUSE_BUTTON_LEFT});
AudioService::getInstance().setMusic(EMusic::GAME);
AudioService::getInstance().playMusic();
}
@@ -42,7 +51,12 @@ public:
void logic()
{
this->camera.offset.x -= 1.5;
// this->camera.offset.x -= 1.5;
this->player.logic();
if(this->player.getTexture().getPosition().x > (StateService::getInstance().getScreenSize().x / 2.0f)) {
this->camera.offset = {(StateService::getInstance().getScreenSize().x / 2.0f) +((StateService::getInstance().getScreenSize().x / 2.0f) - this->player.getTexture().getPosition().x), StateService::getInstance().getScreenSize().y / 2.0f};
}
}
void draw()
@@ -52,6 +66,7 @@ public:
this->background.draw();
this->map.draw();
this->player.draw();
EndMode2D();
EndDrawing();
@@ -61,7 +76,7 @@ private:
Camera2D camera;
Background background;
Map map;
// Player player
Player player;
// std::vector<Enemy> enemies
// std::vector<Create> crates
// std::vector<PBullet> playerBullets
+13 -3
View File
@@ -3,6 +3,8 @@
#include <vector>
#include "raylib.h"
#include "entities/TextureWrapper.h"
#include <cmath>
/**
* Holds the collision and texture maps for the game world
@@ -15,19 +17,27 @@ public:
void draw();
/** Checks and returns which corners of the object have been collided with */
std::unordered_map<ECorner, bool> checkForCollision(TextureWrapper texture);
private:
/** Holds the number for every single tile, which refers to a certain sprite in the tileset */
std::vector<std::vector<Vector2>> map;
/** Holds 1s (collision) and 0s (no collision) for every tile */
/** Holds 1s (collision) and 0s (no collision) for every tile */
std::vector<std::vector<int>> colMap;
float tileSize = 32;
unsigned int tileSize = 32;
Texture2D tileset;
void loadMap();
void loadColMap(){}
void loadColMap();
void loadTileset();
/**
* Converts the corners of the object to the matching tiles, so that they can be checked for collision
*/
std::unordered_map<ECorner, Vector2> cornersToTilePos(std::unordered_map<ECorner, Vector2> corners);
};
#endif
+166
View File
@@ -0,0 +1,166 @@
#ifndef PLAYER_H
#define PLAYER_H
#include "raylib.h"
#include <unordered_map>
#include "services/AssetService.h"
#include "services/MapService.h"
#include "services/InputService.h"
#include "entities/TextureWrapper.h"
#include "enums/ECorner.h"
class Player
{
public:
Player()
{
TextureWrapper wrapper = TextureWrapper(AssetService::getInstance().getSprite(ESprite::PLAYER), {32, 32}, {200, 200});
wrapper.setSourceRect({0, 0, 32, 32});
this->texture = wrapper;
}
~Player()
{
}
void logic()
{
gravity();
movement();
handleCollision();
applyVelocity();
}
void draw()
{
DrawTextureRec(this->texture.getTexture(), this->texture.getSourceRect(), this->texture.getPosition(), WHITE);
}
TextureWrapper &getTexture()
{
return this->texture;
}
protected:
private:
// Vector2 position;
TextureWrapper texture;
Vector2 velocity = {0, 0};
std::vector<unsigned int> timesCollision; // Holds the amount of times the player has collided with a tile, and which corner of the player is touching a tile
bool groundCollision; // Keeps track of whether or not the player has collided with the ground
bool crateTopCollision;
/**
* Holds the corners of the player
* Is used for precise collision detection
*/
// Keeps tracko f whether or not the player has collided with the top of the crate
// std::unordered_map<Vector2, >
void gravity()
{
// PositionUpdate();
// Collision();
// if (!groundCollision && !crateTopCollision)
// {
if (!groundCollision)
{
velocity.y += 0.6f;
}
else
{
velocity.y = 0.0f;
}
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && (groundCollision == true || crateTopCollision == true))
// {
// velocity.y = -GetJumpSpeed();
// }
// this->texture.move(velocity);
}
void movement()
{
velocity.x = 0.0f;
if (InputService::getInstance().isKeyDown(KEY_A)) {
// std::cout << "Left" << std::endl;
velocity.x -= 3.0f;
} else if (InputService::getInstance().isKeyDown(KEY_D)) {
velocity.x += 3.0f;
// std::cout << "Right" << std::endl;
}
if(InputService::getInstance().isKeyPressed(KEY_SPACE) && groundCollision) {
velocity.y -= 7.2f;
}
this->texture.move(velocity);
}
void handleCollision()
{
groundCollision = false;
std::unordered_map<ECorner, bool> collisions = MapService::getInstance().getMap().checkForCollision(this->texture);
if (collisions[ECorner::BOTTOM_LEFT] || collisions[ECorner::BOTTOM_RIGHT])
{
this->texture.move({0, -velocity.y});
velocity.y = 0.0f;
groundCollision = true;
}
if (collisions[ECorner::LEFT_TOP] && collisions[ECorner::LEFT_BOTTOM]) {
this->texture.move({+velocity.x, 0});
velocity.x = 0.0f;
}
if (collisions[ECorner::TOP_LEFT] || collisions[ECorner::TOP_RIGHT]) {
this->texture.move({0, +velocity.y});
velocity.y = 0.0f;
}
if (collisions[ECorner::RIGHT_TOP] && collisions[ECorner::RIGHT_BOTTOM]) {
this->texture.move({-velocity.x, 0});
velocity.x = 0.0f;
}
}
void applyVelocity() {
// std::cout << velocity.x << std::endl;
this->texture.move(velocity);
}
// timesCollision.clear();
// for (int i = 0; i < tiles.size(); i++)
// {
// if (GetColMap()[tiles[i].y][tiles[i].x] == 1)
// {
// timesCollision.push_back(i);
// }
// }
// groundCollision = false;
// if (timesCollision.size() > 0)
// {
// for (int i = 0; i < timesCollision.size(); i++)
// {
// if (timesCollision[i] == 0 || timesCollision[i] == 1) //Topleft and Topright
// {
// velocity.y = 0;
// }
// else if (timesCollision[i] == 2 || timesCollision[i] == 3 || //Lefttop and Leftbottom
// timesCollision[i] == 4 || timesCollision[i] == 5) //Righttop and Lefttop
// {
// velocity.x = 0;
// }
// else if (timesCollision[i] == 6 || timesCollision[i] == 7) //Bottomleft and Bottomright
// {
// groundCollision = true;
// }
// }
// }
// }
};
#endif
+8 -8
View File
@@ -8,14 +8,6 @@
#include "services/AssetService.h"
class Text {
private:
std::string text;
Font font;
int fontSize;
int spacing;
Color color;
Vector2 position;
public:
Text(std::string text, EFont font, int fontSize = 30, int spacing = 0, Color color = WHITE, Vector2 position = {100,100});
~Text(){}
@@ -43,6 +35,14 @@ class Text {
}
void draw();
private:
std::string text;
Font font;
int fontSize;
int spacing;
Color color;
Vector2 position;
};
#endif
+31
View File
@@ -2,9 +2,12 @@
#define TEXTURE_WRAPPER_H
#include "raylib.h"
#include "enums/ECorner.h"
#include <unordered_map>
class TextureWrapper {
public:
TextureWrapper(){}
TextureWrapper(Texture2D texture, Vector2 size = {32,32}, Vector2 position = {0,0}, Rectangle sourceRect = {0,0,32,32}) {
this->texture = texture;
this->size = size;
@@ -34,6 +37,34 @@ class TextureWrapper {
this->sourceRect = rect;
}
void setPosition(Vector2 position) {
this->position = position;
}
void move(Vector2 deltaPos) {
this->position = {this->position.x + deltaPos.x, this->position.y + deltaPos.y};
}
/** Returns the 4 corners of the texture */
std::unordered_map<ECorner, Vector2> getCorners() {
std::unordered_map<ECorner, Vector2> corners;
float top = this->position.y;
float right = this->position.x+this->size.x;
float bottom = this->position.y + this->size.y;
float left = this->position.x;
float margin = 5; // Space to the corner
corners[ECorner::TOP_LEFT] = {left+margin, top};
corners[ECorner::TOP_RIGHT] = {right-margin, top};
corners[ECorner::RIGHT_TOP] = {right, top+margin};
corners[ECorner::RIGHT_BOTTOM] = {right, bottom-margin};
corners[ECorner::BOTTOM_RIGHT] = {right-margin, bottom};
corners[ECorner::BOTTOM_LEFT] = {left+margin, bottom};
corners[ECorner::LEFT_BOTTOM] = {left, bottom-margin};
corners[ECorner::LEFT_TOP] = {left, top+margin};
return corners;
}
private:
Texture2D texture;
Rectangle sourceRect;
+19
View File
@@ -0,0 +1,19 @@
#ifndef CORNERS_H
#define CORNERS_H
/**
* All corners of a GameObject
*/
enum class ECorner {
TOP_LEFT,
TOP_RIGHT,
RIGHT_TOP,
RIGHT_BOTTOM,
BOTTOM_RIGHT,
BOTTOM_LEFT,
LEFT_BOTTOM,
LEFT_TOP
};
#endif
+21
View File
@@ -0,0 +1,21 @@
#ifndef ESPRITE_H
#define ESPRITE_H
enum class ESprite {
PLAYER,
PLAYER_GUN_LEFT,
PLAYER_GUN_RIGHT,
ENEMY,
ENEMY_GUN_LEFT,
ENEMY_GUN_RIGHT,
CRATE,
JELLY,
HEALTHPACK,
AMMOPACK,
POWERUP,
HUD_HEART,
HUD_HEART_EMPTY,
HUD_AMMO
};
#endif
+3 -3
View File
@@ -6,13 +6,13 @@
class GameScreen: public Screen {
private:
Game game;
protected:
public:
GameScreen();
void logic() override;
void draw() override;
private:
Game game;
};
#endif
+6 -6
View File
@@ -6,6 +6,12 @@
#include "entities/Text.h"
class MainScreen : public Screen {
public:
MainScreen();
void logic() override;
void draw() override;
private:
int hoveredButton = 0;
std::vector<Text> buttons;
@@ -13,12 +19,6 @@ private:
void mouseLogic();
void keyboardLogic();
void clickLogic();
public:
MainScreen();
void logic() override;
void draw() override;
};
#endif
+3 -8
View File
@@ -3,14 +3,6 @@
class Screen
{
private:
protected:
// std::optional<Map> map;
// std::optional<Camera2D> camera;
// std::optional<Map> map;
// std::optional<Background> background;
// std::vector<Text> text;
public:
Screen(){}
~Screen() {}
@@ -23,6 +15,9 @@ public:
* Draw frame to the screen
*/
virtual void draw() = 0;
private:
};
#endif
+4 -4
View File
@@ -6,15 +6,15 @@
#include "entities/Text.h"
class TitleScreen : public Screen {
private:
Music bg;
std::vector<Text> text;
public:
TitleScreen();
void logic() override;
void draw() override;
private:
Music bg;
std::vector<Text> text;
};
#endif
+4 -5
View File
@@ -7,17 +7,16 @@
class TutorialScreen : public Screen
{
private:
std::vector<Text> text;
Background background;
protected:
public:
TutorialScreen();
~TutorialScreen() {}
void logic() override;
void draw() override;
private:
std::vector<Text> text;
Background background;
};
#endif
+5
View File
@@ -9,6 +9,7 @@
#include "enums/EMusic.h"
#include "enums/EBackground.h"
#include "enums/EMap.h"
#include "enums/ESprite.h"
#include "services/Service.h"
class AssetService: public Service<AssetService>
@@ -27,6 +28,9 @@ public:
std::string getMapUrl(EMap map) {
return this->assetDir + this->maps[map];
}
Texture2D getSprite(ESprite sprite) {
return this->sprites[sprite];
}
protected:
AssetService();
@@ -41,6 +45,7 @@ private:
std::unordered_map<EMusic, Music> music;
std::unordered_map<EBackground, Texture2D> backgrounds;
std::unordered_map<EMap, std::string> maps;
std::unordered_map<ESprite, Texture2D> sprites;
void setAssets();
+12
View File
@@ -20,11 +20,13 @@ public:
{
this->watchedKeys.clear();
this->keysPressed.clear();
this->keysDown.clear();
for (int i = 0; i < keysToWatch.size(); i++)
{
this->watchedKeys.insert(keysToWatch[i]);
this->keysPressed[keysToWatch[i]] = false;
this->keysDown[keysToWatch[i]] = false;
}
setMouseButtonsToWatch(mouseButtonsToWatch);
@@ -42,10 +44,15 @@ public:
for (auto key : watchedKeys)
{
keysPressed[key] = false;
keysDown[key] = false;
if (IsKeyPressed(key))
{
keysPressed[key] = true;
}
if (IsKeyDown(key))
{
keysDown[key] = true;
}
}
for (auto button : watchedMouseButtons)
{
@@ -62,6 +69,10 @@ public:
return keysPressed[key];
}
bool isKeyDown(KeyboardKey key) {
return keysDown[key];
}
bool isMouseButtonPressed(MouseButton button)
{
return mouseButtonsPressed[button];
@@ -70,6 +81,7 @@ public:
private:
std::unordered_set<KeyboardKey> watchedKeys;
std::unordered_map<KeyboardKey, bool> keysPressed;
std::unordered_map<KeyboardKey, bool> keysDown;
std::unordered_set<MouseButton> watchedMouseButtons;
std::unordered_map<MouseButton, bool> mouseButtonsPressed;
+27
View File
@@ -0,0 +1,27 @@
#ifndef MAP_SERVICE_H
#define MAP_SERVICE_H
#include "services/Service.h"
#include "entities/Map.h"
#include "entities/TextureWrapper.h"
/**
* Allows all rigidbodies to check against the current Map
*/
class MapService : public Service<MapService>
{
public:
void setMap(Map map)
{
this->map = Map();
}
Map& getMap() {
return this->map;
}
private:
Map map;
};
#endif
+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();
// }
}