#include "list.h" #include #include #include "../sprite.h" #include "raylib.h" #include "raymath.h" #include "../Textures/textureIDs.h" Node * ListCreateNode(Sprite *data){ Node *new = (Node *) 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 | %f),\n", current->data.x, current->data.y); current = current->next; } printf("]\n"); } void ListInsertFront(List *list, Sprite *data){ Node *new = ListCreateNode(data); if(list->head == 0){ list->head = new; list->tail = new; } else if(list->head == list->tail){ list->head = new; list->head->next = list->tail; list->tail->prev = list->head; } else{ list->head->prev = new; new->next = list->head; list->head = new; } } void ListInsertBack(List *list, Sprite *data){ Node *new = ListCreateNode(data); if(list->head == 0){ list->head = new; list->tail = new; } else if(list->head == list->tail){ list->tail = new; list->head->next = list->tail; list->tail->prev = list->head; } else{ list->tail->next = new; new->prev = list->tail; list->tail = new; } } void ListInsertSorted(List *list, Sprite *data){ Node *new = ListCreateNode(data); int inserted = 1; Node *current = list->head; while(inserted == 1){ if(current == 0){ free(new); ListInsertBack(list, data); inserted = 0; continue; } else if(data->y <= current->data.y){ if(current == list->head){ free(new); ListInsertFront(list, data); inserted = 0; } else{ new->prev = current->prev; new->next = current; current->prev->next = new; current->prev = new; inserted = 0; } continue; } current = current->next; } } List * ListInit(){ List *newList = (List *) 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(¤t->data, map, camera); } current = current->next; } } // iterates over all Sprites in the list and does their acting (moving etc) void ListActAllSprites(Game *game){ // Sprites move towards their destination float movementSpeed = 150.0f * GetFrameTime(); Node *current = game->sprites->head; int counter = 0; while (current != 0){ 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); } } } // Sorting the sprites by y-coord, bruh if(counter > 2 && current != game->sprites->tail){ if(current->data.y < current->prev->data.y){ Node *prepre = current->prev->prev; Node *toBack = current->prev; Node *toFront = current; Node *back = current->next; prepre->next = toFront; toFront->next = toBack; toFront->prev = prepre; toBack->prev = toFront; toBack->next = back; back->prev = toBack; } } else if(counter > 2 && current == game->sprites->tail){ if(current->data.y < current->prev->data.y){ Node *prepre = current->prev->prev; Node *toBack = current->prev; Node *toFront = current; prepre->next = toFront; toFront->next = toBack; toFront->prev = prepre; toBack->prev = toFront; toBack->next = 0; game->sprites->tail = toBack; } } else if(counter == 2 && current != game->sprites->tail){ if(current->data.y < current->prev->data.y){ Node *toBack = current->prev; Node *toFront = current; Node *back = current->next; toFront->next = toBack; toBack->prev = toFront; toBack->next = back; back->prev = toBack; toFront->prev = 0; game->sprites->head = toFront; } } else if(counter == 2 && current == game->sprites->tail){ if(current->data.y < current->prev->data.y){ Node *toBack = current->prev; Node *toFront = current; toBack->prev = toFront; toBack->next = 0; toFront->next = toBack; toFront->prev = 0; game->sprites->head = toFront; game->sprites->tail = toBack; } } SpriteUpdateAnimation(¤t->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; } }