Refactor erfolgreich

main
Jonathan Hager 3 years ago
parent 9ce0701cc7
commit 356e23e5a8
Signed by: JonathanHager
GPG Key ID: 34881E488569708C

@ -4,9 +4,10 @@
#include <stdlib.h>
#include "../Sprite/sprite.h"
#include "../MapObject/mapobject.h"
#include "../Textures/textureIDs.h"
#include "../definitions.h"
#include "../Textures/textureatlas.h"
Entity * EntityInit(Sprite *sprite, int profession){
Entity * EntityInit(Sprite *sprite, int profession, TextureAtlas *atlas){
Entity *new = malloc(sizeof(Entity));
new->angle = 0;
@ -20,6 +21,17 @@ Entity * EntityInit(Sprite *sprite, int profession){
new->task = TaskInit();
new->profession = profession;
Animation ***animations = 0;
if(profession == TE_WORKER){
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;
}
@ -104,40 +116,41 @@ void EntityListActAllEntities(Game *game){
if(angle <= 22.5 && angle >= -22.5){
// E
AnimationChangeAnimation(current->animationHandler, E);
AnimationChangeDirection(current->animationHandler, E);
}
else if(angle > 0 && angle <= 67.5){
// NE
AnimationChangeAnimation(current->animationHandler, NE);
AnimationChangeDirection(current->animationHandler, NE);
}
else if(angle > 0 && angle <= 112.5){
// N
AnimationChangeAnimation(current->animationHandler, N);
AnimationChangeDirection(current->animationHandler, N);
}
else if(angle > 0 && angle <= 157.5){
// NW
AnimationChangeAnimation(current->animationHandler, NW);
AnimationChangeDirection(current->animationHandler, NW);
}
else if(angle < 0 && angle >= -67.5){
// SE
AnimationChangeAnimation(current->animationHandler, SE);
AnimationChangeDirection(current->animationHandler, SE);
}
else if(angle < 0 && angle >= -112.5){
// S
AnimationChangeAnimation(current->animationHandler, S);
AnimationChangeDirection(current->animationHandler, S);
}
else if(angle < 0 && angle >= -157.5){
// SW
AnimationChangeAnimation(current->animationHandler, SW);
AnimationChangeDirection(current->animationHandler, SW);
}
else{
// W
AnimationChangeAnimation(current->animationHandler, W);
AnimationChangeDirection(current->animationHandler, W);
}
}
}
else{
/*
if(current->profession == PR_BUILDER){
if(current->task->target == 0){
// Prüft, ob eine Baustelle existiert
@ -155,6 +168,7 @@ void EntityListActAllEntities(Game *game){
}
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);
@ -166,9 +180,10 @@ void EntityListActAllEntities(Game *game){
MapObjectListInsert(game->mapObjects, newObject);
current->task->target = 0;
free(obj);
}
*/
//}
}
//}
}

@ -9,7 +9,7 @@ typedef struct Entity Entity;
typedef struct Entity{
Sprite *sprite;
AnimationHandler animationHandler;
AnimationHandler *animationHandler;
float angle;
float destX;
float destY;
@ -27,7 +27,7 @@ typedef struct EntityList{
Entity *tail;
} EntityList;
Entity * EntityInit(Sprite *sprite, int profession);
Entity * EntityInit(Sprite *sprite, int profession, TextureAtlas *atlas);
EntityList * EntityListInit();
void EntityListPrintForward(EntityList *entities);

@ -9,9 +9,7 @@
#include <math.h>
#include "../IsometricMap/tile.h"
#include "../game.h"
#include "../MapObject/mapobjectIDs.h"
#include "../Entity/professions.h"
//#include "../Textures/textureIDs.h"
#include "../definitions.h"
void DrawRect(Vector2 rectStart, Vector2 *mousePosition){
float width = GetMousePosition().x - rectStart.x;
@ -75,10 +73,12 @@ void mouseInput(Game *game){
if(IsKeyPressed(KEY_G)){
// Baustelle adden
/*
Sprite *new = SpriteCreate(game->textures, 3, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
MapObject *newObject = MapObjectInit(new, MO_Baustelle);
SpriteListInsert(game->sprites, new);
MapObjectListInsert(game->mapObjects, newObject);
*/
}
// hardcoded layer amount
@ -111,7 +111,7 @@ void mouseInput(Game *game){
inputHandler->pressed = 1;
// Cursorsprite is changed to "down"
inputHandler->cursorSprite->texture = (inputHandler->cursorTextures) + 1;
game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][1];
}
}
@ -123,7 +123,7 @@ void mouseInput(Game *game){
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
inputHandler->pressed = 0;
// Cursorsprite is changed back to normal
inputHandler->cursorSprite->texture = (inputHandler->cursorTextures);
game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][0];
float width = GetMousePosition().x - inputHandler->rectStart.x;
float height = GetMousePosition().y - inputHandler->rectStart.y;
@ -137,8 +137,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 {
Sprite *newSprite = SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
Entity *entity = EntityInit(newSprite, PR_BUILDER);
Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
Entity *entity = EntityInit(newSprite, TE_WORKER, game->textures);
EntityListInsert(game->entities, entity);
SpriteListInsert(game->sprites, newSprite);
//ListPrintForward(sprites);

@ -12,8 +12,6 @@ typedef struct InputHandler{
Vector2 cursorWorldPos;
Vector2 cursorWorldTile;
int selectedLayer;
Texture2D *cursorTextures;
Sprite *cursorSprite;
} InputHandler;
void mouseInput(Game *game);

@ -49,34 +49,11 @@ void SpriteUpdate(Sprite *sprite){
Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){
Sprite *newSprite = malloc(sizeof(Sprite));
//AnimationHandler create
Animation **animations = 0;
if(textureID == TE_WORKER){
animations = atlas->workerAnimations;
}
else if(textureID == TE_WORKER){
animations = atlas->cursorAnimation;
}
else if(textureID == TE_BUILDING){
animations = atlas->buildingAnimation;
}
else if(textureID == TE_BAUSTELLE){
animations = atlas->baustelleAnimation;
}
else{
printf("\n\n\n\n\n\n\n\nSpriteCreate mit falscher ID (%d) aufgerufen oder ID nicht bekannt!!!\n\n\n\n\n\n\n\n", textureID);
}
AnimationHandler *newHandler = AnimationHandlerInit(animations);
newSprite->animationHandler = newHandler;
newSprite->texture = newSprite->animationHandler->currentFrame->texture;
newSprite->texture = atlas->textures[textureID];
newSprite->x = x - newSprite->texture->width / 2;
newSprite->y = y - newSprite->texture->height / 2;
newSprite->z = 0;
newSprite->depth = newSprite->x + newSprite->y;
newSprite->angle = 0;
newSprite->next = 0;
newSprite->prev = 0;

@ -3,13 +3,15 @@
#include <stdlib.h>
#include <stdio.h>
AnimationHandler * AnimationHandlerInit(Animation **animations, Texture2D **spriteTexture){
AnimationHandler * AnimationHandlerInit(Animation ***animations, Texture2D **spriteTexture){
AnimationHandler *new = malloc(sizeof(AnimationHandler));
new->spriteTexture = spriteTexture;
new->animations = animations;
new->currentAnimation = 0;
new->currentFrame = new->animations[new->currentAnimation]->head;
new->currentType = 0;
new->currentDirection = 0;
new->currentFrame = new->animations[new->currentType][new->currentDirection]->head;
new->spriteTexture = spriteTexture;
new->forward = 1;
new->deltaElapsed = 0;
@ -34,13 +36,22 @@ void AnimationUpdate(AnimationHandler *animationHandler){
}
void AnimationReset(AnimationHandler *animationHandler){
animationHandler->currentFrame = animationHandler->animations[animationHandler->currentAnimation]->head;
animationHandler->currentFrame = animationHandler->animations[animationHandler->currentType][animationHandler->currentDirection]->head;
*(animationHandler->spriteTexture) = animationHandler->currentFrame->texture;
}
void AnimationChangeAnimation(AnimationHandler *animationHandler, int newAnimation){
if(animationHandler->currentAnimation != newAnimation){
animationHandler->currentAnimation = newAnimation;
void AnimationChangeAnimation(AnimationHandler *animationHandler, int animationType, int direction){
if(animationHandler->currentType != animationType || animationHandler->currentDirection != direction){
animationHandler->currentType = animationType;
animationHandler->currentDirection = direction;
AnimationReset(animationHandler);
}
}
void AnimationChangeDirection(AnimationHandler *animationHandler, int direction){
if(animationHandler->currentDirection != direction){
animationHandler->currentDirection = direction;
AnimationReset(animationHandler);
}
}

@ -7,17 +7,19 @@
typedef struct AnimationHandler AnimationHandler;
typedef struct AnimationHandler{
Animation **animations;
Animation ***animations;
AnimationFrame *currentFrame;
Texture2D **spriteTexture;
int currentAnimation;
int currentType;
int currentDirection;
int forward;
float deltaElapsed;
} AnimationHandler;
AnimationHandler * AnimationHandlerInit(Animation **animations, Texture2D **spriteTexture);
AnimationHandler * AnimationHandlerInit(Animation ***animations, Texture2D **spriteTexture);
void AnimationUpdate(AnimationHandler *animationHandler);
void AnimationReset(AnimationHandler *animationHandler);
void AnimationChangeAnimation(AnimationHandler *animationHandler, int newAnimation);
void AnimationChangeAnimation(AnimationHandler *animationHandler, int animationType, int direction);
void AnimationChangeDirection(AnimationHandler *animationHandler, int direction);
#endif

@ -8,39 +8,8 @@ TextureAtlas * TextureAtlasInit(){
TextureAtlas *new = malloc(sizeof(TextureAtlas));
TextureAtlasLoadTextures(new->textures);
//LoadCursorTextures(textures->cursorTextures, textures->cursorAnimation);
//LoadWorkerTextures(textures->workerTextures);
//LoadWorkerAnimations(textures->workerAnimations, textures->workerTextures);
/*
//Das soll nicht hier hin, bin aber zu faul :|
textures->building = LoadTexture("assets/building.png");
Animation *buildingAnimation = AnimationInit();
AnimationInsertBack(buildingAnimation, &textures->building);
textures->buildingAnimation[0] = buildingAnimation;
textures->buildingAnimation[1] = buildingAnimation;
textures->buildingAnimation[2] = buildingAnimation;
textures->buildingAnimation[3] = buildingAnimation;
textures->buildingAnimation[4] = buildingAnimation;
textures->buildingAnimation[5] = buildingAnimation;
textures->buildingAnimation[6] = buildingAnimation;
textures->buildingAnimation[7] = buildingAnimation;
*/
//Das soll nicht hier hin, bin aber zu faul :|
/*
textures->baustelle = LoadTexture("assets/construction.png");
Animation *baustelleAnimation = AnimationInit();
AnimationInsertBack(baustelleAnimation, &textures->baustelle);
textures->baustelleAnimation[0] = baustelleAnimation;
textures->baustelleAnimation[1] = baustelleAnimation;
textures->baustelleAnimation[2] = baustelleAnimation;
textures->baustelleAnimation[3] = baustelleAnimation;
textures->baustelleAnimation[4] = baustelleAnimation;
textures->baustelleAnimation[5] = baustelleAnimation;
textures->baustelleAnimation[6] = baustelleAnimation;
textures->baustelleAnimation[7] = baustelleAnimation;
*/
TextureAtlasLoadAnimations(new->animations, new->textures);
return new;
}
@ -50,16 +19,19 @@ void TextureAtlasLoadTextures(Texture2D **textures){
for(i = 0; i < TE_AMOUNT; i++){
switch(i){
case TE_CURSOR:
textures[TE_CURSOR] = malloc(2 * sizeof(Texture2D)); // So vielleicht
textures[TE_CURSOR] = malloc(2 * sizeof(Texture2D));
LoadCursorTextures(textures[TE_CURSOR]);
break;
case TE_WORKER:
textures[TE_WORKER] = malloc(TE_ENTITY_LENGTH * sizeof(Texture2D));
LoadEntityTextures(textures[TE_WORKER], "worker");
break;
case TE_BUILDING:
textures[TE_BUILDING] = malloc(TE_MAPOBJECT_LENGTH * sizeof(Texture2D));
LoadMapObjectTextures(textures[TE_BUILDING], "building");
break;
case TE_BAUSTELLE:
textures[TE_BAUSTELLE] = malloc(TE_MAPOBJECT_LENGTH * sizeof(Texture2D));
LoadMapObjectTextures(textures[TE_BAUSTELLE], "baustelle");
break;
default:
@ -69,8 +41,6 @@ void TextureAtlasLoadTextures(Texture2D **textures){
}
void LoadEntityTextures(Texture2D *atlasrow, char *directoryPrefix){
// Das klappt so nicht
atlasrow = malloc(TE_ENTITY_LENGTH * sizeof(Texture2D));
// pathToFile wird erstellt. z.B. assets/entities/ + worker ---> assets/entities/worker/
char pathToFile[50] = "assets/entities/";
@ -78,7 +48,7 @@ void LoadEntityTextures(Texture2D *atlasrow, char *directoryPrefix){
strcat(pathToFile, "/");
char animation[30] = "walk";
char filename[100];
char filename[100] = "";
char number[3] = "";
char ending[5] = ".png";
@ -152,7 +122,6 @@ void LoadEntityTextures(Texture2D *atlasrow, char *directoryPrefix){
}
void LoadMapObjectTextures(Texture2D *atlasrow, char *directoryPrefix){
atlasrow = malloc(TE_MAPOBJECT_LENGTH * sizeof(Texture2D));
// pathToFile wird erstellt. z.B. assets/mapobjects/ + building ---> assets/mapobjects/building/
char pathToFile[50] = "assets/mapobjects/";
@ -160,7 +129,7 @@ void LoadMapObjectTextures(Texture2D *atlasrow, char *directoryPrefix){
strcat(pathToFile, "/");
//char animation[30] = "walk";
char filename[100];
char filename[100] = "";
//char number[3] = "";
char ending[5] = ".png";
@ -173,83 +142,52 @@ void LoadMapObjectTextures(Texture2D *atlasrow, char *directoryPrefix){
}
void LoadCursorTextures(Texture2D *cursorTextures){
cursorTextures = malloc(2 * sizeof(Texture2D));
cursorTextures[0] = LoadTexture("assets/cursor.gif");
cursorTextures[1] = LoadTexture("assets/cursor_down.gif");
}
void LoadWorkerTextures(Texture2D *workerTextures){
char beginning[30] = "assets/worker/walk-";
char filename[30] = "";
char number[3] = "";
char ending[5] = ".png";
// Since some Images need to be flipped, we have fewer pngs than textures later loaded. i counts textures
// file counts the filename and is incremented manually when needed
void TextureAtlasLoadAnimations(Animation ****animations, Texture2D **textures){
int i;
int file = 0;
for(i = 0; i < 104; i++){
// Concatenate the correct string for the filename (beginning + i + ending)
sprintf(number, "%d", file);
strcat(filename, beginning);
strcat(filename, number);
strcat(filename, ending);
//printf("file:%s:file\n", filename);
// Set correct values for next iteration
int lol = i % 8;
for(i = 0; i < AN_AMOUNT; i++){
switch(i){
case AN_WORKER:
animations[AN_WORKER] = malloc(AN_ENTITY_AMOUNT * sizeof(Animation *));
LoadEntityAnimations(animations[AN_WORKER], textures[TE_WORKER], AN_WORKER);
break;
default:
printf("WARNING: TEXTUREATLAS TRIED LOADING NON DEFINED ANIMATION!!\n");
}
}
}
void LoadEntityAnimations(Animation ***animationsrow, Texture2D *atlasrow, int id){
int i;
int j;
int frame;
for(i = 0; i < AN_ENTITY_AMOUNT; i++){
animationsrow[i] = malloc(DIRECTIONS_AMOUNT * sizeof(Animation));
// TODO: walk und umackern läuft nicht bis 24 sondern nur 23
if(lol == 0){
*(workerTextures + i) = LoadTexture(filename);
printf("\n");
file++;
}
else if(lol == 2){
Image tmp = LoadImage(filename);
ImageFlipHorizontal(&tmp);
*(workerTextures + i) = LoadTextureFromImage(tmp);
printf("flipped\n");
file++;
}
else if(lol == 4){
Image tmp = LoadImage(filename);
ImageFlipHorizontal(&tmp);
*(workerTextures + i) = LoadTextureFromImage(tmp);
printf("flipped\n");
file++;
}
else if(lol == 6){
Image tmp = LoadImage(filename);
ImageFlipHorizontal(&tmp);
*(workerTextures + i) = LoadTextureFromImage(tmp);
printf("flipped\n");
file++;
}
else if(lol == 7){
*(workerTextures + i) = LoadTexture(filename);
printf("\n");
file++;
int obergrenze;
if(i == AN_ENTITY_DIE){
obergrenze = 3;
}
else{
*(workerTextures + i) = LoadTexture(filename);
printf("\n");
obergrenze = 5;
}
strcpy(filename, "");
strcpy(number, "");
for(j = 0; j < DIRECTIONS_AMOUNT; j++){
Animation *newAnimation = AnimationInit();
frame = i * DIRECTIONS_AMOUNT * 5 + j;
if(i == 39){
strcpy(beginning, "assets/worker/umackern-");
file = 0;
}
else if(i == 79){
strcpy(beginning, "assets/worker/die-");
file = 0;
int frameCounter;
for(frameCounter = 0; frameCounter < obergrenze; frameCounter++){
AnimationInsertBack(newAnimation, (atlasrow + frame));
frame += 8;
}
animationsrow[i][j] = newAnimation;
}
}
}

@ -9,18 +9,10 @@ typedef struct TextureAtlas TextureAtlas;
typedef struct TextureAtlas{
Texture2D *textures[TE_AMOUNT];
Animation ***animations[AN_AMOUNT];
Texture2D cursorTextures[2];
Animation *cursorAnimation[1];
Texture2D workerTextures[104];
Animation *workerAnimations[24];
Texture2D building;
Animation *buildingAnimation[8];
Texture2D baustelle;
Animation *baustelleAnimation[8];
} TextureAtlas;
// Initialize the full TextureAtlas struct with all Textures used in the game
@ -30,7 +22,8 @@ void LoadCursorTextures(Texture2D *cursorTextures);
void LoadEntityTextures(Texture2D *atlasrow, char *directoryPrefix);
void LoadMapObjectTextures(Texture2D *atlasrow, char *directoryPrefix);
void LoadWorkerTextures(Texture2D *workerTextures);
void LoadWorkerAnimations(Animation **workerAnimations, Texture2D *workerTextures);
void TextureAtlasLoadAnimations(Animation ****animations, Texture2D **textures);
void LoadEntityAnimations(Animation ***animationsrow, Texture2D *atlasrow, int id);
#endif

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

@ -9,7 +9,9 @@
#define SW 6
#define W 4
#define NW 2
#define DIRECTIONS_AMOUNT 8
// Texture definitions
#define TE_CURSOR 0
#define TE_WORKER 1
#define TE_BUILDING 2
@ -19,4 +21,16 @@
#define TE_ENTITY_LENGTH 104
#define TE_MAPOBJECT_LENGTH 1
// Definitions for animations
#define AN_WORKER 0
#define AN_AMOUNT 1
#define AN_ENTITY_AMOUNT 3
#define AN_ENTITY_IDLE 0
#define AN_ENTITY_ARBEITEN 1
#define AN_ENTITY_DIE 2
#endif

@ -17,7 +17,7 @@ Game *GameInit()
game->textures = TextureAtlasInit();
game->cursorSprite = SpriteCreate(game->textures, 0, 450, 225);
game->cursorSprite = SpriteCreate(game->textures, TE_CURSOR, 450, 225);
game->inputHandler = malloc(sizeof(InputHandler));
game->inputHandler->pressed = 0;
game->inputHandler->rectStart.x = 0;
@ -29,7 +29,7 @@ Game *GameInit()
game->inputHandler->cursorWorldTile.x = 0;
game->inputHandler->cursorWorldTile.y = 0;
game->inputHandler->selectedLayer = -1;
game->inputHandler->cursorTextures = game->textures->cursorTextures;
game->inputHandler->cursorTextures = game->textures->textures[TE_CURSOR];
game->inputHandler->cursorSprite = game->cursorSprite;
game->screen = SCREEN_MAINMENU;

Loading…
Cancel
Save