From be7665d5c597fe0070aca0fd05e3e9dc3dd5488b Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Sun, 22 Jan 2023 23:49:11 +0100 Subject: [PATCH] Will net beschreiben, geht aber --- Entity/entity.c | 55 +++++++++++++----------- Entity/task.c | 18 +++++++- Entity/task.h | 8 ++-- Input/inputHandler.c | 13 +----- Makefile | 6 +-- MapObject/building.c | 86 +++++++++++++++++++++++++++++++++++++ MapObject/building.h | 28 ++++++++++++ MapObject/mapobject.c | 60 -------------------------- MapObject/mapobject.h | 28 ------------ Textures/animationHandler.c | 6 +++ Textures/animationHandler.h | 1 + Ui/selectable.c | 8 ++-- definitions.h | 8 ++-- game.c | 4 +- game.h | 2 +- 15 files changed, 185 insertions(+), 146 deletions(-) create mode 100644 MapObject/building.c create mode 100644 MapObject/building.h delete mode 100644 MapObject/mapobject.c delete mode 100644 MapObject/mapobject.h diff --git a/Entity/entity.c b/Entity/entity.c index fcf9d60..80a9215 100644 --- a/Entity/entity.c +++ b/Entity/entity.c @@ -3,7 +3,7 @@ #include #include #include "../Sprite/sprite.h" -#include "../MapObject/mapobject.h" +#include "../MapObject/building.h" #include "../definitions.h" #include "../Textures/textureatlas.h" @@ -18,12 +18,12 @@ Entity * EntityInit(Sprite *sprite, int profession, TextureAtlas *atlas){ new->selected = 0; new->next = 0; new->prev = 0; - new->task = TaskInit(); + new->task = TaskInit(profession); new->profession = profession; Animation ***animations = 0; - if(profession == TE_WORKER){ + if(profession == PR_BUILDER){ animations = atlas->animations[AN_WORKER]; } else{ @@ -150,40 +150,43 @@ void EntityListActAllEntities(Game *game){ } else{ - /* if(current->profession == PR_BUILDER){ if(current->task->target == 0){ // Prüft, ob eine Baustelle existiert - MapObject *currentMO = game->mapObjects->head; - while(currentMO != 0){ - if(currentMO->id == MO_Baustelle){ + Building *currentBU = game->buildings->head; + while(currentBU != 0){ + if(currentBU->isBaustelle){ current->hasDestination = 1; - current->destX = currentMO->sprite->x; - current->destY = currentMO->sprite->y; - current->task->target = currentMO; + current->destX = currentBU->sprite->x; + current->destY = currentBU->sprite->y; + + current->task->target = currentBU; + current->task->progress = 0; break; } - currentMO = currentMO->next; + currentBU = currentBU->next; } } else{ // Is beim target angekommen - /* - MapObject *obj = current->task->target; - printf("Hier\n"); - Sprite *new = SpriteCreate(game->textures, 2, obj->sprite->x, obj->sprite->y); - SpriteListRemove(game->sprites, obj->sprite); - SpriteListInsert(game->sprites, new); - free(obj->sprite); - MapObject *newObject = MapObjectInit(new, MO_Building); - MapObjectListRemove(game->mapObjects, obj); - MapObjectListInsert(game->mapObjects, newObject); - current->task->target = 0; - free(obj); - */ - //} + if(current->task->progress == 0){ + // Angekommen, noch nicht mit arbeiten begonnen + AnimationChangeType(current->animationHandler, AN_ENTITY_ARBEITEN); + current->task->progress += 0.01; + } + else if(current->task->progress >= 1.0){ + // Fertig mit arbeiten, Animation zu Idle zurück + AnimationChangeType(current->animationHandler, AN_ENTITY_IDLE); + BuildingFinishConstruction(game, (Building *) current->task->target); + current->task->target = 0; + } + else{ + current->task->progress += 0.2 * GetFrameTime(); + printf("%f\n", current->task->progress); + } + } - //} + } } diff --git a/Entity/task.c b/Entity/task.c index b2f3db4..011a45f 100644 --- a/Entity/task.c +++ b/Entity/task.c @@ -1,8 +1,22 @@ #include #include "task.h" +#include "../definitions.h" -Task * TaskInit(){ +Task * TaskInit(int workerid){ Task *new = malloc(sizeof(Task)); new->target = 0; + new->progress = 0; + int repeat = 0; + + switch(workerid){ + case PR_BUILDER: + repeat = 0; + break; + default: + repeat = 0; + } + + new->repeat = repeat; + return new; -} \ No newline at end of file +} diff --git a/Entity/task.h b/Entity/task.h index 3c99985..47be11d 100644 --- a/Entity/task.h +++ b/Entity/task.h @@ -1,12 +1,12 @@ #ifndef TASK_H_ #define TASK_H_ -typedef struct Task Task; - typedef struct Task{ void *target; + float progress; + int repeat; } Task; -Task * TaskInit(); +Task * TaskInit(int workerid); -#endif \ No newline at end of file +#endif diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 775c1e4..e635f77 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -3,7 +3,6 @@ #include "../Sprite/sprite.h" #include "../IsometricMap/isometricMap.h" #include "../Entity/entity.h" -#include "../MapObject/mapobject.h" #include #include #include @@ -71,16 +70,6 @@ void mouseInput(Game *game){ IsometricMapChangeTextureIdOfTile(map, (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, 0); } - if(IsKeyPressed(KEY_G)){ - // Baustelle adden - - Sprite *new = SpriteCreate(game->textures, TE_BAUSTELLE, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); - MapObject *newObject = MapObjectInit(new, MO_BAUSTELLE); - SpriteListInsert(game->sprites, new); - MapObjectListInsert(game->mapObjects, newObject); - - } - // hardcoded layer amount float tileWidthHalf = map->tileTextures[0].width / 2; float tileHeightQuarter = map->tileTextures[0].height / 4; @@ -138,7 +127,7 @@ void mouseInput(Game *game){ else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");} else { Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); - Entity *entity = EntityInit(newSprite, TE_WORKER, game->textures); + Entity *entity = EntityInit(newSprite, PR_BUILDER, game->textures); EntityListInsert(game->entities, entity); SpriteListInsert(game->sprites, newSprite); //ListPrintForward(sprites); diff --git a/Makefile b/Makefile index 4e3ee93..3b6b35d 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 game.o textureatlas.o animation.o animationHandler.o button.o uiContainer.o debug.o mapobject.o entity.o selectable.o task.o +OBJS = main.o sprite.o inputHandler.o isometricMap.o game.o textureatlas.o animation.o animationHandler.o button.o uiContainer.o debug.o building.o entity.o selectable.o task.o spiel: $(OBJS) $(CC) -o spiel $(OBJS) $(FLAGS) @@ -41,8 +41,8 @@ debug.o: Ui/debug.c entity.o: Entity/entity.c $(CC) -c Entity/entity.c $(FLAGS) -mapobject.o: MapObject/mapobject.c - $(CC) -c MapObject/mapobject.c $(FLAGS) +building.o: MapObject/building.c + $(CC) -c MapObject/building.c $(FLAGS) selectable.o: Ui/selectable.c $(CC) -c Ui/selectable.c $(FLAGS) diff --git a/MapObject/building.c b/MapObject/building.c new file mode 100644 index 0000000..f2dd353 --- /dev/null +++ b/MapObject/building.c @@ -0,0 +1,86 @@ +#include "building.h" +#include +#include "../game.h" + +Building * BuildingInit(Game *game, int id, int x, int y){ + Sprite *newSprite = SpriteCreate(game->textures, TE_BAUSTELLE, x, y); + SpriteListInsert(game->sprites, newSprite); + + Building *new = malloc(sizeof(Building)); + + new->sprite = newSprite; + new->id = id; + new->isBaustelle = 1; + + new->next = 0; + new->prev = 0; + + return new; +} + +BuildingList * BuildingListInit(){ + BuildingList *new = malloc(sizeof(BuildingList)); + new->head = 0; + new->tail = 0; + return new; +} + +void BuildingFinishConstruction(Game *game, Building *building){ + building->isBaustelle = 0; + + int textureId = 0; + + switch(building->id){ + case BU_HOUSE: + textureId = TE_BUILDING; + break; + default: + printf("WARNING: BUILDINGFINDISHEDCONSTRADJFALKDF AUFGERUFEN MIT UNBEKANNTER ID111!!\n"); + } + + building->sprite->texture = game->textures->textures[textureId]; + + // Zur Sicherheit. Sollte eigentlich nix ändern, weil texture hmmpf früher bescheid wisse + SpriteListSpriteChanged(game->sprites, building->sprite); +} + +void BuildingListPrintForward(BuildingList *buildings){ + +} + +void BuildingListInsert(BuildingList *buildings, Building *data){ + if(buildings->head == 0){ + buildings->head = data; + buildings->tail = data; + } + else{ + buildings->tail->next = data; + data->prev = buildings->tail; + buildings->tail = data; + } +} + +void BuildingListRemove(BuildingList *buildings, Building *remove){ + if(remove == 0){ + printf("WARNING: TRIED TO REMOVE NULLPOINTER\n"); + } + else if(buildings->head == remove && buildings->tail == remove){ + buildings->head = 0; + buildings->tail = 0; + } + else if(buildings->head == remove){ + remove->next->prev = 0; + buildings->head = remove->next; + } + else if(buildings->tail == remove){ + remove->prev->next = 0; + buildings->tail = remove->prev; + } + else{ + remove->prev->next = remove->next; + remove->next->prev = remove->prev; + } + + remove->next = 0; + remove->prev = 0; +} diff --git a/MapObject/building.h b/MapObject/building.h new file mode 100644 index 0000000..e2b0ab8 --- /dev/null +++ b/MapObject/building.h @@ -0,0 +1,28 @@ +#ifndef BUILDING_H_ +#define BUILDING_H_ +#include "../Sprite/sprite.h" +#include "../game.h" + +typedef struct Building{ + Sprite *sprite; + int id; + int isBaustelle; + + struct Building *next; + struct Building *prev; +} Building; + +typedef struct BuildingList { + Building *head; + Building *tail; +} BuildingList; + +Building * BuildingInit(Game *game, int id, int x, int y); +BuildingList * BuildingListInit(); + +void BuildingFinishConstruction(Game *game, Building *building); +void BuildingListPrintForward(BuildingList *buildings); +void BuildingListInsert(BuildingList *buildings, Building *data); +void BuildingListRemove(BuildingList *buildings, Building *remove); + +#endif diff --git a/MapObject/mapobject.c b/MapObject/mapobject.c deleted file mode 100644 index 7a1b124..0000000 --- a/MapObject/mapobject.c +++ /dev/null @@ -1,60 +0,0 @@ -#include "mapobject.h" -#include - -MapObject * MapObjectInit(Sprite *sprite, int id){ - MapObject *new = malloc(sizeof(MapObject)); - - new->sprite = sprite; - new->id = id; - 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 deleted file mode 100644 index 68fd140..0000000 --- a/MapObject/mapobject.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef MAPOBJECT_H_ -#define MAPOBJECT_H_ -#include "../Sprite/sprite.h" - -typedef struct MapObject MapObject; -typedef struct MapObjectList MapObjectList; - -typedef struct MapObject{ - Sprite *sprite; - int id; - - MapObject *next; - MapObject *prev; -} MapObject; - -typedef struct MapObjectList{ - MapObject *head; - MapObject *tail; -} MapObjectList; - -MapObject * MapObjectInit(Sprite *sprite, int id); -MapObjectList * MapObjectListInit(); - -void MapObjectListPrintForward(MapObjectList *mapObjects); -void MapObjectListInsert(MapObjectList *mapObjects, MapObject *data); -void MapObjectListRemove(MapObjectList *mapObjects, MapObject *remove); - -#endif diff --git a/Textures/animationHandler.c b/Textures/animationHandler.c index 5226e9d..4bbd3a7 100644 --- a/Textures/animationHandler.c +++ b/Textures/animationHandler.c @@ -48,6 +48,12 @@ void AnimationChangeAnimation(AnimationHandler *animationHandler, int animationT } } +void AnimationChangeType(AnimationHandler *animationHandler, int animationType){ + if(animationHandler->currentType != animationType){ + animationHandler->currentType = animationType; + AnimationReset(animationHandler); + } +} void AnimationChangeDirection(AnimationHandler *animationHandler, int direction){ if(animationHandler->currentDirection != direction){ diff --git a/Textures/animationHandler.h b/Textures/animationHandler.h index 742f876..a33a8b6 100644 --- a/Textures/animationHandler.h +++ b/Textures/animationHandler.h @@ -20,6 +20,7 @@ AnimationHandler * AnimationHandlerInit(Animation ***animations, Texture2D **spr void AnimationUpdate(AnimationHandler *animationHandler); void AnimationReset(AnimationHandler *animationHandler); void AnimationChangeAnimation(AnimationHandler *animationHandler, int animationType, int direction); +void AnimationChangeType(AnimationHandler *animationHandler, int animationType); void AnimationChangeDirection(AnimationHandler *animationHandler, int direction); #endif diff --git a/Ui/selectable.c b/Ui/selectable.c index 7d2d2e8..dc69930 100644 --- a/Ui/selectable.c +++ b/Ui/selectable.c @@ -3,7 +3,7 @@ #include #include #include "../definitions.h" -#include "../MapObject/mapobject.h" +#include "../MapObject/building.h" #include "../Sprite/sprite.h" #include "../Input/inputHandler.h" @@ -44,10 +44,8 @@ void SelectableExecuteSelectable(Selectable *selectable, Game * game){ // Oder das selectable wird abgewählt indem seine ID auf default zurückgesetzt wird // Sonst kann das hier jeden Frame passieren. ID wird nicht automatisch zurückgesetzt if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)){ - Sprite *new = SpriteCreate(game->textures, TE_BAUSTELLE, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); - MapObject *newObject = MapObjectInit(new, MO_BAUSTELLE); - SpriteListInsert(game->sprites, new); - MapObjectListInsert(game->mapObjects, newObject); + Building *newObject = BuildingInit(game, BU_HOUSE, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); + BuildingListInsert(game->buildings, newObject); selectable->state = SELECTABLE_STATE_DEFAULT; } diff --git a/definitions.h b/definitions.h index 1ba8c32..d7fbd53 100644 --- a/definitions.h +++ b/definitions.h @@ -31,9 +31,11 @@ #define AN_ENTITY_ARBEITEN 1 #define AN_ENTITY_DIE 2 -// Definitions for mapobjects -#define MO_BAUSTELLE 0 -#define MO_BUILDING 1 +// Definitions for buildings +#define BU_HOUSE 0 + +// Defintions for professions +#define PR_BUILDER 0 // Definitions for Screen / View / Ui Stuff diff --git a/game.c b/game.c index 9974b8b..e52438a 100644 --- a/game.c +++ b/game.c @@ -7,7 +7,7 @@ #include "Textures/textureatlas.h" #include "stdio.h" #include "Entity/entity.h" -#include "MapObject/mapobject.h" +#include "MapObject/building.h" #include "Ui/uiContainer.h" // returns pointer to new Game instance @@ -42,7 +42,7 @@ Game *GameInit() game->sprites = SpriteListInit(); game->entities = EntityListInit(); - game->mapObjects = MapObjectListInit(); + game->buildings = BuildingListInit(); 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 diff --git a/game.h b/game.h index 5bad9bd..df802c9 100644 --- a/game.h +++ b/game.h @@ -13,7 +13,7 @@ typedef struct Game{ struct InputHandler *inputHandler; struct Camera2D *camera; struct IsometricMap *map; - struct MapObjectList *mapObjects; + struct BuildingList *buildings; struct EntityList *entities; struct UiContainer *UiContainers[SCREEN_AMOUNT]; int screen;