diff --git a/Input/inputHandler.c b/Input/inputHandler.c index e635f77..7b9eb8a 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -92,29 +92,30 @@ void mouseInput(Game *game){ IsometricMapChangeTextureIdOfTile(map, inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, 1); } - - 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" - game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][1]; + if(game->mouseOnUI == 0){ + 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" + game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][1]; + } } - } if(inputHandler->pressed){ DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos)); } - if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ - inputHandler->pressed = 0; - // Cursorsprite is changed back to normal - game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][0]; - float width = GetMousePosition().x - inputHandler->rectStart.x; - float height = GetMousePosition().y - inputHandler->rectStart.y; + if(game->mouseOnUI == 0){ + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + inputHandler->pressed = 0; + // Cursorsprite is changed back to normal + game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][0]; + float width = GetMousePosition().x - inputHandler->rectStart.x; + float height = GetMousePosition().y - inputHandler->rectStart.y; // Add Sprite @@ -126,10 +127,10 @@ void mouseInput(Game *game){ else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");} else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");} else { - Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); - Entity *entity = EntityInit(newSprite, PR_BUILDER, game->textures); - EntityListInsert(game->entities, entity); - SpriteListInsert(game->sprites, newSprite); + //Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); + //Entity *entity = EntityInit(newSprite, PR_BUILDER, game->textures); + //EntityListInsert(game->entities, entity); + //SpriteListInsert(game->sprites, newSprite); //ListPrintForward(sprites); //ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); } @@ -155,10 +156,9 @@ void mouseInput(Game *game){ else{ current->selected = 0; } - current = current->next; - } - } + } + } } if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){ @@ -180,10 +180,11 @@ void mouseInput(Game *game){ current->destY = destY; } -skip: current = current->next; + skip: current = current->next; } } } +} void keyboardInput(InputHandler *inputHandler, Camera2D *camera){ float camSpeed = 1000.0f; diff --git a/Ui/selectable.c b/Ui/selectable.c index dc69930..031fc39 100644 --- a/Ui/selectable.c +++ b/Ui/selectable.c @@ -6,6 +6,7 @@ #include "../MapObject/building.h" #include "../Sprite/sprite.h" #include "../Input/inputHandler.h" +#include "../Entity/entity.h" Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[3], int hasBackground, Vector2 *position, char *description, int showDescripton, int descriptionLEN, int fontSize, int id, int groupID){ @@ -38,18 +39,31 @@ Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[ } void SelectableExecuteSelectable(Selectable *selectable, Game * game){ + // Hier sollte eine Bedingung stehen die nur in einem Frame true sein kann + // Oder das selectable wird abgewählt indem seine ID auf default zurückgesetzt wird + // Sonst kann das hier jeden Frame passieren. ID wird nicht automatisch zurückgesetzt switch(selectable->id){ - case SELECTABLE_ID_TEST: // Test Case Platzhalter + case SELECTABLE_ID_SPAWN_BUILDING: // Test Case Platzhalter // Hier sollte eine Bedingung stehen die nur in einem Frame true sein kann // Oder das selectable wird abgewählt indem seine ID auf default zurückgesetzt wird // Sonst kann das hier jeden Frame passieren. ID wird nicht automatisch zurückgesetzt - if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)){ + if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ Building *newObject = BuildingInit(game, BU_HOUSE, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); BuildingListInsert(game->buildings, newObject); selectable->state = SELECTABLE_STATE_DEFAULT; } break; + case SELECTABLE_ID_SPAWN_WORKER: + if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ + Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); + Entity *entity = EntityInit(newSprite, PR_BUILDER, game->textures); + EntityListInsert(game->entities, entity); + SpriteListInsert(game->sprites, newSprite); + + selectable->state = SELECTABLE_STATE_DEFAULT; + } + break; default: printf("\n\n\n\n\n\n WARNING: Unsupported SELECTABLE ID %d \n\n\n\n\n\n", selectable->id); break; @@ -84,9 +98,13 @@ int SelectableUnselectSelectable(Selectable * selectable){ } void SelectableDrawSelectable(Selectable * selectable){ + int targetWidth = 0; if(selectable->hasBackground == 1) { + targetWidth = selectable->backgroundTexture[selectable->state].width; DrawTexture(selectable->backgroundTexture[selectable->state], selectable->position.x, selectable->position.y, WHITE); } + + //DrawTextureEx(selectable->texture[selectable->state], selectable->position, targetWidth / selectable->texture[selectable->state].width, 2, WHITE); DrawTexture(selectable->texture[selectable->state], selectable->position.x, selectable->position.y, WHITE); if(selectable->showDescription == 1){ DrawText(selectable->description, selectable->position.x, selectable->position.y, selectable->fontSize, WHITE); diff --git a/Ui/uiContainer.c b/Ui/uiContainer.c index a48d149..8458999 100644 --- a/Ui/uiContainer.c +++ b/Ui/uiContainer.c @@ -76,10 +76,14 @@ static UiContainer * UiContainerInitGameUiContainer(){ uiContainer->buttonCounter = 0; - Texture2D textures[3] = { LoadTexture("assets/selectable.png"), //DEFAULT + Texture2D textures[3] = { LoadTexture("assets/selectable.png"), //DEFAULT LoadTexture("assets/selectable.png"), //HOVERED LoadTexture("assets/selectable.png") //SELECTED }; + Texture2D textures2[3] = { LoadTexture("assets/entities/worker/arbeiten-4.png"), //DEFAULT + LoadTexture("assets/entities/worker/arbeiten-4.png"), //HOVERED + LoadTexture("assets/entities/worker/arbeiten-4.png") //SELECTED + }; Texture2D backgroundTextures[3] = { LoadTexture("assets/selectable_background.png"), //DEFAULT LoadTexture("assets/selectable_background_hovered.png"), //HOVERED @@ -92,31 +96,16 @@ static UiContainer * UiContainerInitGameUiContainer(){ int groupTwo = 1; int fontSize = 16; Selectable *selectable1 = SelectableInit(textures, backgroundTextures, hasBackground , &position, "Building", - showDescription, 9, fontSize, SELECTABLE_ID_TEST, groupOne); + showDescription, 9, fontSize, SELECTABLE_ID_SPAWN_BUILDING, groupOne); position.y += 100; - Selectable *selectable2 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building2", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupOne); + Selectable *selectable2 = SelectableInit(textures2, backgroundTextures, hasBackground,&position, "Worker", + showDescription, 10, fontSize, SELECTABLE_ID_SPAWN_WORKER, groupOne); position.y += 100; - Selectable *selectable3 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building3", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupOne); - position.y += 100; - Selectable *selectable4 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building4", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupTwo); - position.x += 100; - Selectable *selectable5 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building5", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupTwo); - position.x += 100; - Selectable *selectable6 = SelectableInit(textures, backgroundTextures, hasBackground,&position, "Building6", - showDescription, 10, fontSize, SELECTABLE_ID_TEST, groupTwo); int selectableCounter = 0; uiContainer->selectables[selectableCounter++] = selectable1; uiContainer->selectables[selectableCounter++] = selectable2; - uiContainer->selectables[selectableCounter++] = selectable3; - uiContainer->selectables[selectableCounter++] = selectable4; - uiContainer->selectables[selectableCounter++] = selectable5; - uiContainer->selectables[selectableCounter++] = selectable6; uiContainer->selectableCounter = selectableCounter; @@ -141,7 +130,10 @@ UiContainer * UiContainerInitUiContainerForScreenID(int id){ static void UpdateButtons(Game *game, Button **buttons, int buttonCounter){ int i = 0; for(i=0 ; i < buttonCounter; i++){ - ButtonUpdateButtonState(buttons[i]); + int state = ButtonUpdateButtonState(buttons[i]); + if(state == BUTTON_STATE_HOVERED || state == BUTTON_STATE_PRESSED){ + game->mouseOnUI = 1; + } if(buttons[i]->state == BUTTON_STATE_RELEASED){ ButtonExecuteButton(buttons[i], game); } @@ -152,7 +144,11 @@ static void UpdateButtons(Game *game, Button **buttons, int buttonCounter){ static void UpdateSelectables(Game *game, Selectable **selectables, int selectableCounter){ int i = 0; for(i=0 ; i < selectableCounter; i++){ + int oldState = selectables[i]->state; int setState = SelectableUpdateSelectableState(selectables[i]); + if(setState == SELECTABLE_STATE_HOVERED || (setState == SELECTABLE_STATE_SELECTED && oldState != SELECTABLE_STATE_SELECTED)){ + game->mouseOnUI = 1; + } // Unselecting every selectable if one is selected if(setState == SELECTABLE_STATE_SELECTED){ int j; @@ -176,7 +172,10 @@ void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game){ // Drawed alle Buttons und Selectables void UiContainerDrawUiContainer(UiContainer *uiContainer){ - if(uiContainer == 0) return; + if(uiContainer == 0){ + printf("WARNING: Trying to draw not existing UiContainer!!\n"); + return; + } int i = 0; // Funktion kann genauso wie update auch noch aufgeteilt werden, aktuell aber noch nicht notwendig for(i=0 ; i < uiContainer->buttonCounter; i++){ diff --git a/definitions.h b/definitions.h index d7fbd53..88ce607 100644 --- a/definitions.h +++ b/definitions.h @@ -63,7 +63,8 @@ #define SELECTABLE_STATE_HOVERED 1 #define SELECTABLE_STATE_SELECTED 2 -#define SELECTABLE_ID_TEST 0 +#define SELECTABLE_ID_SPAWN_BUILDING 0 +#define SELECTABLE_ID_SPAWN_WORKER 1 diff --git a/game.c b/game.c index e52438a..38711d0 100644 --- a/game.c +++ b/game.c @@ -31,6 +31,9 @@ Game *GameInit() game->inputHandler->selectedLayer = -1; game->screen = SCREEN_MAINMENU; + game->mouseOnUI = 0; + game->mouseFrameLock = 0; + game->camera = malloc(sizeof(Camera2D)); game->camera->target.x = 0; game->camera->target.y = 0; diff --git a/game.h b/game.h index df802c9..cdf6ff2 100644 --- a/game.h +++ b/game.h @@ -17,6 +17,9 @@ typedef struct Game{ struct EntityList *entities; struct UiContainer *UiContainers[SCREEN_AMOUNT]; int screen; + + int mouseOnUI ; // 0:not on UI 1:on UI + int mouseFrameLock ; // Sollten wir nicht brauchen wenn wir unsere Inputs gut definieren } Game; // returns pointer to new Game instance diff --git a/main.c b/main.c index d601301..6425883 100644 --- a/main.c +++ b/main.c @@ -28,13 +28,28 @@ int main(){ //SetTargetFPS(60); - + double frame = 0; while(!WindowShouldClose()){ + // Drawen wir einfach immer den UI Container vom aktuellen Screen? Aktuell hat ja jeder Screen einen, es sollte also keine Probleme geben + // Kann man natürlich auch noch oben in die einzelnen Cases schieben falls es mal Probleme machen sollte + UiContainerUpdateUiContainer(game->UiContainers[game->screen], game); + + if(game->mouseOnUI == 1 && frame == 0){ + frame = 1; + } + if(frame > 0){ + frame += GetFrameTime(); + } + if(frame >= 1.25){ + game->mouseOnUI = 0; + frame = 0; + } // Moving cursor Sprite to Mouse Pos game->cursorSprite->x = GetMousePosition().x; game->cursorSprite->y = GetMousePosition().y; + BeginDrawing(); // Drawing ist grundsätzlich immer aktiviert ClearBackground(RAYWHITE); // Screen wird in jedem Frame gecleared switch(game->screen){ // Screenspecific Code @@ -100,7 +115,6 @@ int main(){ // Drawen wir einfach immer den UI Container vom aktuellen Screen? Aktuell hat ja jeder Screen einen, es sollte also keine Probleme geben // Kann man natürlich auch noch oben in die einzelnen Cases schieben falls es mal Probleme machen sollte - UiContainerUpdateUiContainer(game->UiContainers[game->screen], game); UiContainerDrawUiContainer(game->UiContainers[game->screen]); @@ -111,8 +125,6 @@ int main(){ DrawSpriteToScreen(game->cursorSprite); EndDrawing(); - - } CloseWindow();