diff --git a/build/BeagleRescue b/build/BeagleRescue index dd67129..6beaee1 100755 Binary files a/build/BeagleRescue and b/build/BeagleRescue differ diff --git a/src/config/config.json b/src/config/config.json index bffe0fe..7185755 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -3,5 +3,5 @@ "WindowName":"Beagle Rescue", "WindowSize":{"w":320,"h":240}, "WindowFullScreen": 0, -"GlobalScale": 3 +"GlobalScale": 1 } diff --git a/src/config/credits.json b/src/config/credits.json index b48141a..db7738a 100644 --- a/src/config/credits.json +++ b/src/config/credits.json @@ -2,5 +2,5 @@ "GameName":"Beagle Rescue", "Developers": "Alan Youngblood, Simon Zaleski, Daniel Rinaldi", "LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere", -"SpecialThanks":"Nic Allen, Brian Lhota" +"SpecialThanks":"Nic Allen, Brian Lhota, Rodrigo Monteiro" } diff --git a/src/ecs/ColliderComponent.h b/src/ecs/ColliderComponent.h index 383c306..86adaa3 100644 --- a/src/ecs/ColliderComponent.h +++ b/src/ecs/ColliderComponent.h @@ -14,10 +14,13 @@ #include "ECS.h" #include "../assetmgr/TextureManager.h" #include +#include "../game/Vector2D.h" class ColliderComponent : public Component { + public: + SDL_Rect collider; std::string tag; @@ -28,9 +31,16 @@ public: int offsetY = 0; bool hidden = true; + + Vector2D center; TransformComponent* transform; + ColliderComponent() + { + center.Zero(); + } + ColliderComponent(std::string t) { tag = t; @@ -43,6 +53,8 @@ public: collider.y = ypos; collider.w = collider.h = size*scale; setTex(texture); + center.x = collider.x+collider.w/2; + center.y = collider.y+collider.h/2; } ColliderComponent(std::string t, int width, int height, bool hasOffset, int oX, int oY, std::string texture) @@ -53,6 +65,8 @@ public: offsetX = oX; offsetY = oY; setTex(texture); + center.x = collider.x+collider.w/2; + center.y = collider.y+collider.h/2; } void init() override @@ -78,6 +92,10 @@ public: } destR.x = collider.x - Game::camera.x; destR.y = collider.y - Game::camera.y; + center.x = collider.x+collider.w/2; + center.y = collider.y+collider.h/2; +// collider.x = transform->position.x; +// collider.y = transform->position.y; } void draw() override @@ -88,7 +106,10 @@ public: // } if(hidden == false) { - TextureManager::Draw(tex, srcR,destR,SDL_FLIP_NONE); +// TextureManager::Draw(tex, srcR,destR,SDL_FLIP_NONE); + SDL_SetRenderDrawColor(Game::renderer,255,0,255,134); + SDL_RenderDrawRect(Game::renderer, &destR); +// SDL_RenderDrawPoint(Game::renderer, center.x, center.y); } } diff --git a/src/ecs/Components.h b/src/ecs/Components.h index 3e875f6..dbbcb67 100644 --- a/src/ecs/Components.h +++ b/src/ecs/Components.h @@ -13,6 +13,7 @@ #include "SpriteComponent.h" #include "KeyboardController.h" #include "ColliderComponent.h" +#include "PlayerController.h" #include "ProjectileComponent.h" #include "TileComponent.h" #include "UIFontComponent.h" diff --git a/src/ecs/KeyboardController.h b/src/ecs/KeyboardController.h index ee59dcb..6baa65a 100644 --- a/src/ecs/KeyboardController.h +++ b/src/ecs/KeyboardController.h @@ -11,7 +11,10 @@ #include "../game/Game.hpp" #include "ECS.h" #include "Components.h" +#include "PlayerController.h" #include "../assetmgr/AssetManager.h" +#include "ColliderComponent.h" +// #include "../game/Vector2D.h" class KeyboardController : public Component { @@ -19,13 +22,17 @@ public: TransformComponent *transform; SpriteComponent *sprite; - + PlayerController *playerCtrl; + ColliderComponent *collider; + Game *game; void init() override { transform = &entity->getComponent(); sprite = &entity->getComponent(); +// collider = &entity->getComponent(); +// playerCtrl = &entity->getComponent(); } void update() override @@ -40,6 +47,14 @@ public: break; case SDLK_LEFT: if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ +// if (playerCtrl == NULL){ +// printf("No player controller found\n"); +// } else { +// printf("playerctrl.lastsafepos .x: %g .y: %g \n",playerCtrl->lastSafePosition.x,playerCtrl->lastSafePosition.y); +// playerCtrl->lastSafePosition.x = transform->position.x; +// } +// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y); +// printf("lastSafePos .x: %g .y: %g \n",transform->lastSafePos.x,transform->lastSafePos.y); transform->velocity.x = -1; if(Game::playerIsGrounded){ sprite->Play("Walk"); @@ -49,6 +64,13 @@ public: break; case SDLK_RIGHT: if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ +// &entity->getComponent().setLastSafePos(Vector2D(transform->position.x,transform->position.y)); +// playerCtrl->lastSafePosition.x = transform->position.x; +// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y); +// ========= +// Do a pre-check for tile-based smooth algorithm +// ========= +// int intersectionTileX = collider->center.x; transform->velocity.x = 1; if(Game::playerIsGrounded){ sprite->Play("Walk"); @@ -63,14 +85,17 @@ public: {Game::debugCollisionBoxes = false; } break; case SDLK_j: - if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ - if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0) - { - Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0); + if(Game::playerIsGrounded){ + if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ + if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0) + { + Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0); + } + transform->lastSafePos = Vector2D(transform->position.x,transform->position.y); + Game::gravityOnPlayer = true; + sprite->Play("Jump"); + transform->velocity.y = -2; } - Game::gravityOnPlayer = true; - sprite->Play("Jump"); - transform->velocity.y = -2; } break; case SDLK_u: diff --git a/src/ecs/PlayerController.h b/src/ecs/PlayerController.h new file mode 100644 index 0000000..1ea0a0c --- /dev/null +++ b/src/ecs/PlayerController.h @@ -0,0 +1,77 @@ +/* + * PlayerController.h + * + * Created on: Aug 27, 2021 + * Author: ayoungblood + */ + +#ifndef SRC_ECS_PlayerController_H_ +#define SRC_ECS_PlayerController_H_ + +#include "Components.h" +#include "../game/Vector2D.h" + +class PlayerController : public Component +{ + +public: + + Vector2D lastSafePosition; + float jumpTime; + float jumpForce; + + bool isJumping; + bool isGrounded; + + PlayerController() + { + jumpTime = 0.0f; + jumpForce = 0.0f; + + bool isJumping = false; + bool isGrounded = false; + + lastSafePosition = Vector2D(); + } + + PlayerController(float jumpT, float jumpF, bool jumping, bool grounded, Vector2D lastPos) + { + jumpTime = jumpT; + jumpForce = jumpF; + + bool isJumping = jumping; + bool isGrounded = grounded; + + lastSafePosition = lastPos; + } + + ~PlayerController() + { + + } + + void init() override + { + lastSafePosition.Zero(); + } + + void update() override + { + + } + + void setLastSafePos(Vector2D prevSafePos) + { + lastSafePosition = prevSafePos; + } + +// void init() override +// { +// +// } + +}; + + +#endif /* SRC_ECS_PlayerController_H_ */ + diff --git a/src/ecs/TransformComponent.h b/src/ecs/TransformComponent.h index 1348593..4bc80a2 100644 --- a/src/ecs/TransformComponent.h +++ b/src/ecs/TransformComponent.h @@ -18,6 +18,7 @@ public: Vector2D position; Vector2D velocity; + Vector2D lastSafePos; int height = 40; int width = 30; @@ -37,10 +38,10 @@ public: speed = speed*sc; } - TransformComponent(float x, float y) - { - position.Zero(); - } +// TransformComponent(float x, float y) +// { +// position.Zero(); +// } TransformComponent(int x, int y, int w, int h, int sc) { @@ -55,6 +56,7 @@ public: void init() override { velocity.Zero(); + lastSafePos.Zero(); } void update() override { diff --git a/src/game/Game.cpp b/src/game/Game.cpp index 55c4efa..bc06185 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -55,6 +55,8 @@ auto& uiInfo(manager.addEntity()); bool Game::debugCollisionBoxes = false; bool Game::gravityOnPlayer = true; bool Game::playerIsGrounded = false; +bool Game::playerIsJumping = false; + int gScale = 0; @@ -154,11 +156,12 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g map->LoadMap("assets/maps/br-map-color.txt",70,45, globalScale); - player.addComponent(860*globalScale,540*globalScale,22,42,globalScale); -// player.addComponent(150*globalScale,80*globalScale,40,40,globalScale); + player.addComponent(860*globalScale,640*globalScale,22,42,globalScale); player.addComponent("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json"); + +// player.addComponent(0.0,0.0,false,false,Vector2D().Zero()); + player.addComponent("player",16*globalScale,32*globalScale, true, 2*globalScale,10*globalScale, "collider"); player.addComponent(); - player.addComponent("player",8*globalScale,8*globalScale, true, 7*globalScale,34*globalScale, "collider"); player.addGroup(groupPlayers); puppy.addComponent(1024*globalScale,210*globalScale,36,30,globalScale); @@ -224,7 +227,6 @@ void Game::update() // const char* gameOverText = "Game Over"; // } - if (Mix_PlayingMusic() == 0 && gsm->currentState == GameStateManager::ST_COREGAME) { // std::cout << "Play Music Now" << std::endl; @@ -261,17 +263,17 @@ void Game::update() printDebug(std::to_string(playerCol.x)); printDebug(std::to_string(playerCol.y)); printDebug(std::to_string(playerCol.w)); - printDebug(std::to_string(playerCol.h)); - printDebug("playerTransform x,y,w,h"); + printDebug(std::to_string(playerCol.h));*/ +/* printDebug("playerTransform x,y,w,h"); printDebug(std::to_string(player.getComponent().position.x)); printDebug(std::to_string(player.getComponent().position.y)); printDebug(std::to_string(player.getComponent().width)); - printDebug(std::to_string(player.getComponent().height));*/ + printDebug(std::to_string(player.getComponent().height)); */ player.getComponent().Play("Idle"); } - - gravityOnPlayer = false; playerIsGrounded = true; +// player.getComponent().position.y = player.getComponent().lastSafePos.y; + gravityOnPlayer = false; } } @@ -364,6 +366,15 @@ void Game::render() } } +// SDL_Rect myBox; +// myBox = SDL_Rect(); +// myBox.w = 32*gScale; +// myBox.h = 32*gScale; +// myBox.x = 0*gScale; +// myBox.y = 0*gScale; +// +// SDL_SetRenderDrawColor(renderer, 255,0,255,134); +// SDL_RenderDrawRect(renderer, &myBox); SDL_RenderPresent(renderer); } diff --git a/src/game/Game.hpp b/src/game/Game.hpp index 9c7c1b8..1c6372a 100644 --- a/src/game/Game.hpp +++ b/src/game/Game.hpp @@ -41,6 +41,7 @@ public: static bool debugCollisionBoxes; static bool gravityOnPlayer; static bool playerIsGrounded; + static bool playerIsJumping; static SDL_Rect camera; static AssetManager* assets; static GameStateManager* gsm;