first commit

This commit is contained in:
2021-01-29 21:14:20 -05:00
commit b2acceb4b9
78 changed files with 6774 additions and 0 deletions

37
src/game/Collision.cpp Normal file
View File

@ -0,0 +1,37 @@
/*
* Collision.cpp
*
* Created on: Mar 8, 2020
* Author: ayoungblood
*/
#include "Collision.h"
#include "../ecs/ColliderComponent.h"
bool Collision::AABB(const SDL_Rect& recA, const SDL_Rect& recB)
{
if(
recA.x + recA.w >= recB.x &&
recB.x + recB.w >= recA.x &&
recA.y + recA.h >= recB.y &&
recB.y + recB.h >= recA.y
)
{
return true;
}
return false;
}
bool Collision::AABB(const ColliderComponent& colA, const ColliderComponent& colB)
{
if(AABB(colA.collider, colB.collider))
{
// std::cout << colA.tag << " hit: " << colB.tag << std::endl;
return true;
}
else
{
return false;
}
}

23
src/game/Collision.h Normal file
View File

@ -0,0 +1,23 @@
/*
* Collision.h
*
* Created on: Mar 8, 2020
* Author: ayoungblood
*/
#ifndef SRC_COLLISION_H_
#define SRC_COLLISION_H_
#include <SDL2/SDL.h>
class ColliderComponent;
class Collision
{
public:
static bool AABB(const SDL_Rect& recA, const SDL_Rect& recB);
static bool AABB(const ColliderComponent& colA, const ColliderComponent& colB);
};
#endif /* SRC_COLLISION_H_ */

324
src/game/Game.cpp Normal file
View File

@ -0,0 +1,324 @@
/*
* Game.cpp
*
* Created on: Feb 9, 2020
* Author: ayoungblood
* Special Thanks to: Carl Birch of Let's Make Games
* Nic Allen
* Brian Lhota
*/
#include "Game.hpp"
#include "../assetmgr/TextureManager.h"
#include "../assetmgr/Map.h"
#include "../ecs/Components.h"
#include "Collision.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "../assetmgr/AssetManager.h"
#include "../ui/UIText.h"
#include "../ui/UINineSlice.h"
#include "../cjson/cJSON.h"
#include "../fsm/GameManager.h"
#include "../fsm/GameManagerState.h"
// #include "../fsm/ConcreteGMStates.h"
Map* map;
Manager manager;
UIText* text;
UINineSlice* my9Slice;
UIText* scoreboardText;
UINineSlice* scoreboard9Slice;
SDL_Renderer* Game::renderer = nullptr;
SDL_Event Game::event;
SDL_Rect Game::camera;
AssetManager* Game::assets = new AssetManager(&manager);
bool Game::isRunning = false;
auto& player(manager.addEntity());
auto& enemy(manager.addEntity());
auto& scoreboard(manager.addEntity());
auto& uiInfo(manager.addEntity());
auto& tree(manager.addEntity());
bool Game::debugCollisionBoxes = false;
int gScale = 0;
Game::Game() {
// TODO Auto-generated constructor stub
}
Game::~Game() {
// TODO Auto-generated destructor stub
}
void Game::init(const char *title, int width, int height, bool fullscreen, int globalScale)
{
camera = { 0, 0, width, height };
int flags = 0;
gScale = globalScale;
if(fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
if(!window)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Couldn't create window: %s", SDL_GetError());
}
renderer = SDL_CreateRenderer(window, -1, 0);
if(!renderer)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s", SDL_GetError());
isRunning = false;
}
else
{
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
isRunning = true;
}
//Initialize SDL_mixer
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048)<0)
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize SDL audio mixer!, Error: %s", SDL_GetError());
}
assets->AddTexture("terrain", "assets/textures/tiles/GS-tiles2.png");
assets->AddTexture("player", "assets/textures/actors/owl_anim.png");
assets->AddTexture("font", "assets/textures/ui/ui-font-tombstone-serif.png");
assets->AddTexture("textBox", "assets/textures/ui/ui-element-tombstone.png");
assets->AddTexture("tree","assets/textures/objects/tree.png");
assets->AddTexture("jewel","assets/textures/objects/jewel.png");
assets->AddTexture("3","assets/textures/ui/day-night-icon.png");
assets->AddTexture("robber","assets/textures/actors/robberrodent.png");
assets->AddMusicTrack("simonZ","assets/audio/music/sailing16.ogg");
assets->AddSoundClip("bwoop","assets/audio/sfx/bwoop.wav");
map = new Map("terrain",globalScale,16);
const char* myText = "Graveyard Shift";
text = new UIText(myText, "font", 0, 0, 8, 12, globalScale);
text->ParseString(myText, 12, 22, globalScale);
SDL_Rect myDestRect = SDL_Rect();
myDestRect.x = 12;
myDestRect.y = 8;
myDestRect.w = 140;
myDestRect.h = 40;
my9Slice = new UINineSlice("textBox");
my9Slice->MakeSlices("textBox",128,128,16,111,16,111,myDestRect,globalScale);
const char* sbText = "500";
scoreboardText = new UIText(sbText, "font", 0, 0, 8, 12, globalScale);
scoreboardText->ParseString(sbText, 190, 22, globalScale);
SDL_Rect scoreboardDestRect = SDL_Rect();
scoreboardDestRect.x = 164;
scoreboardDestRect.y = 8;
scoreboardDestRect.w = 140;
scoreboardDestRect.h = 40;
scoreboard9Slice = new UINineSlice("textBox");
SDL_Rect jewelSrcRect = SDL_Rect();
jewelSrcRect.x = 0;
jewelSrcRect.y = 0;
jewelSrcRect.w = 24;
jewelSrcRect.h = 22;
SDL_Rect jewelDestRect = SDL_Rect();
jewelDestRect.x = 0;
jewelDestRect.y = 0;
jewelDestRect.w = 24*globalScale;
jewelDestRect.h = 22*globalScale;
scoreboard9Slice->MakeSlices("textBox",128,128,16,111,16,111,scoreboardDestRect,globalScale);
scoreboard.addComponent<TransformComponent>(172*globalScale,16*globalScale,24,22,globalScale);
scoreboard.addComponent<SpriteComponent>("jewel", SpriteComponent::spriteUIL1, jewelSrcRect, jewelDestRect);
scoreboard.addGroup(groupUI_Layer1);
tree.addComponent<TransformComponent>(292*globalScale,168*globalScale,192,142,globalScale);
tree.addComponent<SpriteComponent>("tree",SpriteComponent::spriteObject);
tree.addGroup(groupObjects);
//ecs implementation
map->LoadMap("assets/maps/GS-Night.txt",48,32, globalScale);
player.addComponent<TransformComponent>(50*globalScale,50*globalScale,40,40,globalScale);
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/owl_anim.json");
player.addComponent<KeyboardController>();
player.addComponent<ColliderComponent>("player");
player.addGroup(groupPlayers);
enemy.addComponent<TransformComponent>(180*globalScale,180*globalScale,32,32,globalScale);
enemy.addComponent<SpriteComponent>("robber", SpriteComponent::spriteAnimation, "assets/textures/actors/robberrodent.json");
enemy.addGroup(groupEnemies);
printf("Starting up GameManager\n");
GameManager gamemgr = GameManager();
gamemgr.toggle();
gamemgr.toggle();
gamemgr.toggle();
gamemgr.toggle();
gamemgr.toggle();
gamemgr.toggle();
gamemgr.toggle();
// gamemgr.setState(GameManager::setState(GameManagerState::enter(&PauseScreen)));
printf("GameManager Demo Completed.\n");
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't Initialize SDL: %s", SDL_GetError());
isRunning = false;
}
}
//camera.x = camera.x*globalScale;
auto& tiles(manager.getGroup(Game::groupMap));
auto& players(manager.getGroup(Game::groupPlayers));
auto& colliders(manager.getGroup(Game::groupColliders));
auto& objects(manager.getGroup(Game::groupObjects));
auto& enemies(manager.getGroup(Game::groupEnemies));
// auto& projectiles(manager.getGroup(Game::groupProjectiles));
auto& gui(manager.getGroup(Game::groupUI_Layer0));
auto& uiText(manager.getGroup(Game::groupUI_Layer1));
void Game::handleEvents()
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
isRunning = false;
break;
default:
break;
}
}
void Game::update()
{
SDL_Rect playerCol = player.getComponent<ColliderComponent>().collider;
Vector2D playerPos = player.getComponent<TransformComponent>().position;
if (Mix_PlayingMusic() == 0)
{
// std::cout << "Play Music Now" << std::endl;
// Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
}
manager.refresh();
manager.update();
for (auto& c : colliders)
{
SDL_Rect cCol = c->getComponent<ColliderComponent>().collider;
if(Collision::AABB(cCol, playerCol))
{
player.getComponent<TransformComponent>().position = playerPos;
}
}
// for(auto& p: projectiles)
// {
// if(Collision::AABB(player.getComponent<ColliderComponent>().collider, p->getComponent<ColliderComponent>().collider))
// {
// std::cout << "Projectile hit player" << std::endl;
// p->destroy();
// }
// }
camera.x = player.getComponent<TransformComponent>().position.x - camera.w/2;
camera.y = player.getComponent<TransformComponent>().position.y - camera.h/2;
if(camera.x < 0)
camera.x = 0;
if (camera.y < 0)
camera.y = 0;
if (camera.x > map->width-camera.w)
camera.x = map->width-camera.w;
if (camera.y > map->height-camera.h)
camera.y = map->height-camera.h;
}
void Game::render()
{
SDL_RenderClear(renderer);
for (auto& t : tiles)
{
t->draw();
}
if (Game::debugCollisionBoxes)
{
for (auto& c : colliders)
{
c->draw();
}
}
for (auto& o : objects)
{
o->draw();
}
for (auto& e : enemies)
{
e->draw();
}
for (auto& p : players)
{
p->draw();
}
for (auto& guiElement : gui)
{
guiElement->draw();
}
for (auto& letter : uiText)
{
letter->draw();
}
Vector2D origPt;
Vector2D destPt;
origPt.Zero();
destPt.x = 320.0*gScale;
destPt.y = 240.0*gScale;
drawLine(origPt,destPt,255,0,0);
SDL_RenderPresent(renderer);
}
void Game::clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
IMG_Quit();
Mix_Quit();
SDL_Quit();
printf("Game Cleaned\n");
}
void Game::printDebug(char* debugInfo)
{
printf("%s",debugInfo);
printf("\n");
}
void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue)
{
SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
}

59
src/game/Game.hpp Normal file
View File

@ -0,0 +1,59 @@
/*
* Game.hpp
*
* Created on: Feb 9, 2020
* Author: ayoungblood
*/
#ifndef GAME_HPP_
#define GAME_HPP_
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_mixer.h"
#include <stdio.h>
#include <iostream>
#include <vector>
#include "Vector2D.h"
#include "../assetmgr/AssetManager.h"
class ColliderComponent;
class AssetManager;
class Game {
public:
Game();
virtual ~Game();
void init(const char* title, int width, int height, bool fullscreen, int globalScale);
void handleEvents();
void update();
void render();
void clean();
void printDebug(char* debugInfo);
void drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue);
bool running() { return isRunning; }
// static void AddTile(int srcX, int srcY, int xpos, int ypos);
static SDL_Renderer *renderer;
static SDL_Event event;
// static std::vector<ColliderComponent*> colliders;
static bool isRunning;
static bool debugCollisionBoxes;
static SDL_Rect camera;
static AssetManager* assets;
enum groupLabels : std::size_t
{
groupMap,
groupPlayers,
groupEnemies,
groupColliders,
groupProjectiles,
groupObjects,
groupUI_Layer0,
groupUI_Layer1
};
private:
int counter = 0;
// bool isRunning = false;
SDL_Window *window;
// SDL_Renderer *renderer;
};
#endif /* GAME_HPP_ */

77
src/game/Main.cpp Normal file
View File

@ -0,0 +1,77 @@
/*
* main.cpp
*
* Created on: Feb 9, 2020
* Author: ayoungblood
*/
#include "Game.hpp"
#include "../cjson/cJSON.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
Game *game = nullptr;
int main(int argc, const char * argv[])
{
const int FPS = 60;
const int frameDelay = 1000 / FPS;
Uint32 frameStart;
int frameTime;
// =============================
// Load cJSON config.json file
// =============================
std::ifstream jsonText("src/config/config.json");
std::ostringstream tmp;
tmp << jsonText.rdbuf();
std::string json = tmp.str();
cJSON * myJSON = cJSON_Parse(json.c_str());
cJSON * windowName = cJSON_GetObjectItemCaseSensitive(myJSON, "WindowName");
// if (cJSON_IsString(windowName) && (windowName->valuestring != NULL))
// {
// printf("Window Name is: %s\n", windowName->valuestring);
// }
cJSON * windowSize = cJSON_GetObjectItem(myJSON, "WindowSize");
int windowWidth = cJSON_GetObjectItem(windowSize, "w")->valueint;
int windowHeight = cJSON_GetObjectItem(windowSize, "h")->valueint;
// printf("Window:\nwidth:%d\nheight:%d\n",windowWidth,windowHeight);
int windowFS = cJSON_GetObjectItem(myJSON, "WindowFullScreen")->valueint;
int globalScale = cJSON_GetObjectItem(myJSON, "GlobalScale")->valueint;
bool isWindowFS;
if (windowFS==0)
{
isWindowFS = false;
} else
{
isWindowFS = true;
}
windowWidth = windowWidth*globalScale;
windowHeight = windowHeight*globalScale;
game = new Game();
game->init(windowName->valuestring, windowWidth, windowHeight, isWindowFS, globalScale);
// cJSON memory management
cJSON_Delete(myJSON);
while (game->running())
{
frameStart = SDL_GetTicks();
game->handleEvents();
game->update();
game->render();
frameTime = SDL_GetTicks() - frameStart;
if(frameDelay > frameTime)
{
SDL_Delay(frameDelay - frameTime);
}
}
game->clean();
return 0;
}

122
src/game/Vector2D.cpp Normal file
View File

@ -0,0 +1,122 @@
/*
* Vector2D.cpp
*
* Created on: Mar 1, 2020
* Author: ayoungblood
*/
#include "Vector2D.h"
Vector2D::Vector2D()
{
x = 0.0f;
y = 0.0f;
r = 0.0f;
t = 0.0f;
}
Vector2D::Vector2D(float x, float y)
{
this->x = x;
this->y = y;
this->r = sqrt(pow(x,2.0)+pow(y,2.0));
this->t = atan(y/x);
}
Vector2D::Vector2D(float r, float t, bool isPolar)
{
this->x = r*(cos(t));
this->y = r*(sin(t));
this->r = r;
this->t = t;
}
Vector2D& Vector2D::Add(const Vector2D& vec)
{
this->x += vec.x;
this->y += vec.y;
return *this;
}
Vector2D& Vector2D::Subtract(const Vector2D& vec)
{
this->x -= vec.x;
this->y -= vec.y;
return *this;
}
Vector2D& Vector2D::Multiply(const Vector2D& vec)
{
this->x *= vec.x;
this->y *= vec.y;
return *this;
}
Vector2D& Vector2D::Divide(const Vector2D& vec)
{
this->x /= vec.x;
this->y /= vec.y;
return *this;
}
Vector2D& operator+(Vector2D& v1, const Vector2D& v2)
{
return v1.Add(v2);
}
Vector2D& operator-(Vector2D& v1, const Vector2D& v2)
{
return v1.Subtract(v2);
}
Vector2D& operator*(Vector2D& v1, const Vector2D& v2)
{
return v1.Multiply(v2);
}
Vector2D& operator/(Vector2D& v1, const Vector2D& v2)
{
return v1.Divide(v2);
}
Vector2D& Vector2D::operator +=(const Vector2D& vec)
{
return this->Add(vec);
}
Vector2D& Vector2D::operator -=(const Vector2D& vec)
{
return this->Subtract(vec);
}
Vector2D& Vector2D::operator *=(const Vector2D& vec)
{
return this->Multiply(vec);
}
Vector2D& Vector2D::operator /=(const Vector2D& vec)
{
return this->Divide(vec);
}
Vector2D& Vector2D::operator *(const int& i)
{
this->x *= i;
this->y *= i;
return *this;
}
Vector2D& Vector2D::Zero()
{
this->x = 0;
this->y = 0;
return *this;
}
std::ostream& operator<<(std::ostream& stream, const Vector2D& vec)
{
stream << "(" << vec.x << "," << vec.y << ")";
return stream;
}

50
src/game/Vector2D.h Normal file
View File

@ -0,0 +1,50 @@
/*
* Vector2d.h
*
* Created on: Feb 22, 2020
* Author: ayoungblood
*/
#ifndef SRC_ECS_VECTOR2D_H_
#define SRC_ECS_VECTOR2D_H_
#include <iostream>
#include <cmath>
class Vector2D
{
public:
float x;
float y;
float r;
float t;
Vector2D();
Vector2D(float x, float y);
Vector2D(float r, float t, bool isPolar);
Vector2D& Add(const Vector2D& vec);
Vector2D& Subtract(const Vector2D& vec);
Vector2D& Multiply(const Vector2D& vec);
Vector2D& Divide(const Vector2D& vec);
friend Vector2D& operator+(Vector2D& v1, const Vector2D& v2);
friend Vector2D& operator-(Vector2D& v1, const Vector2D& v2);
friend Vector2D& operator*(Vector2D& v1, const Vector2D& v2);
friend Vector2D& operator/(Vector2D& v1, const Vector2D& v2);
Vector2D& operator+=(const Vector2D& vec);
Vector2D& operator-=(const Vector2D& vec);
Vector2D& operator*=(const Vector2D& vec);
Vector2D& operator/=(const Vector2D& vec);
Vector2D& operator*(const int& i);
Vector2D& Zero();
friend std::ostream& operator<<(std::ostream& stream, const Vector2D& vec);
};
#endif /* SRC_ECS_VECTOR2D_H_ */