Collider debug boxes with SDL_Rect

This commit is contained in:
Alan Youngblood 2021-11-03 17:37:56 -04:00
parent 165052a617
commit bd0c49b3ae
10 changed files with 162 additions and 24 deletions

Binary file not shown.

View File

@ -3,5 +3,5 @@
"WindowName":"Beagle Rescue", "WindowName":"Beagle Rescue",
"WindowSize":{"w":320,"h":240}, "WindowSize":{"w":320,"h":240},
"WindowFullScreen": 0, "WindowFullScreen": 0,
"GlobalScale": 3 "GlobalScale": 1
} }

View File

@ -2,5 +2,5 @@
"GameName":"Beagle Rescue", "GameName":"Beagle Rescue",
"Developers": "Alan Youngblood, Simon Zaleski, Daniel Rinaldi", "Developers": "Alan Youngblood, Simon Zaleski, Daniel Rinaldi",
"LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere", "LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere",
"SpecialThanks":"Nic Allen, Brian Lhota" "SpecialThanks":"Nic Allen, Brian Lhota, Rodrigo Monteiro"
} }

View File

@ -14,10 +14,13 @@
#include "ECS.h" #include "ECS.h"
#include "../assetmgr/TextureManager.h" #include "../assetmgr/TextureManager.h"
#include <iostream> #include <iostream>
#include "../game/Vector2D.h"
class ColliderComponent : public Component class ColliderComponent : public Component
{ {
public: public:
SDL_Rect collider; SDL_Rect collider;
std::string tag; std::string tag;
@ -29,8 +32,15 @@ public:
bool hidden = true; bool hidden = true;
Vector2D center;
TransformComponent* transform; TransformComponent* transform;
ColliderComponent()
{
center.Zero();
}
ColliderComponent(std::string t) ColliderComponent(std::string t)
{ {
tag = t; tag = t;
@ -43,6 +53,8 @@ public:
collider.y = ypos; collider.y = ypos;
collider.w = collider.h = size*scale; collider.w = collider.h = size*scale;
setTex(texture); 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) ColliderComponent(std::string t, int width, int height, bool hasOffset, int oX, int oY, std::string texture)
@ -53,6 +65,8 @@ public:
offsetX = oX; offsetX = oX;
offsetY = oY; offsetY = oY;
setTex(texture); setTex(texture);
center.x = collider.x+collider.w/2;
center.y = collider.y+collider.h/2;
} }
void init() override void init() override
@ -78,6 +92,10 @@ public:
} }
destR.x = collider.x - Game::camera.x; destR.x = collider.x - Game::camera.x;
destR.y = collider.y - Game::camera.y; 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 void draw() override
@ -88,7 +106,10 @@ public:
// } // }
if(hidden == false) 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);
} }
} }

View File

@ -13,6 +13,7 @@
#include "SpriteComponent.h" #include "SpriteComponent.h"
#include "KeyboardController.h" #include "KeyboardController.h"
#include "ColliderComponent.h" #include "ColliderComponent.h"
#include "PlayerController.h"
#include "ProjectileComponent.h" #include "ProjectileComponent.h"
#include "TileComponent.h" #include "TileComponent.h"
#include "UIFontComponent.h" #include "UIFontComponent.h"

View File

@ -11,7 +11,10 @@
#include "../game/Game.hpp" #include "../game/Game.hpp"
#include "ECS.h" #include "ECS.h"
#include "Components.h" #include "Components.h"
#include "PlayerController.h"
#include "../assetmgr/AssetManager.h" #include "../assetmgr/AssetManager.h"
#include "ColliderComponent.h"
// #include "../game/Vector2D.h"
class KeyboardController : public Component class KeyboardController : public Component
{ {
@ -19,6 +22,8 @@ public:
TransformComponent *transform; TransformComponent *transform;
SpriteComponent *sprite; SpriteComponent *sprite;
PlayerController *playerCtrl;
ColliderComponent *collider;
Game *game; Game *game;
@ -26,6 +31,8 @@ public:
{ {
transform = &entity->getComponent<TransformComponent>(); transform = &entity->getComponent<TransformComponent>();
sprite = &entity->getComponent<SpriteComponent>(); sprite = &entity->getComponent<SpriteComponent>();
// collider = &entity->getComponent<ColliderComponent>();
// playerCtrl = &entity->getComponent<PlayerController>();
} }
void update() override void update() override
@ -40,6 +47,14 @@ public:
break; break;
case SDLK_LEFT: case SDLK_LEFT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ 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; transform->velocity.x = -1;
if(Game::playerIsGrounded){ if(Game::playerIsGrounded){
sprite->Play("Walk"); sprite->Play("Walk");
@ -49,6 +64,13 @@ public:
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// &entity->getComponent<PlayerController>().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; transform->velocity.x = 1;
if(Game::playerIsGrounded){ if(Game::playerIsGrounded){
sprite->Play("Walk"); sprite->Play("Walk");
@ -63,14 +85,17 @@ public:
{Game::debugCollisionBoxes = false; } {Game::debugCollisionBoxes = false; }
break; break;
case SDLK_j: case SDLK_j:
if(Game::playerIsGrounded){
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0) if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0)
{ {
Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0); Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0);
} }
transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
Game::gravityOnPlayer = true; Game::gravityOnPlayer = true;
sprite->Play("Jump"); sprite->Play("Jump");
transform->velocity.y = -2; transform->velocity.y = -2;
}
} }
break; break;
case SDLK_u: case SDLK_u:

View File

@ -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_ */

View File

@ -18,6 +18,7 @@ public:
Vector2D position; Vector2D position;
Vector2D velocity; Vector2D velocity;
Vector2D lastSafePos;
int height = 40; int height = 40;
int width = 30; int width = 30;
@ -37,10 +38,10 @@ public:
speed = speed*sc; speed = speed*sc;
} }
TransformComponent(float x, float y) // TransformComponent(float x, float y)
{ // {
position.Zero(); // position.Zero();
} // }
TransformComponent(int x, int y, int w, int h, int sc) TransformComponent(int x, int y, int w, int h, int sc)
{ {
@ -55,6 +56,7 @@ public:
void init() override void init() override
{ {
velocity.Zero(); velocity.Zero();
lastSafePos.Zero();
} }
void update() override void update() override
{ {

View File

@ -55,6 +55,8 @@ auto& uiInfo(manager.addEntity());
bool Game::debugCollisionBoxes = false; bool Game::debugCollisionBoxes = false;
bool Game::gravityOnPlayer = true; bool Game::gravityOnPlayer = true;
bool Game::playerIsGrounded = false; bool Game::playerIsGrounded = false;
bool Game::playerIsJumping = false;
int gScale = 0; 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); map->LoadMap("assets/maps/br-map-color.txt",70,45, globalScale);
player.addComponent<TransformComponent>(860*globalScale,540*globalScale,22,42,globalScale); player.addComponent<TransformComponent>(860*globalScale,640*globalScale,22,42,globalScale);
// player.addComponent<TransformComponent>(150*globalScale,80*globalScale,40,40,globalScale);
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json"); player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
// player.addComponent<PlayerController>(0.0,0.0,false,false,Vector2D().Zero());
player.addComponent<ColliderComponent>("player",16*globalScale,32*globalScale, true, 2*globalScale,10*globalScale, "collider");
player.addComponent<KeyboardController>(); player.addComponent<KeyboardController>();
player.addComponent<ColliderComponent>("player",8*globalScale,8*globalScale, true, 7*globalScale,34*globalScale, "collider");
player.addGroup(groupPlayers); player.addGroup(groupPlayers);
puppy.addComponent<TransformComponent>(1024*globalScale,210*globalScale,36,30,globalScale); puppy.addComponent<TransformComponent>(1024*globalScale,210*globalScale,36,30,globalScale);
@ -224,7 +227,6 @@ void Game::update()
// const char* gameOverText = "Game Over"; // const char* gameOverText = "Game Over";
// } // }
if (Mix_PlayingMusic() == 0 && gsm->currentState == GameStateManager::ST_COREGAME) if (Mix_PlayingMusic() == 0 && gsm->currentState == GameStateManager::ST_COREGAME)
{ {
// std::cout << "Play Music Now" << std::endl; // 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.x));
printDebug(std::to_string(playerCol.y)); printDebug(std::to_string(playerCol.y));
printDebug(std::to_string(playerCol.w)); printDebug(std::to_string(playerCol.w));
printDebug(std::to_string(playerCol.h)); printDebug(std::to_string(playerCol.h));*/
printDebug("playerTransform x,y,w,h"); /* printDebug("playerTransform x,y,w,h");
printDebug(std::to_string(player.getComponent<TransformComponent>().position.x)); printDebug(std::to_string(player.getComponent<TransformComponent>().position.x));
printDebug(std::to_string(player.getComponent<TransformComponent>().position.y)); printDebug(std::to_string(player.getComponent<TransformComponent>().position.y));
printDebug(std::to_string(player.getComponent<TransformComponent>().width)); printDebug(std::to_string(player.getComponent<TransformComponent>().width));
printDebug(std::to_string(player.getComponent<TransformComponent>().height));*/ printDebug(std::to_string(player.getComponent<TransformComponent>().height)); */
player.getComponent<SpriteComponent>().Play("Idle"); player.getComponent<SpriteComponent>().Play("Idle");
} }
gravityOnPlayer = false;
playerIsGrounded = true; playerIsGrounded = true;
// player.getComponent<TransformComponent>().position.y = player.getComponent<TransformComponent>().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); SDL_RenderPresent(renderer);
} }

View File

@ -41,6 +41,7 @@ public:
static bool debugCollisionBoxes; static bool debugCollisionBoxes;
static bool gravityOnPlayer; static bool gravityOnPlayer;
static bool playerIsGrounded; static bool playerIsGrounded;
static bool playerIsJumping;
static SDL_Rect camera; static SDL_Rect camera;
static AssetManager* assets; static AssetManager* assets;
static GameStateManager* gsm; static GameStateManager* gsm;