2019-05-03 17:00:16 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [shapes] example - collision area
|
|
|
|
*
|
|
|
|
* This example has been created using raylib 2.5 (www.raylib.com)
|
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2019 Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
#include <stdlib.h> // Required for abs()
|
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
int main(void)
|
2019-05-03 17:00:16 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//---------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2019-05-03 17:00:16 +03:00
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area");
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Box A: Moving box
|
|
|
|
Rectangle boxA = { 10, GetScreenHeight()/2 - 50, 200, 100 };
|
2019-05-20 17:36:42 +03:00
|
|
|
int boxASpeedX = 4;
|
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Box B: Mouse moved box
|
|
|
|
Rectangle boxB = { GetScreenWidth()/2 - 30, GetScreenHeight()/2 - 30, 60, 60 };
|
2019-05-20 17:36:42 +03:00
|
|
|
|
|
|
|
Rectangle boxCollision = { 0 }; // Collision rectangle
|
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
int screenUpperLimit = 40; // Top menu limits
|
2019-05-20 17:36:42 +03:00
|
|
|
|
|
|
|
bool pause = false; // Movement pause
|
|
|
|
bool collision = false; // Collision detection
|
|
|
|
|
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2019-05-03 17:00:16 +03:00
|
|
|
//----------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// Move box if not paused
|
2019-05-20 17:36:42 +03:00
|
|
|
if (!pause) boxA.x += boxASpeedX;
|
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Bounce box on x screen limits
|
2019-05-20 17:36:42 +03:00
|
|
|
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0)) boxASpeedX *= -1;
|
|
|
|
|
|
|
|
// Update player-controlled-box (box02)
|
|
|
|
boxB.x = GetMouseX() - boxB.width/2;
|
|
|
|
boxB.y = GetMouseY() - boxB.height/2;
|
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Make sure Box B does not go out of move area limits
|
2019-05-20 17:36:42 +03:00
|
|
|
if ((boxB.x + boxB.width) >= GetScreenWidth()) boxB.x = GetScreenWidth() - boxB.width;
|
|
|
|
else if (boxB.x <= 0) boxB.x = 0;
|
|
|
|
|
|
|
|
if ((boxB.y + boxB.height) >= GetScreenHeight()) boxB.y = GetScreenHeight() - boxB.height;
|
|
|
|
else if (boxB.y <= screenUpperLimit) boxB.y = screenUpperLimit;
|
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Check boxes collision
|
|
|
|
collision = CheckCollisionRecs(boxA, boxB);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
|
|
|
// Get collision rectangle (only on collision)
|
|
|
|
if (collision) boxCollision = GetCollisionRec(boxA, boxB);
|
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Pause Box A movement
|
2019-05-20 17:36:42 +03:00
|
|
|
if (IsKeyPressed(KEY_SPACE)) pause = !pause;
|
2019-05-03 17:00:16 +03:00
|
|
|
//-----------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Draw
|
|
|
|
//-----------------------------------------------------
|
|
|
|
BeginDrawing();
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
ClearBackground(RAYWHITE);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
DrawRectangle(0, 0, screenWidth, screenUpperLimit, collision? RED : BLACK);
|
|
|
|
|
|
|
|
DrawRectangleRec(boxA, GOLD);
|
|
|
|
DrawRectangleRec(boxB, BLUE);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
if (collision)
|
|
|
|
{
|
|
|
|
// Draw collision area
|
|
|
|
DrawRectangleRec(boxCollision, LIME);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Draw collision message
|
|
|
|
DrawText("COLLISION!", GetScreenWidth()/2 - MeasureText("COLLISION!", 20)/2, screenUpperLimit/2 - 10, 20, BLACK);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
// Draw collision area
|
|
|
|
DrawText(FormatText("Collision Area: %i", (int)boxCollision.width*(int)boxCollision.height), GetScreenWidth()/2 - 100, screenUpperLimit + 10, 20, BLACK);
|
2019-05-20 17:36:42 +03:00
|
|
|
}
|
2019-05-03 17:00:16 +03:00
|
|
|
|
|
|
|
DrawFPS(10, 10);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
EndDrawing();
|
|
|
|
//-----------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//---------------------------------------------------------
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//----------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-03 17:00:16 +03:00
|
|
|
return 0;
|
|
|
|
}
|