diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 269ea3d..958119e 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -66,7 +66,7 @@ void mouseInput(Game *game){ // resetting last selected Tile to grass texture if(inputHandler->selectedLayer != -1){ - IsometricMapChangeTextureIdOfTile(layers[inputHandler->selectedLayer], (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, 0); + IsometricMapChangeTextureIdOfTile(layers, (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, inputHandler->selectedLayer, 0); } /* TODO: n=9 no good style, Segmentation fault when n > layerAmount @@ -74,6 +74,7 @@ void mouseInput(Game *game){ -> Stash size in another variable. printf("%ld \n", sizeof(*layers) / sizeof(layers[0])); */ + // hardcoded layer amount int n = 9; for(n = 9; n >= 0 ; n--){ if(layers[n] != 0){ @@ -84,14 +85,14 @@ void mouseInput(Game *game){ 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); + Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y, n); 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); + IsometricMapChangeTextureIdOfTile(layers, inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, n, 1); break; } } @@ -123,8 +124,8 @@ void mouseInput(Game *game){ // Add Sprite if(abs(width) + abs(height) < 20){ - int maxWidth = (game->layers[0]->widthBounds-1) * game->layers[0]->textureWidth; - int maxHeight = (game->layers[0]->heightBounds-1) * game->layers[0]->textureHeight; + int maxWidth = (game->layers[0]->width-1) * game->layers[0]->textureWidth; + int maxHeight = (game->layers[0]->height-1) * game->layers[0]->textureHeight; if(inputHandler->cursorWorldPos.x < 0){ printf("OutOfBoundsDestination Spawn\n");} else if(inputHandler->cursorWorldPos.y < 0){ printf("OutOfBoundsDestination Spawn\n");} else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");} @@ -150,8 +151,8 @@ void mouseInput(Game *game){ 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); + Vector2 currPos = {current->data.x + current->data.texture->width, current->data.y + current->data.texture->height/2}; + IsometricMapUnproject(layers, camera, currPos.x, currPos.y, current->data.z, &currPos); deltaX = currPos.x - camera->target.x - (rect.x + camera->target.x); deltaY = currPos.y - camera->target.y - (rect.y + camera->target.y); @@ -175,10 +176,10 @@ void mouseInput(Game *game){ while (current != 0){ if(current->data.selected){ current->data.hasDestination = 1; - float destX = inputHandler->cursorWorldTile.x * game->layers[0]->textureWidth; - float destY = inputHandler->cursorWorldTile.y * game->layers[0]->textureHeight; - int maxWidth = (game->layers[0]->widthBounds-1) * game->layers[0]->textureWidth; - int maxHeight = (game->layers[0]->heightBounds-1) * game->layers[0]->textureHeight; + float destX = inputHandler->cursorWorldPos.x; + float destY = inputHandler->cursorWorldPos.y; + int maxWidth = (game->layers[0]->width-1) * game->layers[0]->textureWidth; + int maxHeight = (game->layers[0]->height-1) * game->layers[0]->textureHeight; if(destX < 0){ printf("OutOfBoundsDestination"); continue; } if(destY < 0){ printf("OutOfBoundsDestination"); continue; } if(destX > maxWidth){ printf("OutOfBoundsDestination"); continue; } diff --git a/IsometricMap/isometricMap.c b/IsometricMap/isometricMap.c index 7907eba..3b083ba 100644 --- a/IsometricMap/isometricMap.c +++ b/IsometricMap/isometricMap.c @@ -15,28 +15,28 @@ IsometricMap * IsometricMapInit(int layer){ //map->tileTextures[0] = LoadTexture("assets/desert.png"); //map->tileTextures[1] = LoadTexture("assets/bigtower.png"); - map->originX = 0; - map->originY = -layer * map->tileTextures[0].width / 4; - map->width = 100 * map->tileTextures[0].width; - map->height = 100 * map->tileTextures[0].height; - map->widthBounds = 100; - map->heightBounds = 100; - map->layer = layer; - + map->width = 100; + map->height = 100; map->textureWidth = map->tileTextures[0].width; map->textureHeight = map->tileTextures[0].height; + map->worldPixelWidth = map->width * map->textureWidth; + map->worldPixelWidth = map->height * map->textureHeight; + map->layer = layer; + - Tile*** tiles = (Tile***)malloc(map->widthBounds*sizeof(Tile*)); + // mallocating the twodimensional Tiles Array + Tile*** tiles = (Tile***)malloc(map->width*sizeof(Tile*)); int n = 0; - for(n=0; n < map->widthBounds; n++){ - tiles[n] = (Tile**)malloc(map->heightBounds*sizeof(Tile*)); + for(n=0; n < map->width; n++){ + tiles[n] = (Tile**)malloc(map->height*sizeof(Tile*)); } int i = 0; int j = 0; - for(i=0; i < map->widthBounds; i++){ - for(j=0; j < map->heightBounds; j++){ + for(i=0; i < map->width; i++){ + for(j=0; j < map->height; j++){ Tile *tmp = (Tile *) malloc(sizeof(Tile)); + // initially all the Tiles are "empty" tmp->textureId = -1; tmp->x = i; tmp->y = j; @@ -50,6 +50,7 @@ IsometricMap * IsometricMapInit(int layer){ return map; } +// Only works for tiles with texture width == height (and for 22.5 degree?) Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){ Vector2* offset = (Vector2 *)malloc(sizeof(Vector2)); offset->x = x * textureSize/2 - y * textureSize/2; @@ -61,11 +62,10 @@ Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){ return map->tiles[x][y]; } -// Project: Screen Coordinates -> World Coordinates void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){ - float tileWidthHalf = isometricMap->tileTextures[0].width / 2; - float tileHeightQuarter = isometricMap->tileTextures[0].height / 4; + float tileWidthHalf = isometricMap->textureWidth / 2; + float tileHeightQuarter = isometricMap->textureHeight / 4; x += camera->target.x; y += camera->target.y; @@ -73,7 +73,6 @@ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float xPos = (float) x; float yPos = (float) y; - float isoX = 0.5 * ( xPos / tileWidthHalf + yPos / tileHeightQuarter); float isoY = 0.5 * ( -xPos / tileWidthHalf + yPos / tileHeightQuarter); @@ -81,47 +80,47 @@ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, tmp->y = isoY * isometricMap->tileTextures[0].height; } -// Unproject: World Coordinates -> Screen Coordinates -void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, Vector2 *tmp){ +// Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector +void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp){ float xPos = (float) x; float yPos = (float) y; - float worldX = (xPos - yPos) / 2; - float worldY = (xPos + yPos) / 4; + float screenX = (xPos - yPos) / 2; + float screenY = (xPos + yPos) / 4; + + screenX += camera->target.x; + screenY += camera->target.y; - worldX += camera->target.x; - worldY += camera->target.y; + // z is currently implemented as z=1 equals 1 layer, z=2 would be two layers height (could be smoothed) + // TODO drift + screenY -= z *(isometricMap[0]->textureHeight/2); - tmp->x = worldX; - tmp->y = worldY; + tmp->x = screenX; + tmp->y = screenY; } -Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y){ +Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float z){ - x = (int)(x / isometricMap->tileTextures->width); - y = (int)(y / isometricMap->tileTextures->height); - - Tile *ptr = (Tile *) malloc(sizeof(Tile *)); - ptr->x = 0; - ptr->y = 0; + int layer = (int) z; - if( x < isometricMap->widthBounds && y < isometricMap->heightBounds && - x >= 0 && y >= 0 ){ - if(isometricMap->tiles[(int)x][(int)y]->textureId != -1){ - ptr->x = isometricMap->tiles[(int)x][(int)y]->x; - ptr->y = isometricMap->tiles[(int)x][(int)y]->y; - return ptr; + x = (int)(x / isometricMap[layer]->textureWidth); + y = (int)(y / isometricMap[layer]->textureHeight); + + if( x < isometricMap[layer]->width && y < isometricMap[layer]->height && x >= 0 && y >= 0 ){ + if(isometricMap[layer]->tiles[(int)x][(int)y]->textureId != -1){ + return (isometricMap[layer]->tiles[(int)x][(int)y]); } } - ptr = 0; + Tile *ptr = 0; return ptr; } Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){ Tile *ptr = (Tile *) malloc(sizeof(Tile *)); + // hardcoded layer amount int n = 9; for(n=9;n>=0;n--){ - if( tile->x < isometricMap[n]->widthBounds && tile->y < isometricMap[n]->heightBounds && + if( tile->x < isometricMap[n]->width && tile->y < isometricMap[n]->height && tile->x >= 0 && tile->y >= 0 ){ if(isometricMap[n]->tiles[tile->x][tile->y]->textureId != -1){ ptr->x = isometricMap[n]->tiles[tile->x][tile->y]->x; @@ -135,17 +134,13 @@ Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){ return ptr; } -// IsometricMapAddTile and IsometricMapChangeTextureIdOfTile pretty much do the same by now... -void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId){ - isometricMap->tiles[x][y]->textureId = textureId; - isometricMap->tiles[x][y]->x = x; - isometricMap->tiles[x][y]->y = y; - isometricMap->tiles[x][y]->z = isometricMap->layer; -} -void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){ - if( x < map->widthBounds && y < map->heightBounds && +void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id){ + if( x < map[layer]->width && y < map[layer]->height && x >= 0 && y >= 0 ){ - (map->tiles[x][y])->textureId = id; - } + (map[layer]->tiles[x][y])->textureId = id; + } + else{ + printf("WARNING: trying to change Texture of Tile which is out of bounds!\n"); + } } diff --git a/IsometricMap/isometricMap.h b/IsometricMap/isometricMap.h index 3471a1e..76265ed 100644 --- a/IsometricMap/isometricMap.h +++ b/IsometricMap/isometricMap.h @@ -4,31 +4,55 @@ #include "tile.c" typedef struct IsometricMap{ + // Array with all the needed textures for this layer Texture2D tileTextures[10]; + + // twodimensional array of all the tiles in this layer Tile ***tiles; - int originX; - int originY; - // TODO: überprüfen, ob width/height und widthBounds/heightBounds überhaupt noch unterschiedlich sind. Wenn nicht eins der beiden löschen + + // amount of tiles in x-direction int width; + + // amount of tiles in y-direction int height; - int widthBounds; - int heightBounds; + + // pixel width of a single tile texture int textureWidth; + + // pixel height of a single tile texture int textureHeight; + + // pixel width of the entire map + int worldPixelWidth; + + // pixel height of the entire map + int worldPixelHeight; + + // layer of the map int layer; } IsometricMap; +// returns pointer to IsometricMap Instance IsometricMap * IsometricMapInit(int layer); + +// For Rendering: calculates Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize); + Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile); + Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y); -// Project: Screen Coordinates -> World Coordinates + +// Project: Screen Coordinates -> World Coordinates writes result in tmp Vector +// Currently only calcing coords on layer 0 void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp); -// Unproject: World Coordinates -> Screen Coordinates -void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, Vector2 *tmp); -void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId); -void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id); -Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y); + +// Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector +void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp); + +// changes to Texture ID of tile at x y on maplayer layer +void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id); + +Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float layer); #endif diff --git a/IsometricMap/isometricRenderer.c b/IsometricMap/isometricRenderer.c index f767d27..92080e5 100644 --- a/IsometricMap/isometricRenderer.c +++ b/IsometricMap/isometricRenderer.c @@ -4,6 +4,7 @@ #include "../Input/inputHandler.h" #include #include +#include "../game.h" // @param deprecated void IsometricRendererDrawMap(IsometricRenderer *renderer, int height){ @@ -38,23 +39,20 @@ void IsometricRendererDrawMap(IsometricRenderer *renderer, int height){ } } -void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input){ +void IsometricRendererRenderIsometricMap(Game *game){ int n = 0; int i = 0; int j = 0; for(n = 0; n < 10; n++){ - for(i=0; i < map[n]->widthBounds; i++){ - for(j=0; j < map[n]->heightBounds; j++){ - if(map[n]->tiles[i][j]->textureId != -1){ - Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, map[n]->textureWidth); + for(i=0; i < game->layers[n]->width; i++){ + for(j=0; j < game->layers[n]->height; j++){ + if(game->layers[n]->tiles[i][j]->textureId != -1){ + Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, game->layers[n]->textureWidth); + offset->y -= n * (game->layers[n]->textureHeight/4); + + int textureId = game->layers[n]->tiles[i][j]->textureId; - int textureId = map[n]->tiles[i][j]->textureId; - - float x = map[n]->originX + offset->x; - float y = map[n]->originY + offset->y;// + map[n]->textureHeight - map[n]->tileTextures[textureId].height; - - - DrawTexture(map[n]->tileTextures[textureId], x, y, WHITE); + DrawTexture(game->layers[n]->tileTextures[textureId], offset->x, offset->y, WHITE); free(offset); } } diff --git a/IsometricMap/isometricRenderer.h b/IsometricMap/isometricRenderer.h index 7de86d0..d88db04 100644 --- a/IsometricMap/isometricRenderer.h +++ b/IsometricMap/isometricRenderer.h @@ -3,6 +3,7 @@ #include "raylib.h" #include "isometricMap.h" #include "../Input/inputHandler.h" +#include "../game.h" typedef struct IsometricRenderer{ Texture *texture; @@ -11,6 +12,6 @@ typedef struct IsometricRenderer{ // @param deprecated void IsometricRendererDrawMap(IsometricRenderer *renderer, int height); -void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input); +void IsometricRendererRenderIsometricMap(Game *game); #endif \ No newline at end of file diff --git a/List/list.c b/List/list.c index 08affa7..dd40c19 100644 --- a/List/list.c +++ b/List/list.c @@ -115,9 +115,9 @@ void ListActAllSprites(Game *game){ } // updating z-position - Tile *floorTile = IsometricMapGetTileFromWorldCoordinates(game->layers[0], current->data.x, current->data.y); - floorTile = IsometricMapGetMostUpperTile(game->layers, floorTile); - current->data.z = floorTile->z; + Tile *floorTile = IsometricMapGetTileFromWorldCoordinates(game->layers, current->data.x, current->data.y, 0); + Tile *topTile = IsometricMapGetMostUpperTile(game->layers, floorTile); + current->data.z = topTile->z; current = current->next; } diff --git a/README.md b/README.md index 71dbf5c..c6ada51 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ Fantasy Welt oder Realistisch? + Parser für Map-Dateien + MapEditor + IsometricMap variablen auf Duplikate prüfen ++ IsometricMap struct erstellen, das den IsometricMap(+Layer) Array speichert ++ Sprites drift too high when going up mountain ### WiP diff --git a/game.c b/game.c index edf1d14..59d0db0 100644 --- a/game.c +++ b/game.c @@ -78,32 +78,32 @@ Game *GameInit() switch (n) { case 0: - IsometricMapAddTile(((game->layers))[n], i, j, 0); + IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0); break; case 1: if (i > 35 && i < 50 && j > 45 && j < 60) { - IsometricMapAddTile(((game->layers))[n], i, j, 0); + IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0); } break; case 2: if (i > 40 && i < 44 && j > 50 && j < 54) { - IsometricMapAddTile(((game->layers))[n], i, j, 2); + IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 2); } break; } if(i == j && n == 1){ - IsometricMapAddTile(((game->layers))[n], i, j, 0); + IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0); } if(i == j && n == 2){ - IsometricMapAddTile(((game->layers))[n], i, j, 0); + IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0); } if(i == j-1 && n == 1){ - IsometricMapAddTile(((game->layers))[n], i, j, 0); + IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0); } if(i-1 == j && n == 1){ - IsometricMapAddTile(((game->layers))[n], i, j, 0); + IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0); } } } @@ -115,11 +115,7 @@ Game *GameInit() { for (j = 0; j < 20 - n * 2; j++) { - IsometricMapAddTile(((game->layers))[n], i, j, 0); - if (n == 9) - { - IsometricMapAddTile(((game->layers))[n], i, j, 2); - } + IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0); } } } diff --git a/game.h b/game.h index 3722fc5..16c8212 100644 --- a/game.h +++ b/game.h @@ -3,7 +3,6 @@ #include "raylib.h" -// So kann man die includes umgehen, also keine Circular dependencies mehr :) typedef struct Game{ Texture2D cursorTextures[2]; Texture2D worker[8]; diff --git a/game.o b/game.o new file mode 100644 index 0000000..5225dd8 Binary files /dev/null and b/game.o differ diff --git a/inputHandler.o b/inputHandler.o new file mode 100644 index 0000000..0b237e7 Binary files /dev/null and b/inputHandler.o differ diff --git a/isometricMap.o b/isometricMap.o new file mode 100644 index 0000000..dd95ea4 Binary files /dev/null and b/isometricMap.o differ diff --git a/isometricRenderer.o b/isometricRenderer.o new file mode 100644 index 0000000..94650e6 Binary files /dev/null and b/isometricRenderer.o differ diff --git a/list.o b/list.o new file mode 100644 index 0000000..1897463 Binary files /dev/null and b/list.o differ diff --git a/main.c b/main.c index 066d04c..0b635ee 100644 --- a/main.c +++ b/main.c @@ -29,10 +29,11 @@ int main(){ ClearBackground(RAYWHITE); BeginDrawing(); + //printf("ALARM\n"); + BeginMode2D(*(game->camera)); - IsometricRendererRenderIsometricMap(game->layers, game->inputHandler); - + IsometricRendererRenderIsometricMap(game); ListDrawAllSprites(game->sprites, game->layers, game->camera); EndMode2D(); diff --git a/main.o b/main.o new file mode 100644 index 0000000..d0bfc7d Binary files /dev/null and b/main.o differ diff --git a/spiel b/spiel new file mode 100755 index 0000000..c3231ab Binary files /dev/null and b/spiel differ diff --git a/sprite.c b/sprite.c index 251820f..317e203 100644 --- a/sprite.c +++ b/sprite.c @@ -28,20 +28,18 @@ void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera){ // Wir müssen beachten, dass sie nach den unprojezierten Screen-Koordinaten sortiert werden müssen. // Macht es vielleicht sinn den Sprites auch einen Vector mit ihren Screen Koordinaten zu geben? - Vector2 pos = {sprite->x, sprite->y}; - IsometricMapUnproject(map[0], camera, pos.x, pos.y, &pos); + Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2}; + IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos); // Also erst ab hier sortieren, mit den Werten aus dem pos Vector - - pos.y -= sprite->z * (map[0]->textureHeight/2); - + // nvm, Isometric map muss mit reingerechnet werden pos.x -= camera->target.x; pos.y -= camera->target.y; if(sprite->selected){ - DrawTexture(*sprite->texture, pos.x - sprite->texture->width/4, pos.y - sprite->texture->height/4, BLACK); + DrawTexture(*sprite->texture, pos.x, pos.y, BLACK); //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); } else{ - DrawTexture(*sprite->texture, pos.x - sprite->texture->width/4, pos.y - sprite->texture->height/4, WHITE); + DrawTexture(*sprite->texture, pos.x, pos.y, WHITE); } //printf("%f %f \n", sprite->x, sprite->y); } diff --git a/sprite.o b/sprite.o new file mode 100644 index 0000000..66a319c Binary files /dev/null and b/sprite.o differ diff --git a/tile.o b/tile.o new file mode 100644 index 0000000..ec204ad Binary files /dev/null and b/tile.o differ