diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 02720f3..269ea3d 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -122,8 +122,16 @@ void mouseInput(Game *game){ float height = GetMousePosition().y - inputHandler->rectStart.y; // Add Sprite - if(width + height <= 1){ - ListInsertBack(sprites, SpriteCreate(texture, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); + 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; + 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");} + else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");} + else { + ListInsertBack(sprites, SpriteCreate(texture, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); + } } // Berechnung, welche Sprites ausgewählt wurden, scuffed @@ -167,14 +175,14 @@ void mouseInput(Game *game){ while (current != 0){ if(current->data.selected){ current->data.hasDestination = 1; - float destX = inputHandler->cursorWorldPos.x; - float destY = inputHandler->cursorWorldPos.y; - if(destX < 0){ destX = 0; } - if(destY < 0){ destY = 0; } + 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; - if(destX > maxWidth){ destX = maxWidth; } - if(destY > maxHeight){ destY = maxHeight; } + if(destX < 0){ printf("OutOfBoundsDestination"); continue; } + if(destY < 0){ printf("OutOfBoundsDestination"); continue; } + if(destX > maxWidth){ printf("OutOfBoundsDestination"); continue; } + if(destY > maxHeight){ printf("OutOfBoundsDestination"); continue; } current->data.destX = destX; current->data.destY = destY; diff --git a/IsometricMap/isometricMap.c b/IsometricMap/isometricMap.c index fe8307d..7907eba 100644 --- a/IsometricMap/isometricMap.c +++ b/IsometricMap/isometricMap.c @@ -40,6 +40,7 @@ IsometricMap * IsometricMapInit(int layer){ tmp->textureId = -1; tmp->x = i; tmp->y = j; + tmp->z = layer; tiles[i][j] = tmp; } } @@ -116,11 +117,30 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float return ptr; } +Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){ + Tile *ptr = (Tile *) malloc(sizeof(Tile *)); + int n = 9; + for(n=9;n>=0;n--){ + if( tile->x < isometricMap[n]->widthBounds && tile->y < isometricMap[n]->heightBounds && + 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; + ptr->y = isometricMap[n]->tiles[tile->x][tile->y]->y; + ptr->z = isometricMap[n]->tiles[tile->x][tile->y]->z; + return ptr; + } + } + } + ptr = 0; + 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]->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 && diff --git a/IsometricMap/isometricMap.h b/IsometricMap/isometricMap.h index 2a4c8fb..3471a1e 100644 --- a/IsometricMap/isometricMap.h +++ b/IsometricMap/isometricMap.h @@ -20,6 +20,7 @@ typedef struct IsometricMap{ IsometricMap * IsometricMapInit(int layer); 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 void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp); diff --git a/IsometricMap/isometricRenderer.c b/IsometricMap/isometricRenderer.c index 3e7f706..f767d27 100644 --- a/IsometricMap/isometricRenderer.c +++ b/IsometricMap/isometricRenderer.c @@ -51,7 +51,7 @@ void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input 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; + float y = map[n]->originY + offset->y;// + map[n]->textureHeight - map[n]->tileTextures[textureId].height; DrawTexture(map[n]->tileTextures[textureId], x, y, WHITE); diff --git a/IsometricMap/tile.h b/IsometricMap/tile.h index 62c052b..b532af8 100644 --- a/IsometricMap/tile.h +++ b/IsometricMap/tile.h @@ -6,6 +6,7 @@ typedef struct Tile{ int textureId; int x; int y; + int z; } Tile; #endif \ No newline at end of file diff --git a/List/list.c b/List/list.c index 3e480e9..08affa7 100644 --- a/List/list.c +++ b/List/list.c @@ -18,7 +18,7 @@ void ListPrintForward(List *list){ printf("\n[\n"); while(current != 0){ - printf("(%f | %f),\n", current->data.x, current->data.y); + //printf("(%f | %f),\n", current->data.x, current->data.y); current = current->next; } printf("]\n"); @@ -69,7 +69,7 @@ List * ListInit(){ return newList; } -void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera){ +void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){ Node *current = list->head; while(current != 0){ @@ -105,7 +105,7 @@ void ListActAllSprites(Game *game){ // Change sprite according to direction Vector2 nullvektor = {0,0}; float f = Vector2Angle(movement, nullvektor); - printf("Angle: %f\n", f); + //printf("Angle: %f\n", f); f /= 3.14; f *= 3.5; f += 3.5; @@ -114,6 +114,11 @@ 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; + current = current->next; } diff --git a/List/list.h b/List/list.h index be3263b..a7e80f9 100644 --- a/List/list.h +++ b/List/list.h @@ -29,7 +29,7 @@ void ListPrintForward(List *list); void ListInsertFront(List *list, Sprite *data); void ListInsertBack(List *list, Sprite *data); List * ListInit(); -void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera); +void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera); void ListActAllSprites(Game *game); #endif diff --git a/game.o b/game.o new file mode 100644 index 0000000..0af0ca5 Binary files /dev/null and b/game.o differ diff --git a/inputHandler.o b/inputHandler.o new file mode 100644 index 0000000..2f95cbd Binary files /dev/null and b/inputHandler.o differ diff --git a/isometricMap.o b/isometricMap.o new file mode 100644 index 0000000..d2ea53a Binary files /dev/null and b/isometricMap.o differ diff --git a/isometricRenderer.o b/isometricRenderer.o new file mode 100644 index 0000000..77a4902 Binary files /dev/null and b/isometricRenderer.o differ diff --git a/list.o b/list.o new file mode 100644 index 0000000..3c4a02e Binary files /dev/null and b/list.o differ diff --git a/main.c b/main.c index a4689fc..066d04c 100644 --- a/main.c +++ b/main.c @@ -33,7 +33,7 @@ int main(){ IsometricRendererRenderIsometricMap(game->layers, game->inputHandler); - ListDrawAllSprites(game->sprites, game->layers[0], game->camera); + ListDrawAllSprites(game->sprites, game->layers, game->camera); EndMode2D(); diff --git a/main.o b/main.o new file mode 100644 index 0000000..3bf089f Binary files /dev/null and b/main.o differ diff --git a/spiel b/spiel new file mode 100755 index 0000000..27245dc Binary files /dev/null and b/spiel differ diff --git a/sprite.c b/sprite.c index 6c31db1..251820f 100644 --- a/sprite.c +++ b/sprite.c @@ -3,12 +3,14 @@ #include #include #include "IsometricMap/isometricMap.h" +#include "IsometricMap/tile.h" void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){ if(*spriteAmount < 100){ (sprites + *spriteAmount) -> texture = texture; (sprites + *spriteAmount) -> x = x; (sprites + *spriteAmount) -> y = y; + (sprites + *spriteAmount) -> z = 0; (sprites + *spriteAmount) -> destX = x; (sprites + *spriteAmount) -> destY = y; (sprites + *spriteAmount) -> hasDestination = 0; @@ -20,15 +22,18 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in } } -void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){ +void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera){ // TODO: Nach y sortieren, bevor sie gedrawed werden // 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, camera, pos.x, pos.y, &pos); + IsometricMapUnproject(map[0], camera, pos.x, pos.y, &pos); // Also erst ab hier sortieren, mit den Werten aus dem pos Vector + + pos.y -= sprite->z * (map[0]->textureHeight/2); + pos.x -= camera->target.x; pos.y -= camera->target.y; if(sprite->selected){ @@ -38,7 +43,7 @@ void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){ else{ DrawTexture(*sprite->texture, pos.x - sprite->texture->width/4, pos.y - sprite->texture->height/4, WHITE); } - printf("%f %f \n", sprite->x, sprite->y); + //printf("%f %f \n", sprite->x, sprite->y); } void DrawSpriteToScreen(Sprite *sprite){ diff --git a/sprite.h b/sprite.h index ddc0c65..5e4702a 100644 --- a/sprite.h +++ b/sprite.h @@ -7,6 +7,7 @@ typedef struct Sprite { Texture2D *texture; float x; float y; + float z; float destX; float destY; int hasDestination; @@ -15,7 +16,7 @@ typedef struct Sprite { void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y); -void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera); +void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera); void DrawSpriteToScreen(Sprite *sprite); Sprite * SpriteCreate(Texture2D *texture, int x, int y); diff --git a/sprite.o b/sprite.o new file mode 100644 index 0000000..be4b53f 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