mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 10:13:42 +00:00
code documentation in form of comments added
This commit is contained in:
parent
98f8cbc221
commit
acbea34632
@ -13,7 +13,6 @@ public:
|
|||||||
AssetManager(Manager* manager);
|
AssetManager(Manager* manager);
|
||||||
~AssetManager();
|
~AssetManager();
|
||||||
|
|
||||||
//game object management
|
|
||||||
void createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath);
|
void createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath);
|
||||||
|
|
||||||
//texture management
|
//texture management
|
||||||
|
|||||||
@ -10,7 +10,7 @@ public:
|
|||||||
const char* tag;
|
const char* tag;
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
|
|
||||||
bool hasCollision;
|
bool hasCollision; //added for removing collision of destroyed projectiles
|
||||||
|
|
||||||
ColliderComponent(const char* tag)
|
ColliderComponent(const char* tag)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -14,8 +14,9 @@ public:
|
|||||||
SDL_Scancode right;
|
SDL_Scancode right;
|
||||||
SDL_Scancode fire;
|
SDL_Scancode fire;
|
||||||
|
|
||||||
|
//for attack cooldown in between shots
|
||||||
Uint32 lastFireTime;
|
Uint32 lastFireTime;
|
||||||
Uint32 fireCooldown = 1000;
|
Uint32 fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
|
||||||
|
|
||||||
KeyboardController();
|
KeyboardController();
|
||||||
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity);
|
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;
|
void update() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TransformComponent* player;
|
//for creation of projectiles
|
||||||
Vector2D fireVelocity;
|
TransformComponent* player; //for starting position of projectile
|
||||||
|
Vector2D fireVelocity; //decide source of projectile and flying direction
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
class ProjectileComponent : public Component {
|
class ProjectileComponent : public Component {
|
||||||
|
|
||||||
|
//can maybe be split in separate .cpp file
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
ProjectileComponent(int range, int speed, Vector2D velocity, bool source) : range(range), speed(speed), velocity(velocity), source(source) {
|
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 {
|
void update() override {
|
||||||
|
|
||||||
transformComponent->velocity = velocity;
|
transformComponent->velocity = velocity;
|
||||||
|
|
||||||
distance += speed;
|
distance += speed;
|
||||||
|
|
||||||
if (distance > range) {
|
if (distance > range) {
|
||||||
entity->destroy();
|
entity->destroy();
|
||||||
entity->getComponent<ColliderComponent>().removeCollision();
|
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 speed = 0;
|
||||||
int distance = 0;
|
int distance = 0;
|
||||||
|
|
||||||
const bool source;
|
const bool source; //true if from player1 / false if from player2
|
||||||
|
|
||||||
Vector2D velocity;
|
Vector2D velocity;
|
||||||
};
|
};
|
||||||
@ -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) {
|
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath) {
|
||||||
|
|
||||||
auto& projectile(man->addEntity());
|
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<SpriteComponent>(texturePath);
|
||||||
projectile.addComponent<ProjectileComponent>(range, speed, velocity, source);
|
projectile.addComponent<ProjectileComponent>(range, speed, velocity, source);
|
||||||
projectile.addComponent<ColliderComponent>("projectile");
|
projectile.addComponent<ColliderComponent>("projectile");
|
||||||
|
|||||||
18
src/Game.cpp
18
src/Game.cpp
@ -63,6 +63,8 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
|||||||
map = new Map();
|
map = new Map();
|
||||||
map->loadMap("assets/SDL_map_test.txt", 25, 20);
|
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("player1", "assets/chicken_neutral_knight.png");
|
||||||
assets->addTexture("player2", "assets/chicken_neutral.png");
|
assets->addTexture("player2", "assets/chicken_neutral.png");
|
||||||
assets->addTexture("bigEgg", "assets/bigger_egg.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<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0));
|
||||||
enemy.addComponent<ColliderComponent>("enemy");
|
enemy.addComponent<ColliderComponent>("enemy");
|
||||||
enemy.addGroup(GROUP_ENEMIES);
|
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));
|
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) {
|
for (auto& p : projectiles) {
|
||||||
if(SDL_HasIntersection(&enemy.getComponent<ColliderComponent>().collider, &p->getComponent<ColliderComponent>().collider)
|
if(SDL_HasIntersection(&enemy.getComponent<ColliderComponent>().collider, &p->getComponent<ColliderComponent>().collider)
|
||||||
&& (p->getComponent<ColliderComponent>().hasCollision) && !p->getComponent<ProjectileComponent>().getSource()) {
|
&& (p->getComponent<ColliderComponent>().hasCollision) && !p->getComponent<ProjectileComponent>().getSource()) {
|
||||||
std::cout << "Enemy hit!";
|
//std::cout << "Enemy hit!";
|
||||||
p->getComponent<ColliderComponent>().removeCollision();
|
p->getComponent<ColliderComponent>().removeCollision();
|
||||||
p->destroy();
|
p->destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(SDL_HasIntersection(&player.getComponent<ColliderComponent>().collider, &p->getComponent<ColliderComponent>().collider)
|
if(SDL_HasIntersection(&player.getComponent<ColliderComponent>().collider, &p->getComponent<ColliderComponent>().collider)
|
||||||
&& (p->getComponent<ColliderComponent>().hasCollision) && p->getComponent<ProjectileComponent>().getSource()) {
|
&& (p->getComponent<ColliderComponent>().hasCollision) && p->getComponent<ProjectileComponent>().getSource()) {
|
||||||
std::cout << "Player hit!";
|
//std::cout << "Player hit!";
|
||||||
p->getComponent<ColliderComponent>().removeCollision();
|
p->getComponent<ColliderComponent>().removeCollision();
|
||||||
p->destroy();
|
p->destroy();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -45,10 +45,15 @@ void KeyboardController::update()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (keystates[this->fire]) {
|
if (keystates[this->fire]) {
|
||||||
|
|
||||||
Uint32 currentTicks = SDL_GetTicks();
|
Uint32 currentTicks = SDL_GetTicks();
|
||||||
|
|
||||||
if (currentTicks - lastFireTime >= fireCooldown) {
|
if (currentTicks - lastFireTime >= fireCooldown) {
|
||||||
|
|
||||||
player = &entity->getComponent<TransformComponent>();
|
player = &entity->getComponent<TransformComponent>();
|
||||||
|
|
||||||
|
//checks player source via the firing velocity
|
||||||
|
//TODO: adding actual projectile textures
|
||||||
if(fireVelocity.x > 0) {
|
if(fireVelocity.x > 0) {
|
||||||
Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity,
|
Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity,
|
||||||
false,1, 180, 1, "assets/chicken_neutral_knight.png");
|
false,1, 180, 1, "assets/chicken_neutral_knight.png");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user