From 1976ce3f2e16eb36829ac48ffd5642127bb1df36 Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Wed, 11 Jan 2023 00:08:22 +0100 Subject: [PATCH] =?UTF-8?q?Gaaaanz=20viel=20ver=C3=A4ndert,=20es=20geht=20?= =?UTF-8?q?sogar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Entity/entity.c | 111 ++++++++++++ Entity/entity.h | 32 ++++ Input/inputHandler.c | 339 ++++++++++++++++++------------------ Input/inputHandler.h | 5 +- IsometricMap/isometricMap.c | 80 +++------ IsometricMap/isometricMap.h | 14 +- List/list.c | 257 --------------------------- List/list.h | 38 ---- Makefile | 15 +- MapObject/mapobject.c | 59 +++++++ MapObject/mapobject.h | 27 +++ Sprite/sprite.c | 270 ++++++++++++++++++++++++++++ Sprite/sprite.h | 45 +++++ assets/pinetree.png | Bin 0 -> 2155 bytes game.c | 31 ++-- game.h | 8 +- main.c | 6 +- sprite.c | 74 -------- sprite.h | 27 --- 19 files changed, 775 insertions(+), 663 deletions(-) create mode 100644 Entity/entity.c create mode 100644 Entity/entity.h delete mode 100644 List/list.c delete mode 100644 List/list.h create mode 100644 MapObject/mapobject.c create mode 100644 MapObject/mapobject.h create mode 100644 Sprite/sprite.c create mode 100644 Sprite/sprite.h create mode 100644 assets/pinetree.png delete mode 100644 sprite.c delete mode 100644 sprite.h diff --git a/Entity/entity.c b/Entity/entity.c new file mode 100644 index 0000000..62db4e5 --- /dev/null +++ b/Entity/entity.c @@ -0,0 +1,111 @@ +#include "entity.h" +#include "raymath.h" +#include +#include +#include "../Sprite/sprite.h" + +Entity * EntityInit(Sprite *sprite){ + Entity *new = malloc(sizeof(Entity)); + + new->sprite = sprite; + new->destX = 0; + new->destY = 0; + new->hasDestination = 0; + new->selected = 0; + new->next = 0; + new->prev = 0; + + return new; +} + +EntityList * EntityListInit(){ + EntityList *new = malloc(sizeof(EntityList)); + new->head = 0; + new->tail = 0; + return new; +} + +void EntityListPrintForward(EntityList *entities){ + +} + +void EntityListInsert(EntityList *entities, Entity *data){ + if(entities->head == 0){ + entities->head = data; + entities->tail = data; + } + else{ + entities->tail->next = data; + data->prev = entities->tail; + entities->tail = data; + } +} +void EntityListRemove(EntityList *entities, Entity *remove){ + if(remove == 0){ + printf("WARNING: TRIED TO REMOVE NULLPOINTER\n"); + } + else if(entities->head == remove && entities->tail == remove){ + entities->head = 0; + entities->tail = 0; + } + else if(entities->head == remove){ + remove->next->prev = 0; + entities->head = remove->next; + } + else if(entities->tail == remove){ + remove->prev->next = 0; + entities->tail = remove->prev; + } + else{ + remove->prev->next = remove->next; + remove->next->prev = remove->prev; + } + + remove->next = 0; + remove->prev = 0; +} +void EntityListActAllEntities(Game *game){ + + EntityList *entities = game->entities; + //SpriteListPrintForward(game->sprites); + // Sprites move towards their destination + float movementSpeed = 150.0f * GetFrameTime(); + Entity *current = entities->head; + + int counter = 0; + while (current != 0){ + counter ++; + if(current->hasDestination == 1){ + Vector2 movement = { + current->destX - current->sprite->x, + current->destY - current->sprite->y + }; + if(Vector2Length(movement) < movementSpeed){ + current->hasDestination = 0; + current->sprite->x = current->destX; + current->sprite->y = current->destY; + } + else{ + movement = Vector2Normalize(movement); + movement = Vector2Scale(movement, movementSpeed); + current->sprite->x += movement.x; + current->sprite->y += movement.y; + + // Change sprite according to direction + Vector2 nullvektor = {0,0}; + float angle = Vector2Angle(movement, nullvektor); + angle = angle * RAD2DEG; + angle -= 35.26; + + current->sprite->angle = angle; + + } + } + + SpriteUpdate(current->sprite); + + SpriteListSpriteChanged(game->sprites, current->sprite); + + current = current->next; + } +} diff --git a/Entity/entity.h b/Entity/entity.h new file mode 100644 index 0000000..672bfc4 --- /dev/null +++ b/Entity/entity.h @@ -0,0 +1,32 @@ +#ifndef ENTITY_H_ +#define ENTITY_H_ +#include "../Sprite/sprite.h" +#include "../game.h" + +typedef struct Entity Entity; + +typedef struct Entity{ + Sprite *sprite; + float destX; + float destY; + int hasDestination; + int selected; + + Entity *next; + Entity *prev; +} Entity; + +typedef struct EntityList{ + Entity *head; + Entity *tail; +} EntityList; + +Entity * EntityInit(Sprite *sprite); +EntityList * EntityListInit(); + +void EntityListPrintForward(EntityList *entities); +void EntityListInsert(EntityList *entities, Entity *data); +void EntityListRemove(EntityList *entities, Entity *remove); +void EntityListActAllEntities(Game *game); + +#endif diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 90c81cf..2beba3c 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -1,204 +1,201 @@ #include "inputHandler.h" #include "raylib.h" -#include "../sprite.h" +#include "../Sprite/sprite.h" #include "../IsometricMap/isometricMap.h" +#include "../Entity/entity.h" #include #include #include -#include "../List/list.h" #include "../IsometricMap/tile.h" #include "../game.h" void DrawRect(Vector2 rectStart, Vector2 *mousePosition){ - float width = GetMousePosition().x - rectStart.x; - float height = GetMousePosition().y - rectStart.y; + float width = GetMousePosition().x - rectStart.x; + float height = GetMousePosition().y - rectStart.y; - rectStart = GetRectangle(rectStart); + rectStart = GetRectangle(rectStart); - DrawRectangleLines(rectStart.x, rectStart.y, abs(width), abs(height), GREEN); + DrawRectangleLines(rectStart.x, rectStart.y, abs(width), abs(height), GREEN); } Vector2 GetRectangle(Vector2 rectStart){ - float width = GetMousePosition().x - rectStart.x; - float height = GetMousePosition().y - rectStart.y; - - if(width < 0 && height >= 0){ - width *= -1; - rectStart.x -= width; - } - else if(height < 0 && width >= 0){ - height *= -1; - rectStart.y -= height; - } - else if(height < 0 && width < 0){ - height *= -1; - width *= -1; - rectStart.x -= width; - rectStart.y -= height; - } - return rectStart; + float width = GetMousePosition().x - rectStart.x; + float height = GetMousePosition().y - rectStart.y; + + if(width < 0 && height >= 0){ + width *= -1; + rectStart.x -= width; + } + else if(height < 0 && width >= 0){ + height *= -1; + rectStart.y -= height; + } + else if(height < 0 && width < 0){ + height *= -1; + width *= -1; + rectStart.x -= width; + rectStart.y -= height; + } + return rectStart; } void mouseInput(Game *game){ - InputHandler *inputHandler = game->inputHandler; - List *sprites = game->sprites; - Camera2D *camera = game->camera; - IsometricMap **layers = game->layers; - Texture2D *texture = game->worker +4; + InputHandler *inputHandler = game->inputHandler; + EntityList *entities = game->entities; + Camera2D *camera = game->camera; + IsometricMap *map = game->map; + Texture2D *texture = game->worker +4; - inputHandler->cursorPos.x = GetMousePosition().x; - inputHandler->cursorPos.y = GetMousePosition().y; + inputHandler->cursorPos.x = GetMousePosition().x; + inputHandler->cursorPos.y = GetMousePosition().y; - // bissl Kamera Zoom - float maxZoom = 5.0f; - float minZoom = 0.2f; - if(IsKeyPressed(KEY_I)){ - if(camera->zoom < maxZoom){ - camera->zoom += 0.2f; - } + // bissl Kamera Zoom + float maxZoom = 5.0f; + float minZoom = 0.2f; + if(IsKeyPressed(KEY_I)){ + if(camera->zoom < maxZoom){ + camera->zoom += 0.2f; } - if(IsKeyPressed(KEY_K)){ - if(camera->zoom > minZoom){ - camera->zoom -= 0.2f; - } + } + if(IsKeyPressed(KEY_K)){ + if(camera->zoom > minZoom){ + camera->zoom -= 0.2f; } - // resetting last selected Tile to grass texture - if(inputHandler->selectedLayer != -1){ - IsometricMapChangeTextureIdOfTile(layers, (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, inputHandler->selectedLayer, 0); + } + // resetting last selected Tile to grass texture + if(inputHandler->selectedLayer != -1){ + IsometricMapChangeTextureIdOfTile(map, (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, 0); + } + + // hardcoded layer amount + float tileWidthHalf = map->tileTextures[0].width / 2; + float tileHeightQuarter = map->tileTextures[0].height / 4; + int mouseAdjustmentX = -tileWidthHalf; + int mouseAdjustmentY = -tileHeightQuarter; + + // Updating inputHandler->cursorWorldPos Vector2D + IsometricMapProject(map, camera, + (inputHandler->cursorPos.x / camera->zoom) + mouseAdjustmentX, + (inputHandler->cursorPos.y / camera->zoom) + mouseAdjustmentY, + &inputHandler->cursorWorldPos); + + Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(game->map, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); + + if(selectedTile != 0){ + inputHandler->selectedLayer = 0; + inputHandler->cursorWorldTile.x = selectedTile->x; + inputHandler->cursorWorldTile.y = selectedTile->y; + // setting currently selected tile to tower + 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" + inputHandler->cursorSprite->texture = (inputHandler->cursorTextures) + 1; } - - // hardcoded layer amount - int n = 0; - for(n = 0; n >= 0 ; n--){ - if(layers[n] != 0){ - float tileWidthHalf = layers[n]->tileTextures[0].width / 2; - float tileHeightQuarter = layers[n]->tileTextures[0].height / 4; - int mouseAdjustmentX = -tileWidthHalf; - int mouseAdjustmentY = -tileHeightQuarter + (tileHeightQuarter * layers[n]->layer); - - // Updating inputHandler->cursorWorldPos Vector2D - IsometricMapProject(layers[n], camera, - (inputHandler->cursorPos.x / camera->zoom) + mouseAdjustmentX, - (inputHandler->cursorPos.y / camera->zoom) + mouseAdjustmentY, - &inputHandler->cursorWorldPos); - -/*N I C E*/ 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, inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, n, 1); - break; - } - } - } - - 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" - inputHandler->cursorSprite->texture = (inputHandler->cursorTextures) + 1; + } + + if(inputHandler->pressed){ + DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos)); + } + + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + inputHandler->pressed = 0; + // Cursorsprite is changed back to normal + inputHandler->cursorSprite->texture = (inputHandler->cursorTextures); + float width = GetMousePosition().x - inputHandler->rectStart.x; + float height = GetMousePosition().y - inputHandler->rectStart.y; + + + // Add Sprite + if(abs(width) + abs(height) < 20){ + int maxWidth = (game->map->width) * game->map->textureWidth; + int maxHeight = (game->map->height) * game->map->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 { + Sprite *newSprite = SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); + Entity *entity = EntityInit(newSprite); + EntityListInsert(game->entities, entity); + SpriteListInsert(game->sprites, newSprite); + //ListPrintForward(sprites); + //ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); + } + } else{ + // Berechnung, welche Sprites ausgewählt wurden + Vector2 rect = GetRectangle(inputHandler->rectStart); + width = abs(width); + height = abs(height); + + float deltaX; + float deltaY; + Entity *current = game->entities->head; + while (current != 0){ + Vector2 currPos = {current->sprite->x + current->sprite->texture->width, current->sprite->y + current->sprite->texture->height/2}; + IsometricMapUnproject(map, camera, currPos.x, currPos.y, current->sprite->z, &currPos); + + deltaX = currPos.x - camera->target.x - (rect.x + camera->target.x); + deltaY = currPos.y - camera->target.y - (rect.y + camera->target.y); + + if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){ + current->selected = 1; } - - } - - if(inputHandler->pressed){ - DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos)); - } - - if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ - inputHandler->pressed = 0; - // Cursorsprite is changed back to normal - inputHandler->cursorSprite->texture = (inputHandler->cursorTextures); - float width = GetMousePosition().x - inputHandler->rectStart.x; - float height = GetMousePosition().y - inputHandler->rectStart.y; - - - // Add Sprite - if(abs(width) + abs(height) < 20){ - int maxWidth = (game->layers[0]->width) * game->layers[0]->textureWidth; - int maxHeight = (game->layers[0]->height) * 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 { - ListInsert(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); - //ListPrintForward(sprites); - //ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); - } - } else{ - // Berechnung, welche Sprites ausgewählt wurden - Vector2 rect = GetRectangle(inputHandler->rectStart); - width = abs(width); - height = abs(height); - - float deltaX; - float deltaY; - Node *current = sprites->head; - while (current != 0){ - 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); - - if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){ - current->data->selected = 1; - } - else{ - current->data->selected = 0; - } - - current = current->next; - } + else{ + current->selected = 0; } - } - if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){ - Node *current = sprites->head; - - while (current != 0){ - if(current->data->selected){ - current->data->hasDestination = 1; - 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\n"); goto skip; } - if(destY < 0){ printf("OutOfBoundsDestination\n"); goto skip; } - if(destX > maxWidth){ printf("OutOfBoundsDestination\n"); goto skip; } - if(destY > maxHeight){ printf("OutOfBoundsDestination\n"); goto skip; } - - current->data->destX = destX; - current->data->destY = destY; - } - - skip: current = current->next; - } + current = current->next; + } + } + } + + if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){ + Entity *current = game->entities->head; + + while (current != 0){ + if(current->selected){ + current->hasDestination = 1; + float destX = inputHandler->cursorWorldPos.x; + float destY = inputHandler->cursorWorldPos.y; + int maxWidth = (game->map->width-1) * game->map->textureWidth; + int maxHeight = (game->map->height-1) * game->map->textureHeight; + if(destX < 0){ printf("OutOfBoundsDestination\n"); goto skip; } + if(destY < 0){ printf("OutOfBoundsDestination\n"); goto skip; } + if(destX > maxWidth){ printf("OutOfBoundsDestination\n"); goto skip; } + if(destY > maxHeight){ printf("OutOfBoundsDestination\n"); goto skip; } + + current->destX = destX; + current->destY = destY; + } + +skip: current = current->next; } + } } void keyboardInput(InputHandler *inputHandler, Camera2D *camera){ - float camSpeed = 1000.0f; - if(IsKeyDown(KEY_W)){ - (*camera).target.y -= camSpeed * GetFrameTime(); - } - if(IsKeyDown(KEY_S)){ - (*camera).target.y += camSpeed * GetFrameTime(); - } - if(IsKeyDown(KEY_D)){ - (*camera).target.x += camSpeed * GetFrameTime(); - } - if(IsKeyDown(KEY_A)){ - (*camera).target.x -= camSpeed * GetFrameTime(); - } -} \ No newline at end of file + float camSpeed = 1000.0f; + if(IsKeyDown(KEY_W)){ + (*camera).target.y -= camSpeed * GetFrameTime(); + } + if(IsKeyDown(KEY_S)){ + (*camera).target.y += camSpeed * GetFrameTime(); + } + if(IsKeyDown(KEY_D)){ + (*camera).target.x += camSpeed * GetFrameTime(); + } + if(IsKeyDown(KEY_A)){ + (*camera).target.x -= camSpeed * GetFrameTime(); + } +} diff --git a/Input/inputHandler.h b/Input/inputHandler.h index bcb557e..c6c4afc 100644 --- a/Input/inputHandler.h +++ b/Input/inputHandler.h @@ -1,8 +1,7 @@ #ifndef INPUTHANDLER_H_ #define INPUTHANDLER_H_ #include "raylib.h" -#include "../sprite.h" -#include "../List/list.h" +#include "../Sprite/sprite.h" #include "../IsometricMap/isometricMap.h" #include "../game.h" @@ -25,4 +24,4 @@ void DrawRect(Vector2 rectStart, Vector2 *mousePosition); Vector2 GetRectangle(Vector2 rectStart); -#endif \ No newline at end of file +#endif diff --git a/IsometricMap/isometricMap.c b/IsometricMap/isometricMap.c index 0d4b69d..cc92c2f 100644 --- a/IsometricMap/isometricMap.c +++ b/IsometricMap/isometricMap.c @@ -4,11 +4,11 @@ #include "tile.h" #include "raymath.h" #include "raylib.h" +#include "../Sprite/sprite.h" #include "../game.h" -#include "../List/list.h" // returns pointer to IsometricMap Instance -IsometricMap * IsometricMapInit(int layer){ +IsometricMap * IsometricMapInit(){ IsometricMap* map = malloc(sizeof(IsometricMap)); map->tileTextures[0] = LoadTexture("assets/grass.png"); @@ -21,8 +21,6 @@ IsometricMap * IsometricMapInit(int layer){ map->textureHeight = map->tileTextures[0].height; map->worldPixelWidth = map->width * map->textureWidth; map->worldPixelWidth = map->height * map->textureHeight; - map->layer = layer; - // mallocating the twodimensional Tiles Array Tile*** tiles = malloc(map->width*sizeof(Tile*)); @@ -42,11 +40,9 @@ IsometricMap * IsometricMapInit(int layer){ tmp->textureId = -1; tmp->x = i; tmp->y = j; - tmp->z = layer; Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, halfTextureSize, quarterTextureSize); // the higher the layer the higher it needs to be drawed - offset->y -= layer * quarterTextureSize; tmp->offsetX = offset->x; tmp->offsetY = offset->y; free(offset); @@ -88,7 +84,7 @@ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, } // Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector -void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp){ +void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp){ float xPos = (float) x; float yPos = (float) y; @@ -100,51 +96,31 @@ void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x, // z is currently implemented as z=1 equals 1 layer, z=2 would be two layers height (could be smoothed) // hardcoded tile height - screenY -= z * 10; + //screenY -= z * 10; tmp->x = screenX; tmp->y = screenY; } // returns Tile * -> tile at coordinates x y z=layer -Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float z){ - - int layer = (int) z; +Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y){ - x = (int)(x / isometricMap[layer]->textureWidth); - y = (int)(y / isometricMap[layer]->textureHeight); + x = (int)(x / isometricMap->textureWidth); + y = (int)(y / isometricMap->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]); + if( x < isometricMap->width && y < isometricMap->height && x >= 0 && y >= 0 ){ + if(isometricMap->tiles[(int)x][(int)y]->textureId != -1){ + return (isometricMap->tiles[(int)x][(int)y]); } } - Tile *ptr = 0; - return ptr; -} - -// Gives the most upper Tile above *tile -Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){ - //Tile *ptr = (Tile *) malloc(sizeof(Tile *)); - // hardcoded layer amount - - int n = 0; - for(n=0;n>=0;n--){ - 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){ - return isometricMap[n]->tiles[tile->x][tile->y]; - } - } - } return 0; } // changes to Texture ID of tile at x y on maplayer layer -void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id){ - if( x < map[layer]->width && y < map[layer]->height && +void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){ + if( x < map->width && y < map->height && x >= 0 && y >= 0 ){ - (map[layer]->tiles[x][y])->textureId = id; + (map->tiles[x][y])->textureId = id; } else{ printf("WARNING: trying to change Texture of Tile which is out of bounds!\n"); @@ -155,39 +131,39 @@ void IsometricMapDraw(Game *game){ int windowWidth = GetScreenWidth(); int windowHeight = GetScreenHeight(); Vector2 topleft = {0, 0}; - IsometricMapProject(game->layers[0], game->camera, topleft.x, topleft.y, &topleft); + IsometricMapProject(game->map, game->camera, topleft.x, topleft.y, &topleft); Vector2 topright = {windowWidth, 0}; - IsometricMapProject(game->layers[0], game->camera, topright.x, topright.y, &topright); + IsometricMapProject(game->map, game->camera, topright.x, topright.y, &topright); Vector2 botleft = {0, windowHeight}; - IsometricMapProject(game->layers[0], game->camera, botleft.x, botleft.y, &botleft); + IsometricMapProject(game->map, game->camera, botleft.x, botleft.y, &botleft); Vector2 botright = {windowWidth, windowHeight}; - IsometricMapProject(game->layers[0], game->camera, botright.x, botright.y, &botright); + IsometricMapProject(game->map, game->camera, botright.x, botright.y, &botright); int extraTiles = 1; int n = 0; - int itmp = (int)(topleft.x / game->layers[0]->textureWidth) - extraTiles; - int jtmp = (int)(topright.y / game->layers[0]->textureHeight) - extraTiles; - int maxI = (int)(botright.x / game->layers[0]->textureWidth) + extraTiles; - int maxJ = (int)(botleft.y / game->layers[0]->textureHeight) + extraTiles; + int itmp = (int)(topleft.x / game->map->textureWidth) - extraTiles; + int jtmp = (int)(topright.y / game->map->textureHeight) - extraTiles; + int maxI = (int)(botright.x / game->map->textureWidth) + extraTiles; + int maxJ = (int)(botleft.y / game->map->textureHeight) + extraTiles; if (itmp < 0){ itmp = 0; } if (jtmp < 0){ jtmp = 0; } - if (maxI > game->layers[0]->width){ maxI = game->layers[0]->width; } - if (maxJ > game->layers[0]->height){ maxJ = game->layers[0]->height; } + if (maxI > game->map->width){ maxI = game->map->width; } + if (maxJ > game->map->height){ maxJ = game->map->height; } int i, j = 0; for (j = jtmp; j < maxJ; j++){ for (i = itmp; i < maxI; i++){ - if (game->layers[0]->tiles[i][j]->textureId == -1){ + if (game->map->tiles[i][j]->textureId == -1){ } else{ DrawTexture( - game->layers[0]->tileTextures[game->layers[n]->tiles[i][j]->textureId], - game->layers[0]->tiles[i][j]->offsetX, - game->layers[0]->tiles[i][j]->offsetY, + game->map->tileTextures[game->map->tiles[i][j]->textureId], + game->map->tiles[i][j]->offsetX, + game->map->tiles[i][j]->offsetY, WHITE); } } } - ListDrawAllSprites(game->sprites, game->layers, game->camera); + SpriteListDrawAllSprites(game->sprites, game->map, game->camera); } diff --git a/IsometricMap/isometricMap.h b/IsometricMap/isometricMap.h index 1429947..9cde1a5 100644 --- a/IsometricMap/isometricMap.h +++ b/IsometricMap/isometricMap.h @@ -28,33 +28,27 @@ typedef struct IsometricMap{ // pixel height of the entire map int worldPixelHeight; - - // layer of the map - int layer; } IsometricMap; // returns pointer to IsometricMap Instance -IsometricMap * IsometricMapInit(int layer); +IsometricMap * IsometricMapInit(); // For Rendering: calculates coordinate offset for a single tile at arrayPosition x y // Only works for tiles with texture width == height (and for 22.5 degree?) Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int halfTextureSize, int quarterTextureSize); -// Gives the most upper Tile above *tile -Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile); - // 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 writes result in tmp Vector -void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp); +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); +void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id); // returns Tile * -> tile at coordinates x y z=layer -Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float layer); +Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y); // Draws Isometric Map and Sprites in between :) void IsometricMapDraw(Game *game); diff --git a/List/list.c b/List/list.c deleted file mode 100644 index 0e7ab59..0000000 --- a/List/list.c +++ /dev/null @@ -1,257 +0,0 @@ -#include "list.h" -#include -#include -#include "../sprite.h" -#include "raylib.h" -#include "raymath.h" -#include "../Textures/textureIDs.h" - -Node * ListCreateNode(Sprite *data){ - Node *new = malloc(sizeof(Node)); - new->data = data; - new->next = 0; - new->prev = 0; - return new; -} - -void ListPrintForward(List *list){ - Node *current = list->head; - - printf("\n[\n"); - while(current != 0){ - printf("%f,\n", current->data->depth); - current = current->next; - } - printf("]\n"); -} - -void ListInsertBefore(List *list, Node *new, Node *current){ - new->next = current; - new->prev = current->prev; - current->prev = new; - - if(current == list->head){ - list->head = new; - } - else{ - new->prev->next = new; - } -} - -void ListInsertAfter(List *list, Node *new, Node *current){ - new->prev = current; - new->next = current->next; - current->next = new; - - if(current == list->tail){ - list->tail = new; - } - else{ - new->next->prev = new; - } -} - -void ListInsert(List *list, Sprite *data){ - Node *new = ListCreateNode(data); - - if(list->head == 0){ - list->head = new; - list->tail = new; - } - else{ - Node *current = list->head; - - while(current != 0){ - if(new->data->depth < current->data->depth){ - ListInsertBefore(list, new, current); - return; - } - current = current->next; - } - - ListInsertAfter(list, new, list->tail); - } -} - -void ListSpriteChanged(List *list, Node *changed){ - if(changed != list->tail && changed->data->depth > changed->next->data->depth){ - //Nach rechts - - Node *current = changed->next; - ListRemove(list, changed); - - while(current != 0){ - if(changed->data->depth < current->data->depth){ - ListInsertBefore(list, changed, current); - return; - } - current = current->next; - } - - ListInsertAfter(list, changed, list->tail); - } - else if(changed != list->head && changed->data->depth < changed->prev->data->depth){ - //Nach links - - Node *current = changed->prev; - ListRemove(list, changed); - - while(current != 0){ - if(changed->data->depth > current->data->depth){ - ListInsertAfter(list, changed, current); - return; - } - current = current->prev; - } - - ListInsertBefore(list, changed, list->head); - } -} - -void ListRemove(List *list, Node *remove){ - if(remove == 0){ - printf("WARNING: TRIED TO REMOVE NULLPOINTER\n"); - } - else if(list->head == remove && list->tail == remove){ - list->head = 0; - list->tail = 0; - } - else if(list->head == remove){ - remove->next->prev = 0; - list->head = remove->next; - } - else if(list->tail == remove){ - remove->prev->next = 0; - list->tail = remove->prev; - } - else{ - remove->prev->next = remove->next; - remove->next->prev = remove->prev; - } - - remove->next = 0; - remove->prev = 0; -} - -List * ListInit(){ - List *newList = malloc(sizeof(List)); - newList->head = 0; - newList->tail = 0; - return newList; -} - -// iterates over all Sprites in the list and draws them to the world -void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){ - Node *current = list->head; - - // Only drawing the Sprites which are within Camera view - Vector2 topleft = {0, 0}; - IsometricMapProject(map[0], camera, topleft.x, topleft.y, &topleft); - Vector2 topright = {GetScreenWidth(), 0}; - IsometricMapProject(map[0], camera, topright.x, topright.y, &topright); - Vector2 botleft = {0, GetScreenHeight()}; - IsometricMapProject(map[0], camera, botleft.x, botleft.y, &botleft); - Vector2 botright = {GetScreenWidth(), GetScreenHeight()}; - IsometricMapProject(map[0], camera, botright.x, botright.y, &botright); - int extraPixels = 0; - int itmp, jtmp, maxI, maxJ; - itmp = (int)(topleft.x) - extraPixels; - jtmp = (int)(topright.y) - extraPixels; - maxI = (int)(botright.x) + extraPixels; - maxJ = (int)(botleft.y) + extraPixels; - while(current != 0){ - // drawing some extra corner pixels - // if extraPixels == 0 you can see flickering in the corners - // Only drawing the Sprites which are within Camera view - if( current->data->x > itmp && - current->data->y > jtmp && - current->data->x < maxI && - current->data->y < maxJ){ - DrawSpriteToWorld(current->data, map, camera); - } - current = current->next; - } -} - -// iterates over all Sprites in the list and does their acting (moving etc) -void ListActAllSprites(Game *game){ - //ListPrintForward(game->sprites); - // Sprites move towards their destination - float movementSpeed = 150.0f * GetFrameTime(); - Node *current = game->sprites->head; - - int counter = 0; - while (current != 0){ - current->data->depth = current->data->x + current->data->y + current->data->z; - ListSpriteChanged(game->sprites, current); - counter ++; - if(current->data->hasDestination == 1){ - Vector2 movement = { - current->data->destX - current->data->x, - current->data->destY - current->data->y - }; - if(Vector2Length(movement) < movementSpeed){ - current->data->hasDestination = 0; - current->data->x = current->data->destX; - current->data->y = current->data->destY; - } - else{ - movement = Vector2Normalize(movement); - movement = Vector2Scale(movement, movementSpeed); - current->data->x += movement.x; - current->data->y += movement.y; - - // Change sprite according to direction - Vector2 nullvektor = {0,0}; - float angle = Vector2Angle(movement, nullvektor); - angle = angle * RAD2DEG; - angle -= 35.26; - - if(angle <= 22.5 && angle >= -22.5){ - // E - AnimationChangeAnimation(current->data->animationHandler, E); - } - else if(angle > 0 && angle <= 67.5){ - // NE - AnimationChangeAnimation(current->data->animationHandler, NE); - } - else if(angle > 0 && angle <= 112.5){ - // N - AnimationChangeAnimation(current->data->animationHandler, N); - } - else if(angle > 0 && angle <= 157.5){ - // NW - AnimationChangeAnimation(current->data->animationHandler, NW); - } - else if(angle < 0 && angle >= -67.5){ - // SE - AnimationChangeAnimation(current->data->animationHandler, SE); - } - else if(angle < 0 && angle >= -112.5){ - // S - AnimationChangeAnimation(current->data->animationHandler, S); - } - else if(angle < 0 && angle >= -157.5){ - // SW - AnimationChangeAnimation(current->data->animationHandler, SW); - } - else{ - // W - AnimationChangeAnimation(current->data->animationHandler, W); - } - - - } - } - - SpriteUpdateAnimation(current->data); - // updating z-position according to the tile the sprite stands on - 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/List/list.h b/List/list.h deleted file mode 100644 index 714a2a9..0000000 --- a/List/list.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef LIST_H_ -#define LIST_H_ -#include "../sprite.h" -#include "../IsometricMap/isometricMap.h" -#include "raylib.h" -#include "../game.h" - -typedef struct Node Node; -typedef struct List List; - -typedef struct List { - Node *head; - Node *tail; -} List; - -typedef struct Node { - Sprite *data; - - Node *next; - Node *prev; -} Node; - -//Only for internal purpose -Node * ListCreateNode(Sprite *data); - -//Print the list in order -void ListPrintForward(List *list); - -void ListInsertBefore(List *list, Node *new, Node *current); -void ListInsertAfter(List *list, Node *new, Node *current); -void ListInsert(List *list, Sprite *data); -void ListRemove(List *list, Node *remove); -void ListSpriteChanged(List *list, Node *changed); -List * ListInit(); -void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera); -void ListActAllSprites(Game *game); - -#endif diff --git a/Makefile b/Makefile index 7581793..2cd745e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc FLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -OBJS = main.o sprite.o inputHandler.o isometricMap.o list.o game.o textureatlas.o animation.o animationHandler.o button.o uiContainer.o debug.o +OBJS = main.o sprite.o inputHandler.o isometricMap.o game.o textureatlas.o animation.o animationHandler.o button.o uiContainer.o debug.o mapobject.o entity.o spiel: $(OBJS) $(CC) -o spiel $(OBJS) $(FLAGS) @@ -8,15 +8,12 @@ spiel: $(OBJS) main.o: main.c $(CC) -c main.c $(FLAGS) -sprite.o: sprite.c - $(CC) -c sprite.c $(FLAGS) +sprite.o: Sprite/sprite.c + $(CC) -c Sprite/sprite.c $(FLAGS) inputHandler.o: Input/inputHandler.c $(CC) -c Input/inputHandler.c $(FLAGS) -list.o: List/list.c - $(CC) -c List/list.c $(FLAGS) - isometricMap.o: IsometricMap/isometricMap.c $(CC) -c IsometricMap/isometricMap.c $(FLAGS) @@ -41,5 +38,11 @@ uiContainer.o: Ui/uiContainer.c debug.o: Ui/debug.c $(CC) -c Ui/debug.c $(FLAGS) +entity.o: Entity/entity.c + $(CC) -c Entity/entity.c $(FLAGS) + +mapobject.o: MapObject/mapobject.c + $(CC) -c MapObject/mapobject.c $(FLAGS) + clean: rm *.o spiel diff --git a/MapObject/mapobject.c b/MapObject/mapobject.c new file mode 100644 index 0000000..b30e3d6 --- /dev/null +++ b/MapObject/mapobject.c @@ -0,0 +1,59 @@ +#include "mapobject.h" +#include + +MapObject * MapObjectInit(Sprite *sprite){ + MapObject *new = malloc(sizeof(MapObject)); + + new->sprite = sprite; + new->next = 0; + new->prev = 0; + + return new; +} + +MapObjectList * MapObjectListInit(){ + MapObjectList *new = malloc(sizeof(MapObjectList)); + new->head = 0; + new->tail = 0; + return new; +} + +void MapObjectListPrintForward(MapObjectList *mapObjects){ + +} + +void MapObjectListInsert(MapObjectList *mapObjects, MapObject *data){ + if(mapObjects->head == 0){ + mapObjects->head = data; + mapObjects->tail = data; + } + else{ + mapObjects->tail->next = data; + data->prev = mapObjects->tail; + mapObjects->tail = data; + } +} +void MapObjectListRemove(MapObjectList *mapObjects, MapObject *remove){ + if(remove == 0){ + printf("WARNING: TRIED TO REMOVE NULLPOINTER\n"); + } + else if(mapObjects->head == remove && mapObjects->tail == remove){ + mapObjects->head = 0; + mapObjects->tail = 0; + } + else if(mapObjects->head == remove){ + remove->next->prev = 0; + mapObjects->head = remove->next; + } + else if(mapObjects->tail == remove){ + remove->prev->next = 0; + mapObjects->tail = remove->prev; + } + else{ + remove->prev->next = remove->next; + remove->next->prev = remove->prev; + } + + remove->next = 0; + remove->prev = 0; +} diff --git a/MapObject/mapobject.h b/MapObject/mapobject.h new file mode 100644 index 0000000..44a570c --- /dev/null +++ b/MapObject/mapobject.h @@ -0,0 +1,27 @@ +#ifndef MAPOBJECT_H_ +#define MAPOBJECT_H_ +#include "../Sprite/sprite.h" + +typedef struct MapObject MapObject; +typedef struct MapObjectList MapObjectList; + +typedef struct MapObject{ + Sprite *sprite; + + MapObject *next; + MapObject *prev; +} MapObject; + +typedef struct MapObjectList{ + MapObject *head; + MapObject *tail; +} MapObjectList; + +MapObject * MapObjectInit(Sprite *sprite); +MapObjectList * MapObjectListInit(); + +void MapObjectListPrintForward(MapObjectList *mapObjects); +void MapObjectListInsert(MapObjectList *mapObjects, MapObject *data); +void MapObjectListRemove(MapObjectList *mapObjects, MapObject *remove); + +#endif diff --git a/Sprite/sprite.c b/Sprite/sprite.c new file mode 100644 index 0000000..865d55a --- /dev/null +++ b/Sprite/sprite.c @@ -0,0 +1,270 @@ +#include "sprite.h" +#include "raylib.h" +#include +#include +#include "../IsometricMap/isometricMap.h" +#include "../Textures/textureIDs.h" +#include "../Textures/animationHandler.h" +#include "../Textures/animation.h" +#include "../Textures/textureatlas.h" +#include "../IsometricMap/tile.h" + +void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){ + + Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2}; + + IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos); + + pos.x -= camera->target.x; + pos.y -= camera->target.y; + /* + if(sprite->selected){ + DrawTexture(*sprite->texture, pos.x, pos.y, (Color){255, 255, 255, 200}); + //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); + } + else{ + */ + DrawTexture(*sprite->texture, pos.x, pos.y, WHITE); + //} +} + +void DrawSpriteToScreen(Sprite *sprite){ + /*if(sprite->selected){ + DrawTexture(*sprite->texture, sprite->x, sprite->y, (Color){255, 255, 255, 200}); + //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); + } + else{ + */ + DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE); + // } +} + +void SpriteUpdate(Sprite *sprite){ + + sprite->depth = sprite->x + sprite->y + sprite->z; + + if(sprite->angle <= 22.5 && sprite->angle >= -22.5){ + // E + AnimationChangeAnimation(sprite->animationHandler, E); + } + else if(sprite->angle > 0 && sprite->angle <= 67.5){ + // NE + AnimationChangeAnimation(sprite->animationHandler, NE); + } + else if(sprite->angle > 0 && sprite->angle <= 112.5){ + // N + AnimationChangeAnimation(sprite->animationHandler, N); + } + else if(sprite->angle > 0 && sprite->angle <= 157.5){ + // NW + AnimationChangeAnimation(sprite->animationHandler, NW); + } + else if(sprite->angle < 0 && sprite->angle >= -67.5){ + // SE + AnimationChangeAnimation(sprite->animationHandler, SE); + } + else if(sprite->angle < 0 && sprite->angle >= -112.5){ + // S + AnimationChangeAnimation(sprite->animationHandler, S); + } + else if(sprite->angle < 0 && sprite->angle >= -157.5){ + // SW + AnimationChangeAnimation(sprite->animationHandler, SW); + } + else{ + // W + AnimationChangeAnimation(sprite->animationHandler, W); + } + + AnimationUpdate(sprite->animationHandler); + sprite->texture = sprite->animationHandler->currentFrame->texture; +} + +Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){ + Sprite *newSprite = malloc(sizeof(Sprite)); + + //AnimationHandler create + Animation **animations = 0; + + if(textureID == worker){ + animations = atlas->workerAnimations; + } + else if(textureID == cursor){ + animations = atlas->cursorAnimation; + } + else{ + printf("\n\n\n\n\n\n\n\nSpriteCreate mit falscher ID (%d) aufgerufen oder ID nicht bekannt!!!\n\n\n\n\n\n\n\n", textureID); + } + + AnimationHandler *newHandler = AnimationHandlerInit(animations); + + newSprite->animationHandler = newHandler; + newSprite->texture = newSprite->animationHandler->currentFrame->texture; + newSprite->x = x - newSprite->texture->width / 2; + newSprite->y = y - newSprite->texture->height / 2; + newSprite->z = 0; + newSprite->depth = newSprite->x + newSprite->y; + newSprite->angle = 0; + newSprite->next = 0; + newSprite->prev = 0; + + return newSprite; +} + +void SpriteListPrintForward(SpriteList *list){ + Sprite *current = list->head; + + printf("\n[\n"); + while(current != 0){ + printf("%f,\n", current->depth); + current = current->next; + } + printf("]\n"); +} + +void SpriteListInsertBefore(SpriteList *list, Sprite *new, Sprite *current){ + new->next = current; + new->prev = current->prev; + current->prev = new; + + if(current == list->head){ + list->head = new; + } + else{ + new->prev->next = new; + } +} + +void SpriteListInsertAfter(SpriteList *list, Sprite *new, Sprite *current){ + new->prev = current; + new->next = current->next; + current->next = new; + + if(current == list->tail){ + list->tail = new; + } + else{ + new->next->prev = new; + } +} + +void SpriteListInsert(SpriteList *list, Sprite *new){ + + if(list->head == 0){ + list->head = new; + list->tail = new; + } + else{ + Sprite *current = list->head; + + while(current != 0){ + if(new->depth < current->depth){ + SpriteListInsertBefore(list, new, current); + return; + } + current = current->next; + } + + SpriteListInsertAfter(list, new, list->tail); + } +} + +void SpriteListSpriteChanged(SpriteList *list, Sprite *changed){ + if(changed != list->tail && changed->depth > changed->next->depth){ + //Nach rechts + + Sprite *current = changed->next; + SpriteListRemove(list, changed); + + while(current != 0){ + if(changed->depth < current->depth){ + SpriteListInsertBefore(list, changed, current); + return; + } + current = current->next; + } + + SpriteListInsertAfter(list, changed, list->tail); + } + else if(changed != list->head && changed->depth < changed->prev->depth){ + //Nach links + + Sprite *current = changed->prev; + SpriteListRemove(list, changed); + + while(current != 0){ + if(changed->depth > current->depth){ + SpriteListInsertAfter(list, changed, current); + return; + } + current = current->prev; + } + + SpriteListInsertBefore(list, changed, list->head); + } +} + +void SpriteListRemove(SpriteList *list, Sprite *remove){ + if(remove == 0){ + printf("WARNING: TRIED TO REMOVE NULLPOINTER\n"); + } + else if(list->head == remove && list->tail == remove){ + list->head = 0; + list->tail = 0; + } + else if(list->head == remove){ + remove->next->prev = 0; + list->head = remove->next; + } + else if(list->tail == remove){ + remove->prev->next = 0; + list->tail = remove->prev; + } + else{ + remove->prev->next = remove->next; + remove->next->prev = remove->prev; + } + + remove->next = 0; + remove->prev = 0; +} + +SpriteList * SpriteListInit(){ + SpriteList *newSpriteList = malloc(sizeof(SpriteList)); + newSpriteList->head = 0; + newSpriteList->tail = 0; + return newSpriteList; +} + +// iterates over all Sprites in the list and draws them to the world +void SpriteListDrawAllSprites(SpriteList *list, IsometricMap *map, Camera2D *camera){ + Sprite *current = list->head; + + // Only drawing the Sprites which are within Camera view + Vector2 topleft = {0, 0}; + IsometricMapProject(map, camera, topleft.x, topleft.y, &topleft); + Vector2 topright = {GetScreenWidth(), 0}; + IsometricMapProject(map, camera, topright.x, topright.y, &topright); + Vector2 botleft = {0, GetScreenHeight()}; + IsometricMapProject(map, camera, botleft.x, botleft.y, &botleft); + Vector2 botright = {GetScreenWidth(), GetScreenHeight()}; + IsometricMapProject(map, camera, botright.x, botright.y, &botright); + int extraPixels = 0; + int itmp, jtmp, maxI, maxJ; + itmp = (int)(topleft.x) - extraPixels; + jtmp = (int)(topright.y) - extraPixels; + maxI = (int)(botright.x) + extraPixels; + maxJ = (int)(botleft.y) + extraPixels; + while(current != 0){ + // drawing some extra corner pixels + // if extraPixels == 0 you can see flickering in the corners + // Only drawing the Sprites which are within Camera view + if( current->x > itmp && + current->y > jtmp && + current->x < maxI && + current->y < maxJ){ + DrawSpriteToWorld(current, map, camera); + } + current = current->next; + } +} diff --git a/Sprite/sprite.h b/Sprite/sprite.h new file mode 100644 index 0000000..a4dbf1c --- /dev/null +++ b/Sprite/sprite.h @@ -0,0 +1,45 @@ +#ifndef SPRITE_H_ +#define SPRITE_H_ +#include "raylib.h" +#include "../IsometricMap/isometricMap.h" +#include "../Textures/animationHandler.h" +#include "../Textures/textureatlas.h" + +typedef struct Sprite Sprite; +typedef struct SpriteList SpriteList; + +typedef struct Sprite { + AnimationHandler *animationHandler; + Texture2D *texture; + float x; + float y; + float z; + float depth; + float angle; + + Sprite *next; + Sprite *prev; +} Sprite; + +typedef struct SpriteList { + Sprite *head; + Sprite *tail; +} SpriteList; + +void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera); +void DrawSpriteToScreen(Sprite *sprite); +void SpriteUpdate(Sprite *sprite); + +Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y); + +//Print the list in order +void SpriteListPrintForward(SpriteList *list); +void SpriteListInsertBefore(SpriteList *list, Sprite *new, Sprite *current); +void SpriteListInsertAfter(SpriteList *list, Sprite *new, Sprite *current); +void SpriteListInsert(SpriteList *list, Sprite *new); +void SpriteListRemove(SpriteList *list, Sprite *remove); +void SpriteListSpriteChanged(SpriteList *list, Sprite *changed); +SpriteList * SpriteListInit(); +void SpriteListDrawAllSprites(SpriteList *list, IsometricMap *map, Camera2D *camera); + +#endif diff --git a/assets/pinetree.png b/assets/pinetree.png new file mode 100644 index 0000000000000000000000000000000000000000..dc846589f5e4a62adadcd85a26ef3a417eeddbd2 GIT binary patch literal 2155 zcmV-x2$c7UP)EX>4Tx04R}tkv&MmP!xqvTct%R4rUN>$WWauh>D1lR-p(LLaorMgUL((ph-iL z;^HW{799LptU9+0Yt2!cN#t}afBE>hxsNufoI7as59yn7Ds-3J646{cBTV}PdH zW;&S=v$<6<{E7e~5D>zQ%q(M0l2Y)kuY2mIx{LEH|Gqz~SIt`t2#Cb9%rI@@4dSUy z+u*!UEU=QS5}y-~nshu?YAB-u8*$ooQY@tDJmKRXbo~;!6mpfp z$gzMbG{~+W{11N5)+$Vldr8p*(EZ{#AHzU+7iiWU=lj@knkPW$8MxBh{%Ql5{v^HL z)?!CM-!^b@-PV*n;Bp5Tcrs*DcBK%lV4(oKpV2qvfc{&cXU&^i>l~*KK!#?Ox&aOj zfsrC*ulu|^(mA((ds_4R0a7$_nqSz#cK`qY24YJ`L;z|40000g!PJug000SaNLh0L z01FcU01FcV0GgZ_00007bV*G`2j>9@6(SC0riyt0000?uMObu0Z*6U5Zgc=ca%Ew3 zWn>_CX>@2HM@dakSAh-}000I;Nkl-EV3y;tN-_Ip zXLY*E*>}N-VA1Ta_r3SM@4Yt?@PGfvW3he6`@uuO=f?uy%z^o_z$B92#({@Y|0oP_ z^bc`n@U>BM?kADNMjIq+f?3nxUuA#+PEJQXaCYcxm=IqY{jTcSiAf}ZV}k_P`p!NY z17=MFRy{`zz=8Wrd`T+q?ZEtn*qVa6Xm1f^vL+zL_EA#c=hT#h(g>W)CDNoqo&%Fp zz!U^T7X05 z2d3X+s}`X(UJw8v&w=R_Zf&%^zB&z&tjUSg6a-VY@BDr{&fqTk8OW4`<@3l1%@hPV z7BB@NIk!>sH$SfyVbAH?(s4IMF}BkQuzVglngJl5kFe1O-kc+2$>0X zPAYq^yr_|Hfca9ul!SZ($Vl7}_TDm-#}?eFzOeZ;8kkOSLtO4XCEPWs1g0Pt_3E_K zf+-2*vDo@fJn_MmB&hEKJe>`@d|ncs+SZSu43Ou595w&|8a4Rz;Tl63;Mz3!SOyqg zTd=1id!Pa2SYUV<01fc*RiA;tr$#M8N3k5+ymBr%0;4Uh%tmSK{d{AYlS;V5x5qYD zKZV=98ej$hKrO;vfA=Y5Fu>$kE@Pmlrj7fX!md*6asc;=L5`&>1U2 zmRd?nBYLIBb5g04`d{GKl<^c>}ugwAT$IE*Hk!lq<8=|->LYMn@MJFtqf9bz%I0RYTmKuADmHHxyMY8Kl;A|v4#;1TRfvvp>pO|N8q7MXIL)%`79 ziop}a*y<~wdMj|%G&nl4P3BNGh-M)~eT_repl$@DXz&g$MZoz=)v^fb~NG`a(iX4Xcsrk*qWKCgQWh)iJl zqi`?r!z@lV63s$)KAl>f?3NfmXA0`=oP6i|oiN7MSFP_U8}=h(hS(VZy>%$NZpcpf zy#}ep4t7(p8%37(!IL%h08L@97o~6x6=l~Abyh=;MNj=Q5^n4Mgcte>xc=O@s9CiD zFgyrwv=~BXwW|)D)i{U*-oI-g>T4V=hWL;IY7!v7xW++17#$7qZcVs+1?Z-M(d2NL z0l^CuSw}`O?NxlT28`}D7)2=%&33l&nxHc)#QY_YgCJJ|SlN53Y^Uf*;0&GyRD|tx zB#?}816X7N;i|jN`q;N-X80O^{0dVgupEcbhH#eCAOL`qIC2rNmt0Z|E;8Y|n#kuoaHodd&06Z$SZ`0VU3rbc-Nv%J_Sa$`@`#`9^BZL=M}jWFcW3k8TOkJ zN5*Z|kGg2h`Wp`gAd&*w(CxpBmg5j7afIne^hD_ooZS-jsrH>1E1N>LF&zo0kwheg z6JXDQvz!Ln5YnP@Oswio(cBdo+-SliBUEqNKPNH^$We1Ca@n!icNy6GgK;$#Tc57H zvLZb%>b<+-(K9$ldcQGSCL<|a&cFC?x|4ahRU?VCsGKpboV+3@tn;Go0rI&|_oIBc hTK;LxW%i%{{0-l{dvDMu)vEvi002ovPDHLkV1jWh^F#mu literal 0 HcmV?d00001 diff --git a/game.c b/game.c index 5a45c0d..d67f67c 100644 --- a/game.c +++ b/game.c @@ -1,13 +1,14 @@ #include "game.h" #include "stdlib.h" #include "raylib.h" -#include "sprite.h" -#include "List/list.h" +#include "Sprite/sprite.h" #include "Input/inputHandler.h" #include "IsometricMap/isometricMap.h" #include "Textures/textureatlas.h" #include "stdio.h" #include "Ui/screenIDs.h" +#include "Entity/entity.h" +#include "MapObject/mapobject.h" // returns pointer to new Game instance Game *GameInit() @@ -41,27 +42,19 @@ Game *GameInit() game->camera->offset.x = 0; game->camera->offset.y = 0; - game->sprites = ListInit(); + game->sprites = SpriteListInit(); + game->entities = EntityListInit(); - game->layers = malloc(1 * sizeof(IsometricMap *)); - //das du es weißt man kann wenn du nicht auf hört was machen und dann bist du leise es gibt was das nett sich rufmord - - // Test Layers --- - int n = 0; - int i = 0; - int j = 0; + game->mapObjects = MapObjectListInit(); - for (n = 0; n < 1; n++) - { - ((game->layers))[n] = IsometricMapInit(n); - } - - n = 0; - for (i = 0; i < game->layers[0]->width; i++) + game->map = IsometricMapInit(); + //das du es weißt man kann wenn du nicht auf hört was machen und dann bist du leise es gibt was das nett sich rufmord + int i,j; + for (i = 0; i < game->map->width; i++) { - for (j = 0; j < game->layers[0]->height; j++) + for (j = 0; j < game->map->height; j++) { - IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0); + IsometricMapChangeTextureIdOfTile(game->map, i, j, 0); } } // ------- diff --git a/game.h b/game.h index ce274d9..c8b03b0 100644 --- a/game.h +++ b/game.h @@ -7,14 +7,16 @@ typedef struct Game{ Texture2D worker[8]; struct TextureAtlas *textures; struct Sprite *cursorSprite; - struct List *sprites; + struct SpriteList *sprites; struct InputHandler *inputHandler; struct Camera2D *camera; - struct IsometricMap **layers; + struct IsometricMap *map; + struct MapObjectList *mapObjects; + struct EntityList *entities; int screen; } Game; // returns pointer to new Game instance Game * GameInit(); -#endif \ No newline at end of file +#endif diff --git a/main.c b/main.c index 72ba7b5..5388ce6 100644 --- a/main.c +++ b/main.c @@ -2,10 +2,10 @@ #include "string.h" #include #include -#include "sprite.h" +#include "Sprite/sprite.h" #include "Input/inputHandler.h" +#include "Entity/entity.h" #include "raymath.h" -#include "List/list.h" #include "IsometricMap/isometricMap.h" #include "game.h" #include "Ui/screenIDs.h" @@ -53,7 +53,7 @@ int main(){ case SCREEN_GAME: // Updating Sprites - ListActAllSprites(game); + EntityListActAllEntities(game); // Drawing IsometricMap BeginMode2D(*(game->camera)); // Sorgt dafür, dass die Kameraposition beachtet wird diff --git a/sprite.c b/sprite.c deleted file mode 100644 index 3d2b0c7..0000000 --- a/sprite.c +++ /dev/null @@ -1,74 +0,0 @@ -#include "sprite.h" -#include "raylib.h" -#include -#include -#include "IsometricMap/isometricMap.h" -#include "Textures/textureIDs.h" -#include "Textures/animationHandler.h" -#include "Textures/animation.h" -#include "Textures/textureatlas.h" -#include "IsometricMap/tile.h" - -void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera){ - - Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2}; - - IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos); - - pos.x -= camera->target.x; - pos.y -= camera->target.y; - if(sprite->selected){ - DrawTexture(*sprite->texture, pos.x, pos.y, (Color){255, 255, 255, 200}); - //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); - } - else{ - DrawTexture(*sprite->texture, pos.x, pos.y, WHITE); - } -} - -void DrawSpriteToScreen(Sprite *sprite){ - if(sprite->selected){ - DrawTexture(*sprite->texture, sprite->x, sprite->y, (Color){255, 255, 255, 200}); - //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); - } - else{ - DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE); - } -} - -void SpriteUpdateAnimation(Sprite *sprite){ - AnimationUpdate(sprite->animationHandler); - sprite->texture = sprite->animationHandler->currentFrame->texture; -} - -Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){ - Sprite *newSprite = malloc(sizeof(Sprite)); - - //AnimationHandler create - Animation **animations = 0; - - if(textureID == worker){ - animations = atlas->workerAnimations; - } - else if(textureID == cursor){ - animations = atlas->cursorAnimation; - } - else{ - printf("\n\n\n\n\n\n\n\nSpriteCreate mit falscher ID (%d) aufgerufen oder ID nicht bekannt!!!\n\n\n\n\n\n\n\n", textureID); - } - - AnimationHandler *newHandler = AnimationHandlerInit(animations); - - newSprite->animationHandler = newHandler; - newSprite->texture = newSprite->animationHandler->currentFrame->texture; - newSprite->x = x - newSprite->texture->width / 2; - newSprite->y = y - newSprite->texture->height / 2; - newSprite->z = 0; - newSprite->destX = x; - newSprite->destY = y; - newSprite->hasDestination = 0; - newSprite->selected = 0; - newSprite->depth = newSprite->x + newSprite->y; - - return newSprite; -} diff --git a/sprite.h b/sprite.h deleted file mode 100644 index 6dc36ec..0000000 --- a/sprite.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef SPRITE_H_ -#define SPRITE_H_ -#include "raylib.h" -#include "IsometricMap/isometricMap.h" -#include "Textures/animationHandler.h" -#include "Textures/textureatlas.h" - -typedef struct Sprite { - AnimationHandler *animationHandler; - Texture2D *texture; - float x; - float y; - float z; - float destX; - float destY; - int hasDestination; - int selected; - float depth; -} Sprite; - -void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera); -void DrawSpriteToScreen(Sprite *sprite); -void SpriteUpdateAnimation(Sprite *sprite); - -Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y); - -#endif