mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 15:53:42 +00:00
framework for randomly spawning items
not collectable yet, dont get destroyed either, WIP
This commit is contained in:
parent
fa480f916f
commit
fcf14b20c4
@ -2,6 +2,8 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "Powerup.h"
|
||||
|
||||
class Vector2D;
|
||||
class Manager;
|
||||
|
||||
@ -13,6 +15,7 @@ public:
|
||||
~AssetManager();
|
||||
|
||||
void createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath);
|
||||
void createPowerup(Vector2D pos, PowerupType type);
|
||||
|
||||
//texture management
|
||||
void addTexture(std::string id, const char* path);
|
||||
|
||||
@ -9,7 +9,8 @@ enum class GroupLabel
|
||||
ENEMIES,
|
||||
COLLIDERS,
|
||||
PROJECTILE,
|
||||
HEARTS
|
||||
HEARTS,
|
||||
POWERUPS
|
||||
};
|
||||
|
||||
class Component
|
||||
|
||||
@ -17,3 +17,5 @@ constexpr int TILE_SIZE = 32;
|
||||
constexpr int MAP_SIZE_X = 25;
|
||||
constexpr int MAP_SIZE_Y = 20;
|
||||
|
||||
constexpr int SPAWN_ATTEMPTS = 30;
|
||||
|
||||
|
||||
26
include/Powerup.h
Normal file
26
include/Powerup.h
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "Component.h"
|
||||
#include "Manager.h"
|
||||
#include "Vector2D.h"
|
||||
#include <map>
|
||||
|
||||
enum class PowerupType
|
||||
{
|
||||
HEART,
|
||||
WALKINGSPEED,
|
||||
SHOOTINGSPEED
|
||||
};
|
||||
|
||||
class Powerup
|
||||
{
|
||||
public:
|
||||
Powerup(){}
|
||||
~Powerup(){}
|
||||
|
||||
static Vector2D calculateSpawnPosition();
|
||||
static PowerupType calculateType();
|
||||
|
||||
private:
|
||||
Manager* manager;
|
||||
};
|
||||
@ -2,15 +2,23 @@
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "Powerup.h"
|
||||
|
||||
class TextureDict
|
||||
{
|
||||
public:
|
||||
const std::map<int, std::string> textureDictionary = {
|
||||
const std::map<int, std::string> tileDictionary = {
|
||||
{1, "assets/water.png"},
|
||||
{2, "assets/dirt.png"},
|
||||
{3, "assets/grass.png"},
|
||||
{7, "assets/grass_water_left.png"},
|
||||
{9, "assets/grass_water_right.png"}
|
||||
};
|
||||
|
||||
|
||||
std::map<PowerupType, std::string> powerupDictionary = {
|
||||
{PowerupType::HEART, "assets/heart.png"},
|
||||
{PowerupType::WALKINGSPEED, "assets/heart.png"},
|
||||
{PowerupType::SHOOTINGSPEED, "assets/heart.png"}
|
||||
};
|
||||
};
|
||||
|
||||
@ -23,4 +23,25 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source
|
||||
projectile.addComponent<ProjectileComponent>(range, speed, velocity, source);
|
||||
projectile.addComponent<ColliderComponent>("projectile", 0.6f);
|
||||
projectile.addGroup((size_t)GroupLabel::PROJECTILE);
|
||||
}
|
||||
|
||||
void AssetManager::createPowerup(Vector2D pos, PowerupType type) {
|
||||
TextureDict textureDict;
|
||||
|
||||
auto& powerups(man->addEntity());
|
||||
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects
|
||||
auto it = textureDict.powerupDictionary.find(type);
|
||||
if (it == textureDict.powerupDictionary.end()) {
|
||||
std::cout << "it end" << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
powerups.addComponent<SpriteComponent>(it->second.data());
|
||||
}
|
||||
catch (std::runtime_error e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
powerups.addComponent<ColliderComponent>("powerup", 0.6f);
|
||||
powerups.addGroup((size_t)GroupLabel::POWERUPS);
|
||||
}
|
||||
28
src/Game.cpp
28
src/Game.cpp
@ -5,6 +5,7 @@
|
||||
#include "AssetManager.h"
|
||||
#include "Map.h"
|
||||
#include "TextureManager.h"
|
||||
#include "Powerup.h"
|
||||
|
||||
Map* map;
|
||||
Manager manager;
|
||||
@ -20,7 +21,6 @@ std::vector<ColliderComponent*> Game::colliders;
|
||||
auto& player(manager.addEntity());
|
||||
auto& enemy(manager.addEntity());
|
||||
auto& wall(manager.addEntity());
|
||||
//auto& projectile (manager.addEntity());
|
||||
|
||||
Game::Game() = default;
|
||||
|
||||
@ -131,6 +131,7 @@ auto& players(manager.getGroup((size_t)GroupLabel::PLAYERS));
|
||||
auto& enemies(manager.getGroup((size_t)GroupLabel::ENEMIES));
|
||||
auto& projectiles(manager.getGroup((size_t)GroupLabel::PROJECTILE));
|
||||
auto& hearts(manager.getGroup((size_t)GroupLabel::HEARTS));
|
||||
auto& powerups(manager.getGroup((size_t)GroupLabel::POWERUPS));
|
||||
|
||||
void Game::handleEvents()
|
||||
{
|
||||
@ -151,16 +152,23 @@ void Game::update()
|
||||
Vector2D playerPos = player.getComponent<TransformComponent>().position;
|
||||
Vector2D enemyPos = enemy.getComponent<TransformComponent>().position;
|
||||
|
||||
int powerupSpawn = rand() % 500;
|
||||
|
||||
manager.refresh();
|
||||
manager.update();
|
||||
|
||||
if (powerupSpawn == 0)
|
||||
{
|
||||
assets->createPowerup(Powerup::calculateSpawnPosition(), Powerup::calculateType());
|
||||
}
|
||||
|
||||
for (auto cc : colliders)
|
||||
{
|
||||
if (SDL_HasIntersection(&player.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "player") && cc->hasCollision)
|
||||
if (SDL_HasIntersection(&player.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "player"))
|
||||
{
|
||||
player.getComponent<TransformComponent>().position = playerPos;
|
||||
}
|
||||
if (SDL_HasIntersection(&enemy.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "enemy") && cc->hasCollision)
|
||||
if (SDL_HasIntersection(&enemy.getComponent<ColliderComponent>().collider, &cc->collider) && strcmp(cc->tag, "enemy"))
|
||||
{
|
||||
enemy.getComponent<TransformComponent>().position = enemyPos;
|
||||
}
|
||||
@ -218,17 +226,17 @@ void Game::render()
|
||||
{
|
||||
SDL_RenderClear(renderer);
|
||||
for (auto& t : tiles)
|
||||
{
|
||||
t->draw();
|
||||
}
|
||||
for (auto& p : players)
|
||||
{
|
||||
|
||||
for (auto& p : powerups)
|
||||
p->draw();
|
||||
}
|
||||
|
||||
for (auto& p : players)
|
||||
p->draw();
|
||||
|
||||
for (auto& e : enemies)
|
||||
{
|
||||
e->draw();
|
||||
}
|
||||
|
||||
for (auto& p : projectiles)
|
||||
p->draw();
|
||||
|
||||
|
||||
38
src/Powerup.cpp
Normal file
38
src/Powerup.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "Powerup.h"
|
||||
|
||||
#include "TextureDict.h"
|
||||
#include <SDL.h>
|
||||
#include "Constants.h"
|
||||
#include "Game.h"
|
||||
#include "ColliderComponent.h"
|
||||
|
||||
Vector2D Powerup::calculateSpawnPosition()
|
||||
{
|
||||
Vector2D spawnPos = Vector2D(-1,-1);
|
||||
bool conflict = false;
|
||||
for (int i = 0; i <= SPAWN_ATTEMPTS; i++)
|
||||
{
|
||||
SDL_Rect spawnRect;
|
||||
spawnRect.h = spawnRect.w = 32;
|
||||
spawnRect.x = rand() % (SCREEN_SIZE_WIDTH - spawnRect.w);
|
||||
spawnRect.y = rand() % (SCREEN_SIZE_HEIGHT - spawnRect.h);
|
||||
conflict = false;
|
||||
for (auto cc : Game::colliders)
|
||||
{
|
||||
if (SDL_HasIntersection(&spawnRect, &cc->collider) && strcmp(cc->tag, "projectile"))
|
||||
{
|
||||
conflict = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (conflict) continue;
|
||||
spawnPos = Vector2D(spawnRect.x, spawnRect.y);
|
||||
}
|
||||
return spawnPos;
|
||||
}
|
||||
|
||||
PowerupType Powerup::calculateType()
|
||||
{
|
||||
PowerupType type = PowerupType(rand() % 3);
|
||||
return type;
|
||||
}
|
||||
@ -73,10 +73,5 @@ void SpriteComponent::playAnimation(AnimationType type)
|
||||
|
||||
void SpriteComponent::setDirection(SpriteDirection direction)
|
||||
{
|
||||
if (direction == RIGHT) {
|
||||
this->flipped = true;
|
||||
return;
|
||||
}
|
||||
|
||||
this->flipped = false;
|
||||
this->flipped = direction == RIGHT;
|
||||
}
|
||||
@ -15,13 +15,11 @@ TileComponent::TileComponent(int x, int y, int w, int h, int id)
|
||||
this->tileRect.h = h;
|
||||
tileID = id;
|
||||
|
||||
auto it = textureDict.textureDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h)
|
||||
if (it == textureDict.textureDictionary.end()) {
|
||||
auto it = textureDict.tileDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h)
|
||||
if (it == textureDict.tileDictionary.end()) {
|
||||
std::cout << "it end" << std::endl;
|
||||
return;
|
||||
}
|
||||
bool test = it == textureDict.textureDictionary.end();
|
||||
// std::cout << it->second.data() << std::endl;
|
||||
this->path = it->second.data();
|
||||
}
|
||||
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
#include "Game.h"
|
||||
#include "Constants.h"
|
||||
#include <ctime>
|
||||
|
||||
Game* game = nullptr;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
srand(time(NULL));
|
||||
const int frameDelay = 1000 / FPS;
|
||||
|
||||
Uint32 frameStart;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user