diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 1d87ea0..75668f4 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -113,7 +113,8 @@ void mouseInput(Game *game){ else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");} else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");} else { - ListInsertBack(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); + 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{ diff --git a/List/list.c b/List/list.c index 643deaa..d83a020 100644 --- a/List/list.c +++ b/List/list.c @@ -19,50 +19,119 @@ void ListPrintForward(List *list){ printf("\n[\n"); while(current != 0){ - //printf("(%f | %f),\n", current->data.x, current->data.y); + printf("%f,\n", current->data->depth); current = current->next; } printf("]\n"); } -void ListInsertFront(List *list, Sprite *data){ - Node *new = ListCreateNode(data); +void ListInsertBefore(List *list, Node *new, Node *current){ + new->next = current; + new->prev = current->prev; + current->prev = new; - if(list->head == 0){ + if(current == list->head){ 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{ + 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{ - list->head->prev = new; - new->next = list->head; - list->head = new; + new->next->prev = new; } } -void ListInsertBack(List *list, Sprite *data){ +void ListInsert(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; + list->head = new; + list->tail = new; } else{ - list->tail->next = new; - new->prev = list->tail; - list->tail = new; + 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)); @@ -106,13 +175,15 @@ void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){ // 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 = { diff --git a/List/list.h b/List/list.h index 668136d..714a2a9 100644 --- a/List/list.h +++ b/List/list.h @@ -26,8 +26,11 @@ Node * ListCreateNode(Sprite *data); //Print the list in order void ListPrintForward(List *list); -void ListInsertFront(List *list, Sprite *data); -void ListInsertBack(List *list, Sprite *data); +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); diff --git a/animation.o b/animation.o new file mode 100644 index 0000000..7a57d23 Binary files /dev/null and b/animation.o differ diff --git a/animationHandler.o b/animationHandler.o new file mode 100644 index 0000000..2d4298b Binary files /dev/null and b/animationHandler.o differ diff --git a/bucket.o b/bucket.o new file mode 100644 index 0000000..65b2669 Binary files /dev/null and b/bucket.o differ diff --git a/game.o b/game.o new file mode 100644 index 0000000..74ffa5f Binary files /dev/null and b/game.o differ diff --git a/inputHandler.o b/inputHandler.o new file mode 100644 index 0000000..250c380 Binary files /dev/null and b/inputHandler.o differ diff --git a/isometricMap.o b/isometricMap.o new file mode 100644 index 0000000..4358dd9 Binary files /dev/null and b/isometricMap.o differ diff --git a/list.o b/list.o new file mode 100644 index 0000000..e3bb01c Binary files /dev/null and b/list.o differ diff --git a/main.o b/main.o new file mode 100644 index 0000000..3888afc Binary files /dev/null and b/main.o differ diff --git a/spiel b/spiel new file mode 100755 index 0000000..34bd307 Binary files /dev/null and b/spiel differ diff --git a/sprite.c b/sprite.c index c3a9eb3..48d21a0 100644 --- a/sprite.c +++ b/sprite.c @@ -64,10 +64,12 @@ Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){ 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; newSprite->sortable = BucketInit(newSprite, SPRITE); diff --git a/sprite.h b/sprite.h index 0c045a0..7da288c 100644 --- a/sprite.h +++ b/sprite.h @@ -16,6 +16,7 @@ typedef struct Sprite { float destY; int hasDestination; int selected; + float depth; Bucket *sortable; } Sprite; diff --git a/sprite.o b/sprite.o new file mode 100644 index 0000000..2e2f2dd Binary files /dev/null and b/sprite.o differ diff --git a/textureatlas.o b/textureatlas.o new file mode 100644 index 0000000..d49a06b Binary files /dev/null and b/textureatlas.o differ