From 428c401f9c1053f7e13ffe641758dfb72791d8dc Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 26 Jun 2015 19:59:28 -0400 Subject: Player now moves --- src/systems/controlling.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/systems/controlling.h (limited to 'src/systems/controlling.h') diff --git a/src/systems/controlling.h b/src/systems/controlling.h new file mode 100644 index 0000000..61f86eb --- /dev/null +++ b/src/systems/controlling.h @@ -0,0 +1,26 @@ +#ifndef CONTROLLING_H_80B1BB8D +#define CONTROLLING_H_80B1BB8D + +#include "system.h" +#include + +class ControllingSystem : public System { + public: + ControllingSystem(Game& game) + : System(game) {} + + void tick(double dt); + void input(int key, int action); + + private: + void walkLeft(int entity); + void walkRight(int entity); + void stopWalking(int entity); + void jump(int entity); + void stopJumping(int entity); + void drop(int entity, bool start); + + std::queue> actions; +}; + +#endif /* end of include guard: CONTROLLING_H_80B1BB8D */ -- cgit 1.4.1 From da3df061699203eccc9a0c98becaee3ce8050a4f Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 5 Feb 2018 11:51:24 -0500 Subject: Whitespace changes --- src/components/controllable.h | 6 +- src/components/droppable.h | 2 +- src/components/ponderable.h | 2 +- src/components/transformable.cpp | 2 +- src/components/transformable.h | 6 +- src/entity_manager.cpp | 8 +- src/entity_manager.h | 64 +++++------ src/game.cpp | 10 +- src/game.h | 6 +- src/main.cpp | 10 +- src/renderer.cpp | 228 +++++++++++++++++++-------------------- src/renderer.h | 2 +- src/system.h | 4 +- src/system_manager.h | 8 +- src/systems/controlling.cpp | 12 +-- src/systems/controlling.h | 6 +- src/systems/pondering.cpp | 6 +- src/systems/pondering.h | 2 +- src/systems/rendering.h | 4 +- 19 files changed, 194 insertions(+), 194 deletions(-) (limited to 'src/systems/controlling.h') diff --git a/src/components/controllable.h b/src/components/controllable.h index 317d68d..baccf13 100644 --- a/src/components/controllable.h +++ b/src/components/controllable.h @@ -14,20 +14,20 @@ class ControllableComponent : public Component { void setJumpKey(int k); int getDropKey() const; void setDropKey(int k); - + bool isFrozen() const; void setFrozen(bool f); bool isHoldingLeft() const; void setHoldingLeft(bool f); bool isHoldingRight() const; void setHoldingRight(bool f); - + private: int leftKey = GLFW_KEY_LEFT; int rightKey = GLFW_KEY_RIGHT; int jumpKey = GLFW_KEY_UP; int dropKey = GLFW_KEY_DOWN; - + bool frozen = false; bool holdingLeft = false; bool holdingRight = false; diff --git a/src/components/droppable.h b/src/components/droppable.h index 1f5608b..83fcb9d 100644 --- a/src/components/droppable.h +++ b/src/components/droppable.h @@ -7,7 +7,7 @@ class DroppableComponent : public Component { public: void setDroppable(bool can); bool isDroppable() const; - + private: bool droppable = false; }; diff --git a/src/components/ponderable.h b/src/components/ponderable.h index 5aab4b3..c836d2a 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -13,7 +13,7 @@ class PonderableComponent : public Component { void setAccelX(double v); double getAccelY() const; void setAccelY(double v); - + private: double velocityX = 0.0; double velocityY = 0.0; diff --git a/src/components/transformable.cpp b/src/components/transformable.cpp index 0d6b67e..89b1e5d 100644 --- a/src/components/transformable.cpp +++ b/src/components/transformable.cpp @@ -3,7 +3,7 @@ TransformableComponent::TransformableComponent(double x, double y, int w, int h) : x(x), y(y), w(w), h(h) { - + } double TransformableComponent::getX() const diff --git a/src/components/transformable.h b/src/components/transformable.h index 87ba84d..69f4f0e 100644 --- a/src/components/transformable.h +++ b/src/components/transformable.h @@ -6,17 +6,17 @@ class TransformableComponent : public Component { public: TransformableComponent(double x, double y, int w, int h); - + double getX() const; double getY() const; int getW() const; int getH() const; - + void setX(double v); void setY(double v); void setW(int v); void setH(int v); - + private: double x; double y; diff --git a/src/entity_manager.cpp b/src/entity_manager.cpp index 4bdfe8a..9ad758b 100644 --- a/src/entity_manager.cpp +++ b/src/entity_manager.cpp @@ -10,13 +10,13 @@ std::set EntityManager::getEntitiesWithComponents<>(std::set& cache = cachedComponents[componentTypes]; for (auto& entity : entities) { EntityData& data = entity.second; bool cacheEntity = true; - + for (auto& componentType : componentTypes) { if (data.components.count(componentType) == 0) @@ -25,13 +25,13 @@ std::set EntityManager::getEntitiesWithComponents<>(std::set> components; }; - + std::map entities; std::map, std::set> cachedComponents; - + int nextEntityID = 0; - + template std::set getEntitiesWithComponentsHelper(std::set& componentTypes) { componentTypes.insert(typeid(T)); - + return getEntitiesWithComponents(componentTypes); } - + template std::set getEntitiesWithComponents(std::set& componentTypes) { return getEntitiesWithComponentsHelper(componentTypes); } - + public: EntityManager() = default; EntityManager(const EntityManager& copy) = delete; - + int emplaceEntity() { // Find a suitable entity ID @@ -44,76 +44,76 @@ class EntityManager { { nextEntityID++; } - + if (nextEntityID < 0) { nextEntityID = 0; - + while ((entities.count(nextEntityID) == 1) && (nextEntityID >= 0)) { nextEntityID++; } - + assert(nextEntityID >= 0); } - + // Initialize the data int id = nextEntityID++; entities[id]; - + return id; } - + void deleteEntity(int entity) { assert(entities.count(entity) == 1); - + // Uncache components for (auto& cache : cachedComponents) { cache.second.erase(entity); } - + // Destroy the data entities.erase(entity); } - + template T& emplaceComponent(int entity, Args&&... args) { assert(entities.count(entity) == 1); - + EntityData& data = entities[entity]; std::type_index componentType = typeid(T); - + assert(data.components.count(componentType) == 0); - + // Initialize the component std::unique_ptr ptr = std::unique_ptr(new T(std::forward(args)...)); T& component = *ptr; data.components[componentType] = std::move(ptr); - + // Invalidate related caches erase_if(cachedComponents, [&componentType] (std::pair, std::set>& cache) { return cache.first.count(componentType) == 1; }); - + return component; } - + template void removeComponent(int entity) { assert(entities.count(entity) == 1); - + EntityData& data = entities[entity]; std::type_index componentType = typeid(T); - + assert(data.components.count(componentType) == 1); - + // Destroy the component data.components.erase(componentType); - + // Uncache the component for (auto& cache : cachedComponents) { @@ -123,25 +123,25 @@ class EntityManager { } } } - + template T& getComponent(int entity) { assert(entities.count(entity) == 1); - + EntityData& data = entities[entity]; std::type_index componentType = typeid(T); - + assert(data.components.count(componentType) == 1); - + return *((T*)data.components[componentType].get()); } - + template std::set getEntitiesWithComponents() { std::set componentTypes; - + return getEntitiesWithComponentsHelper(componentTypes); } }; diff --git a/src/game.cpp b/src/game.cpp index b3fa9a8..5d1ec18 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -45,24 +45,24 @@ void Game::execute() double lastTime = glfwGetTime(); const double dt = 0.01; double accumulator = 0.0; - + while (!(shouldQuit || glfwWindowShouldClose(window))) { double currentTime = glfwGetTime(); double frameTime = currentTime - lastTime; lastTime = currentTime; - + glfwPollEvents(); - + accumulator += frameTime; while (accumulator >= dt) { systemManager.getSystem().tick(dt); systemManager.getSystem().tick(dt); - + accumulator -= dt; } - + systemManager.getSystem().tick(frameTime); } } diff --git a/src/game.h b/src/game.h index 3822700..ec667c8 100644 --- a/src/game.h +++ b/src/game.h @@ -8,12 +8,12 @@ class Game { public: Game(GLFWwindow* window); - + void execute(); EntityManager& getEntityManager(); - + friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); - + private: EntityManager entityManager; SystemManager systemManager; diff --git a/src/main.cpp b/src/main.cpp index 35749f5..d51da7d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,20 +8,20 @@ int main() { srand(time(NULL)); - + GLFWwindow* window = initRenderer(); glfwSwapInterval(1); - + initMuxer(); - + // Put this in a block so game goes out of scope before we destroy the renderer { Game game {window}; game.execute(); } - + destroyMuxer(); destroyRenderer(); - + return 0; } diff --git a/src/renderer.cpp b/src/renderer.cpp index 99d5389..d0d2b75 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -56,11 +56,11 @@ static GLuint mesh_normalbuffer; static int mesh_numvertices; GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) -{ +{ // Create the shaders GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); - + // Read the Vertex Shader code from the file std::string VertexShaderCode; std::ifstream VertexShaderStream(vertex_file_path, std::ios::in); @@ -71,7 +71,7 @@ GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) VertexShaderCode += "\n" + Line; VertexShaderStream.close(); } - + // Read the Fragment Shader code from the file std::string FragmentShaderCode; std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in); @@ -81,53 +81,53 @@ GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) FragmentShaderCode += "\n" + Line; FragmentShaderStream.close(); } - + GLint Result = GL_FALSE; int InfoLogLength; - + // Compile Vertex Shader printf("Compiling shader : %s\n", vertex_file_path); char const * VertexSourcePointer = VertexShaderCode.c_str(); glShaderSource(VertexShaderID, 1, &VertexSourcePointer , nullptr); glCompileShader(VertexShaderID); - + // Check Vertex Shader glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector VertexShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(VertexShaderID, InfoLogLength, nullptr, &VertexShaderErrorMessage[0]); fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); - + // Compile Fragment Shader printf("Compiling shader : %s\n", fragment_file_path); char const * FragmentSourcePointer = FragmentShaderCode.c_str(); glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , nullptr); glCompileShader(FragmentShaderID); - + // Check Fragment Shader glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector FragmentShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(FragmentShaderID, InfoLogLength, nullptr, &FragmentShaderErrorMessage[0]); fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]); - + // Link the program fprintf(stdout, "Linking program\n"); GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); glLinkProgram(ProgramID); - + // Check the program glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector ProgramErrorMessage( glm::max(InfoLogLength, int(1)) ); glGetProgramInfoLog(ProgramID, InfoLogLength, nullptr, &ProgramErrorMessage[0]); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); - + glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); - + return ProgramID; } @@ -135,14 +135,14 @@ void flipImageData(unsigned char* data, int width, int height, int comps) { unsigned char* data_copy = (unsigned char*) malloc(width*height*comps*sizeof(unsigned char)); memcpy(data_copy, data, width*height*comps); - + int row_size = width * comps; - + for (int i=0;i& out_vertices, std::v fprintf(stderr, "Could not open mesh file %s\n", filename); exit(1); } - + std::vector temp_vertices; std::vector temp_uvs; std::vector temp_normals; - + for (;;) { char lineHeader[256]; @@ -167,7 +167,7 @@ void loadMesh(const char* filename, std::vector& out_vertices, std::v { break; } - + if (!strncmp(lineHeader, "v", 2)) { glm::vec3 vertex; @@ -187,7 +187,7 @@ void loadMesh(const char* filename, std::vector& out_vertices, std::v { int vertexIDs[3], uvIDs[3], normalIDs[3]; fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIDs[0], &uvIDs[0], &normalIDs[0], &vertexIDs[1], &uvIDs[1], &normalIDs[1], &vertexIDs[2], &uvIDs[2], &normalIDs[2]); - + for (int i=0; i<3; i++) { out_vertices.push_back(temp_vertices[vertexIDs[i] - 1]); @@ -202,24 +202,24 @@ void setFramebufferSize(GLFWwindow* w, int width, int height) { buffer_width = width; buffer_height = height; - + glDeleteFramebuffers(1, &bloom_framebuffer); glDeleteRenderbuffers(1, &bloom_depthbuffer); - + glGenFramebuffers(1, &bloom_framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, bloom_framebuffer); GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT1}; glDrawBuffers(1, DrawBuffers); - + glGenRenderbuffers(1, &bloom_depthbuffer); glBindRenderbuffer(GL_RENDERBUFFER, bloom_depthbuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, bloom_depthbuffer); - + glDeleteTextures(1, &preBloomTex); glDeleteTextures(1, &bloomPassTex1); glDeleteTextures(1, &bloomPassTex2); - + glGenTextures(1, &preBloomTex); glBindTexture(GL_TEXTURE_2D, preBloomTex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); @@ -227,7 +227,7 @@ void setFramebufferSize(GLFWwindow* w, int width, int height) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - + glGenTextures(1, &bloomPassTex1); glBindTexture(GL_TEXTURE_2D, bloomPassTex1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width/4, height/4, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); @@ -235,7 +235,7 @@ void setFramebufferSize(GLFWwindow* w, int width, int height) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - + glGenTextures(1, &bloomPassTex2); glBindTexture(GL_TEXTURE_2D, bloomPassTex2); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width/4, height/4, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); @@ -252,20 +252,20 @@ GLFWwindow* initRenderer() fprintf(stderr, "Renderer already initialized\n"); exit(-1); } - + // Initialize GLFW if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); exit(-1); } - + glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want version 3.3 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Mac requires this glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - + // Create a window window = glfwCreateWindow(1024, 768, "Aromatherapy", nullptr, nullptr); if (window == nullptr) @@ -274,7 +274,7 @@ GLFWwindow* initRenderer() glfwTerminate(); exit(-1); } - + glfwMakeContextCurrent(window); glewExperimental = true; // Needed in core profile if (glewInit() != GLEW_OK) @@ -282,37 +282,37 @@ GLFWwindow* initRenderer() fprintf(stderr, "Failed to initialize GLEW\n"); exit(-1); } - + glfwSetFramebufferSizeCallback(window, &setFramebufferSize); - + // Set up vertex array object glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); - + // Enable depth testing glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); - + // Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - + // Set up the framebuffer glGenFramebuffers(1, &generic_framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; glDrawBuffers(1, DrawBuffers); - + glGenFramebuffers(1, &bloom_framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, bloom_framebuffer); GLenum DrawBuffers2[1] = {GL_COLOR_ATTACHMENT1}; glDrawBuffers(1, DrawBuffers2); - + glGenRenderbuffers(1, &bloom_depthbuffer); glBindRenderbuffer(GL_RENDERBUFFER, bloom_depthbuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1024, 768); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, bloom_depthbuffer); - + // Set up the NTSC rendering buffers glGenTextures(1, &renderedTex1); glBindTexture(GL_TEXTURE_2D, renderedTex1); @@ -322,7 +322,7 @@ GLFWwindow* initRenderer() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); renderedTexBufs[0] = renderedTex1; - + glGenTextures(1, &renderedTex2); glBindTexture(GL_TEXTURE_2D, renderedTex2); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, GAME_WIDTH, GAME_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); @@ -331,7 +331,7 @@ GLFWwindow* initRenderer() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); renderedTexBufs[1] = renderedTex2; - + // Set up bloom rendering buffers glGenTextures(1, &preBloomTex); glBindTexture(GL_TEXTURE_2D, preBloomTex); @@ -340,7 +340,7 @@ GLFWwindow* initRenderer() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - + glGenTextures(1, &bloomPassTex1); glBindTexture(GL_TEXTURE_2D, bloomPassTex1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024/4, 768/4, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); @@ -348,7 +348,7 @@ GLFWwindow* initRenderer() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - + glGenTextures(1, &bloomPassTex2); glBindTexture(GL_TEXTURE_2D, bloomPassTex2); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024/4, 768/4, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); @@ -356,31 +356,31 @@ GLFWwindow* initRenderer() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - + curBuf = 0; - + // Load the mesh! std::vector mesh_vertices; std::vector mesh_uvs; std::vector mesh_normals; loadMesh("res/monitor-fef.obj", mesh_vertices, mesh_uvs, mesh_normals); - + mesh_numvertices = mesh_vertices.size(); - + glGenBuffers(1, &mesh_vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, mesh_vertexbuffer); glBufferData(GL_ARRAY_BUFFER, mesh_vertices.size() * sizeof(glm::vec3), &mesh_vertices[0], GL_STATIC_DRAW); - + glGenBuffers(1, &mesh_uvbuffer); glBindBuffer(GL_ARRAY_BUFFER, mesh_uvbuffer); glBufferData(GL_ARRAY_BUFFER, mesh_uvs.size() * sizeof(glm::vec3), &mesh_uvs[0], GL_STATIC_DRAW); - + glGenBuffers(1, &mesh_normalbuffer); glBindBuffer(GL_ARRAY_BUFFER, mesh_normalbuffer); glBufferData(GL_ARRAY_BUFFER, mesh_normals.size() * sizeof(glm::vec3), &mesh_normals[0], GL_STATIC_DRAW); - + // Load the vertices of a flat surface - GLfloat g_quad_vertex_buffer_data[] = { + GLfloat g_quad_vertex_buffer_data[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, @@ -392,7 +392,7 @@ GLFWwindow* initRenderer() glGenBuffers(1, &quad_vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW); - + glGenTextures(1, &artifactsTex); glBindTexture(GL_TEXTURE_2D, artifactsTex); int atdw, atdh; @@ -403,7 +403,7 @@ GLFWwindow* initRenderer() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); glGenerateMipmap(GL_TEXTURE_2D); - + glGenTextures(1, &scanlinesTex); glBindTexture(GL_TEXTURE_2D, scanlinesTex); int stdw, stdh; @@ -414,7 +414,7 @@ GLFWwindow* initRenderer() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); glGenerateMipmap(GL_TEXTURE_2D); - + // Load the shaders ntscShader = LoadShaders("shaders/ntsc.vertex", "shaders/ntsc.fragment"); finalShader = LoadShaders("shaders/final.vertex", "shaders/final.fragment"); @@ -422,9 +422,9 @@ GLFWwindow* initRenderer() fillShader = LoadShaders("shaders/fill.vertex", "shaders/fill.fragment"); bloom1Shader = LoadShaders("shaders/bloom1.vertex", "shaders/bloom1.fragment"); bloom2Shader = LoadShaders("shaders/bloom2.vertex", "shaders/bloom2.fragment"); - + rendererInitialized = true; - + return window; } @@ -435,13 +435,13 @@ void destroyRenderer() fprintf(stderr, "Renderer not initialized\n"); exit(-1); } - + // Delete the plane buffer glDeleteBuffers(1, &quad_vertexbuffer); glDeleteBuffers(1, &mesh_vertexbuffer); glDeleteBuffers(1, &mesh_uvbuffer); glDeleteBuffers(1, &mesh_normalbuffer); - + // Delete the shaders glDeleteProgram(ntscShader); glDeleteProgram(finalShader); @@ -449,7 +449,7 @@ void destroyRenderer() glDeleteProgram(fillShader); glDeleteProgram(bloom1Shader); glDeleteProgram(bloom2Shader); - + // Delete the NTSC rendering buffers glDeleteTextures(1, &renderedTex1); glDeleteTextures(1, &renderedTex2); @@ -458,18 +458,18 @@ void destroyRenderer() glDeleteTextures(1, &preBloomTex); glDeleteTextures(1, &bloomPassTex1); glDeleteTextures(1, &bloomPassTex2); - + // Delete the framebuffer glDeleteRenderbuffers(1, &bloom_depthbuffer); glDeleteFramebuffers(1, &bloom_framebuffer); glDeleteFramebuffers(1, &generic_framebuffer); - + // Delete the VAO glDeleteVertexArrays(1, &VertexArrayID); - + // Kill the window glfwTerminate(); - + rendererInitialized = false; } @@ -480,10 +480,10 @@ Texture::Texture(int width, int height) fprintf(stderr, "Renderer not initialized\n"); exit(-1); } - + this->width = width; this->height = height; - + glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); @@ -500,7 +500,7 @@ Texture::Texture(const char* filename) fprintf(stderr, "Renderer not initialized\n"); exit(-1); } - + glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); unsigned char* data = stbi_load(filename, &width, &height, 0, 4); @@ -520,14 +520,14 @@ Texture::Texture(const Texture& tex) fprintf(stderr, "Renderer not initialized\n"); exit(-1); } - + width = tex.width; height = tex.height; - + unsigned char* data = (unsigned char*) malloc(4 * width * height); glBindTexture(GL_TEXTURE_2D, tex.texID); glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - + glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); @@ -535,7 +535,7 @@ Texture::Texture(const Texture& tex) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - + free(data); } @@ -551,14 +551,14 @@ Texture::~Texture() fprintf(stderr, "Renderer not initialized\n"); exit(-1); } - + glDeleteTextures(1, &texID); } Texture& Texture::operator= (Texture tex) { swap(*this, tex); - + return *this; } @@ -576,17 +576,17 @@ void Texture::fill(Rectangle dstrect, int r, int g, int b) fprintf(stderr, "Renderer not initialized\n"); exit(-1); } - + // Target the framebuffer glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texID, 0); - + // Set up the vertex attributes GLfloat minx = (GLfloat) dstrect.x / width * 2.0 - 1.0; GLfloat miny = -((GLfloat) dstrect.y / height * 2.0 - 1.0); GLfloat maxx = (GLfloat) (dstrect.x + dstrect.w) / width * 2.0 - 1.0; GLfloat maxy = -((GLfloat) (dstrect.y + dstrect.h) / height * 2.0 - 1.0); - + GLfloat vertexbuffer_data[] = { minx, miny, maxx, miny, @@ -601,14 +601,14 @@ void Texture::fill(Rectangle dstrect, int r, int g, int b) glBufferData(GL_ARRAY_BUFFER, sizeof(vertexbuffer_data), vertexbuffer_data, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); - + glViewport(0, 0, width, height); glClear(GL_DEPTH_BUFFER_BIT); glUseProgram(fillShader); glUniform3f(glGetUniformLocation(fillShader, "vecColor"), r / 255.0, g / 255.0, b / 255.0); - + glDrawArrays(GL_TRIANGLES, 0, 6); - + glDisableVertexAttribArray(0); glDeleteBuffers(1, &vertexbuffer); } @@ -620,19 +620,19 @@ void Texture::blit(const Texture& srctex, Rectangle srcrect, Rectangle dstrect, fprintf(stderr, "Renderer not initialized\n"); exit(-1); } - + alpha = glm::clamp(alpha, 0.0, 1.0); - + // Target the framebuffer glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texID, 0); - + // Set up the vertex attributes GLfloat minx = (GLfloat) dstrect.x / width * 2.0 - 1.0; GLfloat miny = -((GLfloat) dstrect.y / height * 2.0 - 1.0); GLfloat maxx = (GLfloat) (dstrect.x + dstrect.w) / width * 2.0 - 1.0; GLfloat maxy = -((GLfloat) (dstrect.y + dstrect.h) / height * 2.0 - 1.0); - + GLfloat vertexbuffer_data[] = { minx, miny, maxx, miny, @@ -645,12 +645,12 @@ void Texture::blit(const Texture& srctex, Rectangle srcrect, Rectangle dstrect, glBufferData(GL_ARRAY_BUFFER, sizeof(vertexbuffer_data), vertexbuffer_data, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); - + GLfloat minu = (GLfloat) srcrect.x / srctex.width; GLfloat minv = 1 - ((GLfloat) srcrect.y / srctex.height); GLfloat maxu = (GLfloat) (srcrect.x + srcrect.w) / srctex.width; GLfloat maxv = 1 - ((GLfloat) (srcrect.y + srcrect.h) / srctex.height); - + GLfloat texcoordbuffer_data[] = { minu, minv, maxu, minv, @@ -663,20 +663,20 @@ void Texture::blit(const Texture& srctex, Rectangle srcrect, Rectangle dstrect, glBufferData(GL_ARRAY_BUFFER, sizeof(texcoordbuffer_data), texcoordbuffer_data, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); - + // Set up the shader glUseProgram(blitShader); glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, width, height); - + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, srctex.texID); glUniform1i(glGetUniformLocation(blitShader, "srctex"), 0); glUniform1f(glGetUniformLocation(blitShader, "alpha"), alpha); - + // Blit! glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - + // Unload everything glDisableVertexAttribArray(1); glDisableVertexAttribArray(0); @@ -691,11 +691,11 @@ void bloomPass1(GLuint srcTex, GLuint dstTex, bool horizontal, glm::vec2 srcRes, glViewport(0,0,dstRes.x,dstRes.y); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(bloom1Shader); - + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, srcTex); glUniform1i(glGetUniformLocation(bloom1Shader, "inTex"), 0); - + glm::vec2 offset = glm::vec2(0.0); if (horizontal) { @@ -703,9 +703,9 @@ void bloomPass1(GLuint srcTex, GLuint dstTex, bool horizontal, glm::vec2 srcRes, } else { offset.y = 1.2/srcRes.y; } - + glUniform2f(glGetUniformLocation(bloom1Shader, "offset"), offset.x, offset.y); - + glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); @@ -720,17 +720,17 @@ void Texture::renderScreen() const fprintf(stderr, "Renderer not initialized\n"); exit(-1); } - + // First we're going to composite our frame with the previous frame // We start by setting up the framebuffer glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexBufs[curBuf], 0); - + // Set up the shader glViewport(0,0,GAME_WIDTH,GAME_HEIGHT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(ntscShader); - + // Use the current frame texture, nearest neighbor and clamped to edge glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texID); @@ -739,7 +739,7 @@ void Texture::renderScreen() const glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glUniform1i(glGetUniformLocation(ntscShader, "curFrameSampler"), 0); - + // Use the previous frame composite texture, nearest neighbor and clamped to edge glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, renderedTexBufs[(curBuf + 1) % 2]); @@ -748,13 +748,13 @@ void Texture::renderScreen() const glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glUniform1i(glGetUniformLocation(ntscShader, "prevFrameSampler"), 1); - + // Load the NTSC artifact texture glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, artifactsTex); glUniform1i(glGetUniformLocation(ntscShader, "NTSCArtifactSampler"), 2); glUniform1f(glGetUniformLocation(ntscShader, "NTSCLerp"), curBuf * 1.0); - + if ((rand() % 60) == 0) { // Change the 0.0 to a 1.0 or a 10.0 for a glitchy effect! @@ -762,7 +762,7 @@ void Texture::renderScreen() const } else { glUniform1f(glGetUniformLocation(ntscShader, "Tuning_NTSC"), 0.0); } - + // Render our composition glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); @@ -776,7 +776,7 @@ void Texture::renderScreen() const glViewport(0,0,buffer_width,buffer_height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(finalShader); - + // Use the composited frame texture, linearly filtered and filling in black for the border glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, renderedTexBufs[curBuf]); @@ -788,69 +788,69 @@ void Texture::renderScreen() const glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color); glGenerateMipmap(GL_TEXTURE_2D); glUniform1i(glGetUniformLocation(finalShader, "rendertex"), 0); - + // Use the scanlines texture glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, scanlinesTex); glUniform1i(glGetUniformLocation(finalShader, "scanlinestex"), 1); - + // Initialize the MVP matrices glm::mat4 p_matrix = glm::perspective(42.5f, (float) buffer_width / (float) buffer_height, 0.1f, 100.0f); glm::mat4 v_matrix = glm::lookAt(glm::vec3(2,0,0), glm::vec3(0,0,0), glm::vec3(0,1,0)); glm::mat4 m_matrix = glm::mat4(1.0); glm::mat4 mvp_matrix = p_matrix * v_matrix * m_matrix; - + glUniformMatrix4fv(glGetUniformLocation(finalShader, "MVP"), 1, GL_FALSE, &mvp_matrix[0][0]); glUniformMatrix4fv(glGetUniformLocation(finalShader, "worldMat"), 1, GL_FALSE, &m_matrix[0][0]); glUniform2f(glGetUniformLocation(finalShader, "resolution"), buffer_width, buffer_height); glUniform1f(glGetUniformLocation(finalShader, "iGlobalTime"), glfwGetTime()); - + glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, mesh_vertexbuffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); - + glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, mesh_normalbuffer); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); - + glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, mesh_uvbuffer); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); - + glDrawArrays(GL_TRIANGLES, 0, mesh_numvertices); glDisableVertexAttribArray(2); glDisableVertexAttribArray(1); glDisableVertexAttribArray(0); - + // First pass of bloom! glm::vec2 buffer_size = glm::vec2(buffer_width, buffer_height); bloomPass1(preBloomTex, bloomPassTex1, true, buffer_size, buffer_size / 4.0f); bloomPass1(bloomPassTex1, bloomPassTex2, false, buffer_size / 4.0f, buffer_size / 4.0f); - + // Do the second pass of bloom and render to screen glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, buffer_width, buffer_height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(bloom2Shader); - + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, preBloomTex); glUniform1i(glGetUniformLocation(bloom2Shader, "clearTex"), 0); - + glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, bloomPassTex2); glUniform1i(glGetUniformLocation(bloom2Shader, "blurTex"), 1); - + glUniform1f(glGetUniformLocation(bloom2Shader, "iGlobalTime"), glfwGetTime()); - + glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); glDrawArrays(GL_TRIANGLES, 0, 6); glDisableVertexAttribArray(0); - + glfwSwapBuffers(window); - + curBuf = (curBuf + 1) % 2; } diff --git a/src/renderer.h b/src/renderer.h index 84ad688..6dccf7a 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -24,7 +24,7 @@ class Texture { void blit(const Texture& src, Rectangle srcrect, Rectangle dstrect, double alpha = 1.0); void renderScreen() const; Rectangle entirety() const; - + private: GLuint texID; int width; diff --git a/src/system.h b/src/system.h index e08db0a..af3fb77 100644 --- a/src/system.h +++ b/src/system.h @@ -7,9 +7,9 @@ class System { public: System(Game& game) : game(game) {} - + virtual void tick(double dt) = 0; - + protected: Game& game; }; diff --git a/src/system_manager.h b/src/system_manager.h index 8f76db2..087b71c 100644 --- a/src/system_manager.h +++ b/src/system_manager.h @@ -18,18 +18,18 @@ class SystemManager { { std::unique_ptr ptr = std::unique_ptr(new T(game, std::forward(args)...)); std::type_index systemType = typeid(T); - + systems[systemType] = ptr.get(); loop.push_back(std::move(ptr)); } - + template T& getSystem() { std::type_index systemType = typeid(T); - + assert(systems.count(systemType) == 1); - + return *((T*)systems[systemType]); } }; diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index b1e73ad..456da3b 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp @@ -14,12 +14,12 @@ void ControllingSystem::tick(double dt) { int key = actions.front().first; int action = actions.front().second; - + auto entities = game.getEntityManager().getEntitiesWithComponents(); for (auto entity : entities) { auto& controllable = game.getEntityManager().getComponent(entity); - + if (action == GLFW_PRESS) { if (key == controllable.getLeftKey()) @@ -33,7 +33,7 @@ void ControllingSystem::tick(double dt) } else if (key == controllable.getRightKey()) { controllable.setHoldingRight(true); - + if (!controllable.isFrozen()) { walkRight(entity); @@ -56,7 +56,7 @@ void ControllingSystem::tick(double dt) if (key == controllable.getLeftKey()) { controllable.setHoldingLeft(false); - + if (!controllable.isFrozen()) { if (controllable.isHoldingRight()) @@ -69,7 +69,7 @@ void ControllingSystem::tick(double dt) } else if (key == controllable.getRightKey()) { controllable.setHoldingRight(false); - + if (!controllable.isFrozen()) { if (controllable.isHoldingRight()) @@ -94,7 +94,7 @@ void ControllingSystem::tick(double dt) } } } - + actions.pop(); } } diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 61f86eb..30210b3 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h @@ -8,10 +8,10 @@ class ControllingSystem : public System { public: ControllingSystem(Game& game) : System(game) {} - + void tick(double dt); void input(int key, int action); - + private: void walkLeft(int entity); void walkRight(int entity); @@ -19,7 +19,7 @@ class ControllingSystem : public System { void jump(int entity); void stopJumping(int entity); void drop(int entity, bool start); - + std::queue> actions; }; diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 96775d0..50a8bc8 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -6,16 +6,16 @@ void PonderingSystem::tick(double dt) { auto entities = game.getEntityManager().getEntitiesWithComponents(); - + for (auto entity : entities) { auto& transformable = game.getEntityManager().getComponent(entity); auto& ponderable = game.getEntityManager().getComponent(entity); - + // Accelerate ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); ponderable.setVelocityY(ponderable.getVelocityY() + ponderable.getAccelY() * dt); - + // Move transformable.setX(transformable.getX() + ponderable.getVelocityX() * dt); transformable.setY(transformable.getY() + ponderable.getVelocityY() * dt); diff --git a/src/systems/pondering.h b/src/systems/pondering.h index ad01a22..3fe5473 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h @@ -7,7 +7,7 @@ class PonderingSystem : public System { public: PonderingSystem(Game& game) : System(game) {} - + void tick(double dt); }; diff --git a/src/systems/rendering.h b/src/systems/rendering.h index 9b6e27e..cec72e2 100644 --- a/src/systems/rendering.h +++ b/src/systems/rendering.h @@ -9,9 +9,9 @@ class RenderingSystem : public System { public: RenderingSystem(Game& game) : System(game) {} - + void tick(double dt); - + private: Texture texture {GAME_WIDTH, GAME_HEIGHT}; }; -- cgit 1.4.1 From cefe66cdbb8786dc455657376e36f0ff8785d5bc Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 8 Feb 2018 12:34:42 -0500 Subject: Introduced animated sprites Also restyled a lot of the code. --- CMakeLists.txt | 2 +- src/animation.cpp | 35 +++++++++++ src/animation.h | 100 ++++++++++++++++++++++++++++++ src/components/animatable.cpp | 27 --------- src/components/animatable.h | 75 ++++++++++++++++------- src/components/orientable.h | 24 ++++++++ src/components/ponderable.h | 13 ++++ src/game.cpp | 73 +++++++++++++--------- src/game.h | 36 ++++++++--- src/system.h | 12 +++- src/systems/animating.cpp | 38 ++++++++++++ src/systems/animating.h | 20 ++++++ src/systems/controlling.cpp | 138 +++++++++++++++++++++++++++--------------- src/systems/controlling.h | 36 ++++++----- src/systems/pondering.cpp | 10 +-- src/systems/pondering.h | 10 +-- src/systems/rendering.cpp | 40 ++++++++---- src/systems/rendering.h | 15 +++-- 18 files changed, 521 insertions(+), 183 deletions(-) create mode 100644 src/animation.cpp create mode 100644 src/animation.h delete mode 100644 src/components/animatable.cpp create mode 100644 src/components/orientable.h create mode 100644 src/systems/animating.cpp create mode 100644 src/systems/animating.h (limited to 'src/systems/controlling.h') diff --git a/CMakeLists.txt b/CMakeLists.txt index e43b056..0ac3a38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,10 +61,10 @@ add_executable(Aromatherapy src/entity_manager.cpp src/game.cpp src/animation.cpp - src/components/animatable.cpp src/systems/rendering.cpp src/systems/controlling.cpp src/systems/pondering.cpp + src/systems/animating.cpp ) target_link_libraries(Aromatherapy ${ALL_LIBS}) install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR}) diff --git a/src/animation.cpp b/src/animation.cpp new file mode 100644 index 0000000..31ba21f --- /dev/null +++ b/src/animation.cpp @@ -0,0 +1,35 @@ +#include "animation.h" + +AnimationSet::AnimationSet( + Texture texture, + int frameWidth, + int frameHeight, + int framesAcross) : + texture_(std::move(texture)), + frameWidth_(frameWidth), + frameHeight_(frameHeight), + framesAcross_(framesAcross) +{ +} + +void AnimationSet::emplaceAnimation( + std::string animation, + size_t firstFrame, + size_t numFrames, + size_t delay) +{ + animations_.emplace( + std::piecewise_construct, + std::make_tuple(animation), + std::make_tuple(firstFrame, numFrames, delay)); +} + +Rectangle AnimationSet::getFrameRect(int frame) const +{ + return { + frameWidth_ * (frame % framesAcross_), + frameHeight_ * (frame / framesAcross_), + frameWidth_, + frameHeight_ + }; +} diff --git a/src/animation.h b/src/animation.h new file mode 100644 index 0000000..50446d0 --- /dev/null +++ b/src/animation.h @@ -0,0 +1,100 @@ +#ifndef ANIMATION_H_74EB0901 +#define ANIMATION_H_74EB0901 + +#include "renderer.h" +#include +#include +#include + +class Animation { +public: + + Animation( + size_t firstFrame, + size_t numFrames, + size_t delay) : + firstFrame_(firstFrame), + numFrames_(numFrames), + delay_(delay) + { + } + + inline size_t getFirstFrame() const + { + return firstFrame_; + } + + inline size_t getNumFrames() const + { + return numFrames_; + } + + inline size_t getDelay() const + { + return delay_; + } + +private: + + size_t firstFrame_; + size_t numFrames_; + size_t delay_; +}; + +class AnimationSet { +public: + + AnimationSet( + Texture texture, + int frameWidth, + int frameHeight, + int framesAcross); + + void emplaceAnimation( + std::string animation, + size_t firstFrame, + size_t numFrames, + size_t delay); + + inline const Animation& getAnimation(std::string animation) const + { + if (!animations_.count(animation)) + { + throw std::invalid_argument("Animation does not exist"); + } + + return animations_.at(animation); + } + + inline const Texture& getTexture() const + { + return texture_; + } + + inline int getFrameWidth() const + { + return frameWidth_; + } + + inline int getFrameHeight() const + { + return frameHeight_; + } + + inline int getFramesAcross() const + { + return framesAcross_; + } + + Rectangle getFrameRect(int frame) const; + +private: + + std::map animations_; + Texture texture_; + int frameWidth_; + int frameHeight_; + int framesAcross_; +}; + +#endif /* end of include guard: ANIMATION_H_74EB0901 */ diff --git a/src/components/animatable.cpp b/src/components/animatable.cpp deleted file mode 100644 index fcd277c..0000000 --- a/src/components/animatable.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "animatable.h" - -AnimatableComponent::AnimatableComponent(const char* filename, int frame_width, int frame_height, int frames_across) - : texture(filename), frame_width(frame_width), frame_height(frame_height), frames_across(frames_across) -{ - -} - -int AnimatableComponent::getFrame() const -{ - return frame; -} - -void AnimatableComponent::setFrame(int frame) -{ - this->frame = frame; -} - -const Texture& AnimatableComponent::getTexture() const -{ - return texture; -} - -Rectangle AnimatableComponent::getFrameRect() const -{ - return {frame_width * (frame % frames_across), frame_height * (frame / frames_across), frame_width, frame_height}; -} diff --git a/src/components/animatable.h b/src/components/animatable.h index cf6ee54..ed0133e 100644 --- a/src/components/animatable.h +++ b/src/components/animatable.h @@ -2,30 +2,61 @@ #define SPRITE_RENDERABLE_H_D3AACBBF #include "component.h" -#include "renderer.h" -#include "direction.h" +#include "animation.h" +#include class AnimatableComponent : public Component { - public: - AnimatableComponent(const char* filename, int frame_width, int frame_height, int frames_across); - - int getFrame() const; - void setFrame(int frame); - - const Texture& getTexture() const; - Rectangle getFrameRect() const; - - void setDirection(Direction dir) {}; - void setWalking(bool w) {}; - void setJumping(bool w) {}; - void setCrouching(bool w) {}; - - private: - Texture texture; - int frame_width; - int frame_height; - int frames_across; - int frame = 0; +public: + + AnimatableComponent( + AnimationSet animationSet, + std::string animation) : + animationSet_(std::move(animationSet)), + animation_(std::move(animation)) + { + } + + inline size_t getFrame() const + { + return frame_; + } + + inline void setFrame(size_t v) + { + frame_ = v; + } + + inline size_t getCountdown() const + { + return countdown_; + } + + inline void setCountdown(size_t v) + { + countdown_ = v; + } + + inline const AnimationSet& getAnimationSet() const + { + return animationSet_; + } + + inline const Animation& getAnimation() const + { + return animationSet_.getAnimation(animation_); + } + + inline void setAnimation(std::string animation) + { + animation_ = std::move(animation); + } + +private: + + AnimationSet animationSet_; + std::string animation_; + size_t frame_ = 0; + size_t countdown_ = 0; }; #endif /* end of include guard: SPRITE_RENDERABLE_H_D3AACBBF */ diff --git a/src/components/orientable.h b/src/components/orientable.h new file mode 100644 index 0000000..8f56912 --- /dev/null +++ b/src/components/orientable.h @@ -0,0 +1,24 @@ +#ifndef ORIENTABLE_H_EDB6C4A1 +#define ORIENTABLE_H_EDB6C4A1 + +#include "component.h" + +class OrientableComponent : public Component { +public: + + inline bool isFacingRight() const + { + return facingRight_; + } + + inline void setFacingRight(bool v) + { + facingRight_ = v; + } + +private: + + bool facingRight_ = false; +}; + +#endif /* end of include guard: ORIENTABLE_H_EDB6C4A1 */ diff --git a/src/components/ponderable.h b/src/components/ponderable.h index 80100d7..dfbf908 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -6,6 +6,13 @@ class PonderableComponent : public Component { public: + enum class state { + grounded, + jumping, + falling, + dropping + }; + inline double getVelocityX() const { return velX_; @@ -51,12 +58,18 @@ public: return state_; } + inline void setState(state arg) + { + state_ = arg; + } + private: double velX_ = 0.0; double velY_ = 0.0; double accelX_ = 0.0; double accelY_ = 0.0; + state state_ = state::grounded; }; #endif /* end of include guard: TANGIBLE_H_746DB3EE */ diff --git a/src/game.cpp b/src/game.cpp index 5d1ec18..492e4d0 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -4,40 +4,59 @@ #include "components/controllable.h" #include "components/droppable.h" #include "components/ponderable.h" +#include "components/orientable.h" #include "systems/rendering.h" #include "systems/controlling.h" #include "systems/pondering.h" +#include "systems/animating.h" +#include "animation.h" void key_callback(GLFWwindow* window, int key, int, int action, int) { Game& game = *((Game*) glfwGetWindowUserPointer(window)); - + if ((action == GLFW_PRESS) && (key == GLFW_KEY_ESCAPE)) { - game.shouldQuit = true; - + game.shouldQuit_ = true; + return; } - - game.systemManager.getSystem().input(key, action); + + game.systemManager_.getSystem().input(key, action); } -Game::Game(GLFWwindow* window) : window(window) +Game::Game(GLFWwindow* window) : window_(window) { - systemManager.emplaceSystem(*this); - systemManager.emplaceSystem(*this); - systemManager.emplaceSystem(*this); - - int player = entityManager.emplaceEntity(); - entityManager.emplaceComponent(player, "res/Starla.png", 10, 12, 6); - entityManager.emplaceComponent(player, 203, 44, 10, 12); - entityManager.emplaceComponent(player); - entityManager.emplaceComponent(player); - entityManager.emplaceComponent(player); - + systemManager_.emplaceSystem(*this); + systemManager_.emplaceSystem(*this); + systemManager_.emplaceSystem(*this); + systemManager_.emplaceSystem(*this); + + int player = entityManager_.emplaceEntity(); + + AnimationSet playerGraphics {"res/Starla2.bmp", 10, 12, 6}; + playerGraphics.emplaceAnimation("stillLeft", 3, 1, 1); + playerGraphics.emplaceAnimation("stillRight", 0, 1, 1); + playerGraphics.emplaceAnimation("walkingLeft", 4, 2, 10); + playerGraphics.emplaceAnimation("walkingRight", 1, 2, 10); + + entityManager_.emplaceComponent( + player, + std::move(playerGraphics), + "stillLeft"); + + entityManager_.emplaceComponent( + player, + 203, 44, 10, 12); + + entityManager_.emplaceComponent(player); + entityManager_.emplaceComponent(player); + entityManager_.emplaceComponent(player); + entityManager_.emplaceComponent(player); + glfwSwapInterval(1); - glfwSetWindowUserPointer(window, this); - glfwSetKeyCallback(window, key_callback); + glfwSetWindowUserPointer(window_, this); + glfwSetKeyCallback(window_, key_callback); } void Game::execute() @@ -46,7 +65,7 @@ void Game::execute() const double dt = 0.01; double accumulator = 0.0; - while (!(shouldQuit || glfwWindowShouldClose(window))) + while (!(shouldQuit_ || glfwWindowShouldClose(window_))) { double currentTime = glfwGetTime(); double frameTime = currentTime - lastTime; @@ -57,19 +76,13 @@ void Game::execute() accumulator += frameTime; while (accumulator >= dt) { - systemManager.getSystem().tick(dt); - systemManager.getSystem().tick(dt); + systemManager_.getSystem().tick(dt); + systemManager_.getSystem().tick(dt); + systemManager_.getSystem().tick(dt); accumulator -= dt; } - systemManager.getSystem().tick(frameTime); + systemManager_.getSystem().tick(frameTime); } } - -EntityManager& Game::getEntityManager() -{ - return entityManager; -} - - diff --git a/src/game.h b/src/game.h index ec667c8..7bd038e 100644 --- a/src/game.h +++ b/src/game.h @@ -6,19 +6,35 @@ #include "system_manager.h" class Game { - public: - Game(GLFWwindow* window); +public: - void execute(); - EntityManager& getEntityManager(); + Game(GLFWwindow* window); - friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); + void execute(); - private: - EntityManager entityManager; - SystemManager systemManager; - GLFWwindow* const window; - bool shouldQuit = false; + inline EntityManager& getEntityManager() + { + return entityManager_; + } + + inline SystemManager& getSystemManager() + { + return systemManager_; + } + + friend void key_callback( + GLFWwindow* window, + int key, + int scancode, + int action, + int mods); + +private: + + EntityManager entityManager_; + SystemManager systemManager_; + GLFWwindow* const window_; + bool shouldQuit_ = false; }; #endif /* end of include guard: GAME_H_1014DDC9 */ diff --git a/src/system.h b/src/system.h index 489afd0..6a3cd14 100644 --- a/src/system.h +++ b/src/system.h @@ -1,12 +1,18 @@ #ifndef SYSTEM_H_B61A8CEA #define SYSTEM_H_B61A8CEA +#include "entity_manager.h" + class Game; class System { public: - System(Game& game) - : game(game) {} + + using id_type = EntityManager::id_type; + + System(Game& game) : game_(game) + { + } virtual ~System() = default; @@ -14,7 +20,7 @@ public: protected: - Game& game; + Game& game_; }; #endif /* end of include guard: SYSTEM_H_B61A8CEA */ diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp new file mode 100644 index 0000000..fcbfca5 --- /dev/null +++ b/src/systems/animating.cpp @@ -0,0 +1,38 @@ +#include "animating.h" +#include "game.h" +#include "components/animatable.h" + +void AnimatingSystem::tick(double) +{ + std::set spriteEntities = + game_.getEntityManager().getEntitiesWithComponents(); + + for (id_type entity : spriteEntities) + { + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + sprite.setCountdown(sprite.getCountdown() + 1); + + const Animation& anim = sprite.getAnimation(); + if (sprite.getCountdown() >= anim.getDelay()) + { + sprite.setFrame(sprite.getFrame() + 1); + sprite.setCountdown(0); + + if (sprite.getFrame() >= anim.getFirstFrame() + anim.getNumFrames()) + { + sprite.setFrame(anim.getFirstFrame()); + } + } + } +} + +void AnimatingSystem::startAnimation(id_type entity, std::string animation) +{ + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + sprite.setAnimation(animation); + sprite.setFrame(sprite.getAnimation().getFirstFrame()); +} diff --git a/src/systems/animating.h b/src/systems/animating.h new file mode 100644 index 0000000..150a74a --- /dev/null +++ b/src/systems/animating.h @@ -0,0 +1,20 @@ +#ifndef ANIMATING_H_5BBF0094 +#define ANIMATING_H_5BBF0094 + +#include "system.h" +#include + +class AnimatingSystem : public System { +public: + + AnimatingSystem(Game& game) : System(game) + { + } + + void tick(double dt); + + void startAnimation(id_type entity, std::string animation); + +}; + +#endif /* end of include guard: ANIMATING_H_5BBF0094 */ diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index ec62e9a..3647ff8 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp @@ -4,21 +4,30 @@ #include "components/ponderable.h" #include "components/animatable.h" #include "components/droppable.h" +#include "components/orientable.h" +#include "systems/animating.h" #include "direction.h" #include "muxer.h" #include "consts.h" -void ControllingSystem::tick(double dt) +void ControllingSystem::tick(double) { - while (!actions.empty()) + while (!actions_.empty()) { - int key = actions.front().first; - int action = actions.front().second; + int key = actions_.front().first; + int action = actions_.front().second; + + auto entities = game_.getEntityManager().getEntitiesWithComponents< + ControllableComponent, + PonderableComponent, + AnimatableComponent, + DroppableComponent, + OrientableComponent>(); - auto entities = game.getEntityManager().getEntitiesWithComponents(); for (auto entity : entities) { - auto& controllable = game.getEntityManager().getComponent(entity); + auto& controllable = game_.getEntityManager(). + getComponent(entity); if (action == GLFW_PRESS) { @@ -95,74 +104,107 @@ void ControllingSystem::tick(double dt) } } - actions.pop(); + actions_.pop(); } } void ControllingSystem::input(int key, int action) { - actions.push(std::make_pair(key, action)); + actions_.push(std::make_pair(key, action)); } -void ControllingSystem::walkLeft(int entity) +void ControllingSystem::walkLeft(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - + auto& ponderable = game_.getEntityManager().getComponent(entity); + auto& orientable = game_.getEntityManager().getComponent(entity); + + orientable.setFacingRight(false); ponderable.setVelocityX(-90); - - animatable.setDirection(Direction::Left); - animatable.setWalking(true); + + auto& animating = game_.getSystemManager().getSystem(); + + if (ponderable.getState() == PonderableComponent::state::grounded) + { + animating.startAnimation(entity, "walkingLeft"); + } else { + animating.startAnimation(entity, "stillLeft"); + } } -void ControllingSystem::walkRight(int entity) +void ControllingSystem::walkRight(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - + auto& ponderable = game_.getEntityManager().getComponent(entity); + auto& orientable = game_.getEntityManager().getComponent(entity); + + orientable.setFacingRight(true); ponderable.setVelocityX(90); - animatable.setDirection(Direction::Right); - animatable.setWalking(true); + auto& animating = game_.getSystemManager().getSystem(); + + if (ponderable.getState() == PonderableComponent::state::grounded) + { + animating.startAnimation(entity, "walkingRight"); + } else { + animating.startAnimation(entity, "stillRight"); + } } -void ControllingSystem::stopWalking(int entity) +void ControllingSystem::stopWalking(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - + auto& ponderable = game_.getEntityManager().getComponent(entity); + auto& orientable = game_.getEntityManager().getComponent(entity); + ponderable.setVelocityX(0); - - animatable.setWalking(false); + + if (ponderable.getState() == PonderableComponent::state::grounded) + { + auto& animating = game_.getSystemManager().getSystem(); + + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } + } } -void ControllingSystem::jump(int entity) +void ControllingSystem::jump(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - - playSound("res/Randomize87.wav", 0.25); - - ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); - - animatable.setJumping(true); + auto& ponderable = game_.getEntityManager().getComponent(entity); + + if (ponderable.getState() == PonderableComponent::state::grounded) + { + playSound("res/Randomize87.wav", 0.25); + + ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); + ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); + ponderable.setState(PonderableComponent::state::jumping); + } } -void ControllingSystem::stopJumping(int entity) +void ControllingSystem::stopJumping(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); - animatable.setJumping(false); + auto& ponderable = game_.getEntityManager().getComponent(entity); + + if (ponderable.getState() == PonderableComponent::state::jumping) + { + ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); + ponderable.setState(PonderableComponent::state::falling); + } } -void ControllingSystem::drop(int entity, bool start) +void ControllingSystem::drop(id_type entity, bool start) { - auto& animatable = game.getEntityManager().getComponent(entity); - auto& droppable = game.getEntityManager().getComponent(entity); - + auto& droppable = game_.getEntityManager().getComponent(entity); + auto& ponderable = game_.getEntityManager().getComponent(entity); + + if (start && (ponderable.getState() == PonderableComponent::state::grounded)) + { + ponderable.setState(PonderableComponent::state::dropping); + } else if ((!start) && (ponderable.getState() == PonderableComponent::state::dropping)) + { + ponderable.setState(PonderableComponent::state::grounded); + } droppable.setDroppable(start); - animatable.setCrouching(start); } diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 30210b3..1f1e8a0 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h @@ -3,24 +3,28 @@ #include "system.h" #include +#include "entity_manager.h" class ControllingSystem : public System { - public: - ControllingSystem(Game& game) - : System(game) {} - - void tick(double dt); - void input(int key, int action); - - private: - void walkLeft(int entity); - void walkRight(int entity); - void stopWalking(int entity); - void jump(int entity); - void stopJumping(int entity); - void drop(int entity, bool start); - - std::queue> actions; +public: + + ControllingSystem(Game& game) : System(game) + { + } + + void tick(double dt); + void input(int key, int action); + +private: + + void walkLeft(id_type entity); + void walkRight(id_type entity); + void stopWalking(id_type entity); + void jump(id_type entity); + void stopJumping(id_type entity); + void drop(id_type entity, bool start); + + std::queue> actions_; }; #endif /* end of include guard: CONTROLLING_H_80B1BB8D */ diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 50a8bc8..e40db1d 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -5,12 +5,14 @@ void PonderingSystem::tick(double dt) { - auto entities = game.getEntityManager().getEntitiesWithComponents(); + auto entities = game_.getEntityManager().getEntitiesWithComponents< + PonderableComponent, + TransformableComponent>(); - for (auto entity : entities) + for (id_type entity : entities) { - auto& transformable = game.getEntityManager().getComponent(entity); - auto& ponderable = game.getEntityManager().getComponent(entity); + auto& transformable = game_.getEntityManager().getComponent(entity); + auto& ponderable = game_.getEntityManager().getComponent(entity); // Accelerate ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); diff --git a/src/systems/pondering.h b/src/systems/pondering.h index 3fe5473..44e7600 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h @@ -4,11 +4,13 @@ #include "system.h" class PonderingSystem : public System { - public: - PonderingSystem(Game& game) - : System(game) {} +public: - void tick(double dt); + PonderingSystem(Game& game) : System(game) + { + } + + void tick(double dt); }; #endif /* end of include guard: PONDERING_H_F2530E0E */ diff --git a/src/systems/rendering.cpp b/src/systems/rendering.cpp index 251c2bc..8219732 100644 --- a/src/systems/rendering.cpp +++ b/src/systems/rendering.cpp @@ -3,19 +3,35 @@ #include "components/animatable.h" #include "components/transformable.h" -void RenderingSystem::tick(double dt) +void RenderingSystem::tick(double) { - texture.fill(texture.entirety(), 0, 0, 0); - - std::set spriteEntities = game.getEntityManager().getEntitiesWithComponents(); - for (int entity : spriteEntities) + texture_.fill(texture_.entirety(), 0, 0, 0); + + std::set spriteEntities = + game_.getEntityManager().getEntitiesWithComponents< + AnimatableComponent, + TransformableComponent>(); + + for (id_type entity : spriteEntities) { - auto& sprite = game.getEntityManager().getComponent(entity); - auto& transform = game.getEntityManager().getComponent(entity); - Rectangle dstrect {(int) transform.getX(), (int) transform.getY(), transform.getW(), transform.getH()}; - - texture.blit(sprite.getTexture(), sprite.getFrameRect(), dstrect); + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + auto& transform = game_.getEntityManager(). + getComponent(entity); + + Rectangle dstrect { + static_cast(transform.getX()), + static_cast(transform.getY()), + transform.getW(), + transform.getH()}; + + const AnimationSet& aset = sprite.getAnimationSet(); + texture_.blit( + aset.getTexture(), + aset.getFrameRect(sprite.getFrame()), + dstrect); } - - texture.renderScreen(); + + texture_.renderScreen(); } diff --git a/src/systems/rendering.h b/src/systems/rendering.h index cec72e2..a53ee64 100644 --- a/src/systems/rendering.h +++ b/src/systems/rendering.h @@ -6,14 +6,17 @@ #include "consts.h" class RenderingSystem : public System { - public: - RenderingSystem(Game& game) - : System(game) {} +public: - void tick(double dt); + RenderingSystem(Game& game) : System(game) + { + } - private: - Texture texture {GAME_WIDTH, GAME_HEIGHT}; + void tick(double dt); + +private: + + Texture texture_ {GAME_WIDTH, GAME_HEIGHT}; }; #endif /* end of include guard: RENDERING_H_76ABC02A */ -- cgit 1.4.1 From 5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 12 Feb 2018 16:39:49 -0500 Subject: Abstracted behavior related to "orientable" entities A lot of the stuff that ControllingSystem did to control the player character was moved into the new OrientingSystem. This is so that the player, or any player-like entities, can also be controlled by AI, with the underlying behavior being delegated in the same way as if the player were being controlled by the user. Fixed the issue where, if the player were blocked while moving horizontally, they would remain blocked even if vertical movement were to remove the collision. Fixed cases of the player animating incorrectly after performing certain movements. --- CMakeLists.txt | 1 + src/components/droppable.h | 24 ----- src/components/orientable.h | 45 +++++++++ src/components/ponderable.h | 17 +--- src/consts.h | 10 +- src/game.cpp | 4 +- src/systems/controlling.cpp | 129 +++--------------------- src/systems/controlling.h | 8 -- src/systems/mapping.cpp | 2 - src/systems/orienting.cpp | 236 ++++++++++++++++++++++++++++++++++++++++++++ src/systems/orienting.h | 35 +++++++ src/systems/pondering.cpp | 89 +++++++++-------- 12 files changed, 393 insertions(+), 207 deletions(-) delete mode 100644 src/components/droppable.h create mode 100644 src/systems/orienting.cpp create mode 100644 src/systems/orienting.h (limited to 'src/systems/controlling.h') diff --git a/CMakeLists.txt b/CMakeLists.txt index f918156..39b1bbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ add_executable(Aromatherapy src/systems/pondering.cpp src/systems/animating.cpp src/systems/mapping.cpp + src/systems/orienting.cpp ) target_link_libraries(Aromatherapy ${ALL_LIBS}) install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR}) diff --git a/src/components/droppable.h b/src/components/droppable.h deleted file mode 100644 index 722c139..0000000 --- a/src/components/droppable.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef DROPPABLE_H_5DB254EF -#define DROPPABLE_H_5DB254EF - -#include "component.h" - -class DroppableComponent : public Component { -public: - - inline bool isDroppable() const - { - return droppable_; - } - - inline void setDroppable(bool can) - { - droppable_ = can; - } - -private: - - bool droppable_ = false; -}; - -#endif /* end of include guard: DROPPABLE_H_5DB254EF */ diff --git a/src/components/orientable.h b/src/components/orientable.h index 8f56912..e356b78 100644 --- a/src/components/orientable.h +++ b/src/components/orientable.h @@ -6,6 +6,18 @@ class OrientableComponent : public Component { public: + enum class WalkState { + still, + left, + right + }; + + enum class DropState { + none, + ready, + active + }; + inline bool isFacingRight() const { return facingRight_; @@ -16,9 +28,42 @@ public: facingRight_ = v; } + inline WalkState getWalkState() const + { + return walkState_; + } + + inline void setWalkState(WalkState v) + { + walkState_ = v; + } + + inline bool isJumping() const + { + return jumping_; + } + + inline void setJumping(bool v) + { + jumping_ = v; + } + + inline DropState getDropState() const + { + return dropState_; + } + + inline void setDropState(DropState v) + { + dropState_ = v; + } + private: bool facingRight_ = false; + WalkState walkState_ = WalkState::still; + bool jumping_ = false; + DropState dropState_ = DropState::none; }; #endif /* end of include guard: ORIENTABLE_H_EDB6C4A1 */ diff --git a/src/components/ponderable.h b/src/components/ponderable.h index ac759b6..e21cbab 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -11,13 +11,6 @@ public: freefalling }; - enum class State { - grounded, - jumping, - falling, - dropping - }; - PonderableComponent(Type type) : type_(type) { } @@ -67,14 +60,14 @@ public: accelY_ = v; } - inline State getState() const + inline bool isGrounded() const { - return state_; + return grounded_; } - inline void setState(State arg) + inline void setGrounded(bool v) { - state_ = arg; + grounded_ = v; } private: @@ -84,7 +77,7 @@ private: double accelX_ = 0.0; double accelY_ = 0.0; Type type_ = Type::vacuumed; - State state_ = State::grounded; + bool grounded_ = false; }; #endif /* end of include guard: TANGIBLE_H_746DB3EE */ diff --git a/src/consts.h b/src/consts.h index a6c9985..581018d 100644 --- a/src/consts.h +++ b/src/consts.h @@ -14,7 +14,13 @@ const int FONT_COLS = 16; const int FRAMES_PER_SECOND = 60; const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; -#define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) -#define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) +#define CALC_VELOCITY(h, l) (-2 * (h) / (l)) +#define CALC_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) + +const double NORMAL_GRAVITY = CALC_GRAVITY(TILE_HEIGHT*3.5, 0.233); +const double JUMP_GRAVITY = CALC_GRAVITY(TILE_HEIGHT*4.5, 0.3); +const double JUMP_VELOCITY = CALC_VELOCITY(TILE_HEIGHT*4.5, 0.3); + +const double WALK_SPEED = 90; #endif diff --git a/src/game.cpp b/src/game.cpp index 7cbe7e0..39bb3f1 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,13 +2,13 @@ #include "components/animatable.h" #include "components/transformable.h" #include "components/controllable.h" -#include "components/droppable.h" #include "components/ponderable.h" #include "components/orientable.h" #include "systems/controlling.h" #include "systems/pondering.h" #include "systems/animating.h" #include "systems/mapping.h" +#include "systems/orienting.h" #include "animation.h" #include "renderer.h" #include "consts.h" @@ -33,6 +33,7 @@ Game::Game( world_("res/maps.xml") { systemManager_.emplaceSystem(*this); + systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); @@ -58,7 +59,6 @@ Game::Game( player, PonderableComponent::Type::freefalling); - entityManager_.emplaceComponent(player); entityManager_.emplaceComponent(player); entityManager_.emplaceComponent(player); diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index fa09d11..e1609bd 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp @@ -1,14 +1,8 @@ #include "controlling.h" #include "game.h" #include "components/controllable.h" -#include "components/ponderable.h" -#include "components/animatable.h" -#include "components/droppable.h" #include "components/orientable.h" -#include "systems/animating.h" -#include "direction.h" -#include "muxer.h" -#include "consts.h" +#include "systems/orienting.h" void ControllingSystem::tick(double) { @@ -19,9 +13,6 @@ void ControllingSystem::tick(double) auto entities = game_.getEntityManager().getEntitiesWithComponents< ControllableComponent, - PonderableComponent, - AnimatableComponent, - DroppableComponent, OrientableComponent>(); for (auto entity : entities) @@ -29,6 +20,8 @@ void ControllingSystem::tick(double) auto& controllable = game_.getEntityManager(). getComponent(entity); + auto& orienting = game_.getSystemManager().getSystem(); + if (action == GLFW_PRESS) { if (key == controllable.getLeftKey()) @@ -37,7 +30,7 @@ void ControllingSystem::tick(double) if (!controllable.isFrozen()) { - walkLeft(entity); + orienting.moveLeft(entity); } } else if (key == controllable.getRightKey()) { @@ -45,19 +38,19 @@ void ControllingSystem::tick(double) if (!controllable.isFrozen()) { - walkRight(entity); + orienting.moveRight(entity); } } else if (key == controllable.getJumpKey()) { if (!controllable.isFrozen()) { - jump(entity); + orienting.jump(entity); } } else if (key == controllable.getDropKey()) { if (!controllable.isFrozen()) { - drop(entity, true); + orienting.drop(entity); } } } else if (action == GLFW_RELEASE) @@ -70,9 +63,9 @@ void ControllingSystem::tick(double) { if (controllable.isHoldingRight()) { - walkRight(entity); + orienting.moveRight(entity); } else { - stopWalking(entity); + orienting.stopWalking(entity); } } } else if (key == controllable.getRightKey()) @@ -83,22 +76,22 @@ void ControllingSystem::tick(double) { if (controllable.isHoldingLeft()) { - walkLeft(entity); + orienting.moveLeft(entity); } else { - stopWalking(entity); + orienting.stopWalking(entity); } } } else if (key == controllable.getDropKey()) { if (!controllable.isFrozen()) { - drop(entity, false); + orienting.stopDropping(entity); } } else if (key == controllable.getJumpKey()) { if (!controllable.isFrozen()) { - stopJumping(entity); + orienting.stopJumping(entity); } } } @@ -112,99 +105,3 @@ void ControllingSystem::input(int key, int action) { actions_.push(std::make_pair(key, action)); } - -void ControllingSystem::walkLeft(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - auto& orientable = game_.getEntityManager().getComponent(entity); - - orientable.setFacingRight(false); - ponderable.setVelocityX(-90); - - auto& animating = game_.getSystemManager().getSystem(); - - if (ponderable.getState() == PonderableComponent::State::grounded) - { - animating.startAnimation(entity, "walkingLeft"); - } else { - animating.startAnimation(entity, "stillLeft"); - } -} - -void ControllingSystem::walkRight(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - auto& orientable = game_.getEntityManager().getComponent(entity); - - orientable.setFacingRight(true); - ponderable.setVelocityX(90); - - auto& animating = game_.getSystemManager().getSystem(); - - if (ponderable.getState() == PonderableComponent::State::grounded) - { - animating.startAnimation(entity, "walkingRight"); - } else { - animating.startAnimation(entity, "stillRight"); - } -} - -void ControllingSystem::stopWalking(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - auto& orientable = game_.getEntityManager().getComponent(entity); - - ponderable.setVelocityX(0); - - if (ponderable.getState() == PonderableComponent::State::grounded) - { - auto& animating = game_.getSystemManager().getSystem(); - - if (orientable.isFacingRight()) - { - animating.startAnimation(entity, "stillRight"); - } else { - animating.startAnimation(entity, "stillLeft"); - } - } -} - -void ControllingSystem::jump(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - - if (ponderable.getState() == PonderableComponent::State::grounded) - { - playSound("res/Randomize87.wav", 0.25); - - ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); - ponderable.setState(PonderableComponent::State::jumping); - } -} - -void ControllingSystem::stopJumping(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - - if (ponderable.getState() == PonderableComponent::State::jumping) - { - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); - ponderable.setState(PonderableComponent::State::falling); - } -} - -void ControllingSystem::drop(id_type entity, bool start) -{ - auto& droppable = game_.getEntityManager().getComponent(entity); - auto& ponderable = game_.getEntityManager().getComponent(entity); - - if (start && (ponderable.getState() == PonderableComponent::State::grounded)) - { - ponderable.setState(PonderableComponent::State::dropping); - } else if ((!start) && (ponderable.getState() == PonderableComponent::State::dropping)) - { - ponderable.setState(PonderableComponent::State::grounded); - } - droppable.setDroppable(start); -} diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 1f1e8a0..01ed7a0 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h @@ -3,7 +3,6 @@ #include "system.h" #include -#include "entity_manager.h" class ControllingSystem : public System { public: @@ -17,13 +16,6 @@ public: private: - void walkLeft(id_type entity); - void walkRight(id_type entity); - void stopWalking(id_type entity); - void jump(id_type entity); - void stopJumping(id_type entity); - void drop(id_type entity, bool start); - std::queue> actions_; }; diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index 8723e16..5b63ded 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp @@ -3,8 +3,6 @@ #include "game.h" #include "consts.h" -#include - template inline void addBoundary( Storage& boundaries, diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp new file mode 100644 index 0000000..187bebc --- /dev/null +++ b/src/systems/orienting.cpp @@ -0,0 +1,236 @@ +#include "orienting.h" +#include "game.h" +#include "components/orientable.h" +#include "components/ponderable.h" +#include "systems/animating.h" +#include "consts.h" +#include "muxer.h" + +void OrientingSystem::tick(double) +{ + auto entities = game_.getEntityManager().getEntitiesWithComponents< + OrientableComponent, + PonderableComponent>(); + + for (id_type entity : entities) + { + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + switch (orientable.getWalkState()) + { + case OrientableComponent::WalkState::still: + { + ponderable.setVelocityX(0); + + break; + } + + case OrientableComponent::WalkState::left: + { + ponderable.setVelocityX(-WALK_SPEED); + + break; + } + + case OrientableComponent::WalkState::right: + { + ponderable.setVelocityX(WALK_SPEED); + + break; + } + } + + if (orientable.isJumping() && (ponderable.getVelocityY() > 0)) + { + orientable.setJumping(false); + } + } +} + +void OrientingSystem::moveLeft(id_type entity) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + orientable.setFacingRight(false); + orientable.setWalkState(OrientableComponent::WalkState::left); + + auto& animating = game_.getSystemManager().getSystem(); + if (ponderable.isGrounded()) + { + animating.startAnimation(entity, "walkingLeft"); + } else { + animating.startAnimation(entity, "stillLeft"); + } +} + +void OrientingSystem::moveRight(id_type entity) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + orientable.setFacingRight(true); + orientable.setWalkState(OrientableComponent::WalkState::right); + + auto& animating = game_.getSystemManager().getSystem(); + if (ponderable.isGrounded()) + { + animating.startAnimation(entity, "walkingRight"); + } else { + animating.startAnimation(entity, "stillRight"); + } +} + +void OrientingSystem::stopWalking(id_type entity) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + orientable.setWalkState(OrientableComponent::WalkState::still); + + if (ponderable.isGrounded()) + { + auto& animating = game_.getSystemManager().getSystem(); + + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } + } +} + +void OrientingSystem::jump(id_type entity) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + if (ponderable.isGrounded()) + { + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + orientable.setJumping(true); + + playSound("res/Randomize87.wav", 0.25); + + ponderable.setVelocityY(JUMP_VELOCITY); + ponderable.setAccelY(JUMP_GRAVITY); + + auto& animating = game_.getSystemManager().getSystem(); + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } + } +} + +void OrientingSystem::stopJumping(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + if (orientable.isJumping()) + { + orientable.setJumping(false); + + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + ponderable.setAccelY(NORMAL_GRAVITY); + } +} + +void OrientingSystem::land(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + auto& animating = game_.getSystemManager().getSystem(); + + switch (orientable.getWalkState()) + { + case OrientableComponent::WalkState::still: + { + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } + + break; + } + + case OrientableComponent::WalkState::left: + { + animating.startAnimation(entity, "walkingLeft"); + + break; + } + + case OrientableComponent::WalkState::right: + { + animating.startAnimation(entity, "walkingRight"); + + break; + } + } +} + +void OrientingSystem::startFalling(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + auto& animating = game_.getSystemManager().getSystem(); + + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } +} + +void OrientingSystem::drop(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + if (ponderable.isGrounded() + && (orientable.getDropState() == OrientableComponent::DropState::none)) + { + orientable.setDropState(OrientableComponent::DropState::ready); + } +} + +void OrientingSystem::stopDropping(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + if (orientable.getDropState() == OrientableComponent::DropState::ready) + { + orientable.setDropState(OrientableComponent::DropState::none); + } +} diff --git a/src/systems/orienting.h b/src/systems/orienting.h new file mode 100644 index 0000000..4ded612 --- /dev/null +++ b/src/systems/orienting.h @@ -0,0 +1,35 @@ +#ifndef ORIENTING_H_099F0C23 +#define ORIENTING_H_099F0C23 + +#include "system.h" + +class OrientingSystem : public System { +public: + + OrientingSystem(Game& game) : System(game) + { + } + + void tick(double dt); + + void moveLeft(id_type entity); + + void moveRight(id_type entity); + + void stopWalking(id_type entity); + + void jump(id_type entity); + + void stopJumping(id_type entity); + + void land(id_type entity); + + void startFalling(id_type entity); + + void drop(id_type entity); + + void stopDropping(id_type entity); + +}; + +#endif /* end of include guard: ORIENTING_H_099F0C23 */ diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 26a6f56..4a165b1 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -2,7 +2,9 @@ #include "game.h" #include "components/ponderable.h" #include "components/transformable.h" -#include "components/droppable.h" +#include "components/orientable.h" +#include "components/mappable.h" +#include "systems/orienting.h" #include "consts.h" void PonderingSystem::tick(double dt) @@ -37,10 +39,8 @@ void PonderingSystem::tick(double dt) double newX = oldX + ponderable.getVelocityX() * dt; double newY = oldY + ponderable.getVelocityY() * dt; - if (ponderable.getVelocityY() > 0.0) - { - ponderable.setState(PonderableComponent::State::falling); - } + bool oldGrounded = ponderable.isGrounded(); + ponderable.setGrounded(false); for (id_type mapEntity : maps) { @@ -64,8 +64,6 @@ void PonderingSystem::tick(double dt) newY, it->first, it->second.getType()); - - break; } } } else if (newX > oldX) @@ -86,8 +84,6 @@ void PonderingSystem::tick(double dt) newY, it->first, it->second.getType()); - - break; } } } @@ -109,8 +105,6 @@ void PonderingSystem::tick(double dt) newY, it->first, it->second.getType()); - - break; } } } else if (newY > oldY) @@ -131,8 +125,6 @@ void PonderingSystem::tick(double dt) newY, it->first, it->second.getType()); - - break; } } } @@ -141,6 +133,31 @@ void PonderingSystem::tick(double dt) // Move transformable.setX(newX); transformable.setY(newY); + + // Perform cleanup for orientable entites + if (game_.getEntityManager().hasComponent(entity)) + { + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + // Handle changes in groundedness + if (ponderable.isGrounded() != oldGrounded) + { + if (ponderable.isGrounded()) + { + game_.getSystemManager().getSystem().land(entity); + } else { + game_.getSystemManager(). + getSystem().startFalling(entity); + } + } + + // Complete dropping, if necessary + if (orientable.getDropState() == OrientableComponent::DropState::active) + { + orientable.setDropState(OrientableComponent::DropState::none); + } + } } } @@ -153,8 +170,7 @@ void PonderingSystem::initializeBody( if (type == PonderableComponent::Type::freefalling) { - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); - ponderable.setState(PonderableComponent::State::falling); + ponderable.setAccelY(NORMAL_GRAVITY); } } @@ -172,6 +188,8 @@ void PonderingSystem::processCollision( auto& transformable = game_.getEntityManager(). getComponent(entity); + bool touchedGround = false; + switch (type) { case MappableComponent::Boundary::Type::wall: @@ -204,13 +222,7 @@ void PonderingSystem::processCollision( case Direction::down: { - newY = axis - transformable.getH(); - ponderable.setVelocityY(0.0); - - if (ponderable.getState() == PonderableComponent::State::falling) - { - ponderable.setState(PonderableComponent::State::grounded); - } + touchedGround = true; break; } @@ -221,31 +233,19 @@ void PonderingSystem::processCollision( case MappableComponent::Boundary::Type::platform: { - if (game_.getEntityManager().hasComponent(entity)) + if (game_.getEntityManager().hasComponent(entity)) { - auto& droppable = game_.getEntityManager(). - getComponent(entity); + auto& orientable = game_.getEntityManager(). + getComponent(entity); - if (droppable.isDroppable()) + if (orientable.getDropState() != OrientableComponent::DropState::none) { - droppable.setDroppable(false); + orientable.setDropState(OrientableComponent::DropState::active); } else { - newY = axis - transformable.getH(); - ponderable.setVelocityY(0.0); - - if (ponderable.getState() == PonderableComponent::State::falling) - { - ponderable.setState(PonderableComponent::State::grounded); - } + touchedGround = true; } } else { - newY = axis - transformable.getH(); - ponderable.setVelocityY(0.0); - - if (ponderable.getState() == PonderableComponent::State::falling) - { - ponderable.setState(PonderableComponent::State::grounded); - } + touchedGround = true; } break; @@ -258,4 +258,11 @@ void PonderingSystem::processCollision( break; } } + + if (touchedGround) + { + newY = axis - transformable.getH(); + ponderable.setVelocityY(0.0); + ponderable.setGrounded(true); + } } -- cgit 1.4.1 From e4e2f2d2a7b6a282b9618aa0004d9453936f43c6 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 18 Feb 2018 15:25:52 -0500 Subject: Added player death and event scheduling Also added ability to make sprites flicker, to freeze physics for an entity, and to freeze progression of a sprite's animation loop. --- CMakeLists.txt | 1 + res/maps.xml | 2 +- src/components/animatable.h | 33 +++++++++++++++++ src/components/playable.h | 9 +++++ src/components/ponderable.h | 22 ++++++++++++ src/components/schedulable.h | 21 +++++++++++ src/game.cpp | 2 ++ src/systems/animating.cpp | 19 ++++++++-- src/systems/controlling.cpp | 29 +++++++++++++++ src/systems/controlling.h | 5 +++ src/systems/mapping.cpp | 21 +++++++++++ src/systems/orienting.cpp | 18 ++++------ src/systems/playing.cpp | 85 +++++++++++++++++++++++++++++++++++++++++--- src/systems/playing.h | 9 ++++- src/systems/pondering.cpp | 16 +++++++++ src/systems/scheduling.cpp | 54 ++++++++++++++++++++++++++++ src/systems/scheduling.h | 22 ++++++++++++ 17 files changed, 348 insertions(+), 20 deletions(-) create mode 100644 src/components/schedulable.h create mode 100644 src/systems/scheduling.cpp create mode 100644 src/systems/scheduling.h (limited to 'src/systems/controlling.h') diff --git a/CMakeLists.txt b/CMakeLists.txt index 155063e..34246ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ add_executable(Aromatherapy src/systems/mapping.cpp src/systems/orienting.cpp src/systems/playing.cpp + src/systems/scheduling.cpp ) set_property(TARGET Aromatherapy PROPERTY CXX_STANDARD 11) diff --git a/res/maps.xml b/res/maps.xml index 9d855a2..c561912 100644 --- a/res/maps.xml +++ b/res/maps.xml @@ -1,5 +1,5 @@ -00,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,20,0,0,0,0,0,0,0,18,9,8,10,8,11,8,10,10,8,11,8,9,10,21,0, +00,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,20,0,0,0,0,0,0,0,18,9,8,10,8,11,8,10,10,8,11,8,9,10,21,0, 0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,22,21, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,8,8,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12, 0,19,0,0,0,0,0,0,0,0,0,0,0,18,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12, diff --git a/src/components/animatable.h b/src/components/animatable.h index ed0133e..ec72be0 100644 --- a/src/components/animatable.h +++ b/src/components/animatable.h @@ -51,12 +51,45 @@ public: animation_ = std::move(animation); } + inline bool isFlickering() const + { + return flickering_; + } + + inline void setFlickering(bool v) + { + flickering_ = v; + } + + inline size_t getFlickerTimer() const + { + return flickerTimer_; + } + + inline void setFlickerTimer(size_t v) + { + flickerTimer_ = v; + } + + inline bool isFrozen() const + { + return frozen_; + } + + inline void setFrozen(bool v) + { + frozen_ = v; + } + private: AnimationSet animationSet_; std::string animation_; size_t frame_ = 0; size_t countdown_ = 0; + bool flickering_ = false; + size_t flickerTimer_ = 0; + bool frozen_ = false; }; #endif /* end of include guard: SPRITE_RENDERABLE_H_D3AACBBF */ diff --git a/src/components/playable.h b/src/components/playable.h index a6e71b0..86a7ee7 100644 --- a/src/components/playable.h +++ b/src/components/playable.h @@ -2,14 +2,23 @@ #define PLAYABLE_H_DDC566C3 #include "component.h" +#include class PlayableComponent : public Component { public: + using MapChangeCallback = std::function; + bool changingMap = false; int newMapId = -1; double newMapX = 0; double newMapY = 0; + MapChangeCallback newMapCallback; + + int checkpointMapId = -1; + double checkpointX = 0; + double checkpointY = 0; + }; #endif /* end of include guard: PLAYABLE_H_DDC566C3 */ diff --git a/src/components/ponderable.h b/src/components/ponderable.h index e21cbab..78af25f 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -70,6 +70,26 @@ public: grounded_ = v; } + inline bool isFrozen() const + { + return frozen_; + } + + inline void setFrozen(bool v) + { + frozen_ = v; + } + + inline bool isCollidable() const + { + return collidable_; + } + + inline void setCollidable(bool v) + { + collidable_ = v; + } + private: double velX_ = 0.0; @@ -78,6 +98,8 @@ private: double accelY_ = 0.0; Type type_ = Type::vacuumed; bool grounded_ = false; + bool frozen_ = false; + bool collidable_ = true; }; #endif /* end of include guard: TANGIBLE_H_746DB3EE */ diff --git a/src/components/schedulable.h b/src/components/schedulable.h new file mode 100644 index 0000000..a92bbba --- /dev/null +++ b/src/components/schedulable.h @@ -0,0 +1,21 @@ +#ifndef SCHEDULABLE_H_1DA3FA2A +#define SCHEDULABLE_H_1DA3FA2A + +#include "component.h" +#include +#include +#include +#include "entity_manager.h" + +class SchedulableComponent : public Component { +public: + + using id_type = EntityManager::id_type; + + using Callback = std::function; + using Action = std::tuple; + + std::list actions; +}; + +#endif /* end of include guard: SCHEDULABLE_H_1DA3FA2A */ diff --git a/src/game.cpp b/src/game.cpp index f245e7c..3da23a3 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -10,6 +10,7 @@ #include "systems/mapping.h" #include "systems/orienting.h" #include "systems/playing.h" +#include "systems/scheduling.h" #include "animation.h" #include "consts.h" @@ -30,6 +31,7 @@ void key_callback(GLFWwindow* window, int key, int, int action, int) Game::Game() : world_("res/maps.xml") { systemManager_.emplaceSystem(*this); + systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp index 22224c9..634af67 100644 --- a/src/systems/animating.cpp +++ b/src/systems/animating.cpp @@ -13,7 +13,10 @@ void AnimatingSystem::tick(double) auto& sprite = game_.getEntityManager(). getComponent(entity); - sprite.setCountdown(sprite.getCountdown() + 1); + if (!sprite.isFrozen()) + { + sprite.setCountdown(sprite.getCountdown() + 1); + } const Animation& anim = sprite.getAnimation(); if (sprite.getCountdown() >= anim.getDelay()) @@ -26,6 +29,11 @@ void AnimatingSystem::tick(double) sprite.setFrame(anim.getFirstFrame()); } } + + if (sprite.isFlickering()) + { + sprite.setFlickerTimer((sprite.getFlickerTimer() + 1) % 6); + } } } @@ -44,6 +52,12 @@ void AnimatingSystem::render(Texture& texture) auto& transform = game_.getEntityManager(). getComponent(entity); + double alpha = 1.0; + if (sprite.isFlickering() && (sprite.getFlickerTimer() < 3)) + { + alpha = 0.0; + } + Rectangle dstrect { static_cast(transform.getX()), static_cast(transform.getY()), @@ -55,7 +69,8 @@ void AnimatingSystem::render(Texture& texture) aset.getTexture(), texture, aset.getFrameRect(sprite.getFrame()), - dstrect); + dstrect, + alpha); } } diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index e1609bd..e65c09a 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp @@ -105,3 +105,32 @@ void ControllingSystem::input(int key, int action) { actions_.push(std::make_pair(key, action)); } + +void ControllingSystem::freeze(id_type entity) +{ + auto& controllable = game_.getEntityManager(). + getComponent(entity); + + controllable.setFrozen(true); +} + +void ControllingSystem::unfreeze(id_type entity) +{ + auto& controllable = game_.getEntityManager(). + getComponent(entity); + + if (controllable.isFrozen()) + { + controllable.setFrozen(false); + + auto& orienting = game_.getSystemManager().getSystem(); + + if (controllable.isHoldingLeft()) + { + orienting.moveLeft(entity); + } else if (controllable.isHoldingRight()) + { + orienting.moveRight(entity); + } + } +} diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 01ed7a0..d6f0789 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h @@ -12,8 +12,13 @@ public: } void tick(double dt); + void input(int key, int action); + void freeze(id_type entity); + + void unfreeze(id_type entity); + private: std::queue> actions_; diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index 05167c1..a3a17ec 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp @@ -166,12 +166,33 @@ void MappingSystem::loadMap(size_t mapId) MappableComponent::Boundary::Type::wall); } else if (tile == 42) { + addBoundary( + mappable.getRightBoundaries(), + x * TILE_WIDTH, + y * TILE_HEIGHT, + (y+1) * TILE_HEIGHT, + MappableComponent::Boundary::Type::danger); + + addBoundary( + mappable.getLeftBoundaries(), + (x+1) * TILE_WIDTH, + y * TILE_HEIGHT, + (y+1) * TILE_HEIGHT, + MappableComponent::Boundary::Type::danger); + addBoundary( mappable.getDownBoundaries(), y * TILE_HEIGHT, x * TILE_WIDTH, (x+1) * TILE_WIDTH, MappableComponent::Boundary::Type::danger); + + addBoundary( + mappable.getUpBoundaries(), + (y+1) * TILE_HEIGHT, + x * TILE_WIDTH, + (x+1) * TILE_WIDTH, + MappableComponent::Boundary::Type::danger); } } } diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp index 187bebc..2df8f87 100644 --- a/src/systems/orienting.cpp +++ b/src/systems/orienting.cpp @@ -93,24 +93,18 @@ void OrientingSystem::moveRight(id_type entity) void OrientingSystem::stopWalking(id_type entity) { - auto& ponderable = game_.getEntityManager(). - getComponent(entity); - auto& orientable = game_.getEntityManager(). getComponent(entity); orientable.setWalkState(OrientableComponent::WalkState::still); - if (ponderable.isGrounded()) - { - auto& animating = game_.getSystemManager().getSystem(); + auto& animating = game_.getSystemManager().getSystem(); - if (orientable.isFacingRight()) - { - animating.startAnimation(entity, "stillRight"); - } else { - animating.startAnimation(entity, "stillLeft"); - } + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); } } diff --git a/src/systems/playing.cpp b/src/systems/playing.cpp index 2c6a419..40d9706 100644 --- a/src/systems/playing.cpp +++ b/src/systems/playing.cpp @@ -7,13 +7,18 @@ #include "components/orientable.h" #include "systems/mapping.h" #include "systems/pondering.h" +#include "systems/orienting.h" +#include "systems/scheduling.h" +#include "systems/controlling.h" #include "animation.h" +#include "muxer.h" void PlayingSystem::tick(double) { // Check if we need to change the map auto players = game_.getEntityManager().getEntitiesWithComponents< - PlayableComponent>(); + PlayableComponent, + TransformableComponent>(); for (id_type player : players) { @@ -44,6 +49,12 @@ void PlayingSystem::tick(double) playable.changingMap = false; + if (playable.newMapCallback) + { + playable.newMapCallback(); + playable.newMapCallback = nullptr; + } + break; } } @@ -66,7 +77,10 @@ void PlayingSystem::initPlayer() game_.getEntityManager().emplaceComponent( player, - 203, 44, 10, 12); + game_.getWorld().getStartingX(), + game_.getWorld().getStartingY(), + 10, + 12); game_.getSystemManager().getSystem().initializeBody( player, @@ -74,13 +88,20 @@ void PlayingSystem::initPlayer() game_.getEntityManager().emplaceComponent(player); game_.getEntityManager().emplaceComponent(player); - game_.getEntityManager().emplaceComponent(player); + + auto& playable = game_.getEntityManager(). + emplaceComponent(player); + + playable.checkpointMapId = game_.getWorld().getStartingMapId(); + playable.checkpointX = game_.getWorld().getStartingX(); + playable.checkpointY = game_.getWorld().getStartingY(); } void PlayingSystem::changeMap( size_t mapId, double x, - double y) + double y, + PlayableComponent::MapChangeCallback callback) { auto players = game_.getEntityManager().getEntitiesWithComponents< PlayableComponent>(); @@ -94,5 +115,61 @@ void PlayingSystem::changeMap( playable.newMapId = mapId; playable.newMapX = x; playable.newMapY = y; + playable.newMapCallback = std::move(callback); + } +} + +void PlayingSystem::die() +{ + playSound("res/Hit_Hurt5.wav", 0.25); + + auto players = game_.getEntityManager().getEntitiesWithComponents< + OrientableComponent, + ControllableComponent, + AnimatableComponent, + PonderableComponent, + PlayableComponent>(); + + for (id_type player : players) + { + auto& animatable = game_.getEntityManager(). + getComponent(player); + + auto& ponderable = game_.getEntityManager(). + getComponent(player); + + auto& controlling = game_.getSystemManager().getSystem(); + controlling.freeze(player); + + animatable.setFrozen(true); + animatable.setFlickering(true); + ponderable.setFrozen(true); + ponderable.setCollidable(false); + + auto& scheduling = game_.getSystemManager().getSystem(); + + scheduling.schedule(player, 0.75, [&] (id_type player) { + auto& playable = game_.getEntityManager(). + getComponent(player); + + changeMap( + playable.checkpointMapId, + playable.checkpointX, + playable.checkpointY, + [&, player] () { + animatable.setFrozen(false); + animatable.setFlickering(false); + ponderable.setFrozen(false); + ponderable.setCollidable(true); + + // Reset the walk state, and then potentially let the + // ControllingSystem set it again. + auto& orienting = game_.getSystemManager(). + getSystem(); + orienting.stopWalking(player); + + controlling.unfreeze(player); + }); + }); } } diff --git a/src/systems/playing.h b/src/systems/playing.h index c98a464..ff16808 100644 --- a/src/systems/playing.h +++ b/src/systems/playing.h @@ -2,6 +2,7 @@ #define PLAYING_H_70A54F7D #include "system.h" +#include "components/playable.h" class PlayingSystem : public System { public: @@ -14,7 +15,13 @@ public: void initPlayer(); - void changeMap(size_t mapId, double x, double y); + void changeMap( + size_t mapId, + double x, + double y, + PlayableComponent::MapChangeCallback callback = nullptr); + + void die(); }; diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 2490dc9..d3601ac 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -27,6 +27,11 @@ void PonderingSystem::tick(double dt) auto& ponderable = game_.getEntityManager(). getComponent(entity); + if (ponderable.isFrozen()) + { + continue; + } + // Accelerate ponderable.setVelocityX( ponderable.getVelocityX() + ponderable.getAccelX() * dt); @@ -289,6 +294,17 @@ void PonderingSystem::tick(double dt) break; } } + + break; + } + + case Collision::Type::danger: + { + game_.getSystemManager().getSystem().die(); + + stopProcessing = true; + + break; } default: diff --git a/src/systems/scheduling.cpp b/src/systems/scheduling.cpp new file mode 100644 index 0000000..220efae --- /dev/null +++ b/src/systems/scheduling.cpp @@ -0,0 +1,54 @@ +#include "scheduling.h" +#include "game.h" +#include "components/schedulable.h" +#include "util.h" + +void SchedulingSystem::tick(double dt) +{ + auto entities = game_.getEntityManager().getEntitiesWithComponents< + SchedulableComponent>(); + + for (id_type entity : entities) + { + auto& schedulable = game_.getEntityManager(). + getComponent(entity); + + for (auto& action : schedulable.actions) + { + std::get<0>(action) -= dt; + + if (std::get<0>(action) < 0) + { + std::get<1>(action)(entity); + } + } + + erase_if(schedulable.actions, + [] (const SchedulableComponent::Action& action) { + return (std::get<0>(action) < 0); + }); + + if (schedulable.actions.empty()) + { + game_.getEntityManager().removeComponent(entity); + } + } +} + +void SchedulingSystem::schedule( + id_type entity, + double length, + std::function action) +{ + if (!game_.getEntityManager().hasComponent(entity)) + { + game_.getEntityManager().emplaceComponent(entity); + } + + auto& schedulable = game_.getEntityManager(). + getComponent(entity); + + schedulable.actions.emplace_back( + length, + std::move(action)); +} diff --git a/src/systems/scheduling.h b/src/systems/scheduling.h new file mode 100644 index 0000000..7950d62 --- /dev/null +++ b/src/systems/scheduling.h @@ -0,0 +1,22 @@ +#ifndef SCHEDULING_H_7B02E3E3 +#define SCHEDULING_H_7B02E3E3 + +#include "system.h" + +class SchedulingSystem : public System { +public: + + SchedulingSystem(Game& game) : System(game) + { + } + + void tick(double dt); + + void schedule( + id_type entity, + double length, + std::function action); + +}; + +#endif /* end of include guard: SCHEDULING_H_7B02E3E3 */ -- cgit 1.4.1