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