even better button animation
This commit is contained in:
+11
-15
@@ -1,10 +1,11 @@
|
||||
#include "button.h"
|
||||
#include "cube.h"
|
||||
#include "raylib.h"
|
||||
#include "util.h"
|
||||
|
||||
void DrawButton(Button *button, Cube *cube)
|
||||
{
|
||||
bool pressed = !cube->grabbed && CheckCollisionBoxes(
|
||||
bool pressed = CheckCollisionBoxes(
|
||||
(BoundingBox){
|
||||
(Vector3){
|
||||
button->position.x - button->length / 2.0f,
|
||||
@@ -13,14 +14,14 @@ void DrawButton(Button *button, Cube *cube)
|
||||
},
|
||||
(Vector3){
|
||||
button->position.x + button->length / 2.0f,
|
||||
button->position.y + button->height / 2.0f,
|
||||
button->position.y + button->height / 2.0f + 0.02f,
|
||||
button->position.z + button->length / 2.0f,
|
||||
},
|
||||
},
|
||||
(BoundingBox){
|
||||
(Vector3){
|
||||
cube->position.x - cube->edge / 2.0f,
|
||||
cube->position.y - cube->edge / 2.0f,
|
||||
cube->position.y - cube->edge / 2.0f - 0.02f,
|
||||
cube->position.z - cube->edge / 2.0f,
|
||||
},
|
||||
(Vector3){
|
||||
@@ -32,24 +33,19 @@ void DrawButton(Button *button, Cube *cube)
|
||||
|
||||
if (pressed)
|
||||
{
|
||||
button->height = cube->position.y - cube->edge / 2.0f;
|
||||
if (button->height <= 0.0f)
|
||||
{
|
||||
button->height = 0.01f;
|
||||
}
|
||||
button->color = RED;
|
||||
button->height = MIN(cube->position.y - cube->edge / 2.0f + 0.01f, 0.1f);
|
||||
button->color = GREEN;
|
||||
button->pressed = true;
|
||||
cube->minY = cube->edge / 2.0f + 0.02f;
|
||||
}
|
||||
else
|
||||
{
|
||||
button->height += 0.01f;
|
||||
if (button->height > 0.1f)
|
||||
{
|
||||
button->height = 0.1f;
|
||||
}
|
||||
button->color = BLUE;
|
||||
button->height = MIN(0.1f, button->height + 0.01f);
|
||||
button->color = PINK;
|
||||
button->pressed = false;
|
||||
cube->minY = cube->edge / 2.0f + 0.01f;
|
||||
}
|
||||
|
||||
DrawCube(button->position, button->length, button->height, button->length, button->color);
|
||||
DrawCubeWires(button->position, button->length, button->height, button->length, button->wiresColor);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ typedef struct
|
||||
float length;
|
||||
float height;
|
||||
Color color;
|
||||
Color wiresColor;
|
||||
bool pressed;
|
||||
} Button;
|
||||
|
||||
|
||||
+4
-3
@@ -1,16 +1,17 @@
|
||||
#include "cube.h"
|
||||
#include "util.h"
|
||||
|
||||
void MyDrawCube(Cube *cube)
|
||||
{
|
||||
// Gravity
|
||||
if (!cube->grabbed && cube->position.y > cube->edge / 2.0f)
|
||||
if (!cube->grabbed && cube->position.y > cube->edge / 2.0f + 0.02f)
|
||||
{
|
||||
float delta = GetFrameTime();
|
||||
cube->yvel -= delta * 32.0f;
|
||||
cube->position.y += cube->yvel * delta;
|
||||
if (cube->position.y <= cube->edge / 2.0f)
|
||||
if (cube->position.y <= cube->minY)
|
||||
{
|
||||
cube->position.y = cube->edge / 2.0f;
|
||||
cube->position.y = cube->minY;
|
||||
cube->yvel = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ typedef struct
|
||||
bool initialized;
|
||||
Model model;
|
||||
Vector3 position;
|
||||
float minY;
|
||||
float edge;
|
||||
Color color;
|
||||
Color wiresColor;
|
||||
|
||||
+3
-1
@@ -10,13 +10,15 @@ Button button = {
|
||||
.position = (Vector3){0.0f, 0.0f, -5.0f},
|
||||
.length = 1.0f,
|
||||
.height = 0.1f,
|
||||
.color = BLUE,
|
||||
.color = PINK,
|
||||
.wiresColor = BLACK,
|
||||
.pressed = false,
|
||||
};
|
||||
|
||||
Cube cube = {
|
||||
.initialized = false,
|
||||
.position = (Vector3){0.0f, 0.5f, 0.0f},
|
||||
.minY = 0.4f + 0.01f,
|
||||
.edge = 0.8f,
|
||||
.color = RED,
|
||||
.wiresColor = BLACK,
|
||||
|
||||
+2
-2
@@ -47,9 +47,9 @@ void UpdateFpsCamera(Player *player)
|
||||
player->grab->position = cubePosition;
|
||||
|
||||
// Don't let putting grab under the floor
|
||||
if (player->grab->position.y <= player->grab->edge / 2.0f)
|
||||
if (player->grab->position.y <= player->grab->minY)
|
||||
{
|
||||
player->grab->position.y = player->grab->edge / 2.0f;
|
||||
player->grab->position.y = player->grab->minY;
|
||||
}
|
||||
player->grab->hRotation = player->lookRotation.x * RAD2DEG; // Rottate cube to be perpendicular to camera
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#define MAX(a, b) (a) > (b) ? (a) : (b)
|
||||
#define MIN(a, b) (a) > (b) ? (b) : (a)
|
||||
|
||||
#endif // UTIL_H
|
||||
Reference in New Issue
Block a user