0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 07:53:43 +00:00

code documentation in form of comments added

This commit is contained in:
Markus 2024-01-21 22:13:21 -06:00
parent 98f8cbc221
commit acbea34632
7 changed files with 23 additions and 21 deletions

View File

@ -13,7 +13,6 @@ public:
AssetManager(Manager* manager);
~AssetManager();
//game object management
void createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath);
//texture management

View File

@ -10,7 +10,7 @@ public:
const char* tag;
TransformComponent* transform;
bool hasCollision;
bool hasCollision; //added for removing collision of destroyed projectiles
ColliderComponent(const char* tag)
{

View File

@ -14,8 +14,9 @@ public:
SDL_Scancode right;
SDL_Scancode fire;
//for attack cooldown in between shots
Uint32 lastFireTime;
Uint32 fireCooldown = 1000;
Uint32 fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
KeyboardController();
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity);
@ -27,6 +28,7 @@ public:
void update() override;
private:
TransformComponent* player;
Vector2D fireVelocity;
//for creation of projectiles
TransformComponent* player; //for starting position of projectile
Vector2D fireVelocity; //decide source of projectile and flying direction
};

View File

@ -6,6 +6,8 @@
class ProjectileComponent : public Component {
//can maybe be split in separate .cpp file
public:
ProjectileComponent(int range, int speed, Vector2D velocity, bool source) : range(range), speed(speed), velocity(velocity), source(source) {
@ -19,13 +21,15 @@ public:
}
void update() override {
transformComponent->velocity = velocity;
distance += speed;
if (distance > range) {
entity->destroy();
entity->getComponent<ColliderComponent>().removeCollision();
std::cout << "out of range" << std::endl;
//std::cout << "out of range" << std::endl;
}
}
@ -43,7 +47,7 @@ private:
int speed = 0;
int distance = 0;
const bool source;
const bool source; //true if from player1 / false if from player2
Vector2D velocity;
};

View File

@ -18,7 +18,7 @@ SDL_Texture* AssetManager::getTexture(std::string id) {
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath) {
auto& projectile(man->addEntity());
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale);
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
projectile.addComponent<SpriteComponent>(texturePath);
projectile.addComponent<ProjectileComponent>(range, speed, velocity, source);
projectile.addComponent<ColliderComponent>("projectile");

View File

@ -63,6 +63,8 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
map = new Map();
map->loadMap("assets/SDL_map_test.txt", 25, 20);
//adding textures to the library in AssetManager
assets->addTexture("player1", "assets/chicken_neutral_knight.png");
assets->addTexture("player2", "assets/chicken_neutral.png");
assets->addTexture("bigEgg", "assets/bigger_egg.png");
@ -81,16 +83,6 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0));
enemy.addComponent<ColliderComponent>("enemy");
enemy.addGroup(GROUP_ENEMIES);
/*
projectile.addComponent<TransformComponent>(500, 500, 32, 32, 2);
projectile.addComponent<SpriteComponent>("assets/chicken_neutral_knight.png");
projectile.addComponent<ProjectileComponent>(200, 1, Vector2D(1,0));
projectile.addComponent<ColliderComponent>("projectile");
projectile.addGroup(Game::PROJECTILE);
assets->createProjectile(Vector2D(50, 100), Vector2D(1,0), 1, 180, 1, "assets/chicken_neutral_knight.png");
*/
}
auto& tiles(manager.getGroup(Game::GROUP_MAP));
@ -133,18 +125,18 @@ void Game::update()
}
}
//checking if projectiles hit player1 or player2
for (auto& p : projectiles) {
if(SDL_HasIntersection(&enemy.getComponent<ColliderComponent>().collider, &p->getComponent<ColliderComponent>().collider)
&& (p->getComponent<ColliderComponent>().hasCollision) && !p->getComponent<ProjectileComponent>().getSource()) {
std::cout << "Enemy hit!";
//std::cout << "Enemy hit!";
p->getComponent<ColliderComponent>().removeCollision();
p->destroy();
}
if(SDL_HasIntersection(&player.getComponent<ColliderComponent>().collider, &p->getComponent<ColliderComponent>().collider)
&& (p->getComponent<ColliderComponent>().hasCollision) && p->getComponent<ProjectileComponent>().getSource()) {
std::cout << "Player hit!";
//std::cout << "Player hit!";
p->getComponent<ColliderComponent>().removeCollision();
p->destroy();
}

View File

@ -45,10 +45,15 @@ void KeyboardController::update()
}
if (keystates[this->fire]) {
Uint32 currentTicks = SDL_GetTicks();
if (currentTicks - lastFireTime >= fireCooldown) {
player = &entity->getComponent<TransformComponent>();
//checks player source via the firing velocity
//TODO: adding actual projectile textures
if(fireVelocity.x > 0) {
Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity,
false,1, 180, 1, "assets/chicken_neutral_knight.png");