From 1a392a79b0491c5acc766705698191ed2ed6c2e6 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 12 Mar 2015 13:48:10 -0400 Subject: Added death to my game --- maps/second.txt | 2 +- res/tiles.png | Bin 688 -> 701 bytes shaders/blit.fragment | 3 ++- src/components.cpp | 68 +++++++++++++++++++++++++++++++++++++------------- src/components.h | 6 ++--- src/entity.h | 3 ++- src/game.cpp | 38 ++++++++++++++++++++++++++-- src/game.h | 3 +++ src/renderer.cpp | 5 +++- src/renderer.h | 2 +- 10 files changed, 102 insertions(+), 28 deletions(-) diff --git a/maps/second.txt b/maps/second.txt index 386af10..de5ec83 100644 --- a/maps/second.txt +++ b/maps/second.txt @@ -21,5 +21,5 @@ 0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,27,2,2,3,3,3,4,26,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,20,0,0,0,0,0,19,0,0,27,1,1,1,2,2,2,26,0,0,0,0,0,0,0,0,0,0,24,4,2,2,1,3,2,2,1,2,2,3, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,19,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,42,42,42,42,42,42,42,42,42,42,12,0,0,0,0,0,19,0,0,0,0,0, It's A Trap! \ No newline at end of file diff --git a/res/tiles.png b/res/tiles.png index 270f557..5704190 100644 Binary files a/res/tiles.png and b/res/tiles.png differ diff --git a/shaders/blit.fragment b/shaders/blit.fragment index a96f217..2d412c0 100644 --- a/shaders/blit.fragment +++ b/shaders/blit.fragment @@ -5,8 +5,9 @@ in vec2 UV; out vec4 color; uniform sampler2D srctex; +uniform float alpha; void main() { - color = texture(srctex, UV); + color = vec4(texture(srctex, UV).rgb, alpha); } diff --git a/src/components.cpp b/src/components.cpp index 11350ea..e2eb2ed 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -125,25 +125,42 @@ void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& c void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer) { + animFrame++; + int frame = 0; if (isMoving) { frame += 2; - if (animFrame < 10) + if (animFrame % 20 < 10) { frame += 2; } } - if (facingLeft) frame++; + + if (facingLeft) + { + frame++; + } + + double alpha = 1.0; + if (dying && (animFrame % 4 < 2)) + { + alpha = 0.0; + } Rectangle src_rect {frame*10, 0, 10, 12}; Rectangle dst_rect {(int) entity.position.first, (int) entity.position.second, entity.size.first, entity.size.second}; - buffer.blit(sprite, src_rect, dst_rect); + buffer.blit(sprite, src_rect, dst_rect, alpha); } void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) { + if (dying) + { + return; + } + if (msg.type == Message::Type::walkLeft) { facingLeft = true; @@ -155,15 +172,13 @@ void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) } else if (msg.type == Message::Type::stopWalking) { isMoving = false; + } else if (msg.type == Message::Type::die) + { + dying = true; + isMoving = false; } } -void PlayerSpriteComponent::tick(Game&, Entity&) -{ - animFrame++; - animFrame %= 20; -} - // Player physics #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) @@ -220,11 +235,20 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) entity.position.second = msg.dropAxis - entity.size.second; velocity.second = 0; } + } else if (msg.type == Message::Type::die) + { + frozen = true; } } void PlayerPhysicsComponent::tick(Game& game, Entity& entity) { + // If frozen, do nothing + if (frozen) + { + return; + } + // Continue walking even if blocked earlier if (velocity.first == 0) { @@ -302,13 +326,10 @@ void MapRenderComponent::render(Game&, Entity&, Texture& buffer) // Map collision -MapCollisionComponent::MapCollisionComponent(const Map& map) +MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) { - leftMap = map.getLeftMap(); - rightMap = map.getRightMap(); - - addCollision(-6, 0, GAME_WIDTH, Direction::left, (leftMap == nullptr) ? 1 : 2); - addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (rightMap == nullptr) ? 3 : 2); + addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? 1 : 2); + addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? 3 : 2); for (int i=0; i 0) && (!((tile >= 5) && (tile <= 7)))) + if ((tile > 0) && (tile < 28) && (!((tile >= 5) && (tile <= 7)))) { addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, 0); addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, 0); @@ -325,6 +346,9 @@ MapCollisionComponent::MapCollisionComponent(const Map& map) } else if ((tile >= 5) && (tile <= 7)) { addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 4); + } else if (tile == 42) + { + addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 5); } } } @@ -518,11 +542,11 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli if (dir == Direction::left) { collider.position.first = GAME_WIDTH-collider.size.first/2; - game.loadMap(*leftMap); + game.loadMap(*(map.getLeftMap())); } else if (dir == Direction::right) { collider.position.first = -collider.size.first/2; - game.loadMap(*rightMap); + game.loadMap(*(map.getRightMap())); } return true; @@ -541,6 +565,14 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli msg.dropAxis = collision.axis; collider.send(game, msg); + } else if (collision.type == 5) + { + Message msg(Message::Type::die); + collider.send(game, msg); + + game.schedule(FRAMES_PER_SECOND * 0.75, [&] () { + game.loadGame(map); + }); } return false; diff --git a/src/components.h b/src/components.h index 2585d73..f182590 100644 --- a/src/components.h +++ b/src/components.h @@ -31,13 +31,13 @@ class PlayerSpriteComponent : public Component { public: void render(Game& game, Entity& entity, Texture& buffer); void receive(Game& game, Entity& entity, const Message& msg); - void tick(Game& game, Entity& entity); private: Texture sprite{"../res/Starla.png"}; int animFrame = 0; bool facingLeft = false; bool isMoving = false; + bool dying = false; }; class PlayerPhysicsComponent : public Component { @@ -54,6 +54,7 @@ class PlayerPhysicsComponent : public Component { double jump_gravity_short; int direction = 0; bool canDrop = false; + bool frozen = false; }; class MapRenderComponent : public Component { @@ -89,8 +90,7 @@ class MapCollisionComponent : public Component { std::list right_collisions; std::list up_collisions; std::list down_collisions; - const Map* leftMap; - const Map* rightMap; + const Map& map; }; #endif diff --git a/src/entity.h b/src/entity.h index 266fe11..772c98b 100644 --- a/src/entity.h +++ b/src/entity.h @@ -21,7 +21,8 @@ class Message { stopJump, drop, canDrop, - cantDrop + cantDrop, + die }; Message(Type type) : type(type) {} diff --git a/src/game.cpp b/src/game.cpp index e392923..043c82b 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -77,6 +77,19 @@ void Game::execute(GLFWwindow* window) { entity->tick(*this); } + + // Do any scheduled tasks + for (auto& task : scheduled) + { + task.first--; + + if (task.first == 0) + { + task.second(); + } + } + + scheduled.remove_if([] (std::pair> value) { return value.first == 0; }); // Do rendering buffer.fill(buffer.entirety(), 0, 0, 0); @@ -121,10 +134,31 @@ void Game::saveGame(const Map& map, std::pair position) void Game::loadGame(const Map& curMap) { - if (&curMap != save.map) + if (&curMap == save.map) { - loadMap(*(save.map)); + entities.remove(player); } + player = std::make_shared(); player->position = save.position; + player->size = std::make_pair(10.0,12.0); + + auto player_input = std::make_shared(); + player->addComponent(player_input); + + auto player_physics = std::make_shared(); + player->addComponent(player_physics); + + auto player_anim = std::make_shared(); + player->addComponent(player_anim); + + if (&curMap != save.map) + { + loadMap(*(save.map)); + } +} + +void Game::schedule(int frames, std::function&& callback) +{ + scheduled.emplace(begin(scheduled), frames, callback); } diff --git a/src/game.h b/src/game.h index 69b8df7..cea154a 100644 --- a/src/game.h +++ b/src/game.h @@ -6,6 +6,7 @@ class Game; #include "map.h" #include #include "entity.h" +#include const int TILE_WIDTH = 8; const int TILE_HEIGHT = 8; @@ -30,6 +31,7 @@ class Game { void detectCollision(Entity& collider, std::pair old_position); void saveGame(const Map& map, std::pair position); void loadGame(const Map& curMap); + void schedule(int frames, std::function&& callback); bool shouldQuit = false; private: @@ -42,6 +44,7 @@ class Game { Map m{"../maps/embarass.txt"}; Map m2{"../maps/second.txt"}; Savefile save; + std::list>> scheduled; }; #endif diff --git a/src/renderer.cpp b/src/renderer.cpp index f01d72a..64c9fd0 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -613,7 +613,7 @@ void Texture::fill(Rectangle dstrect, int r, int g, int b) glDeleteBuffers(1, &vertexbuffer); } -void Texture::blit(const Texture& srctex, Rectangle srcrect, Rectangle dstrect) +void Texture::blit(const Texture& srctex, Rectangle srcrect, Rectangle dstrect, double alpha) { if (!rendererInitialized) { @@ -621,6 +621,8 @@ void Texture::blit(const Texture& srctex, Rectangle srcrect, Rectangle dstrect) 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); @@ -670,6 +672,7 @@ void Texture::blit(const Texture& srctex, Rectangle srcrect, Rectangle dstrect) 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); diff --git a/src/renderer.h b/src/renderer.h index 377b9ee..90ec101 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -21,7 +21,7 @@ class Texture { Texture& operator= (Texture tex); friend void swap(Texture& tex1, Texture& tex2); void fill(Rectangle loc, int r, int g, int b); - void blit(const Texture& src, Rectangle srcrect, Rectangle dstrect); + void blit(const Texture& src, Rectangle srcrect, Rectangle dstrect, double alpha = 1.0); void renderScreen() const; Rectangle entirety() const; -- cgit 1.4.1