You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
6.6 KiB
201 lines
6.6 KiB
#include "entity.h"
|
|
#include "raymath.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "../Sprite/sprite.h"
|
|
#include "../MapObject/building.h"
|
|
#include "../definitions.h"
|
|
#include "../Textures/textureatlas.h"
|
|
|
|
Entity * EntityInit(Sprite *sprite, int profession, TextureAtlas *atlas){
|
|
Entity *new = malloc(sizeof(Entity));
|
|
|
|
new->angle = 0;
|
|
new->sprite = sprite;
|
|
new->destX = 0;
|
|
new->destY = 0;
|
|
new->hasDestination = 0;
|
|
new->selected = 0;
|
|
new->next = 0;
|
|
new->prev = 0;
|
|
new->task = TaskInit(profession);
|
|
new->profession = profession;
|
|
|
|
Animation ***animations = 0;
|
|
|
|
if(profession == PR_BUILDER){
|
|
animations = atlas->animations[AN_WORKER];
|
|
}
|
|
else{
|
|
printf("\n\n\n\n\n\n\n\nEntityCreate mit falscher ID (%d) aufgerufen oder ID nicht bekannt!!!\n\n\n\n\n\n\n\n", profession);
|
|
}
|
|
|
|
new->animationHandler = AnimationHandlerInit(animations, &new->sprite->texture);
|
|
|
|
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;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AnimationUpdate(current->animationHandler);
|
|
|
|
SpriteUpdate(current->sprite);
|
|
SpriteListSpriteChanged(game->sprites, current->sprite);
|
|
|
|
current = current->next;
|
|
}
|
|
}
|