Skip to content

Commit

Permalink
Bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
traguill committed Jun 20, 2018
1 parent 11b481a commit e0c88aa
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 35 deletions.
8 changes: 4 additions & 4 deletions BeeT/BeeTDemo/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Block::Block(SDL_Renderer* renderer, float posX, float posY) : Entity(renderer, posX, posY)
{
type = BLOCK;
LoadSprite("Game/block.bmp", 200, 200);
g_Physics->AddBody(this, 100);
LoadSprite("Game/block.bmp", 20, 20);
g_Physics->AddBody(this, 10);

CalculateCorners();
}
Expand Down Expand Up @@ -75,13 +75,13 @@ void Block::CalculateCorners()
6 5 4
*/
float size = 100;
float size = 10;
cornerA = fPoint(pos.x - size, pos.y - size);
cornerB = fPoint(pos.x + size, pos.y - size);
cornerC = fPoint(pos.x - size, pos.y + size);
cornerD = fPoint(pos.x + size, pos.y + size);

float dst = 20;
float dst = 2;
hideSpots.resize(8);
hideSpots[0] = fPoint(cornerA.x - dst, cornerA.y - dst);
hideSpots[1] = fPoint(pos.x, cornerA.y - dst);
Expand Down
2 changes: 1 addition & 1 deletion BeeT/BeeTDemo/Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Bullet::~Bullet()
void Bullet::UpdateLogic(float dt)
{
timer += dt;
if (timer >= 3000.0f)
if (timer >= 3.0f)
g_GameManager->RemoveEntity(this);
}

Expand Down
19 changes: 13 additions & 6 deletions BeeT/BeeTDemo/Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include "Block.h"
#include "Globals.h"

#include <stdlib.h>

Enemy::Enemy(SDL_Renderer* renderer, float posX, float posY) : Entity(renderer, posX, posY)
{
type = ENEMY;
LoadSprite("Game/flower.bmp", 100, 100);
g_Physics->AddBody(this, 50);
LoadSprite("Game/flower.bmp", 10, 10);
g_Physics->AddBody(this, 5);

btId = BEET_LoadBehaviorTreeFromFile("Enemy.json", BEET_TRUE);
btId = BEET_LoadBehaviorTreeFromFile("Enemy.json", BEET_FALSE);
// OnInit
std::function<void(const char*)> btTasksOnInitFunc = std::bind(&Enemy::BTTaskOnInit, this, std::placeholders::_1);
g_GameManager->taskOnInitFunctions.insert(std::pair<int, std::function<void(const char*)>>(btId, btTasksOnInitFunc));
Expand All @@ -25,8 +27,8 @@ Enemy::Enemy(SDL_Renderer* renderer, float posX, float posY) : Entity(renderer,
g_GameManager->taskOnFinishFunctions.insert(std::pair<int, std::function<void(const char*)>>(btId, btTasksOnFinishFunc));

speed = 150.0f;
dir.x = 0.3f;
dir.y = 0.67f; // Set a rnd
dir.x = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
dir.y = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}

Enemy::~Enemy()
Expand Down Expand Up @@ -141,7 +143,7 @@ void Enemy::BTTaskOnFinish(const char * taskId)
void Enemy::Movement()
{

float halfSize = 50.0f;
float halfSize = 5.0f;
if (pos.x - halfSize < 0.0f)
{
pos.x = 0.0f + halfSize;
Expand Down Expand Up @@ -214,6 +216,11 @@ bool Enemy::FollowRoute()
{
if (!hasDestination)
{
if (route.size() == 0)
{
isStop = true;
return true;
}
destination = route[0];
route.erase(route.begin());
dir = destination - pos;
Expand Down
17 changes: 14 additions & 3 deletions BeeT/BeeTDemo/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "Block.h"
#include "SDL/include/SDL.h"


#include <stdlib.h> // TEST RND
#include "Globals.h"
GameManager::GameManager(SDL_Renderer* renderer) : renderer(renderer)
{
}
Expand All @@ -21,8 +24,14 @@ void GameManager::Init()
AddEntity(player);

// Enemies
enemy = new Enemy(renderer, 300, 100);
AddEntity(enemy);
//enemy = new Enemy(renderer, 300, 100);
//AddEntity(enemy);

for (int i = 0; i < 200; i++)
{
Enemy* tst = new Enemy(renderer, rand() % SCREEN_WIDTH + 1, rand() % SCREEN_HEIGHT + 1);
AddEntity(tst);
}

// Block
block = new Block(renderer, 600, 400);
Expand Down Expand Up @@ -63,7 +72,9 @@ void GameManager::AddEntity(Entity * entity)

void GameManager::RemoveEntity(Entity * entity)
{
entitiesToRemove.push_back(entity);
auto found = std::find(entitiesToRemove.begin(), entitiesToRemove.end(), entity);
if(found == entitiesToRemove.end())
entitiesToRemove.push_back(entity);
}

void GameManager::OnInitBTTask(unsigned int btId, const char * taskId)
Expand Down
19 changes: 12 additions & 7 deletions BeeT/BeeTDemo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "Globals.h"

#include <stdlib.h> // TEST RND

Input* g_Input = NULL;
GameManager* g_GameManager = NULL;
Physics * g_Physics = NULL;
Expand All @@ -27,7 +29,7 @@ int main(int argc, char* args[])
{
BEET_Init();
BEET_SetTaskCallbackFunc(TaskCallbackFunc);
BEET_InitDebugger("127.0.0.1", 8080);
//BEET_InitDebugger("127.0.0.1", 8080);
SDL_Init(SDL_INIT_EVERYTHING);

SDL_Window* window;
Expand All @@ -53,22 +55,25 @@ int main(int argc, char* args[])
frequency = SDL_GetPerformanceFrequency();
timeStart = SDL_GetPerformanceCounter();

srand(timeStart);

float time = 0.0f;
// Loop
while (g_Input->Update())
{
// Logic ------------------
float dt = LastFrameSec();
if (dt > 0.1)
dt = 0.00016;
time += dt;
char buffer[256];
size_t siz = sprintf_s(buffer, "Time: %f", dt*1000.0f);
SDL_SetWindowTitle(window, buffer);

//if (dt > 0.5)
//dt = 0.00016;
BEET_Tick(dt);
g_Physics->Tick();
g_GameManager->Tick(dt);

time += dt;
char buffer[256];
size_t siz = sprintf_s(buffer, "Time: %.2f", time);
SDL_SetWindowTitle(window, buffer);
// Graphics ---------------

SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
Expand Down
3 changes: 2 additions & 1 deletion BeeT/BeeTLib/BeeT_behaviortree.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
typedef struct BeeT_BehaviorTree BeeT_BehaviorTree;
struct BeeT_BehaviorTree
{
unsigned int uid;
unsigned int instanceUID; // Unique BT id. Same copies of this BT have the uid shared but not the instanceUID
unsigned int uid;
BeeT_Node* rootNode;
BeeT_Blackboard* bb;
dequeue* runningNodes;
Expand Down
6 changes: 3 additions & 3 deletions BeeT/BeeTLib/BeeT_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void BTN_Task_OnInit(BeeT_Node* self)
BTN_Task* btn = (BTN_Task*)self;
BeetContext* context = BeeT_GetContext();
BEET_ASSERT(context != NULL);
context->taskCallbackFunc(btn->node.bt->uid, btn->name, NF_ONINIT);
context->taskCallbackFunc(btn->node.bt->instanceUID, btn->name, NF_ONINIT);
}

void BTN_Wait_OnInit(BeeT_Node * self)
Expand Down Expand Up @@ -390,7 +390,7 @@ NodeStatus BTN_Task_Update(BeeT_Node* self)
BTN_Task* btn = (BTN_Task*)self;
BeetContext* context = BeeT_GetContext();
BEET_ASSERT(context != NULL);
return context->taskCallbackFunc(btn->node.bt->uid, btn->name, NF_UPDATE);
return context->taskCallbackFunc(btn->node.bt->instanceUID, btn->name, NF_UPDATE);
}

NodeStatus BTN_Wait_Update(BeeT_Node * self)
Expand Down Expand Up @@ -450,7 +450,7 @@ void BTN_Task_OnFinish(BeeT_Node* self, NodeStatus status)
BTN_Task* btn = (BTN_Task*)self;
BeetContext* context = BeeT_GetContext();
BEET_ASSERT(context != NULL);
context->taskCallbackFunc(btn->node.bt->uid, btn->name, NF_ONFINISH);
context->taskCallbackFunc(btn->node.bt->instanceUID, btn->name, NF_ONFINISH);
}

void BTN_Wait_OnFinish(BeeT_Node * self, NodeStatus status)
Expand Down
16 changes: 6 additions & 10 deletions BeeT/BeeTLib/beet.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,17 @@ unsigned int BeetContext__AddTree(BeetContext* ctx, BeeT_BehaviorTree* bt)
if (ctx->numTreesLoaded == ctx->maxNumTreesLoaded)
{
ctx->maxNumTreesLoaded += 32;
*ctx->trees = (BeeT_BehaviorTree*)BEET_realloc(ctx->trees, sizeof(BeeT_BehaviorTree*) * ctx->maxNumTreesLoaded);
ctx->trees = (BeeT_BehaviorTree*)BEET_realloc(ctx->trees, sizeof(BeeT_BehaviorTree*) * ctx->maxNumTreesLoaded);
}

ctx->trees[ctx->numTreesLoaded++] = bt;
return ctx->numTreesLoaded - 1;
bt->instanceUID = ctx->numTreesLoaded - 1;
return bt->instanceUID;
}

BeeT_BehaviorTree* BeeTContext__GetTree(BeetContext* ctx, unsigned int btId)
{
for (int i = 0; i < ctx->numTreesLoaded; ++i)
{
if (ctx->trees[i]->uid == btId)
return ctx->trees[i];
}
return NULL;
return ((int)btId < ctx->numTreesLoaded) ? ctx->trees[btId] : NULL;
}

//-----------------------------------------------------------------
Expand Down Expand Up @@ -149,12 +145,12 @@ unsigned int BEET_LoadBehaviorTree(const char * buffer, int size, BEET_bool debu
if (bt == NULL)
return 0;

BeetContext__AddTree(g_Beet, bt);
unsigned int uid = BeetContext__AddTree(g_Beet, bt);
if(debug)
bt->debug = BeeT_Debugger_LoadBT(g_Debug, buffer, size);

BeeT_Serializer_Destroy(parser);
return bt->uid;
return uid;
}

unsigned int BEET_LoadBehaviorTreeFromFile(const char * filename, BEET_bool debug)
Expand Down

0 comments on commit e0c88aa

Please sign in to comment.