[Added] Gun angling
This commit is contained in:
+86
-83
@@ -40,13 +40,13 @@ public:
|
|||||||
camera.target = (Vector2){screenSize.x / 2.0f, screenSize.y / 2.0f}; // Initially at the center of the world
|
camera.target = (Vector2){screenSize.x / 2.0f, screenSize.y / 2.0f}; // Initially at the center of the world
|
||||||
camera.rotation = 0.0f;
|
camera.rotation = 0.0f;
|
||||||
camera.zoom = 1.0f;
|
camera.zoom = 1.0f;
|
||||||
this->camera = camera;
|
// this->camera = camera;
|
||||||
|
GameService::getInstance().setCamera(camera);
|
||||||
|
|
||||||
this->player = Player();
|
this->player = Player();
|
||||||
this->player.getTexture().setPosition({200, 350});
|
this->player.setPosition({200, 350});
|
||||||
|
|
||||||
|
this->crosshair = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CROSSHAIR), {15, 15}, {0, 0}, {0, 0, 15, 15});
|
||||||
this->crosshair = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CROSSHAIR), {15,15}, {0,0}, {0,0,15,15});
|
|
||||||
|
|
||||||
initPauseScreen();
|
initPauseScreen();
|
||||||
initGameOverScreen();
|
initGameOverScreen();
|
||||||
@@ -55,10 +55,89 @@ public:
|
|||||||
AudioService::getInstance().setMusic(EMusic::GAME);
|
AudioService::getInstance().setMusic(EMusic::GAME);
|
||||||
AudioService::getInstance().playMusic();
|
AudioService::getInstance().playMusic();
|
||||||
}
|
}
|
||||||
|
|
||||||
~Game()
|
~Game()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void logic()
|
||||||
|
{
|
||||||
|
this->crosshair.setPosition(GetMousePosition());
|
||||||
|
|
||||||
|
if (GameService::getInstance().isPaused())
|
||||||
|
{
|
||||||
|
pausedLogic();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (GameService::getInstance().isGameOver())
|
||||||
|
{
|
||||||
|
gameOverLogic();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (InputService::getInstance().isKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
GameService::getInstance().setPaused(true);
|
||||||
|
AudioService::getInstance().pauseMusic();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// this->camera.offset.x -= 1.5;
|
||||||
|
this->player.logic();
|
||||||
|
if (this->player.getTexture().getPosition().x > (StateService::getInstance().getScreenSize().x / 2.0f))
|
||||||
|
{
|
||||||
|
GameService::getInstance().getCamera().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()
|
||||||
|
{
|
||||||
|
BeginDrawing();
|
||||||
|
BeginMode2D(GameService::getInstance().getCamera());
|
||||||
|
|
||||||
|
this->background.draw();
|
||||||
|
this->map.draw();
|
||||||
|
this->player.draw();
|
||||||
|
|
||||||
|
EndMode2D();
|
||||||
|
|
||||||
|
if (GameService::getInstance().isPaused())
|
||||||
|
{
|
||||||
|
drawPaused();
|
||||||
|
}
|
||||||
|
else if (GameService::getInstance().isGameOver())
|
||||||
|
{
|
||||||
|
drawGameOver();
|
||||||
|
}
|
||||||
|
drawCrosshair();
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Camera2D camera;
|
||||||
|
Background background;
|
||||||
|
Map map;
|
||||||
|
Player player;
|
||||||
|
TextureWrapper crosshair;
|
||||||
|
|
||||||
|
std::vector<Text> pauseScreenButtons;
|
||||||
|
int hoveredPauseScreenButton = 0;
|
||||||
|
|
||||||
|
Text gameOverText;
|
||||||
|
std::vector<Text> gameOverScreenButtons;
|
||||||
|
int hoveredGameOverScreenButton = 0;
|
||||||
|
|
||||||
|
// bool isPaused = false;
|
||||||
|
// bool isGameOver = false;
|
||||||
|
|
||||||
|
// std::vector<Enemy> enemies
|
||||||
|
// std::vector<Create> crates
|
||||||
|
// std::vector<PBullet> playerBullets
|
||||||
|
// std::vector<EBullet> enemyBullets
|
||||||
|
// std::vector<Jelly> jellies
|
||||||
|
// std::vector<Ammo> ammo
|
||||||
|
// std::vector<Healthpack> healthpacks
|
||||||
|
// Powerup powerup
|
||||||
|
|
||||||
void initPauseScreen()
|
void initPauseScreen()
|
||||||
{
|
{
|
||||||
const int TOP_GAP = 100;
|
const int TOP_GAP = 100;
|
||||||
@@ -106,35 +185,6 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void logic()
|
|
||||||
{
|
|
||||||
this->crosshair.setPosition(GetMousePosition());
|
|
||||||
|
|
||||||
if (GameService::getInstance().isPaused())
|
|
||||||
{
|
|
||||||
pausedLogic();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (GameService::getInstance().isGameOver())
|
|
||||||
{
|
|
||||||
gameOverLogic();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (InputService::getInstance().isKeyPressed(KEY_ENTER))
|
|
||||||
{
|
|
||||||
GameService::getInstance().setPaused(true);
|
|
||||||
AudioService::getInstance().pauseMusic();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 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 pausedLogic()
|
void pausedLogic()
|
||||||
{
|
{
|
||||||
if (InputService::getInstance().isKeyPressed(KEY_S) || InputService::getInstance().isKeyPressed(KEY_W))
|
if (InputService::getInstance().isKeyPressed(KEY_S) || InputService::getInstance().isKeyPressed(KEY_W))
|
||||||
@@ -191,29 +241,6 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw()
|
|
||||||
{
|
|
||||||
BeginDrawing();
|
|
||||||
BeginMode2D(this->camera);
|
|
||||||
|
|
||||||
this->background.draw();
|
|
||||||
this->map.draw();
|
|
||||||
this->player.draw();
|
|
||||||
|
|
||||||
EndMode2D();
|
|
||||||
|
|
||||||
if (GameService::getInstance().isPaused())
|
|
||||||
{
|
|
||||||
drawPaused();
|
|
||||||
}
|
|
||||||
else if (GameService::getInstance().isGameOver())
|
|
||||||
{
|
|
||||||
drawGameOver();
|
|
||||||
}
|
|
||||||
drawCrosshair();
|
|
||||||
EndDrawing();
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawPaused()
|
void drawPaused()
|
||||||
{
|
{
|
||||||
// 100, 100, 255, 60
|
// 100, 100, 255, 60
|
||||||
@@ -239,8 +266,9 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawCrosshair() {
|
void drawCrosshair()
|
||||||
DrawTextureEx(this->crosshair.getTexture(),GetMousePosition(),0,1.2f,RED);
|
{
|
||||||
|
DrawTextureEx(this->crosshair.getTexture(), GetMousePosition(), 0, 1.2f, RED);
|
||||||
// DrawTexturePro(
|
// DrawTexturePro(
|
||||||
// this->crosshair.getTexture(),
|
// this->crosshair.getTexture(),
|
||||||
// this->crosshair.getSourceRect(),
|
// this->crosshair.getSourceRect(),
|
||||||
@@ -249,31 +277,6 @@ public:
|
|||||||
// 0,
|
// 0,
|
||||||
// WHITE);
|
// WHITE);
|
||||||
}
|
}
|
||||||
private:
|
|
||||||
Camera2D camera;
|
|
||||||
Background background;
|
|
||||||
Map map;
|
|
||||||
Player player;
|
|
||||||
TextureWrapper crosshair;
|
|
||||||
|
|
||||||
std::vector<Text> pauseScreenButtons;
|
|
||||||
int hoveredPauseScreenButton = 0;
|
|
||||||
|
|
||||||
Text gameOverText;
|
|
||||||
std::vector<Text> gameOverScreenButtons;
|
|
||||||
int hoveredGameOverScreenButton = 0;
|
|
||||||
|
|
||||||
// bool isPaused = false;
|
|
||||||
// bool isGameOver = false;
|
|
||||||
|
|
||||||
// std::vector<Enemy> enemies
|
|
||||||
// std::vector<Create> crates
|
|
||||||
// std::vector<PBullet> playerBullets
|
|
||||||
// std::vector<EBullet> enemyBullets
|
|
||||||
// std::vector<Jelly> jellies
|
|
||||||
// std::vector<Ammo> ammo
|
|
||||||
// std::vector<Healthpack> healthpacks
|
|
||||||
// Powerup powerup
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -18,7 +18,7 @@ public:
|
|||||||
void draw();
|
void draw();
|
||||||
|
|
||||||
/** Checks and returns which corners of the object have been collided with */
|
/** Checks and returns which corners of the object have been collided with */
|
||||||
std::unordered_map<ECorner, bool> checkForCollision(TextureWrapper texture);
|
// std::unordered_map<ECorner, bool> checkForCollision(TextureWrapper texture);
|
||||||
|
|
||||||
int getTileSize() {
|
int getTileSize() {
|
||||||
return this->tileSize;
|
return this->tileSize;
|
||||||
@@ -45,7 +45,7 @@ private:
|
|||||||
/**
|
/**
|
||||||
* Converts the corners of the object to the matching tiles, so that they can be checked for collision
|
* 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);
|
// std::unordered_map<ECorner, Vector2> cornersToTilePos(std::unordered_map<ECorner, Vector2> corners);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+133
-74
@@ -16,11 +16,12 @@ class Player
|
|||||||
public:
|
public:
|
||||||
Player()
|
Player()
|
||||||
{
|
{
|
||||||
TextureWrapper wrapper = TextureWrapper(AssetService::getInstance().getSprite(ESprite::PLAYER), {32, 32}, {200, 200});
|
this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::PLAYER), {32, 32}, {200, 200});
|
||||||
wrapper.setSourceRect({0, 0, 32, 32});
|
|
||||||
this->texture = wrapper;
|
|
||||||
prevFrame = GetTime();
|
prevFrame = GetTime();
|
||||||
spriteAnimation = {0,0};
|
spriteAnimation = {0, 0};
|
||||||
|
this->gunRight = TextureWrapper(AssetService::getInstance().getSprite(ESprite::PLAYER_GUN_RIGHT), {58, 29}, {0, 0});
|
||||||
|
this->gunLeft = TextureWrapper(AssetService::getInstance().getSprite(ESprite::PLAYER_GUN_LEFT), {58, 29}, {0, 0});
|
||||||
}
|
}
|
||||||
~Player()
|
~Player()
|
||||||
{
|
{
|
||||||
@@ -28,14 +29,14 @@ public:
|
|||||||
|
|
||||||
void logic()
|
void logic()
|
||||||
{
|
{
|
||||||
// applyGravity();
|
rotateGun();
|
||||||
movement();
|
movement();
|
||||||
// handleCollision();
|
|
||||||
outofboundsCheck();
|
outofboundsCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw()
|
void draw()
|
||||||
{
|
{
|
||||||
|
drawGun();
|
||||||
DrawTextureRec(this->texture.getTexture(), this->texture.getSourceRect(), this->texture.getPosition(), WHITE);
|
DrawTextureRec(this->texture.getTexture(), this->texture.getSourceRect(), this->texture.getPosition(), WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,26 +45,35 @@ public:
|
|||||||
return this->texture;
|
return this->texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setPosition(Vector2 pos)
|
||||||
|
{
|
||||||
|
this->texture.setPosition(pos);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
private:
|
private:
|
||||||
TextureWrapper texture;
|
TextureWrapper texture;
|
||||||
|
TextureWrapper gunLeft;
|
||||||
|
TextureWrapper gunRight;
|
||||||
Vector2 velocity = {0, 0};
|
Vector2 velocity = {0, 0};
|
||||||
float jumpPower = 7.2f;
|
float jumpPower = 7.2f;
|
||||||
float moveSpeed = 5.0f;
|
float moveSpeed = 5.0f;
|
||||||
float gravity = 0.6f;
|
float gravity = 0.6f;
|
||||||
float animationDelay = 0.2f; // seconds
|
float animationDelay = 0.2f; // In seconds
|
||||||
double prevFrame;
|
double prevFrame;
|
||||||
Vector2 spriteAnimation; // Keeps track of (1) Sprite cycling and (2) Player direction
|
Vector2 spriteAnimation; // Keeps track of (1) Sprite cycling and (2) Player direction
|
||||||
enum Direction { Down, Left, Right, Up }; // Used in conjuction with spriteAnimation, makes sure sprite direction matches with player direction
|
enum Direction
|
||||||
std::vector <Vector2i> tiles; // Holds eight points of the player for accurate collision
|
{
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
Up
|
||||||
|
}; // Used in conjuction with spriteAnimation, makes sure sprite direction matches with player direction
|
||||||
|
|
||||||
|
std::vector<Vector2i> tiles; // Holds eight points of the player for accurate collision
|
||||||
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
|
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 groundCollision; // Keeps track of whether or not the player has collided with the ground
|
||||||
bool crateTopCollision;
|
bool crateTopCollision; // Whether the player is standing on top of a crate
|
||||||
/**
|
|
||||||
* Holds the corners of the player
|
|
||||||
* Is used for precise collision detection
|
|
||||||
*/
|
|
||||||
|
|
||||||
void applyGravity()
|
void applyGravity()
|
||||||
{
|
{
|
||||||
@@ -90,15 +100,20 @@ private:
|
|||||||
spriteAnimation.y = Right;
|
spriteAnimation.y = Right;
|
||||||
velocity.x = moveSpeed;
|
velocity.x = moveSpeed;
|
||||||
animate();
|
animate();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
velocity.x = 0.0f;
|
velocity.x = 0.0f;
|
||||||
}
|
}
|
||||||
positionUpdates();
|
positionUpdates();
|
||||||
handleCollision();
|
handleCollision();
|
||||||
|
|
||||||
if (!groundCollision) {
|
if (!groundCollision)
|
||||||
|
{
|
||||||
velocity.y += gravity;
|
velocity.y += gravity;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
velocity.y = 0.0f;
|
velocity.y = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +121,11 @@ private:
|
|||||||
{
|
{
|
||||||
velocity.y -= jumpPower;
|
velocity.y -= jumpPower;
|
||||||
}
|
}
|
||||||
|
move(velocity);
|
||||||
|
}
|
||||||
|
|
||||||
|
void move(Vector2 velocity)
|
||||||
|
{
|
||||||
this->texture.move(velocity);
|
this->texture.move(velocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +133,7 @@ private:
|
|||||||
{
|
{
|
||||||
if (GetTime() - prevFrame > animationDelay)
|
if (GetTime() - prevFrame > animationDelay)
|
||||||
{
|
{
|
||||||
spriteAnimation.x = std::fmod(spriteAnimation.x + 1,3);
|
spriteAnimation.x = std::fmod(spriteAnimation.x + 1, 3);
|
||||||
|
|
||||||
Rectangle sourceRect = this->texture.getSourceRect();
|
Rectangle sourceRect = this->texture.getSourceRect();
|
||||||
sourceRect.x = spriteAnimation.x * this->texture.getSize().x;
|
sourceRect.x = spriteAnimation.x * this->texture.getSize().x;
|
||||||
@@ -125,82 +144,76 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void positionUpdates() {
|
void positionUpdates()
|
||||||
const Vector2 pos = this->texture.getPosition();
|
{
|
||||||
const Vector2 size = this->texture.getSize();
|
const Vector2 pos = this->texture.getPosition();
|
||||||
const int tileSize = MapService::getInstance().getMap().getTileSize();
|
const Vector2 size = this->texture.getSize();
|
||||||
|
const int tileSize = MapService::getInstance().getMap().getTileSize();
|
||||||
|
|
||||||
const float bottom = pos.y + size.y + velocity.y;
|
const float bottom = pos.y + size.y + velocity.y;
|
||||||
const float left = pos.x + velocity.x;
|
const float left = pos.x + velocity.x;
|
||||||
const float right = pos.x + size.x + velocity.x;
|
const float right = pos.x + size.x + velocity.x;
|
||||||
const float top = pos.y + velocity.y;
|
const float top = pos.y + velocity.y;
|
||||||
|
|
||||||
Vector2i topLeft(Vector2i((int)(left + 5) / tileSize, (int)top / tileSize)); // +5 to separate top from side
|
Vector2i topLeft(Vector2i((int)(left + 5) / tileSize, (int)top / tileSize)); // +5 to separate top from side
|
||||||
Vector2i topRight(Vector2i((int)(right - 5) / tileSize, (int)top / tileSize)); // -5 to separate top from side
|
Vector2i topRight(Vector2i((int)(right - 5) / tileSize, (int)top / tileSize)); // -5 to separate top from side
|
||||||
|
|
||||||
Vector2i leftTop(Vector2i((int)left / tileSize, (int)(top + 5) / tileSize));
|
Vector2i leftTop(Vector2i((int)left / tileSize, (int)(top + 5) / tileSize));
|
||||||
Vector2i leftBottom(Vector2i((int)left / tileSize, (int)(bottom - 5) / tileSize));
|
Vector2i leftBottom(Vector2i((int)left / tileSize, (int)(bottom - 5) / tileSize));
|
||||||
Vector2i rightTop(Vector2i((int)right / tileSize, (int)(top + 5) / tileSize));
|
Vector2i rightTop(Vector2i((int)right / tileSize, (int)(top + 5) / tileSize));
|
||||||
Vector2i rightBottom(Vector2i((int)right / tileSize, (int)(bottom - 5) / tileSize));
|
Vector2i rightBottom(Vector2i((int)right / tileSize, (int)(bottom - 5) / tileSize));
|
||||||
|
|
||||||
Vector2i bottomLeft(Vector2i((int)(left + 5) / tileSize, (int)bottom / tileSize)); // +5 to separate bottom from side
|
Vector2i bottomLeft(Vector2i((int)(left + 5) / tileSize, (int)bottom / tileSize)); // +5 to separate bottom from side
|
||||||
Vector2i bottomRight(Vector2i((int)(right - 5) / tileSize, (int)bottom / tileSize)); // -5 to separate bottom from side
|
Vector2i bottomRight(Vector2i((int)(right - 5) / tileSize, (int)bottom / tileSize)); // -5 to separate bottom from side
|
||||||
|
|
||||||
|
tiles.clear();
|
||||||
|
|
||||||
tiles.clear();
|
tiles.push_back(topLeft);
|
||||||
|
tiles.push_back(topRight);
|
||||||
|
|
||||||
tiles.push_back(topLeft);
|
tiles.push_back(leftTop);
|
||||||
tiles.push_back(topRight);
|
tiles.push_back(leftBottom);
|
||||||
|
tiles.push_back(rightTop);
|
||||||
|
tiles.push_back(rightBottom);
|
||||||
|
|
||||||
tiles.push_back(leftTop);
|
tiles.push_back(bottomLeft);
|
||||||
tiles.push_back(leftBottom);
|
tiles.push_back(bottomRight);
|
||||||
tiles.push_back(rightTop);
|
|
||||||
tiles.push_back(rightBottom);
|
|
||||||
|
|
||||||
tiles.push_back(bottomLeft);
|
|
||||||
tiles.push_back(bottomRight);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores the current position
|
|
||||||
* Applies the current velocity
|
|
||||||
* Checks for collision, and removes all velocities that are causing collisions
|
|
||||||
* Set the object exactly against the object it is colliding with position
|
|
||||||
*/
|
|
||||||
void handleCollision()
|
void handleCollision()
|
||||||
{
|
{
|
||||||
|
|
||||||
timesCollision.clear();
|
timesCollision.clear();
|
||||||
|
|
||||||
for (int i = 0; i < tiles.size(); i++)
|
for (int i = 0; i < tiles.size(); i++)
|
||||||
{
|
|
||||||
if (MapService::getInstance().getMap().getColMap()[tiles[i].y][tiles[i].x] == 1)
|
|
||||||
{
|
{
|
||||||
timesCollision.push_back(i);
|
if (MapService::getInstance().getMap().getColMap()[tiles[i].y][tiles[i].x] == 1)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
timesCollision.push_back(i);
|
||||||
}
|
}
|
||||||
else if (timesCollision[i] == 2 || timesCollision[i] == 3 || //Lefttop and Leftbottom
|
}
|
||||||
timesCollision[i] == 4 || timesCollision[i] == 5) //Righttop and Lefttop
|
|
||||||
|
groundCollision = false;
|
||||||
|
|
||||||
|
if (timesCollision.size() > 0)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < timesCollision.size(); i++)
|
||||||
{
|
{
|
||||||
velocity.x = 0;
|
if (timesCollision[i] == 0 || timesCollision[i] == 1) // Topleft and Topright
|
||||||
}
|
{
|
||||||
else if (timesCollision[i] == 6 || timesCollision[i] == 7) //Bottomleft and Bottomright
|
velocity.y = 0;
|
||||||
{
|
}
|
||||||
groundCollision = true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Set velocity.x based on going right or left
|
// Set velocity.x based on going right or left
|
||||||
|
|
||||||
@@ -261,6 +274,52 @@ private:
|
|||||||
AudioService::getInstance().playMusic();
|
AudioService::getInstance().playMusic();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void rotateGun()
|
||||||
|
{
|
||||||
|
if (GameService::getInstance().getMouseWorldPos().x > this->texture.getPosition().x + (this->gunLeft.getSize().x) && spriteAnimation.y == Right)
|
||||||
|
{
|
||||||
|
float gunRotationRight = atan2(GameService::getInstance().getMouseWorldPos().y - this->texture.getPosition().y,
|
||||||
|
GameService::getInstance().getMouseWorldPos().x - this->texture.getPosition().x);
|
||||||
|
this->gunRight.setRotation(gunRotationRight * 90);
|
||||||
|
}
|
||||||
|
else if (GameService::getInstance().getMouseWorldPos().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
|
||||||
|
{
|
||||||
|
float gunRotationLeft = atan2(GameService::getInstance().getMouseWorldPos().y - this->texture.getPosition().y,
|
||||||
|
this->texture.getPosition().x - GameService::getInstance().getMouseWorldPos().x);
|
||||||
|
this->gunLeft.setRotation(-gunRotationLeft * 90);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawGun()
|
||||||
|
{
|
||||||
|
if (GameService::getInstance().getMouseWorldPos().x > this->texture.getPosition().x + this->gunLeft.getSize().x && spriteAnimation.y == Right)
|
||||||
|
{
|
||||||
|
DrawTexturePro(
|
||||||
|
this->gunRight.getTexture(),
|
||||||
|
{0, 0, this->gunRight.getSize().x, this->gunRight.getSize().y},
|
||||||
|
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
|
||||||
|
this->texture.getPosition().y + this->texture.getSize().x / 2.0f,
|
||||||
|
this->gunRight.getSize().x,
|
||||||
|
this->gunRight.getSize().y},
|
||||||
|
{this->gunRight.getSize().x / 2.0f, this->gunRight.getSize().y / 2.0f},
|
||||||
|
this->gunRight.getRotation(),
|
||||||
|
WHITE);
|
||||||
|
}
|
||||||
|
else if (GameService::getInstance().getMouseWorldPos().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
|
||||||
|
{
|
||||||
|
DrawTexturePro(
|
||||||
|
this->gunLeft.getTexture(),
|
||||||
|
{0, 0, this->gunLeft.getSize().x, this->gunLeft.getSize().y},
|
||||||
|
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
|
||||||
|
this->texture.getPosition().y + this->texture.getSize().x / 2.0f,
|
||||||
|
this->gunLeft.getSize().x,
|
||||||
|
this->gunLeft.getSize().y},
|
||||||
|
{this->gunLeft.getSize().x / 2.0f, this->gunLeft.getSize().y / 2.0f},
|
||||||
|
this->gunLeft.getRotation(),
|
||||||
|
WHITE);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class TextureWrapper {
|
|||||||
this->texture = texture;
|
this->texture = texture;
|
||||||
this->size = size;
|
this->size = size;
|
||||||
this->position = position;
|
this->position = position;
|
||||||
|
this->sourceRect = sourceRect;
|
||||||
}
|
}
|
||||||
~TextureWrapper() {
|
~TextureWrapper() {
|
||||||
|
|
||||||
@@ -33,6 +34,10 @@ class TextureWrapper {
|
|||||||
return this->position;
|
return this->position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float getRotation() {
|
||||||
|
return this->rotation;
|
||||||
|
}
|
||||||
|
|
||||||
void setSourceRect(Rectangle rect) {
|
void setSourceRect(Rectangle rect) {
|
||||||
this->sourceRect = rect;
|
this->sourceRect = rect;
|
||||||
}
|
}
|
||||||
@@ -41,35 +46,40 @@ class TextureWrapper {
|
|||||||
this->position = position;
|
this->position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setRotation(float rot) {
|
||||||
|
this->rotation = rot;
|
||||||
|
}
|
||||||
|
|
||||||
void move(Vector2 deltaPos) {
|
void move(Vector2 deltaPos) {
|
||||||
this->position = {this->position.x + deltaPos.x, this->position.y + deltaPos.y};
|
this->position = {this->position.x + deltaPos.x, this->position.y + deltaPos.y};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the 4 corners of the texture */
|
/** Returns the 4 corners of the texture */
|
||||||
std::unordered_map<ECorner, Vector2> getCorners() {
|
// std::unordered_map<ECorner, Vector2> getCorners() {
|
||||||
std::unordered_map<ECorner, Vector2> corners;
|
// std::unordered_map<ECorner, Vector2> corners;
|
||||||
float top = this->position.y;
|
// float top = this->position.y;
|
||||||
float right = this->position.x+this->size.x;
|
// float right = this->position.x+this->size.x;
|
||||||
float bottom = this->position.y + this->size.y;
|
// float bottom = this->position.y + this->size.y;
|
||||||
float left = this->position.x;
|
// float left = this->position.x;
|
||||||
float margin = 8; // Space to the corner
|
// float margin = 8; // Space to the corner
|
||||||
corners[ECorner::TOP_LEFT] = {left+margin, top};
|
// corners[ECorner::TOP_LEFT] = {left+margin, top};
|
||||||
corners[ECorner::TOP_RIGHT] = {right-margin, top};
|
// corners[ECorner::TOP_RIGHT] = {right-margin, top};
|
||||||
corners[ECorner::RIGHT_TOP] = {right, top+margin};
|
// corners[ECorner::RIGHT_TOP] = {right, top+margin};
|
||||||
corners[ECorner::RIGHT_BOTTOM] = {right, bottom-margin};
|
// corners[ECorner::RIGHT_BOTTOM] = {right, bottom-margin};
|
||||||
corners[ECorner::BOTTOM_RIGHT] = {right-margin, bottom};
|
// corners[ECorner::BOTTOM_RIGHT] = {right-margin, bottom};
|
||||||
corners[ECorner::BOTTOM_LEFT] = {left+margin, bottom};
|
// corners[ECorner::BOTTOM_LEFT] = {left+margin, bottom};
|
||||||
corners[ECorner::LEFT_BOTTOM] = {left, bottom-margin};
|
// corners[ECorner::LEFT_BOTTOM] = {left, bottom-margin};
|
||||||
corners[ECorner::LEFT_TOP] = {left, top+margin};
|
// corners[ECorner::LEFT_TOP] = {left, top+margin};
|
||||||
|
|
||||||
return corners;
|
// return corners;
|
||||||
}
|
// }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Texture2D texture;
|
Texture2D texture;
|
||||||
Rectangle sourceRect;
|
Rectangle sourceRect;
|
||||||
Vector2 size;
|
Vector2 size;
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
|
float rotation;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,30 +1,61 @@
|
|||||||
#ifndef GAME_SERVICE_H
|
#ifndef GAME_SERVICE_H
|
||||||
#define GAME_SERVICE_H
|
#define GAME_SERVICE_H
|
||||||
|
|
||||||
#include "Service.h"
|
#include "services/Service.h"
|
||||||
|
#include "entities/Game.h"
|
||||||
|
|
||||||
class GameService : public Service<GameService> {
|
class GameService : public Service<GameService>
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
bool isPaused() {
|
bool isPaused()
|
||||||
|
{
|
||||||
return this->paused;
|
return this->paused;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isGameOver() {
|
bool isGameOver()
|
||||||
|
{
|
||||||
return this->gameOver;
|
return this->gameOver;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPaused(bool paused) {
|
void setPaused(bool paused)
|
||||||
|
{
|
||||||
this->paused = paused;
|
this->paused = paused;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setGameOver(bool gameOver) {
|
void setGameOver(bool gameOver)
|
||||||
|
{
|
||||||
this->gameOver = gameOver;
|
this->gameOver = gameOver;
|
||||||
}
|
}
|
||||||
|
void setCamera(Camera2D camera)
|
||||||
|
{
|
||||||
|
this->camera = camera;
|
||||||
|
}
|
||||||
|
|
||||||
|
Camera2D &getCamera()
|
||||||
|
{
|
||||||
|
return this->camera;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the position of the mouse in world coordinates
|
||||||
|
*/
|
||||||
|
Vector2 getMouseWorldPos()
|
||||||
|
{
|
||||||
|
Vector2 mouseWorldPos = {GetMousePosition().x + camera.target.x - camera.offset.x, GetMousePosition().y};
|
||||||
|
return mouseWorldPos;
|
||||||
|
}
|
||||||
|
// void setGame(Game game) {
|
||||||
|
// this->game = game;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Game& getGame() {
|
||||||
|
// return this->game;
|
||||||
|
// }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool paused = false;
|
bool paused = false;
|
||||||
bool gameOver = false;
|
bool gameOver = false;
|
||||||
|
Camera2D camera;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+39
-39
@@ -114,18 +114,18 @@ void Map::loadTileset()
|
|||||||
this->tileset = AssetService::getInstance().loadTexture("Textures/tileset.png");
|
this->tileset = AssetService::getInstance().loadTexture("Textures/tileset.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_map<ECorner, Vector2> Map::cornersToTilePos(std::unordered_map<ECorner, Vector2> corners)
|
// std::unordered_map<ECorner, Vector2> Map::cornersToTilePos(std::unordered_map<ECorner, Vector2> corners)
|
||||||
{
|
// {
|
||||||
// std::cout << "Starting converting" << std::endl;
|
// // std::cout << "Starting converting" << std::endl;
|
||||||
std::unordered_map<ECorner, Vector2> cornersTilePos;
|
// std::unordered_map<ECorner, Vector2> cornersTilePos;
|
||||||
for (const auto &pair : corners)
|
// for (const auto &pair : corners)
|
||||||
{
|
// {
|
||||||
// std::cout << "Foreach pair" << std::endl;
|
// // std::cout << "Foreach pair" << std::endl;
|
||||||
Vector2 original = corners[pair.first];
|
// Vector2 original = corners[pair.first];
|
||||||
cornersTilePos[pair.first] = {floorf(original.x / this->tileSize), floorf(original.y / this->tileSize)};
|
// cornersTilePos[pair.first] = {floorf(original.x / this->tileSize), floorf(original.y / this->tileSize)};
|
||||||
}
|
// }
|
||||||
return cornersTilePos;
|
// return cornersTilePos;
|
||||||
}
|
// }
|
||||||
|
|
||||||
void Map::draw()
|
void Map::draw()
|
||||||
{
|
{
|
||||||
@@ -146,35 +146,35 @@ void Map::draw()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_map<ECorner, bool> Map::checkForCollision(TextureWrapper texture)
|
// std::unordered_map<ECorner, bool> Map::checkForCollision(TextureWrapper texture)
|
||||||
{
|
// {
|
||||||
// std::cout << "Converting.." << std::endl;
|
// // std::cout << "Converting.." << std::endl;
|
||||||
std::unordered_map<ECorner, Vector2> cornersTilePos = cornersToTilePos(texture.getCorners());
|
// std::unordered_map<ECorner, Vector2> cornersTilePos = cornersToTilePos(texture.getCorners());
|
||||||
// std::cout << "Done with converting" << std::endl;
|
// // std::cout << "Done with converting" << std::endl;
|
||||||
|
|
||||||
std::unordered_map<ECorner, bool> cornerCollisions;
|
// std::unordered_map<ECorner, bool> cornerCollisions;
|
||||||
|
|
||||||
for (const auto &pair : cornersTilePos)
|
// for (const auto &pair : cornersTilePos)
|
||||||
{
|
// {
|
||||||
// std::cout << "foreach collision check" << std::endl;
|
// // std::cout << "foreach collision check" << std::endl;
|
||||||
Vector2 pos = cornersTilePos[pair.first];
|
// Vector2 pos = cornersTilePos[pair.first];
|
||||||
// std::cout << pos.x << " " << pos.y << std::endl;
|
// // std::cout << pos.x << " " << pos.y << std::endl;
|
||||||
if(colMap[pos.y][pos.x] == 1) {
|
// if(colMap[pos.y][pos.x] == 1) {
|
||||||
cornerCollisions[pair.first] = true;
|
// cornerCollisions[pair.first] = true;
|
||||||
} else {
|
// } else {
|
||||||
cornerCollisions[pair.first] = false;
|
// cornerCollisions[pair.first] = false;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// std::cout << "Finished collision checks" << std::endl;
|
// // std::cout << "Finished collision checks" << std::endl;
|
||||||
|
|
||||||
return cornerCollisions;
|
// return cornerCollisions;
|
||||||
|
|
||||||
// for (int i = 0; i < tiles.size(); i++)
|
// // for (int i = 0; i < tiles.size(); i++)
|
||||||
// {
|
// // {
|
||||||
// if (colMap[tiles[i].y][tiles[i].x] == 1)
|
// // if (colMap[tiles[i].y][tiles[i].x] == 1)
|
||||||
// {
|
// // {
|
||||||
// timesCollision.push_back(i);
|
// // timesCollision.push_back(i);
|
||||||
// }
|
// // }
|
||||||
// }
|
// // }
|
||||||
|
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "screens/GameScreen.h"
|
#include "screens/GameScreen.h"
|
||||||
#include "entities/Map.h"
|
#include "entities/Map.h"
|
||||||
#include "services/StateService.h"
|
#include "services/StateService.h"
|
||||||
|
#include "services/GameService.h"
|
||||||
|
|
||||||
GameScreen::GameScreen()
|
GameScreen::GameScreen()
|
||||||
{
|
{
|
||||||
@@ -12,6 +13,7 @@ GameScreen::GameScreen()
|
|||||||
// this->map = Map();
|
// this->map = Map();
|
||||||
|
|
||||||
this->game = Game();
|
this->game = Game();
|
||||||
|
// GameService::getInstance().setGame(this->game);
|
||||||
// this->camera = camera;
|
// this->camera = camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,13 +29,13 @@ TitleScreen::TitleScreen()
|
|||||||
AudioService::getInstance().playMusic();
|
AudioService::getInstance().playMusic();
|
||||||
}
|
}
|
||||||
|
|
||||||
InputService::getInstance().setKeysToWatch({KEY_ENTER}, {});
|
InputService::getInstance().setKeysToWatch({KEY_ENTER}, {MOUSE_BUTTON_LEFT});
|
||||||
}
|
}
|
||||||
|
|
||||||
void TitleScreen::logic()
|
void TitleScreen::logic()
|
||||||
{
|
{
|
||||||
// std::cout << "Next screen!" << std::endl;
|
// std::cout << "Next screen!" << std::endl;
|
||||||
if (InputService::getInstance().isKeyPressed(KEY_ENTER))
|
if (InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||||
{
|
{
|
||||||
StateService::getInstance().setScreen(EScreen::MAIN);
|
StateService::getInstance().setScreen(EScreen::MAIN);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user