#include "inputHandler.h" #include "raylib.h" #include "../sprite.h" #include "../IsometricMap/isometricMap.h" #include #include #include #include "../List/list.h" #include "../IsometricMap/tile.h" #include "../game.h" void DrawRect(Vector2 rectStart, Vector2 *mousePosition){ float width = GetMousePosition().x - rectStart.x; float height = GetMousePosition().y - rectStart.y; if(width < 0 && height >= 0){ width *= -1; rectStart.x -= width; } else if(height < 0 && width >= 0){ height *= -1; rectStart.y -= height; } else if(height < 0 && width < 0){ height *= -1; width *= -1; rectStart.x -= width; rectStart.y -= height; } DrawRectangleLines(rectStart.x, rectStart.y, width, height, GREEN); } Vector2 GetRectangle(Vector2 rectStart){ float width = GetMousePosition().x - rectStart.x; float height = GetMousePosition().y - rectStart.y; if(width < 0 && height >= 0){ width *= -1; rectStart.x -= width; } else if(height < 0 && width >= 0){ height *= -1; rectStart.y -= height; } else if(height < 0 && width < 0){ height *= -1; width *= -1; rectStart.x -= width; rectStart.y -= height; } return rectStart; } void mouseInput(Game *game){ InputHandler *inputHandler = game->inputHandler; List *sprites = game->sprites; Camera2D *camera = game->camera; IsometricMap **layers = *(game->layers); Texture2D *texture = game->worker +4; inputHandler->cursorPos.x = GetMousePosition().x; inputHandler->cursorPos.y = GetMousePosition().y; // resetting last selected Tile to grass texture if(inputHandler->selectedLayer != -1){ IsometricMapChangeTextureIdOfTile(layers[inputHandler->selectedLayer], (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, 0); } /* TODO: n=9 no good style, Segmentation fault when n > layerAmount impossible to find out size of array on the fly? -> Stash size in another variable. printf("%ld \n", sizeof(*layers) / sizeof(layers[0])); */ int n = 9; for(n = 9; n >= 0 ; n--){ if(layers[n] != 0){ float tileWidthHalf = layers[n]->tileTextures[0].width / 2; float tileHeightQuarter = layers[n]->tileTextures[0].height / 4; int mouseAdjustmentX = -tileWidthHalf; int mouseAdjustmentY = -tileHeightQuarter + (tileHeightQuarter * layers[n]->layer); IsometricMapProject(layers[n], camera, inputHandler->cursorPos.x + mouseAdjustmentX, inputHandler->cursorPos.y + mouseAdjustmentY, &inputHandler->cursorWorldPos); Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers[n], inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); if(selectedTile != 0){ inputHandler->cursorWorldTile.x = selectedTile->x; inputHandler->cursorWorldTile.y = selectedTile->y; inputHandler->selectedLayer = n; // setting currently selected tile to tower IsometricMapChangeTextureIdOfTile(layers[n], inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, 1); break; } } } if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ if(inputHandler->pressed == 0){ inputHandler->rectStart.x = GetMousePosition().x; inputHandler->rectStart.y = GetMousePosition().y; inputHandler->pressed = 1; // Cursorsprite is changed to "down" inputHandler->cursorSprite->texture = (inputHandler->cursorTextures) + 1; } } if(inputHandler->pressed){ DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos)); } if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ inputHandler->pressed = 0; // Cursorsprite is changed back to normal inputHandler->cursorSprite->texture = (inputHandler->cursorTextures); float width = GetMousePosition().x - inputHandler->rectStart.x; float height = GetMousePosition().y - inputHandler->rectStart.y; // Add Sprite if(width + height <= 1){ ListInsertBack(sprites, SpriteCreate(texture, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); } // Berechnung, welche Sprites ausgewählt wurden, scuffed Vector2 rect = GetRectangle(inputHandler->rectStart); width = abs(width); height = abs(height); printf("Auswahl: x: %f, y: %f, w: %f, h: %f\n", rect.x, rect.y, width, height); float deltaX; float deltaY; Node *current = sprites->head; while (current != 0){ Vector2 currPos = {current->data.x, current->data.y}; IsometricMapUnproject(layers[0], camera, currPos.x, currPos.y, &currPos); deltaX = currPos.x + current->data.texture->width/2 - (rect.x + camera->target.x); deltaY = currPos.y + current->data.texture->height/2 - (rect.y + camera->target.y); printf("deltaX: %f, deltaY: %f\n", deltaX, deltaY); if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){ current->data.selected = 1; } else{ current->data.selected = 0; } current = current->next; } } if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){ Node *current = sprites->head; while (current != 0){ if(current->data.selected){ current->data.hasDestination = 1; current->data.destX = inputHandler->cursorWorldPos.x; current->data.destY = inputHandler->cursorWorldPos.y; } current = current->next; } } } void keyboardInput(InputHandler *inputHandler, Camera2D *camera){ float camSpeed = 1000.0f; if(IsKeyDown(KEY_W)){ (*camera).target.y -= camSpeed * GetFrameTime(); } if(IsKeyDown(KEY_S)){ (*camera).target.y += camSpeed * GetFrameTime(); } if(IsKeyDown(KEY_D)){ (*camera).target.x += camSpeed * GetFrameTime(); } if(IsKeyDown(KEY_A)){ (*camera).target.x -= camSpeed * GetFrameTime(); } }