diff --git a/Entity/entity.c b/Entity/entity.c index 80a9215..0638bc6 100644 --- a/Entity/entity.c +++ b/Entity/entity.c @@ -6,6 +6,7 @@ #include "../MapObject/building.h" #include "../definitions.h" #include "../Textures/textureatlas.h" +#include "entityacts.h" Entity * EntityInit(Sprite *sprite, int profession, TextureAtlas *atlas){ Entity *new = malloc(sizeof(Entity)); @@ -21,6 +22,14 @@ Entity * EntityInit(Sprite *sprite, int profession, TextureAtlas *atlas){ new->task = TaskInit(profession); new->profession = profession; + switch(profession){ + case PR_BUILDER: + new->act = BuilderAct; + break; + default: + printf("WARNING: ENTITYINIT MIT FALSCHER ID AUFGERUFEN!\n"); + } + Animation ***animations = 0; if(profession == PR_BUILDER){ @@ -42,6 +51,67 @@ EntityList * EntityListInit(){ return new; } +void EntityMoveToDestination(Entity *entity){ + float movementSpeed = 150.0f * GetFrameTime(); + + if(entity->hasDestination == 1){ + Vector2 movement = { + entity->destX - entity->sprite->x, + entity->destY - entity->sprite->y + }; + if(Vector2Length(movement) < movementSpeed){ + entity->hasDestination = 0; + entity->sprite->x = entity->destX; + entity->sprite->y = entity->destY; + } + else{ + movement = Vector2Normalize(movement); + movement = Vector2Scale(movement, movementSpeed); + entity->sprite->x += movement.x; + entity->sprite->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 + AnimationChangeDirection(entity->animationHandler, E); + } + else if(angle > 0 && angle <= 67.5){ + // NE + AnimationChangeDirection(entity->animationHandler, NE); + } + else if(angle > 0 && angle <= 112.5){ + // N + AnimationChangeDirection(entity->animationHandler, N); + } + else if(angle > 0 && angle <= 157.5){ + // NW + AnimationChangeDirection(entity->animationHandler, NW); + } + else if(angle < 0 && angle >= -67.5){ + // SE + AnimationChangeDirection(entity->animationHandler, SE); + } + else if(angle < 0 && angle >= -112.5){ + // S + AnimationChangeDirection(entity->animationHandler, S); + } + else if(angle < 0 && angle >= -157.5){ + // SW + AnimationChangeDirection(entity->animationHandler, SW); + } + else{ + // W + AnimationChangeDirection(entity->animationHandler, W); + } + } + } +} + void EntityListPrintForward(EntityList *entities){ } @@ -83,112 +153,10 @@ void EntityListRemove(EntityList *entities, Entity *remove){ } 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; + Entity *current = game->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; - - if(angle <= 22.5 && angle >= -22.5){ - // E - AnimationChangeDirection(current->animationHandler, E); - } - else if(angle > 0 && angle <= 67.5){ - // NE - AnimationChangeDirection(current->animationHandler, NE); - } - else if(angle > 0 && angle <= 112.5){ - // N - AnimationChangeDirection(current->animationHandler, N); - } - else if(angle > 0 && angle <= 157.5){ - // NW - AnimationChangeDirection(current->animationHandler, NW); - } - else if(angle < 0 && angle >= -67.5){ - // SE - AnimationChangeDirection(current->animationHandler, SE); - } - else if(angle < 0 && angle >= -112.5){ - // S - AnimationChangeDirection(current->animationHandler, S); - } - else if(angle < 0 && angle >= -157.5){ - // SW - AnimationChangeDirection(current->animationHandler, SW); - } - else{ - // W - AnimationChangeDirection(current->animationHandler, W); - } - } - - } - else{ - if(current->profession == PR_BUILDER){ - if(current->task->target == 0){ - // Prüft, ob eine Baustelle existiert - Building *currentBU = game->buildings->head; - while(currentBU != 0){ - if(currentBU->isBaustelle){ - current->hasDestination = 1; - current->destX = currentBU->sprite->x; - current->destY = currentBU->sprite->y; - - current->task->target = currentBU; - current->task->progress = 0; - break; - } - currentBU = currentBU->next; - } - } - else{ - // Is beim target angekommen - 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); - } - } - - } - - } + current->act(game, current); AnimationUpdate(current->animationHandler); diff --git a/Entity/entity.h b/Entity/entity.h index a7f1e76..c1df302 100644 --- a/Entity/entity.h +++ b/Entity/entity.h @@ -10,14 +10,17 @@ typedef struct Entity Entity; typedef struct Entity{ Sprite *sprite; AnimationHandler *animationHandler; + float angle; float destX; float destY; int hasDestination; int selected; - Task *task; int profession; + Task *task; + void (*act)(Game *, Entity *); + Entity *next; Entity *prev; } Entity; @@ -30,6 +33,8 @@ typedef struct EntityList{ Entity * EntityInit(Sprite *sprite, int profession, TextureAtlas *atlas); EntityList * EntityListInit(); +void EntityMoveToDestination(Entity *entity); + void EntityListPrintForward(EntityList *entities); void EntityListInsert(EntityList *entities, Entity *data); void EntityListRemove(EntityList *entities, Entity *remove); diff --git a/Entity/entityacts.c b/Entity/entityacts.c new file mode 100644 index 0000000..ed1c383 --- /dev/null +++ b/Entity/entityacts.c @@ -0,0 +1,51 @@ +#include "entityacts.h" +#include "../Textures/animationHandler.h" +#include "../MapObject/building.h" + +void BuilderAct(Game *game, Entity *entity){ + Building *target = (Building *) entity->task->target; + + // Hat noch keine Baustelle oder die Baustelle wurde fertiggestellt + if(entity->task->target == 0 || !target->isBaustelle){ + AnimationChangeType(entity->animationHandler, AN_ENTITY_IDLE); + + if(entity->hasDestination == 1){ + entity->hasDestination = 0; + } + + // Prüft, ob eine Baustelle existiert + Building *current = game->buildings->head; + while(current != 0){ + if(current->isBaustelle){ + entity->hasDestination = 1; + entity->destX = current->sprite->x; + entity->destY = current->sprite->y; + + entity->task->target = current; + entity->task->progress = 0; + break; + } + current = current->next; + } + } + else if(entity->hasDestination == 1){ + EntityMoveToDestination(entity); + } + else{ + // Is beim target angekommen + if(entity->task->progress == 0){ + // Angekommen, noch nicht mit arbeiten begonnen + AnimationChangeType(entity->animationHandler, AN_ENTITY_ARBEITEN); + target->progress += 0.01; + } + else if(target->progress >= 1.0){ + // Fertig mit arbeiten, Animation zu Idle zurück + AnimationChangeType(entity->animationHandler, AN_ENTITY_IDLE); + BuildingFinishConstruction(game, (Building *) entity->task->target); + entity->task->target = 0; + } + else{ + target->progress += 0.2 * GetFrameTime(); + } + } +} diff --git a/Entity/entityacts.h b/Entity/entityacts.h new file mode 100644 index 0000000..be5e21c --- /dev/null +++ b/Entity/entityacts.h @@ -0,0 +1,8 @@ +#ifndef ENTITYACTS_H_ +#define ENTITYACTS_H_ +#include "entity.h" +#include "../game.h" + +void BuilderAct(Game *game, Entity *entity); + +#endif diff --git a/Makefile b/Makefile index 3b6b35d..e8ffd86 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 building.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 entityacts.o spiel: $(OBJS) $(CC) -o spiel $(OBJS) $(FLAGS) @@ -50,5 +50,8 @@ selectable.o: Ui/selectable.c task.o: Entity/task.c $(CC) -c Entity/task.c $(FLAGS) +entityacts.o: Entity/entityacts.c + $(CC) -c Entity/entityacts.c $(FLAGS) + clean: rm *.o spiel diff --git a/MapObject/building.c b/MapObject/building.c index f2dd353..a755061 100644 --- a/MapObject/building.c +++ b/MapObject/building.c @@ -11,6 +11,7 @@ Building * BuildingInit(Game *game, int id, int x, int y){ new->sprite = newSprite; new->id = id; new->isBaustelle = 1; + new->progress = 0; new->next = 0; new->prev = 0; diff --git a/MapObject/building.h b/MapObject/building.h index e2b0ab8..6f5b410 100644 --- a/MapObject/building.h +++ b/MapObject/building.h @@ -7,6 +7,7 @@ typedef struct Building{ Sprite *sprite; int id; int isBaustelle; + float progress; struct Building *next; struct Building *prev;