/
egevtech
/
blocker
Обзор
Документация
Войти
/
egevtech
/
blocker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
blocker.c
310 строк
6 KB
Павел
v0.0.2
17 май 2025, 19:24
17 май 2025, 19:24
bf1862c
Код
Авторство
О чём код?
#include <raylib.h> #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include "include/vec.h" #include "include/blocks.h" uint32_t tmr; Block air; Block grass; Block sand; Block ice; Block snow; Block granite; Block all_blocks[6]; int current_block_id; #define blocks_num (sizeof(all_blocks)/sizeof(Block)) Vec2 grid_pos; Vec2 cell_size; Vec2 cell_min_size; Vec2 cell_max_size; #define grid_size_x 10 #define get_grid_size grid_size*grid_size Block* getBlock(int); void drawGrid(void); void updateMouse(void); void drawOverlay(void); Vec2 mousePos; Block blocks[grid_size_x][grid_size_x]; int mouseOnGrid(void); int mouseOnCell(Vec2 pos); BAC bac; int main(void) { SetConfigFlags(FLAG_WINDOW_RESIZABLE); InitWindow(800, 600, "Blocker"); SetTargetFPS(60); bac.grid_size = grid_size_x; bac.blocks = (Block**) malloc(grid_size_x*sizeof(Block*)); for (int i = 0; i < grid_size_x; i++) bac.blocks[i] = (Block*) malloc(grid_size_x*sizeof(Block)); bac.null = &air; air = (Block) { 0, "Nothing", "Do you need more? No", (Color){0, 0, 0, 255} }; grass = (Block) { 1, "Grass", "Sounds like lie", (Color){0, 255, 0, 255} }; sand = (Block) { 2, "Sand", "It must fall..?", (Color){255, 255, 0, 255}}; ice = (Block) { 3, "Ice", "Don't lick it!", (Color){140, 200, 255, 255}}; snow = (Block) { 4, "Snow", "Snowmen love it", (Color){255, 255, 255, 255}}; granite = (Block) { 5, "Granite", "Very smart", (Color){255, 140, 140, 255}}; tmr = 0; all_blocks[0] = air; all_blocks[1] = grass; all_blocks[2] = sand; all_blocks[3] = ice; all_blocks[4] = snow; all_blocks[5] = granite; initField( bac ); put( bac, sand, (Vec2){1, 1}); current_block_id = 1; // Player can't take air as a block grid_pos = (Vec2){10, 10}; cell_size = (Vec2){30, 30}; cell_min_size = (Vec2){20, 20}; cell_max_size = (Vec2){100, 100}; mousePos = getMousePos(); while (!WindowShouldClose()) { BeginDrawing(); ClearBackground(BLACK); updateMouse(); drawGrid(); drawOverlay(); EndDrawing(); } } void updateMouse(void) { Vec2 cell_under_mouse = (Vec2){0, 0}; if (mouseOnGrid() == 1) cell_under_mouse = (Vec2) { (mousePos.x - grid_pos.x) / cell_size.x, (mousePos.y - grid_pos.y) / cell_size.y }; else cell_under_mouse = (Vec2){-1, -1}; Vec2 last_mouse_pos = mousePos; mousePos = getMousePos(); if ((last_mouse_pos.x != mousePos.x || last_mouse_pos.y != mousePos.y) && IsMouseButtonDown(MOUSE_BUTTON_MIDDLE)) { grid_pos.x -= last_mouse_pos.x - mousePos.x; grid_pos.y -= last_mouse_pos.y - mousePos.y; } float k = 5.00f; float wheel_move = GetMouseWheelMove(); if (wheel_move != 0) { if (IsKeyDown(341)) { // 341 - Left control current_block_id -= wheel_move; if ( current_block_id > blocks_num - 1) current_block_id = 1; if ( current_block_id < 1 ) current_block_id = blocks_num - 1; } else { cell_size.x += wheel_move * k; cell_size.y += wheel_move * k; grid_pos.x -= wheel_move * 20; grid_pos.y -= wheel_move * 20; } // grid_pos.x -= wheel_move * (k / 2); // grid_pos.y -= wheel_move * (k / 2); } if ( cell_size.x > cell_max_size.x ) cell_size.x = cell_max_size.x; if ( cell_size.x < cell_min_size.x ) cell_size.x = cell_min_size.x; if ( cell_size.y > cell_max_size.y ) cell_size.y = cell_max_size.y; if ( cell_size.y < cell_min_size.y ) cell_size.y = cell_min_size.y; if (( cell_size.x > cell_max_size.x || cell_size.x < cell_min_size.x || cell_size.y > cell_max_size.y || cell_size.y < cell_min_size.y)) { grid_pos.x -= wheel_move * (k/2) * 7.0f; grid_pos.y -= wheel_move * (k/2) * 7.0f; } if ( IsMouseButtonDown(MOUSE_BUTTON_RIGHT) ) { if (mouseOnCell(cell_under_mouse) ) { put(bac, *getBlock(current_block_id), cell_under_mouse); } } if ( IsMouseButtonDown(MOUSE_BUTTON_LEFT) ) { if ( mouseOnCell(cell_under_mouse) ) //put(air, cell_under_mouse); destroy(bac, cell_under_mouse); } if ( IsKeyDown( KEY_C ) ) { clearGrid(bac); } if ( IsKeyDown( KEY_S ) ) // save(); {} } int mouseOnCell(Vec2 cell_under_mouse) { return ( cell_under_mouse.x != -1 && cell_under_mouse.y != -1); } void drawGrid(void) { Vec2 cell_num = {10, 10}; for ( int x = 0; x < cell_num.x; x++ ) { for ( int y = 0; y < cell_num.y; y++ ) { Vec2 cell_pos = { grid_pos.x + x * cell_size.x, grid_pos.y + y * cell_size.y}; DrawRectangle(cell_pos.x, cell_pos.y, cell_size.x, cell_size.y, bac.blocks[x][y].hint_color); DrawRectangleLines(cell_pos.x, cell_pos.y, cell_size.x, cell_size.y, WHITE); if ( mousePos.x > cell_pos.x && mousePos.x < cell_pos.x + cell_size.x + 1 && mousePos.y > cell_pos.y && mousePos.y < cell_pos.y + cell_size.y + 1) DrawRectangle( cell_pos.x, cell_pos.y, cell_size.x, cell_size.y, (Color) {170, 140, 140, 140}); } } if (IsKeyDown(341)) { DrawRectangle(10, 10, 500, 120, BLACK); DrawRectangleLines(10, 10, 500, 120, WHITE); Block* current = getBlock(current_block_id); if (current == NULL) ClearBackground(RED); DrawRectangle(20, 20, 100, 100, current->hint_color); DrawText(current->title, 130, 20, 20, WHITE); DrawText(current->subtitle, 130, 40, 20, GRAY); } } Block *getBlock(int id) { if (id < blocks_num) return &all_blocks[id]; else return NULL; } void drawOverlay(void) { Vec2 pos; pos.x = 10; pos.y = GetScreenHeight() - 10 - 20; DrawFPS(pos.x, pos.y); } Vec2 getMousePos(void) { return (Vec2) { GetMouseX(), GetMouseY() }; } int mouseOnGrid(void) { return ( mousePos.x > grid_pos.x && mousePos.y > grid_pos.y && mousePos.x < grid_pos.x + grid_size_x * cell_size.x && mousePos.y < grid_pos.y + grid_size_x * cell_size.y ); }