From ef2f6d004c810090e46ecb834587993566c1929a Mon Sep 17 00:00:00 2001 From: speckitor Date: Tue, 7 Jul 2026 18:18:43 +0700 Subject: [PATCH] wires --- src/button.c | 7 +++--- src/button.h | 3 ++- src/level00.c | 65 +++++++++++++++++++++++++++++++++++---------------- src/wire.c | 36 ++++++++++++++++++++++++++++ src/wire.h | 32 +++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 24 deletions(-) create mode 100644 src/wire.c create mode 100644 src/wire.h diff --git a/src/button.c b/src/button.c index e65ef8e..9576256 100644 --- a/src/button.c +++ b/src/button.c @@ -31,21 +31,22 @@ void DrawButton(Button *button, Cube *cube) }, }); + Color color; if (pressed) { button->height = MIN(cube->position.y - cube->edge / 2.0f + 0.01f, 0.1f); - button->color = GREEN; + color = button->colorEnabled; button->pressed = true; cube->minY = cube->edge / 2.0f + 0.02f; } else { button->height = MIN(0.1f, button->height + 0.01f); - button->color = PINK; + color = button->colorDisabled; 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, color); DrawCubeWires(button->position, button->length, button->height, button->length, button->wiresColor); } diff --git a/src/button.h b/src/button.h index 7c070d6..15f956b 100644 --- a/src/button.h +++ b/src/button.h @@ -8,7 +8,8 @@ typedef struct Vector3 position; float length; float height; - Color color; + Color colorEnabled; + Color colorDisabled; Color wiresColor; bool pressed; } Button; diff --git a/src/level00.c b/src/level00.c index fed2c6a..b4d8382 100644 --- a/src/level00.c +++ b/src/level00.c @@ -3,34 +3,58 @@ #include "player.h" #include "raylib.h" #include "screen.h" +#include "wire.h" #include #include -Button button = { - .position = (Vector3){0.0f, 0.0f, -5.0f}, - .length = 1.0f, - .height = 0.1f, - .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, - .grabbed = false, -}; +static bool level_started = false; void DrawLevel00(Player *player) { - if (!cube.initialized) + static Button button = { + .position = (Vector3){0.0f, 0.0f, -5.0f}, + .length = 1.0f, + .height = 0.1f, + .colorEnabled = GREEN, + .colorDisabled = RED, + .wiresColor = BLACK, + .pressed = false, + }; + + static Wire wire = { + .power = &button, + .colorEnabled = GREEN, + .colorDisabled = BLACK, + }; + + static Cube cube = { + .position = (Vector3){0.0f, 0.5f, 0.0f}, + .minY = 0.4f + 0.01f, + .edge = 0.8f, + .color = RED, + .wiresColor = BLACK, + .grabbed = false, + }; + + if (!level_started) { + level_started = true; + cube.model = LoadModelFromMesh(GenMeshCube(cube.edge, cube.edge, cube.edge)); - cube.initialized = true; + + float wireW = 0.1f; + float wireH = 0.1f; + float wireL = 5.0f; + float wireZ = button.position.z - wireL / 2.0f - button.length / 2.0f; + Vector3 wirePos = {0.0f, wireH / 2.0f, wireZ}; + WireAppend(&wire, wirePos, wireW, wireH, wireL); + + wireZ -= wireL / 2.0f; + wireW = 5.0f; + wireH = 0.1f; + wireL = 0.1f; + wirePos = (Vector3){wireW / 2.0f, wireH / 2.0f, wireZ}; + WireAppend(&wire, wirePos, wireW, wireH, wireL); } const int floorExtent = 25; @@ -56,4 +80,5 @@ void DrawLevel00(Player *player) MyDrawCube(&cube); // Draw a cube DrawButton(&button, &cube); // Draw a button + DrawWire(&wire); // Draw a wire } diff --git a/src/wire.c b/src/wire.c new file mode 100644 index 0000000..1b8f12e --- /dev/null +++ b/src/wire.c @@ -0,0 +1,36 @@ +#include "wire.h" +#include "raylib.h" +#include + +void WireAppend(Wire *wire, Vector3 position, float width, float height, float length) +{ + if (!wire->nodes) + { + wire->nodes_capasity = DEFAULT_WIRES_CAPASITY; + wire->nodes = calloc(wire->nodes_capasity, sizeof(WireNode)); + } + + if (wire->nodes_count == wire->nodes_capasity) + { + wire->nodes_capasity *= 1.5; + wire->nodes = realloc(wire->nodes, sizeof(WireNode) * wire->nodes_capasity); + } + + WireNode *node = &wire->nodes[wire->nodes_count++]; + node->position = position; + node->width = width; + node->height = height; + node->length = length; +} + +void DrawWire(Wire *wire) +{ + Color color = wire->power->pressed ? wire->colorEnabled : wire->colorDisabled; + + WireNode *node; + for (int i = 0; i < wire->nodes_count; ++i) + { + node = &wire->nodes[i]; + DrawCube(node->position, node->width, node->height, node->length, color); + } +} diff --git a/src/wire.h b/src/wire.h new file mode 100644 index 0000000..ffe02d4 --- /dev/null +++ b/src/wire.h @@ -0,0 +1,32 @@ +#ifndef WIRE_H +#define WIRE_H + +#include "button.h" +#include "raylib.h" +#include + +#define DEFAULT_WIRES_CAPASITY 4 + +typedef struct +{ + Vector3 position; + float width; + float height; + float length; +} WireNode; + +typedef struct +{ + Button *power; + WireNode *nodes; + size_t nodes_count; + size_t nodes_capasity; + Color colorEnabled; + Color colorDisabled; +} Wire; + +void WireAppend(Wire *wire, Vector3 position, float width, float height, float length); + +void DrawWire(Wire *wire); + +#endif // WIRE_H