[Updated] Code for use with WASM, [TODO] Make enemy gun follow player, spawn bullets from tip of the barrel

This commit is contained in:
Ninjaws
2024-12-17 23:15:26 +01:00
parent 2400b0500a
commit d740e1a167
25 changed files with 371 additions and 438 deletions
+4 -3
View File
@@ -6,6 +6,7 @@
#include "entities/TextureWrapper.h"
#include "entities/Vector2i.h"
#include <cmath>
#include <cstdint>
class Enemy
{
@@ -47,7 +48,7 @@ public:
return this->texture;
}
u_int8_t getHealth()
uint8_t getHealth()
{
return this->health;
}
@@ -61,8 +62,8 @@ private:
TextureWrapper texture;
TextureWrapper gunLeft;
TextureWrapper gunRight;
u_int8_t health;
u_int8_t maxHealth = 5;
uint8_t health;
uint8_t maxHealth = 5;
Vector2 velocity = {0, 0};
float spottingRange = 300; // From how far away enemies can spot the player
float gravity = 0.6f;
+4 -4
View File
@@ -20,10 +20,10 @@ public:
void logic()
{
if (touched)
{
StateService::getInstance().setScreen(EScreen::VICTORY);
}
// if (touched)
// {
// // StateService::getInstance().setScreen(EScreen::VICTORY);
// }
}
void draw()
+12 -15
View File
@@ -66,7 +66,7 @@ public:
void logic()
{
this->crosshair.setPosition(GetMousePosition());
this->crosshair.setPosition(InputService::getInstance().getMousePos());
if (GameService::getInstance().isPaused())
{
@@ -96,6 +96,9 @@ public:
{
GameService::getInstance().getCamera().offset = {(StateService::getInstance().getScreenSize().x / 2.0f) + ((StateService::getInstance().getScreenSize().x / 2.0f) - player.getTexture().getPosition().x), StateService::getInstance().getScreenSize().y / 2.0f};
}
if(player.isFinishReached()) {
StateService::getInstance().setScreen(EScreen::VICTORY);
}
}
void draw()
@@ -113,6 +116,7 @@ public:
EndMode2D();
drawHud();
if (GameService::getInstance().isPaused())
{
drawPaused();
@@ -120,9 +124,9 @@ public:
else if (GameService::getInstance().isGameOver())
{
drawGameOver();
} else {
drawCrosshair();
}
drawHud();
drawCrosshair();
EndDrawing();
}
@@ -184,7 +188,7 @@ private:
const int SPACE_PER_ITEM = TOTAL_SPACE / AMNT_OF_BUTTONS;
for (int i = 0; i < AMNT_OF_BUTTONS; i++)
{
Text txt = Text("Play Again", EFont::SANSATION, 30, 0, GRAY);
Text txt = Text("Play Again", EFont::SANSATION, 30, 0, BLACK);
gameOverScreenButtons.push_back(txt);
}
gameOverScreenButtons[0].setColor(WHITE);
@@ -229,7 +233,7 @@ private:
{
if (InputService::getInstance().isKeyPressed(KEY_D) || InputService::getInstance().isKeyPressed(KEY_A))
{
gameOverScreenButtons[hoveredGameOverScreenButton].setColor(GRAY);
gameOverScreenButtons[hoveredGameOverScreenButton].setColor(BLACK);
/** Move up or down depending on the pressed button. +4 is used so that -1 becomes 3 (the last button) */
hoveredGameOverScreenButton = ((hoveredGameOverScreenButton - (InputService::getInstance().isKeyPressed(KEY_D) ? -1 : 1)) + gameOverScreenButtons.size()) % gameOverScreenButtons.size();
gameOverScreenButtons[hoveredGameOverScreenButton].setColor(WHITE);
@@ -254,7 +258,7 @@ private:
void drawPaused()
{
// 100, 100, 255, 60
DrawRectangle(0, 0, StateService::getInstance().getScreenSize().x, StateService::getInstance().getScreenSize().y, {0, 0, 255, 100});
DrawRectangle(0, 0, StateService::getInstance().getScreenSize().x, StateService::getInstance().getScreenSize().y, {0, 0, 0, 200});
if (pauseScreenButtons.size() > 0)
{
for (int i = 0; i < pauseScreenButtons.size(); i++)
@@ -266,7 +270,7 @@ private:
void drawGameOver()
{
DrawRectangle(0, 0, StateService::getInstance().getScreenSize().x, StateService::getInstance().getScreenSize().y, {255, 0, 0, 100});
DrawRectangle(0, 0, StateService::getInstance().getScreenSize().x, StateService::getInstance().getScreenSize().y, {100, 0, 0, 200});
gameOverText.draw();
@@ -280,17 +284,10 @@ private:
{
DrawTextureEx(
this->crosshair.getTexture(),
{GetMousePosition().x - this->crosshair.getSize().x / 2.0f, GetMousePosition().y - this->crosshair.getSize().y / 2.0f},
{InputService::getInstance().getMousePos().x - this->crosshair.getSize().x / 2.0f, InputService::getInstance().getMousePos().y - this->crosshair.getSize().y / 2.0f},
0,
1.2f,
RED);
// DrawTexturePro(
// this->crosshair.getTexture(),
// this->crosshair.getSourceRect(),
// {GetMousePosition().x, GetMousePosition().y, GetMousePosition().x+this->crosshair.getSize().x, GetMousePosition().y+this->crosshair.getSize().y},
// {this->crosshair.getSize().x/2.0f, this->crosshair.getSize().y/2.0f},
// 0,
// WHITE);
}
void drawHud()
+5
View File
@@ -81,6 +81,10 @@ public:
this->health = std::max(health - damage, 0);
}
bool isFinishReached() {
return this->finishReached;
}
protected:
private:
TextureWrapper texture;
@@ -113,6 +117,7 @@ private:
uint8_t health; // Current health of the player
uint8_t max_health = 10; // Max health of the player
bool gainedPowerup = false;
bool finishReached = false;
void applyGravity();
void movement();
+3 -2
View File
@@ -4,6 +4,7 @@
#include "raylib.h"
#include "services/AssetService.h"
#include "entities/TextureWrapper.h"
#include <cstdint>
class Powerup {
public:
@@ -40,8 +41,8 @@ class Powerup {
bool pickedUp = false; // When the player has picked up the pack
float animationDelay = 0.08f; // How long to wait in between animation frames (in seconds)
double lastFrame; // Time since the last frame
u_int8_t currentFrame = 0; // Current frame of the animation
u_int8_t amountOfFrames = 8; // Total amount of animation frames
uint8_t currentFrame = 0; // Current frame of the animation
uint8_t amountOfFrames = 8; // Total amount of animation frames
void animate() {
if(GetTime() - lastFrame > animationDelay) {
+37 -31
View File
@@ -3,47 +3,53 @@
#include "raylib.h"
#include "enums/EFont.h"
#include <string>
#include <iostream>
#include "services/AssetService.h"
#include <string>
class Text {
public:
Text(){}
Text(std::string text, EFont font, int fontSize = 30, int spacing = 0, Color color = WHITE, Vector2 position = {100,100});
~Text(){}
class Text
{
public:
Text() {}
Text(std::string text, EFont font, int fontSize = 30, int spacing = 0, Color color = WHITE, Vector2 position = {100, 100});
~Text() {
}
Vector2 getDimensions();
Vector2 getDimensions();
void setPosition(Vector2 position) {
this->position = position;
}
void setPosition(Vector2 position)
{
this->position = position;
}
void setColor(Color color) {
this->color = color;
}
void setColor(Color color)
{
this->color = color;
}
void setText(std::string text) {
this->text = text;
}
void setText(std::string text)
{
this->text = text;
}
Vector2 getPosition() {
return position;
}
Vector2 getPosition()
{
return position;
}
std::string getText() {
return this->text;
}
std::string getText()
{
return this->text;
}
void draw();
void draw();
private:
std::string text;
Font font;
int fontSize;
int spacing;
Color color;
Vector2 position;
private:
std::string text;
Font font;
int fontSize;
int spacing;
Color color;
Vector2 position;
};
#endif