Collider debug boxes with SDL_Rect
This commit is contained in:
@ -14,10 +14,13 @@
|
||||
#include "ECS.h"
|
||||
#include "../assetmgr/TextureManager.h"
|
||||
#include <iostream>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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<TransformComponent>();
|
||||
sprite = &entity->getComponent<SpriteComponent>();
|
||||
// collider = &entity->getComponent<ColliderComponent>();
|
||||
// playerCtrl = &entity->getComponent<PlayerController>();
|
||||
}
|
||||
|
||||
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<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;
|
||||
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:
|
||||
|
77
src/ecs/PlayerController.h
Normal file
77
src/ecs/PlayerController.h
Normal 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_ */
|
||||
|
@ -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
|
||||
{
|
||||
|
Reference in New Issue
Block a user