diff --git a/Input/inputHandler.h b/Input/inputHandler.h index 6fe2405..b6ed88c 100644 --- a/Input/inputHandler.h +++ b/Input/inputHandler.h @@ -11,6 +11,7 @@ typedef struct InputHandler{ Vector2 cursorWorldPos; // position of cursor in World Vector2 cursorWorldTile; // Selected Tile int selectionRectActive; //0: not active | 1: active + int drawTileId; } InputHandler; void mouseInput(Game *game); diff --git a/IsometricMap/isometricMap.c b/IsometricMap/isometricMap.c index 24db7b0..a53f1a1 100644 --- a/IsometricMap/isometricMap.c +++ b/IsometricMap/isometricMap.c @@ -9,10 +9,17 @@ IsometricMap * IsometricMapInit(){ IsometricMap* map = malloc(sizeof(IsometricMap)); - - map->tileTextures[0] = LoadTexture("assets/grass.png"); - map->tileTextures[1] = LoadTexture("assets/grass_selected.png"); - map->tileTextures[2] = LoadTexture("assets/tower.png"); + + int counter = 0; + map->tileTextures[counter++] = LoadTexture("assets/tiles/grass.png"); + map->tileTextures[counter++] = LoadTexture("assets/tiles/desert.png"); + map->tileTextures[counter++] = LoadTexture("assets/tiles/desert_palm.png"); + map->tileTextures[counter++] = LoadTexture("assets/tiles/tower.png"); + map->tileTextures[counter++] = LoadTexture("assets/tiles/bigtower.png"); + map->tileTextures[counter++] = LoadTexture("assets/tiles/grass_selected.png"); + map->tileTextures[counter++] = LoadTexture("assets/tiles/ice.png"); + map->tileTextures[counter++] = LoadTexture("assets/tiles/water.png"); + map->tileTextures[counter++] = LoadTexture("assets/tiles/empty.png"); map->width = 500; map->height = 500; @@ -157,10 +164,11 @@ void IsometricMapDraw(Game *game){ continue; } else{ + ; DrawTexture( game->map->tileTextures[game->map->tiles[i][j]->textureId], game->map->tiles[i][j]->offsetX, - game->map->tiles[i][j]->offsetY, + game->map->tiles[i][j]->offsetY - game->map->tileTextures[game->map->tiles[i][j]->textureId].height + 32, WHITE); continue; } diff --git a/Screen/screen.c b/Screen/screen.c index 99fe328..ec6830f 100644 --- a/Screen/screen.c +++ b/Screen/screen.c @@ -65,6 +65,7 @@ void ScreenRenderPauseScreen(Game *game){ DrawText("P: Pause", 5, halfScreenHeight + 32, 16, WHITE); DrawText("WASD: Move Camera", 5, halfScreenHeight + 48, 16, WHITE); DrawText("ESC: Exit Game", 5, halfScreenHeight + 64, 16, WHITE); + DrawText("CTRL: 3x3 Brush for changing Tiles", 5, halfScreenHeight + 80, 16, WHITE); if(IsKeyPressed(KEY_P)){ game->currentScreen = SCREEN_GAME; diff --git a/Ui/debug.c b/Ui/debug.c index c06b8e2..21bc7d8 100644 --- a/Ui/debug.c +++ b/Ui/debug.c @@ -24,6 +24,7 @@ void DebugDraw(Game *game){ sprintf(strings[lineamount++], "DEPTH: %d", (int)(game->inputHandler->cursorWorldPos.x + game->inputHandler->cursorWorldPos.y)); sprintf(strings[lineamount++], "Camera Zoom: %f", game->camera->zoom); sprintf(strings[lineamount++], "Sprite Amount: %d", game->sprites->spriteAmount); + sprintf(strings[lineamount++], "Draw Tile ID: %d", game->inputHandler->drawTileId); // Hier müssten wir eine bessere Lösung finden, das flackert weil pressed nur für einen Frame gilt. Eine ähnliche Funktion gibt es für CharDown leider nicht, müssten wir selbst programmieren. Ich habe es erstmal nicht auskommentiert. Kann man aber easy machen sollte es stören int pressed = GetCharPressed(); diff --git a/Ui/onClickFunctions.c b/Ui/onClickFunctions.c index cdbcde7..349dd7b 100644 --- a/Ui/onClickFunctions.c +++ b/Ui/onClickFunctions.c @@ -83,3 +83,21 @@ void OnSelectedSpawnLumberjack(Game *game, Selectable *selectable){ } } } +void OnSelectedDrawTile(Game *game, Selectable *selectable){ + game->inputHandler->drawTileId = selectable->id - SELECTABLE_ID_DRAWING_TILE; + if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)&& game->mouseOnUI == 0){ + IsometricMapChangeTextureIdOfTile(game->map, game->inputHandler->cursorWorldTile.x, game->inputHandler->cursorWorldTile.y, game->inputHandler->drawTileId); + if(IsKeyDown(KEY_LEFT_CONTROL)){ + int x = game->inputHandler->cursorWorldTile.x; + int y = game->inputHandler->cursorWorldTile.y; + IsometricMapChangeTextureIdOfTile(game->map, x-1, y-1, game->inputHandler->drawTileId); + IsometricMapChangeTextureIdOfTile(game->map, x, y-1, game->inputHandler->drawTileId); + IsometricMapChangeTextureIdOfTile(game->map, x+1, y-1, game->inputHandler->drawTileId); + IsometricMapChangeTextureIdOfTile(game->map, x-1, y, game->inputHandler->drawTileId); + IsometricMapChangeTextureIdOfTile(game->map, x+1, y, game->inputHandler->drawTileId); + IsometricMapChangeTextureIdOfTile(game->map, x+1, y+1, game->inputHandler->drawTileId); + IsometricMapChangeTextureIdOfTile(game->map, x-1, y+1, game->inputHandler->drawTileId); + IsometricMapChangeTextureIdOfTile(game->map, x, y+1, game->inputHandler->drawTileId); + } + } +} diff --git a/Ui/onClickFunctions.h b/Ui/onClickFunctions.h index 3b34320..a352f93 100644 --- a/Ui/onClickFunctions.h +++ b/Ui/onClickFunctions.h @@ -13,5 +13,6 @@ void OnSelectedSelectable(Game *game, Selectable *selectable); void OnSelectedSpawnBuilding(Game *game, Selectable *selectable); void OnSelectedSpawnWorker(Game *game, Selectable *selectable); void OnSelectedSpawnLumberjack(Game *game, Selectable *selectable); +void OnSelectedDrawTile(Game *game, Selectable *selectable); #endif \ No newline at end of file diff --git a/Ui/selectable.c b/Ui/selectable.c index aed0711..79826e8 100644 --- a/Ui/selectable.c +++ b/Ui/selectable.c @@ -10,11 +10,12 @@ #include "onClickFunctions.h" Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures, int hasBackground, Vector2 *position, char *description, - int showDescripton, int descriptionLEN, int fontSize, int id, int groupID, int unselectAfterExecute){ + int showDescripton, int descriptionLEN, int fontSize, int id, int groupID, int unselectAfterExecute, float scale){ Selectable *selectable = malloc(sizeof(Selectable)); selectable->texture = texture; selectable->hasBackground = hasBackground; + selectable->scale = scale; if(selectable->hasBackground == 1){ selectable->backgroundTexture = backgroundTextures; } @@ -33,6 +34,12 @@ Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures, selectable->groupID = groupID; selectable->unselectAfterExecute = unselectAfterExecute; + int i = 0; + if(selectable->id > SELECTABLE_ID_DRAWING_TILE && selectable->id < ISOMETRICMAP_TILE_TEXTURE_AMOUNT + SELECTABLE_ID_DRAWING_TILE){ + selectable->onSelected = &OnSelectedDrawTile; + return selectable; + } + switch(selectable->id){ case SELECTABLE_ID_SPAWN_BUILDING: selectable->onSelected = &OnSelectedSpawnBuilding; @@ -43,6 +50,9 @@ Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures, case SELECTABLE_ID_SPAWN_LUMBERJACK: selectable->onSelected = &OnSelectedSpawnLumberjack; break; + case SELECTABLE_ID_DRAWING_TILE: + selectable->onSelected = &OnSelectedDrawTile; + break; default: selectable->onSelected = &OnSelectedSelectable; printf("\n\n\n\n\n\n WARNING: Unsupported SELECTABLE ID %d \n\n\n\n\n\n", selectable->id); @@ -60,9 +70,9 @@ void SelectableExecuteSelectable(Selectable *selectable, Game * game){ int SelectableUpdateSelectableState(Selectable * selectable, Game *game){ int mouseOnElement = 0; if(GetMouseX() > selectable->position.x && - GetMouseX() < selectable->position.x + (*selectable->backgroundTexture)[selectable->state].width && + GetMouseX() < selectable->position.x + (*selectable->backgroundTexture)[selectable->state].width * selectable->scale && GetMouseY() > selectable->position.y && - GetMouseY() < selectable->position.y + (*selectable->backgroundTexture)[selectable->state].height + GetMouseY() < selectable->position.y + (*selectable->backgroundTexture)[selectable->state].height * selectable->scale ){ mouseOnElement = 1; game->mouseOnUI = 1; @@ -93,17 +103,21 @@ int SelectableUnselectSelectable(Selectable * selectable){ } void SelectableDrawSelectable(Selectable * selectable){ - int targetWidth = 0; + int padding = 19; + Rectangle from = {0, 0, selectable->backgroundTexture[0]->width, selectable->backgroundTexture[0]->height}; + Rectangle to = {selectable->position.x, selectable->position.y , selectable->backgroundTexture[0]->width* selectable->scale, selectable->backgroundTexture[0]->height* selectable->scale}; + if(selectable->hasBackground == 1) { - targetWidth = selectable->backgroundTexture[selectable->state]->width; - DrawTexture((*(selectable->backgroundTexture))[selectable->state], selectable->position.x, selectable->position.y, WHITE); + DrawTexturePro((*(selectable->backgroundTexture))[selectable->state], from, to, (Vector2){0,0}, 0.0f, WHITE); } - - // ich weiss zwar nicht wieso genau aber das hier skaliert die Texturen richtig - int padding = 19; - Rectangle from = {0, 0 , selectable->texture->width, selectable->texture->height}; - Rectangle to = {selectable->position.x +padding, selectable->position.y + padding, selectable->backgroundTexture[0]->width - 2*padding, selectable->backgroundTexture[0]->height - 2*padding}; + from.width = selectable->texture->width; + from.height = selectable->texture->height; + to.x += padding * selectable->scale; + to.y += padding * selectable->scale; + to.width -= 2*padding * selectable->scale; + to.height -= 2*padding * selectable->scale; DrawTexturePro(*selectable->texture, from, to, (Vector2){0,0}, 0.0f, WHITE); + if(selectable->state == SELECTABLE_STATE_SELECTED){ DrawTexture(*selectable->texture, GetMouseX() - selectable->texture->width / 2, GetMouseY() - selectable->texture->height*0.75, (Color){255, 255, 255, 150}); DrawRectangleLines(GetMouseX() - selectable->texture->width / 2, GetMouseY() - selectable->texture->height*0.75, selectable->texture->width, selectable->texture->height, GREEN); diff --git a/Ui/selectable.h b/Ui/selectable.h index b912891..067197d 100644 --- a/Ui/selectable.h +++ b/Ui/selectable.h @@ -21,6 +21,7 @@ typedef struct Selectable{ // Notiz: Die Selectables können auch in einem 2-Dimensionalen Array im UiContainer gespeichert werden, worüber die Groups auch definiert werden könnten int groupID; // Selectables können gruppiert werden, man kann also mehrere Dinge gleichzeitig selected haben + float scale; void (*onSelected)(Game *game, Selectable *selectable); }Selectable; @@ -29,7 +30,7 @@ typedef struct Selectable{ // showDescription 0: zeigt Description nicht | 1: zeigt Description // unselectAfterExecute 0: wird abgewählt | 1: bleibt ausgewählt // Max Description LEN 20 -Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures, int hasBackground, Vector2 *position, char *description, int showDescripton, int descriptionLEN, int fontSize, int id, int groupID, int unselectAfterExecute); +Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures, int hasBackground, Vector2 *position, char *description, int showDescripton, int descriptionLEN, int fontSize, int id, int groupID, int unselectAfterExecute, float scale); void SelectableExecuteSelectable(Selectable *selectable, Game * game); diff --git a/Ui/uiContainer.c b/Ui/uiContainer.c index 4644ae2..6c903cf 100644 --- a/Ui/uiContainer.c +++ b/Ui/uiContainer.c @@ -5,6 +5,7 @@ #include "raylib.h" #include "stdlib.h" #include "stdio.h" +#include "../IsometricMap/isometricMap.h" static UiContainer * UiContainerInitEmptyUiContainer(){ UiContainer *uiContainer = malloc(sizeof(UiContainer)); @@ -66,7 +67,7 @@ static UiContainer * UiContainerInitGameUiContainer(Game *game){ Texture2D *texture2 = game->textures->textures[TE_WORKER] ; Texture2D *texture3 = game->textures->textures[TE_LUMBERJACK] ; Texture2D **backgroundTextures = &(game->textures->textures[TE_SELECTABLE_BACKGROUND]); - Vector2 position = (Vector2){20, 300}; + Vector2 position = (Vector2){4, GetScreenHeight() - 100}; int showDescription = 1; int hasBackground = 1; int fontSize = 16; @@ -75,21 +76,34 @@ static UiContainer * UiContainerInitGameUiContainer(Game *game){ // creating the ui elements Selectable *selectable1 = SelectableInit(texture1, backgroundTextures, hasBackground , &position, "Building", showDescription, 9/*String len*/, fontSize, SELECTABLE_ID_SPAWN_BUILDING, - 1/*Group*/, unselectAfterExecute); - position.y += 100; + 1/*Group*/, unselectAfterExecute, 1); + position.x += 100; Selectable *selectable2 = SelectableInit(texture2, backgroundTextures, hasBackground,&position, "Worker", showDescription, 7/*String len*/, fontSize, SELECTABLE_ID_SPAWN_WORKER, 1/*Group*/, - unselectAfterExecute); - position.y += 100; + unselectAfterExecute, 1); + position.x += 100; Selectable *selectable3 = SelectableInit(texture3, backgroundTextures, hasBackground,&position, "Lumberjack", showDescription, 11/*String len*/, fontSize, SELECTABLE_ID_SPAWN_LUMBERJACK, 1/*Group*/, - unselectAfterExecute); + unselectAfterExecute, 1); // adding the ui elements UiContainerAddSelectable(uiContainer, selectable1); UiContainerAddSelectable(uiContainer, selectable2); UiContainerAddSelectable(uiContainer, selectable3); + showDescription = 0; + unselectAfterExecute = 0; + int i = 0; + position = (Vector2){GetScreenWidth()-50, 2}; + for(i; i < ISOMETRICMAP_TILE_TEXTURE_AMOUNT; i++){ + UiContainerAddSelectable(uiContainer, + SelectableInit(&(game->map->tileTextures[i]), backgroundTextures, hasBackground , &position, "", + showDescription, 0/*String len*/, fontSize, SELECTABLE_ID_DRAWING_TILE+i, + 1/*Group*/, 1, 0.5f) + ); + position.y += 50; + } + return uiContainer; } diff --git a/assets/mapobjects/ressources.png b/assets/dump/ressources.png similarity index 100% rename from assets/mapobjects/ressources.png rename to assets/dump/ressources.png diff --git a/assets/bigtower.png b/assets/tiles/bigtower.png similarity index 100% rename from assets/bigtower.png rename to assets/tiles/bigtower.png diff --git a/assets/desert.png b/assets/tiles/desert.png similarity index 100% rename from assets/desert.png rename to assets/tiles/desert.png diff --git a/assets/tiles/desert_palm.png b/assets/tiles/desert_palm.png new file mode 100644 index 0000000..b0235ce Binary files /dev/null and b/assets/tiles/desert_palm.png differ diff --git a/assets/tiles/empty.png b/assets/tiles/empty.png new file mode 100644 index 0000000..0d7e1c3 Binary files /dev/null and b/assets/tiles/empty.png differ diff --git a/assets/grass.png b/assets/tiles/grass.png similarity index 100% rename from assets/grass.png rename to assets/tiles/grass.png diff --git a/assets/grass_selected.png b/assets/tiles/grass_selected.png similarity index 100% rename from assets/grass_selected.png rename to assets/tiles/grass_selected.png diff --git a/assets/tiles/ice.png b/assets/tiles/ice.png new file mode 100644 index 0000000..0cc232f Binary files /dev/null and b/assets/tiles/ice.png differ diff --git a/assets/tower.png b/assets/tiles/tower.png similarity index 100% rename from assets/tower.png rename to assets/tiles/tower.png diff --git a/assets/tiles/water.png b/assets/tiles/water.png new file mode 100644 index 0000000..b0ed387 Binary files /dev/null and b/assets/tiles/water.png differ diff --git a/definitions.h b/definitions.h index d30074e..b8f901c 100644 --- a/definitions.h +++ b/definitions.h @@ -84,9 +84,14 @@ #define SELECTABLE_ID_SPAWN_BUILDING 0 #define SELECTABLE_ID_SPAWN_WORKER 1 #define SELECTABLE_ID_SPAWN_LUMBERJACK 2 +#define SELECTABLE_ID_DRAWING_TILE 30 // Ab dieser ID sind ISOMETRICMAP_TILE_TEXTURE_AMOUNT IDs reserviert für Map Tiles!!! #define UNSELECT_SELECTABLE_ON_SELECT 1 +// Definitions for IsometricMap tiles + +#define ISOMETRICMAP_TILE_TEXTURE_AMOUNT 8 // in IsometricMapInit werden TILE_TEXTURE_AMOUNT Texturen geladen + #endif diff --git a/game.c b/game.c index 8fb4eb4..4ae4561 100644 --- a/game.c +++ b/game.c @@ -28,6 +28,7 @@ Game *GameInit() game->inputHandler->cursorWorldPos.y = 0; game->inputHandler->cursorWorldTile.x = 0; game->inputHandler->cursorWorldTile.y = 0; + game->inputHandler->drawTileId = 0; game->mouseOnUI = 0;