[Added] Start of player class, start of collision detection
This commit is contained in:
+17
-2
@@ -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
@@ -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
|
||||
@@ -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,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
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user