predictCollisions func added to Game.cpp
This commit is contained in:
parent
e28a9cebdb
commit
36adcdeac7
File diff suppressed because one or more lines are too long
Binary file not shown.
@ -1,7 +1,7 @@
|
||||
{
|
||||
"GameName":"Beagle Rescue",
|
||||
"WindowName":"Beagle Rescue",
|
||||
"WindowSize":{"w":320,"h":240},
|
||||
"WindowSize":{"w":320,"h":200},
|
||||
"WindowFullScreen": 0,
|
||||
"GlobalScale": 3
|
||||
"GlobalScale": 4
|
||||
}
|
||||
|
@ -43,13 +43,15 @@ public:
|
||||
{
|
||||
case SDLK_UP:
|
||||
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
|
||||
if(transform->position.y>0){
|
||||
// if(transform->position.y>0){
|
||||
// ====== Get Player.yTile and scan column up ScreenTilesHeight/2 on Player.xTile-1, Player.xTile, and Player.xTile+1 return first hit that is > 0 for each row.
|
||||
// transform->tilePos.x
|
||||
transform->velocity.y = -1;
|
||||
// if(Game::playerIsGrounded){
|
||||
sprite->Play("Walk");
|
||||
sprite->Play("Walk");
|
||||
// }
|
||||
sprite->spriteFlip = SDL_FLIP_NONE;
|
||||
}
|
||||
// }
|
||||
}
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
@ -59,6 +61,8 @@ public:
|
||||
// if(Game::playerIsGrounded){
|
||||
sprite->Play("Walk");
|
||||
// }
|
||||
int *borders;
|
||||
borders = Game::predictCollisions();
|
||||
sprite->spriteFlip = SDL_FLIP_NONE;
|
||||
}
|
||||
}
|
||||
@ -73,13 +77,22 @@ public:
|
||||
// }
|
||||
// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
|
||||
// printf("lastSafePos .x: %g .y: %g \n",transform->lastSafePos.x,transform->lastSafePos.y);
|
||||
if(transform->position.x>0){
|
||||
// Game::pTileX
|
||||
// if(transform->position.x>0){
|
||||
// ====== For Each Row ====
|
||||
// for (int r=Game::pTileY-1;r<=Game::pTileY+1;r++){
|
||||
// ====== For Each Tile (Column) =====
|
||||
// for (int c=Game::pTileX;c>Game::pTileX-Game::camera.w;c--){
|
||||
//
|
||||
//
|
||||
// }
|
||||
// }
|
||||
transform->velocity.x = -1;
|
||||
// if(Game::playerIsGrounded){
|
||||
sprite->Play("Walk");
|
||||
// }
|
||||
sprite->spriteFlip = SDL_FLIP_NONE;
|
||||
}
|
||||
// }
|
||||
}
|
||||
break;
|
||||
case SDLK_RIGHT:
|
||||
@ -135,7 +148,7 @@ public:
|
||||
{
|
||||
Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0);
|
||||
}
|
||||
transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
|
||||
// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
|
||||
Game::gravityOnPlayer = true;
|
||||
sprite->Play("Jump");
|
||||
transform->velocity.y = -2;
|
||||
|
@ -18,7 +18,8 @@ public:
|
||||
|
||||
Vector2D position;
|
||||
Vector2D velocity;
|
||||
Vector2D lastSafePos;
|
||||
// Vector2D lastSafePos;
|
||||
Vector2D tilePos;
|
||||
|
||||
int height = 40;
|
||||
int width = 30;
|
||||
@ -56,14 +57,19 @@ public:
|
||||
void init() override
|
||||
{
|
||||
velocity.Zero();
|
||||
lastSafePos.Zero();
|
||||
tilePos.Zero();
|
||||
// lastSafePos.Zero();
|
||||
}
|
||||
void update() override
|
||||
{
|
||||
position.x += velocity.x * speed;
|
||||
position.y += velocity.y * speed;
|
||||
}
|
||||
|
||||
|
||||
void updateTilePosition(int x,int y){
|
||||
tilePos.x = x;
|
||||
tilePos.y = y;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -21,10 +21,8 @@
|
||||
// #include "../ui/UIText.h"
|
||||
#include "../ui/UINineSlice.h"
|
||||
#include "../cjson/cJSON.h"
|
||||
#include "tmxparser.h"
|
||||
|
||||
|
||||
// Map* map;
|
||||
// tmxparser::TmxMap map;
|
||||
Manager manager;
|
||||
UINineSlice* my9Slice;
|
||||
UINineSlice* scoreboard9Slice;
|
||||
@ -71,6 +69,8 @@ int last_time;
|
||||
int current_time;
|
||||
int diff_time;
|
||||
|
||||
int Game::pTileX = 0;
|
||||
int Game::pTileY = 0;
|
||||
|
||||
std::string Game::BoolToString(bool b) {
|
||||
std::string myString;
|
||||
@ -332,11 +332,13 @@ void Game::update()
|
||||
camera.x = player.getComponent<TransformComponent>().position.x - camera.w/2 + player.getComponent<TransformComponent>().width/2;
|
||||
camera.y = player.getComponent<TransformComponent>().position.y - camera.h/2 + player.getComponent<TransformComponent>().height/2;
|
||||
|
||||
int pTileX = player.getComponent<TransformComponent>().position.x/gScale/tileMap.tileWidth;
|
||||
int pTileY = player.getComponent<TransformComponent>().position.y/gScale/tileMap.tileWidth;
|
||||
pTileX = player.getComponent<TransformComponent>().position.x/gScale/tileMap.tileWidth;
|
||||
pTileY = player.getComponent<TransformComponent>().position.y/gScale/tileMap.tileWidth;
|
||||
|
||||
uiCamXInfo.getComponent<UITextComponent>().updateString(std::to_string(pTileX));
|
||||
uiCamYInfo.getComponent<UITextComponent>().updateString(std::to_string(pTileY));
|
||||
player.getComponent<TransformComponent>().updateTilePosition(pTileX,pTileY);
|
||||
|
||||
uiCamXInfo.getComponent<UITextComponent>().updateString(std::to_string((int)player.getComponent<TransformComponent>().tilePos.x));
|
||||
uiCamYInfo.getComponent<UITextComponent>().updateString(std::to_string((int)player.getComponent<TransformComponent>().tilePos.y));
|
||||
int playerX = player.getComponent<TransformComponent>().position.x;
|
||||
int playerY = player.getComponent<TransformComponent>().position.y;
|
||||
|
||||
@ -451,3 +453,25 @@ void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blu
|
||||
SDL_SetRenderDrawColor(renderer, red, green, blue, 200);
|
||||
SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
|
||||
}
|
||||
|
||||
int * predictCollisions(){
|
||||
static int boundaries[3];
|
||||
if (player.getComponent<TransformComponent>().velocity.x<0){
|
||||
// ====== For Each Row ====
|
||||
int i = 0;
|
||||
for (int r=Game::pTileY-1;r<=Game::pTileY+1;r++){
|
||||
// ====== For Each Tile (Column) =====
|
||||
for (int c=Game::pTileX;c>Game::pTileX-Game::camera.w/gameScene.getComponent<TileMapComponent>().tileWidth;c--){
|
||||
if(gameScene.getComponent<TileMapComponent>().colliders[r][c]>0){
|
||||
boundaries[i] = c*gameScene.getComponent<TileMapComponent>().tileWidth+gameScene.getComponent<TileMapComponent>().tileWidth;
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// ====== TODO: setup player moving in positive X, and Y axes =====
|
||||
|
||||
|
||||
return boundaries;
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "Vector2D.h"
|
||||
#include "../assetmgr/AssetManager.h"
|
||||
#include "GameStateManager.h"
|
||||
#include "tmxparser.h"
|
||||
|
||||
class ColliderComponent;
|
||||
class AssetManager;
|
||||
@ -29,13 +30,17 @@ public:
|
||||
void update();
|
||||
void render();
|
||||
void clean();
|
||||
void printDebug(std::string debugInfo);
|
||||
// void printDebug(std::string debugInfo);
|
||||
static void drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue);
|
||||
static int * predictCollisions();
|
||||
bool running() { return isRunning; }
|
||||
// static void AddTile(int srcX, int srcY, int xpos, int ypos);
|
||||
static SDL_Renderer *renderer;
|
||||
static SDL_Event event;
|
||||
// static std::vector<ColliderComponent*> colliders;
|
||||
// static tmxparser::TmxMap map;
|
||||
// gameScene;
|
||||
|
||||
static bool debugMenu;
|
||||
static bool isRunning;
|
||||
static bool debugCollisionBoxes;
|
||||
@ -48,6 +53,8 @@ public:
|
||||
static SDL_Rect levelMap;
|
||||
static AssetManager* assets;
|
||||
static GameStateManager* gsm;
|
||||
static int pTileX;
|
||||
static int pTileY;
|
||||
std::string BoolToString(bool b);
|
||||
enum groupLabels : std::size_t
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user