Versuch einer Kollisionsabfrage der Blöcke
This commit is contained in:
1 parent
32be3483ad
commit
ece1ca8bba
1 file changed
+59
-9
@@ -1,11 +1,11 @@
|
|||||||
#include "include/raylib.h"
|
#include "include/raylib.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
||||||
typedef struct Block {
|
typedef struct Block {
|
||||||
Vector2 position;
|
Vector2 position, size;
|
||||||
int form, onGround;
|
int form, onGround;
|
||||||
float speed;
|
float speed, step;
|
||||||
|
|
||||||
struct Block *next;
|
struct Block *next;
|
||||||
} Block;
|
} Block;
|
||||||
@@ -14,9 +14,11 @@ typedef struct Block {
|
|||||||
void createBlock(Block **head) {
|
void createBlock(Block **head) {
|
||||||
Block *block = MemAlloc(sizeof(Block));
|
Block *block = MemAlloc(sizeof(Block));
|
||||||
block->position = (Vector2){ 400, -40 };
|
block->position = (Vector2){ 400, -40 };
|
||||||
|
block->size = (Vector2){ 20, 20 };
|
||||||
block->form = 1;
|
block->form = 1;
|
||||||
block->onGround = 0;
|
block->onGround = 0;
|
||||||
block->speed = 2;
|
block->speed = 4;
|
||||||
|
block->step = 20;
|
||||||
block->next = NULL;
|
block->next = NULL;
|
||||||
|
|
||||||
// Falls die Liste leer ist
|
// Falls die Liste leer ist
|
||||||
@@ -50,22 +52,59 @@ int main(void) {
|
|||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
||||||
// UPDATE GAME LOGIC
|
// UPDATE GAME LOGIC
|
||||||
|
// INPUT
|
||||||
Block *currentBlock = block;
|
Block *currentBlock = block;
|
||||||
// Alle Blöcke fallen lassen
|
// Alle Blöcke fallen lassen
|
||||||
while (currentBlock != NULL) {
|
while (currentBlock != NULL) {
|
||||||
// Wenn Block noch nicht auf Boden, dann fallen
|
// Wenn Block noch nicht auf Boden, dann fallen
|
||||||
if (currentBlock->onGround == 0) {
|
if (currentBlock->onGround == 0) {
|
||||||
|
|
||||||
|
|
||||||
if (currentBlock->position.y < screenHeight - 20) {
|
// MOVEMENT -------------------------------------------------
|
||||||
currentBlock->position.y += currentBlock->speed;
|
if (IsKeyPressed(KEY_LEFT)) {
|
||||||
} else {
|
currentBlock->position.x -= currentBlock->step;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsKeyPressed(KEY_RIGHT)) {
|
||||||
|
currentBlock->position.x += currentBlock->step;
|
||||||
|
}
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
// Kollision gegen anderen Block checken
|
||||||
|
|
||||||
|
float nextY = currentBlock->position.y + currentBlock->speed;
|
||||||
|
|
||||||
|
if (nextY > screenHeight - currentBlock->size.y) {
|
||||||
|
currentBlock->position.y = screenHeight - currentBlock->size.y;
|
||||||
blockMoving = 0;
|
blockMoving = 0;
|
||||||
currentBlock->onGround = 1;
|
currentBlock->onGround = 1;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
Rectangle nextRect = { currentBlock->position.x, nextY, currentBlock->size.x, currentBlock->size.y };
|
||||||
|
|
||||||
|
int collided = 0;
|
||||||
|
Block *collisionCheck = block;
|
||||||
|
while (collisionCheck != NULL) {
|
||||||
|
// Nur gegen andere, bereits gelandete Blöcke prüfen (und nicht gegen sich selbst!)
|
||||||
|
if (collisionCheck != currentBlock && collisionCheck->onGround == 1) {
|
||||||
|
Rectangle otherRect = { collisionCheck->position.x, collisionCheck->position.y, collisionCheck->size.x, collisionCheck->size.y };
|
||||||
|
|
||||||
|
if (CheckCollisionRecs(nextRect, otherRect)) {
|
||||||
|
collided = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
collisionCheck = collisionCheck->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collided) {
|
||||||
|
blockMoving = 0;
|
||||||
|
currentBlock->onGround = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
currentBlock->position.y = nextY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +118,7 @@ int main(void) {
|
|||||||
blockMoving = 1;
|
blockMoving = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
||||||
// ZEICHNEN
|
// ZEICHNEN
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
@@ -86,16 +126,26 @@ int main(void) {
|
|||||||
DrawFPS(20, 20);
|
DrawFPS(20, 20);
|
||||||
DrawText(TextFormat("Blocks: %i", blockCounter), 20, 40, 20, GREEN);
|
DrawText(TextFormat("Blocks: %i", blockCounter), 20, 40, 20, GREEN);
|
||||||
|
|
||||||
|
// BLOCK ZEICHNEN
|
||||||
currentBlock = block;
|
currentBlock = block;
|
||||||
while (currentBlock != NULL) {
|
while (currentBlock != NULL) {
|
||||||
DrawRectangleV(currentBlock->position, (Vector2){20, 20}, BLUE);
|
DrawRectangleV(currentBlock->position, currentBlock->size, BLUE);
|
||||||
currentBlock = currentBlock->next;
|
currentBlock = currentBlock->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Alle Blöcke im Speicher freigeben
|
||||||
|
Block *currentBlock = block;
|
||||||
|
while (currentBlock != NULL) {
|
||||||
|
Block *nextBlock = currentBlock->next;
|
||||||
|
MemFree(currentBlock);
|
||||||
|
currentBlock = nextBlock;
|
||||||
|
}
|
||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user