Collision tile layer and checking completed

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 6a357cb58d
commit 6a9726953e
  1. BIN
      C++ProjectTemplate
  2. 75
      main.cpp

Binary file not shown.

@ -79,7 +79,7 @@ class Object{
this->scale=scale; this->scale=scale;
this->color=color; this->color=color;
this->animationSpd=animationSpd; this->animationSpd=animationSpd;
this->originPoint={spr->spr->sprite->width/2,spr->spr->sprite->height-4}; this->originPoint={spr->width/2,spr->spr->sprite->height-4};
} }
}; };
@ -152,7 +152,11 @@ public:
SetDrawTarget(nullptr); SetDrawTarget(nullptr);
Clear(BLANK); Clear(BLANK);
SetDrawTarget(layer::COLLISION); SetDrawTarget(layer::COLLISION);
Clear(BLANK); if (EDITING_LAYER!=layer::COLLISION) {
Clear(MAGENTA);
} else {
Clear(BLANK);
}
SetDrawTarget(layer::HIGH); SetDrawTarget(layer::HIGH);
Clear(BLANK); Clear(BLANK);
SetDrawTarget(layer::DYNAMIC); SetDrawTarget(layer::DYNAMIC);
@ -173,9 +177,8 @@ public:
obj->frameIndex++; obj->frameIndex++;
} }
} }
SetDrawTarget(layer::COLLISION);
Pixel collisionData = GetDrawTarget()->GetPixel((int)OBJECTS[0]->pos.x-cameraPos.x,(int)OBJECTS[0]->pos.y-cameraPos.y);
std::cout << Collision(GetMousePos());
switch (GAME_STATE) { switch (GAME_STATE) {
case state::TILE_SELECT:{ case state::TILE_SELECT:{
@ -194,29 +197,25 @@ public:
if (TabHeld()) { if (TabHeld()) {
GAME_STATE=state::TILE_SELECT; GAME_STATE=state::TILE_SELECT;
} }
if (UpHeld()) {
cameraPos.y-=CAMERA_MOVESPD;
}
if (RightHeld()) {
cameraPos.x+=CAMERA_MOVESPD;
}
if (LeftHeld()) {
cameraPos.x-=CAMERA_MOVESPD;
}
if (DownHeld()) {
cameraPos.y+=CAMERA_MOVESPD;
}
if (GetKey(I).bHeld) { if (GetKey(I).bHeld) {
OBJECTS[0]->pos.y-=1; if (!Collision({OBJECTS[0]->pos.x+OBJECTS[0]->originPoint.x,OBJECTS[0]->pos.y-1+OBJECTS[0]->originPoint.y})) {
OBJECTS[0]->pos.y-=1;
}
} }
if (GetKey(K).bHeld) { if (GetKey(K).bHeld) {
OBJECTS[0]->pos.y+=1; if (!Collision({OBJECTS[0]->pos.x+OBJECTS[0]->originPoint.x,OBJECTS[0]->pos.y+1+OBJECTS[0]->originPoint.y})) {
OBJECTS[0]->pos.y+=1;
}
} }
if (GetKey(J).bHeld) { if (GetKey(J).bHeld) {
OBJECTS[0]->pos.x-=1; if (!Collision({OBJECTS[0]->pos.x-1+OBJECTS[0]->originPoint.x,OBJECTS[0]->pos.y+OBJECTS[0]->originPoint.y})) {
OBJECTS[0]->pos.x-=1;
}
} }
if (GetKey(L).bHeld) { if (GetKey(L).bHeld) {
OBJECTS[0]->pos.x+=1; if (!Collision({OBJECTS[0]->pos.x+1+OBJECTS[0]->originPoint.x,OBJECTS[0]->pos.y+OBJECTS[0]->originPoint.y})) {
OBJECTS[0]->pos.x+=1;
}
} }
int selectedTileX=(GetMouseX()+cameraPos.x)/32; int selectedTileX=(GetMouseX()+cameraPos.x)/32;
int selectedTileY=(GetMouseY()+cameraPos.y)/32; int selectedTileY=(GetMouseY()+cameraPos.y)/32;
@ -276,11 +275,42 @@ public:
}break; }break;
} }
} }
}break; }break;
} }
//CAMERA UPDATES MUST BE LAST!!! COLLISIONS RELY ON THE GAME POSITION STATES REMAINING THE SAME!
cameraUpdate();
}
//CAMERA UPDATES MUST BE LAST!!! COLLISIONS RELY ON THE GAME POSITION STATES REMAINING THE SAME!
void cameraUpdate() {
switch (GAME_STATE) {
case state::EDITOR:{
//CAMERA MOVEMENTS MUST BE LAST!!!
if (UpHeld()) {
cameraPos.y-=CAMERA_MOVESPD;
}
if (RightHeld()) {
cameraPos.x+=CAMERA_MOVESPD;
}
if (LeftHeld()) {
cameraPos.x-=CAMERA_MOVESPD;
}
if (DownHeld()) {
cameraPos.y+=CAMERA_MOVESPD;
}
}break;
}
} }
void keyUpdates() { void keyUpdates() {
if (GetKey(F1).bPressed) {
ConsoleShow(F1,false);
}
switch (GAME_STATE) { switch (GAME_STATE) {
case state::TILE_SELECT:{ case state::TILE_SELECT:{
if (UpPressed()) { if (UpPressed()) {
@ -623,6 +653,11 @@ public:
OBJ_INFO[PLAYER]=new Object("player",{0,0},ANIMATIONS[PLAYER]); OBJ_INFO[PLAYER]=new Object("player",{0,0},ANIMATIONS[PLAYER]);
} }
bool Collision(vd2d pos) {
SetDrawTarget(layer::COLLISION);
Pixel collisionData = GetDrawTarget()->GetPixel((int)pos.x-cameraPos.x,(int)pos.y-cameraPos.y);
return collisionData!=MAGENTA;
}
bool TabHeld(){ bool TabHeld(){

Loading…
Cancel
Save