map can be edited easily now

main
Jan 3 years ago
parent 666961f6e1
commit 394190999a

@ -11,6 +11,7 @@ typedef struct InputHandler{
Vector2 cursorWorldPos; // position of cursor in World Vector2 cursorWorldPos; // position of cursor in World
Vector2 cursorWorldTile; // Selected Tile Vector2 cursorWorldTile; // Selected Tile
int selectionRectActive; //0: not active | 1: active int selectionRectActive; //0: not active | 1: active
int drawTileId;
} InputHandler; } InputHandler;
void mouseInput(Game *game); void mouseInput(Game *game);

@ -9,10 +9,17 @@
IsometricMap * IsometricMapInit(){ IsometricMap * IsometricMapInit(){
IsometricMap* map = malloc(sizeof(IsometricMap)); IsometricMap* map = malloc(sizeof(IsometricMap));
map->tileTextures[0] = LoadTexture("assets/grass.png"); int counter = 0;
map->tileTextures[1] = LoadTexture("assets/grass_selected.png"); map->tileTextures[counter++] = LoadTexture("assets/tiles/grass.png");
map->tileTextures[2] = LoadTexture("assets/tower.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->width = 500;
map->height = 500; map->height = 500;
@ -157,10 +164,11 @@ void IsometricMapDraw(Game *game){
continue; continue;
} }
else{ else{
;
DrawTexture( DrawTexture(
game->map->tileTextures[game->map->tiles[i][j]->textureId], game->map->tileTextures[game->map->tiles[i][j]->textureId],
game->map->tiles[i][j]->offsetX, 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); WHITE);
continue; continue;
} }

@ -65,6 +65,7 @@ void ScreenRenderPauseScreen(Game *game){
DrawText("P: Pause", 5, halfScreenHeight + 32, 16, WHITE); DrawText("P: Pause", 5, halfScreenHeight + 32, 16, WHITE);
DrawText("WASD: Move Camera", 5, halfScreenHeight + 48, 16, WHITE); DrawText("WASD: Move Camera", 5, halfScreenHeight + 48, 16, WHITE);
DrawText("ESC: Exit Game", 5, halfScreenHeight + 64, 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)){ if(IsKeyPressed(KEY_P)){
game->currentScreen = SCREEN_GAME; game->currentScreen = SCREEN_GAME;

@ -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++], "DEPTH: %d", (int)(game->inputHandler->cursorWorldPos.x + game->inputHandler->cursorWorldPos.y));
sprintf(strings[lineamount++], "Camera Zoom: %f", game->camera->zoom); sprintf(strings[lineamount++], "Camera Zoom: %f", game->camera->zoom);
sprintf(strings[lineamount++], "Sprite Amount: %d", game->sprites->spriteAmount); 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 // 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(); int pressed = GetCharPressed();

@ -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);
}
}
}

@ -13,5 +13,6 @@ void OnSelectedSelectable(Game *game, Selectable *selectable);
void OnSelectedSpawnBuilding(Game *game, Selectable *selectable); void OnSelectedSpawnBuilding(Game *game, Selectable *selectable);
void OnSelectedSpawnWorker(Game *game, Selectable *selectable); void OnSelectedSpawnWorker(Game *game, Selectable *selectable);
void OnSelectedSpawnLumberjack(Game *game, Selectable *selectable); void OnSelectedSpawnLumberjack(Game *game, Selectable *selectable);
void OnSelectedDrawTile(Game *game, Selectable *selectable);
#endif #endif

@ -10,11 +10,12 @@
#include "onClickFunctions.h" #include "onClickFunctions.h"
Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures, int hasBackground, Vector2 *position, char *description, 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 *selectable = malloc(sizeof(Selectable));
selectable->texture = texture; selectable->texture = texture;
selectable->hasBackground = hasBackground; selectable->hasBackground = hasBackground;
selectable->scale = scale;
if(selectable->hasBackground == 1){ if(selectable->hasBackground == 1){
selectable->backgroundTexture = backgroundTextures; selectable->backgroundTexture = backgroundTextures;
} }
@ -33,6 +34,12 @@ Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures,
selectable->groupID = groupID; selectable->groupID = groupID;
selectable->unselectAfterExecute = unselectAfterExecute; 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){ switch(selectable->id){
case SELECTABLE_ID_SPAWN_BUILDING: case SELECTABLE_ID_SPAWN_BUILDING:
selectable->onSelected = &OnSelectedSpawnBuilding; selectable->onSelected = &OnSelectedSpawnBuilding;
@ -43,6 +50,9 @@ Selectable * SelectableInit(Texture2D *texture, Texture2D **backgroundTextures,
case SELECTABLE_ID_SPAWN_LUMBERJACK: case SELECTABLE_ID_SPAWN_LUMBERJACK:
selectable->onSelected = &OnSelectedSpawnLumberjack; selectable->onSelected = &OnSelectedSpawnLumberjack;
break; break;
case SELECTABLE_ID_DRAWING_TILE:
selectable->onSelected = &OnSelectedDrawTile;
break;
default: default:
selectable->onSelected = &OnSelectedSelectable; selectable->onSelected = &OnSelectedSelectable;
printf("\n\n\n\n\n\n WARNING: Unsupported SELECTABLE ID %d \n\n\n\n\n\n", selectable->id); 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 SelectableUpdateSelectableState(Selectable * selectable, Game *game){
int mouseOnElement = 0; int mouseOnElement = 0;
if(GetMouseX() > selectable->position.x && 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 &&
GetMouseY() < selectable->position.y + (*selectable->backgroundTexture)[selectable->state].height GetMouseY() < selectable->position.y + (*selectable->backgroundTexture)[selectable->state].height * selectable->scale
){ ){
mouseOnElement = 1; mouseOnElement = 1;
game->mouseOnUI = 1; game->mouseOnUI = 1;
@ -93,17 +103,21 @@ int SelectableUnselectSelectable(Selectable * selectable){
} }
void SelectableDrawSelectable(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) { if(selectable->hasBackground == 1) {
targetWidth = selectable->backgroundTexture[selectable->state]->width; DrawTexturePro((*(selectable->backgroundTexture))[selectable->state], from, to, (Vector2){0,0}, 0.0f, WHITE);
DrawTexture((*(selectable->backgroundTexture))[selectable->state], selectable->position.x, selectable->position.y, WHITE);
} }
from.width = selectable->texture->width;
// ich weiss zwar nicht wieso genau aber das hier skaliert die Texturen richtig from.height = selectable->texture->height;
int padding = 19; to.x += padding * selectable->scale;
Rectangle from = {0, 0 , selectable->texture->width, selectable->texture->height}; to.y += padding * selectable->scale;
Rectangle to = {selectable->position.x +padding, selectable->position.y + padding, selectable->backgroundTexture[0]->width - 2*padding, selectable->backgroundTexture[0]->height - 2*padding}; to.width -= 2*padding * selectable->scale;
to.height -= 2*padding * selectable->scale;
DrawTexturePro(*selectable->texture, from, to, (Vector2){0,0}, 0.0f, WHITE); DrawTexturePro(*selectable->texture, from, to, (Vector2){0,0}, 0.0f, WHITE);
if(selectable->state == SELECTABLE_STATE_SELECTED){ 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}); 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); DrawRectangleLines(GetMouseX() - selectable->texture->width / 2, GetMouseY() - selectable->texture->height*0.75, selectable->texture->width, selectable->texture->height, GREEN);

@ -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 // 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 int groupID; // Selectables können gruppiert werden, man kann also mehrere Dinge gleichzeitig selected haben
float scale;
void (*onSelected)(Game *game, Selectable *selectable); void (*onSelected)(Game *game, Selectable *selectable);
}Selectable; }Selectable;
@ -29,7 +30,7 @@ typedef struct Selectable{
// showDescription 0: zeigt Description nicht | 1: zeigt Description // showDescription 0: zeigt Description nicht | 1: zeigt Description
// unselectAfterExecute 0: wird abgewählt | 1: bleibt ausgewählt // unselectAfterExecute 0: wird abgewählt | 1: bleibt ausgewählt
// Max Description LEN 20 // 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); void SelectableExecuteSelectable(Selectable *selectable, Game * game);

@ -5,6 +5,7 @@
#include "raylib.h" #include "raylib.h"
#include "stdlib.h" #include "stdlib.h"
#include "stdio.h" #include "stdio.h"
#include "../IsometricMap/isometricMap.h"
static UiContainer * UiContainerInitEmptyUiContainer(){ static UiContainer * UiContainerInitEmptyUiContainer(){
UiContainer *uiContainer = malloc(sizeof(UiContainer)); UiContainer *uiContainer = malloc(sizeof(UiContainer));
@ -66,7 +67,7 @@ static UiContainer * UiContainerInitGameUiContainer(Game *game){
Texture2D *texture2 = game->textures->textures[TE_WORKER] ; Texture2D *texture2 = game->textures->textures[TE_WORKER] ;
Texture2D *texture3 = game->textures->textures[TE_LUMBERJACK] ; Texture2D *texture3 = game->textures->textures[TE_LUMBERJACK] ;
Texture2D **backgroundTextures = &(game->textures->textures[TE_SELECTABLE_BACKGROUND]); Texture2D **backgroundTextures = &(game->textures->textures[TE_SELECTABLE_BACKGROUND]);
Vector2 position = (Vector2){20, 300}; Vector2 position = (Vector2){4, GetScreenHeight() - 100};
int showDescription = 1; int showDescription = 1;
int hasBackground = 1; int hasBackground = 1;
int fontSize = 16; int fontSize = 16;
@ -75,21 +76,34 @@ static UiContainer * UiContainerInitGameUiContainer(Game *game){
// creating the ui elements // creating the ui elements
Selectable *selectable1 = SelectableInit(texture1, backgroundTextures, hasBackground , &position, "Building", Selectable *selectable1 = SelectableInit(texture1, backgroundTextures, hasBackground , &position, "Building",
showDescription, 9/*String len*/, fontSize, SELECTABLE_ID_SPAWN_BUILDING, showDescription, 9/*String len*/, fontSize, SELECTABLE_ID_SPAWN_BUILDING,
1/*Group*/, unselectAfterExecute); 1/*Group*/, unselectAfterExecute, 1);
position.y += 100; position.x += 100;
Selectable *selectable2 = SelectableInit(texture2, backgroundTextures, hasBackground,&position, "Worker", Selectable *selectable2 = SelectableInit(texture2, backgroundTextures, hasBackground,&position, "Worker",
showDescription, 7/*String len*/, fontSize, SELECTABLE_ID_SPAWN_WORKER, 1/*Group*/, showDescription, 7/*String len*/, fontSize, SELECTABLE_ID_SPAWN_WORKER, 1/*Group*/,
unselectAfterExecute); unselectAfterExecute, 1);
position.y += 100; position.x += 100;
Selectable *selectable3 = SelectableInit(texture3, backgroundTextures, hasBackground,&position, "Lumberjack", Selectable *selectable3 = SelectableInit(texture3, backgroundTextures, hasBackground,&position, "Lumberjack",
showDescription, 11/*String len*/, fontSize, SELECTABLE_ID_SPAWN_LUMBERJACK, 1/*Group*/, showDescription, 11/*String len*/, fontSize, SELECTABLE_ID_SPAWN_LUMBERJACK, 1/*Group*/,
unselectAfterExecute); unselectAfterExecute, 1);
// adding the ui elements // adding the ui elements
UiContainerAddSelectable(uiContainer, selectable1); UiContainerAddSelectable(uiContainer, selectable1);
UiContainerAddSelectable(uiContainer, selectable2); UiContainerAddSelectable(uiContainer, selectable2);
UiContainerAddSelectable(uiContainer, selectable3); 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; return uiContainer;
} }

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

Before

Width:  |  Height:  |  Size: 7.7 KiB

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

@ -84,9 +84,14 @@
#define SELECTABLE_ID_SPAWN_BUILDING 0 #define SELECTABLE_ID_SPAWN_BUILDING 0
#define SELECTABLE_ID_SPAWN_WORKER 1 #define SELECTABLE_ID_SPAWN_WORKER 1
#define SELECTABLE_ID_SPAWN_LUMBERJACK 2 #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 #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 #endif

@ -28,6 +28,7 @@ Game *GameInit()
game->inputHandler->cursorWorldPos.y = 0; game->inputHandler->cursorWorldPos.y = 0;
game->inputHandler->cursorWorldTile.x = 0; game->inputHandler->cursorWorldTile.x = 0;
game->inputHandler->cursorWorldTile.y = 0; game->inputHandler->cursorWorldTile.y = 0;
game->inputHandler->drawTileId = 0;
game->mouseOnUI = 0; game->mouseOnUI = 0;

Loading…
Cancel
Save