diff --git a/Entity/entity.c b/Entity/entity.c index c5df0ad..63a9743 100644 --- a/Entity/entity.c +++ b/Entity/entity.c @@ -56,7 +56,7 @@ EntityList * EntityListInit(){ } void EntityMoveToDestination(Entity *entity){ - float movementSpeed = 150.0f * GetFrameTime(); + float movementSpeed = 150.0f * ACT_TIME; if(entity->hasDestination == 1){ Vector2 movement = { diff --git a/Entity/entityacts.c b/Entity/entityacts.c index a273ae9..b1b317a 100644 --- a/Entity/entityacts.c +++ b/Entity/entityacts.c @@ -47,7 +47,7 @@ void BuilderAct(Game *game, Entity *entity){ entity->task->progress = 0; } else{ - target->progress += 0.2 * GetFrameTime(); + target->progress += 0.2 * ACT_TIME; } } } @@ -100,7 +100,7 @@ void LumberjackAct(Game *game, Entity *entity){ } else{ - entity->task->progress += 0.2 * GetFrameTime(); + entity->task->progress += 0.2 * ACT_TIME; } } } diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 550f029..4faf175 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -61,7 +61,7 @@ static void SelectEntitiesInsideSelectionRect(Game *game){ // THEN SELECTING ENTITIES while (current != 0){ Vector2 currPos = {current->sprite->x + current->sprite->texture->width, current->sprite->y + current->sprite->texture->height/2}; - IsometricMapUnproject(game->map, game->camera, currPos.x, currPos.y, current->sprite->z, &currPos); + IsometricMapUnproject(game->map, game->camera, currPos.x, currPos.y, &currPos); deltaX = currPos.x - game->camera->target.x - (rect.x + game->camera->target.x); deltaY = currPos.y - game->camera->target.y - (rect.y + game->camera->target.y); @@ -85,8 +85,8 @@ void mouseInput(Game *game){ // Updating cursorWorldPos IsometricMapProject(game->map, game->camera, - (game->inputHandler->cursorPos.x / game->camera->zoom) - (game->map->textureWidth / 2), - (game->inputHandler->cursorPos.y / game->camera->zoom) - (game->map->textureHeight / 4), + (game->inputHandler->cursorPos.x / game->camera->zoom) - (game->map->textureWidth/2), + (game->inputHandler->cursorPos.y / game->camera->zoom) - (game->map->textureHeight/4), &game->inputHandler->cursorWorldPos); // Updating cursorWorldTile @@ -133,9 +133,7 @@ void mouseInput(Game *game){ if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){ float destX = game->inputHandler->cursorWorldPos.x; float destY = game->inputHandler->cursorWorldPos.y; - int maxWidth = (game->map->width) * game->map->textureWidth; - int maxHeight = (game->map->height) * game->map->textureHeight; - if(destX < 0 || destY < 0 || destX > maxWidth || destY > maxHeight){ + if(destX < 0 || destY < 0 || destX > ((game->map->width) * game->map->textureWidth) || destY > ((game->map->height) * game->map->textureHeight)){ printf("OutOfBoundsDestination\n"); } else{ @@ -187,4 +185,7 @@ void keyboardInput(Game *game){ StaticObject *tree = StaticObjectInit(game, SO_PINETREE, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); StaticObjectListInsert(game->objects, tree); } + if(IsKeyPressed(KEY_P)){ + game->screen = SCREEN_PAUSE; + } } diff --git a/IsometricMap/isometricMap.c b/IsometricMap/isometricMap.c index aa151fb..6b90840 100644 --- a/IsometricMap/isometricMap.c +++ b/IsometricMap/isometricMap.c @@ -31,8 +31,8 @@ IsometricMap * IsometricMapInit(){ int i = 0; int j = 0; - int quarterTextureSize = map->textureWidth / 4; - int halfTextureSize = map->textureWidth / 2; + int halfTextureSize = map->textureWidth/2; + int quarterTextureSize = map->textureWidth/4; for(i=0; i < map->width; i++){ for(j=0; j < map->height; j++){ Tile *tmp = malloc(sizeof(Tile)); @@ -67,39 +67,19 @@ Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int halfTextureSize, int // Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){ - float tileWidthHalf = isometricMap->textureWidth / 2; - float tileHeightQuarter = isometricMap->textureHeight / 4; + x = (x+camera->target.x) / ((isometricMap->textureWidth)/2); + y = (y+camera->target.y) / ((isometricMap->textureHeight)/4); - x += camera->target.x; - y += camera->target.y; + tmp->x = ( x + y) * (isometricMap->textureWidth/2); + tmp->y = ( -x + y) * (isometricMap->textureHeight/2); - float xPos = (float) x; - float yPos = (float) y; - - float isoX = 0.5 * ( xPos / tileWidthHalf + yPos / tileHeightQuarter); - float isoY = 0.5 * ( -xPos / tileWidthHalf + yPos / tileHeightQuarter); - - tmp->x = isoX * isometricMap->tileTextures[0].width; - tmp->y = isoY * isometricMap->tileTextures[0].height; } // Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector -void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp){ - float xPos = (float) x; - float yPos = (float) y; - - float screenX = (xPos - yPos) / 2; - float screenY = (xPos + yPos) / 4; - - screenX += camera->target.x; - screenY += camera->target.y; - - // z is currently implemented as z=1 equals 1 layer, z=2 would be two layers height (could be smoothed) - // hardcoded tile height - //screenY -= z * 10; +void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, Vector2 *tmp){ + tmp->x = ((x - y)/2) + camera->target.x; + tmp->y = ((x + y)/4) + camera->target.y; - tmp->x = screenX; - tmp->y = screenY; } // returns Tile * -> tile at coordinates x y z=layer @@ -150,10 +130,11 @@ void IsometricMapDraw(Game *game){ if (maxI > game->map->width){ maxI = game->map->width; } if (maxJ > game->map->height){ maxJ = game->map->height; } - int i, j = 0; - for (j = jtmp; j < maxJ; j++){ - for (i = itmp; i < maxI; i++){ + register int i, j = 0; + for (j = jtmp; j < maxJ; ++j){ + for (i = itmp; i < maxI; ++i){ if (game->map->tiles[i][j]->textureId == -1){ + continue; } else{ DrawTexture( @@ -161,6 +142,7 @@ void IsometricMapDraw(Game *game){ game->map->tiles[i][j]->offsetX, game->map->tiles[i][j]->offsetY, WHITE); + continue; } } } diff --git a/IsometricMap/isometricMap.h b/IsometricMap/isometricMap.h index 9cde1a5..414b6c6 100644 --- a/IsometricMap/isometricMap.h +++ b/IsometricMap/isometricMap.h @@ -42,7 +42,7 @@ Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int halfTextureSize, int void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp); // Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector -void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp); +void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, Vector2 *tmp); // changes to Texture ID of tile at x y on maplayer layer void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id); diff --git a/Sprite/sprite.c b/Sprite/sprite.c index a9144b0..51ad07c 100644 --- a/Sprite/sprite.c +++ b/Sprite/sprite.c @@ -13,30 +13,15 @@ void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){ Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2}; - IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos); + IsometricMapUnproject(map, camera, pos.x, pos.y, &pos); pos.x -= camera->target.x; pos.y -= camera->target.y; - /* - if(sprite->selected){ - DrawTexture(*sprite->texture, pos.x, pos.y, (Color){255, 255, 255, 200}); - //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); - } - else{ - */ - DrawTexture(*sprite->texture, pos.x, pos.y, WHITE); - //} + DrawTexture(*sprite->texture, pos.x, pos.y, WHITE); } void DrawSpriteToScreen(Sprite *sprite){ - /*if(sprite->selected){ - DrawTexture(*sprite->texture, sprite->x, sprite->y, (Color){255, 255, 255, 200}); - //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); - } - else{ - */ - DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE); - // } + DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE); } void SpriteUpdate(Sprite *sprite){ @@ -50,8 +35,8 @@ Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){ Sprite *newSprite = malloc(sizeof(Sprite)); newSprite->texture = atlas->textures[textureID]; - newSprite->x = x - newSprite->texture->width / 2; - newSprite->y = y - newSprite->texture->height / 2; + newSprite->x = x - (newSprite->texture->width/2); + newSprite->y = y - (newSprite->texture->height/2); newSprite->z = 0; newSprite->depth = newSprite->x + newSprite->y; newSprite->next = 0; diff --git a/Textures/animationHandler.c b/Textures/animationHandler.c index 4bbd3a7..b26ae17 100644 --- a/Textures/animationHandler.c +++ b/Textures/animationHandler.c @@ -2,6 +2,7 @@ #include "animation.h" #include #include +#include "../definitions.h" AnimationHandler * AnimationHandlerInit(Animation ***animations, Texture2D **spriteTexture){ AnimationHandler *new = malloc(sizeof(AnimationHandler)); @@ -19,7 +20,7 @@ AnimationHandler * AnimationHandlerInit(Animation ***animations, Texture2D **spr } void AnimationUpdate(AnimationHandler *animationHandler){ - animationHandler->deltaElapsed += GetFrameTime(); + animationHandler->deltaElapsed += ACT_TIME; if(animationHandler->deltaElapsed >= 0.2){ if(animationHandler->forward == 1){ diff --git a/Ui/debug.c b/Ui/debug.c index 8b59d7f..7b6cefc 100644 --- a/Ui/debug.c +++ b/Ui/debug.c @@ -13,6 +13,7 @@ void DebugDraw(Game *game){ // Hier die Debug Information in den Strings Array einfügen // im Endeffekt einfach im Array an der Stelle lineamount++ die Elemente einfügen + sprintf(strings[lineamount++], "FPS: %d", GetFPS()); sprintf(strings[lineamount++], "Screen: %d", game->screen); sprintf(strings[lineamount++], "MouseScreenX: %d", GetMouseX()); sprintf(strings[lineamount++], "MouseScreenY: %d", GetMouseY()); diff --git a/Ui/onClickFunctions.c b/Ui/onClickFunctions.c index 1d95ba6..86498dc 100644 --- a/Ui/onClickFunctions.c +++ b/Ui/onClickFunctions.c @@ -25,9 +25,7 @@ void OnClickStartButton(Game *game, Button *button){ // RETURNS 1 IF COORDS INSIDE MAP static int InsideMapBounds(Vector2 coords, IsometricMap *map){ - int maxWidth = map->width * map->textureWidth; - int maxHeight = map->height * map->textureHeight; - if(coords.x < 0 || coords.y < 0 || coords.x > maxWidth || coords.y > maxHeight){ + if(coords.x < 0 || coords.y < 0 || coords.x > (map->width * map->textureWidth) || coords.y > (map->height * map->textureHeight)){ printf("OutOfBoundsDestination Spawn\n"); return 0; } @@ -42,7 +40,7 @@ void OnSelectedSelectable(Game *game, Selectable *selectable){ return; } void OnSelectedSpawnBuilding(Game *game, Selectable *selectable){ - if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ // Can only be spawned inside map bounds if(InsideMapBounds(game->inputHandler->cursorWorldPos, game->map) == 0){ @@ -52,11 +50,13 @@ void OnSelectedSpawnBuilding(Game *game, Selectable *selectable){ Building *newObject = BuildingInit(game, BU_HOUSE, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); BuildingListInsert(game->buildings, newObject); - selectable->state = SELECTABLE_STATE_DEFAULT; + if(selectable->unselectAfterExecute == 0){ + selectable->state = SELECTABLE_STATE_DEFAULT; + } } } void OnSelectedSpawnWorker(Game *game, Selectable *selectable){ - if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ if(InsideMapBounds(game->inputHandler->cursorWorldPos, game->map) == 0){ return; } @@ -64,11 +64,13 @@ void OnSelectedSpawnWorker(Game *game, Selectable *selectable){ Entity *builder = EntityInit(game, PR_BUILDER, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); EntityListInsert(game->entities, builder); - selectable->state = SELECTABLE_STATE_DEFAULT; + if(selectable->unselectAfterExecute == 0){ + selectable->state = SELECTABLE_STATE_DEFAULT; + } } } void OnSelectedSpawnLumberjack(Game *game, Selectable *selectable){ - if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){ if(InsideMapBounds(game->inputHandler->cursorWorldPos, game->map) == 0){ return; } @@ -76,6 +78,8 @@ void OnSelectedSpawnLumberjack(Game *game, Selectable *selectable){ Entity *lumberjack = EntityInit(game, PR_LUMBERJACK, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y); EntityListInsert(game->entities, lumberjack); - selectable->state = SELECTABLE_STATE_DEFAULT; + if(selectable->unselectAfterExecute == 0){ + selectable->state = SELECTABLE_STATE_DEFAULT; + } } } diff --git a/Ui/selectable.c b/Ui/selectable.c index 9161ac0..b2863f1 100644 --- a/Ui/selectable.c +++ b/Ui/selectable.c @@ -10,7 +10,7 @@ #include "onClickFunctions.h" Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[3], int hasBackground, Vector2 *position, char *description, - int showDescripton, int descriptionLEN, int fontSize, int id, int groupID){ + int showDescripton, int descriptionLEN, int fontSize, int id, int groupID, int unselectAfterExecute){ Selectable *selectable = malloc(sizeof(Selectable)); selectable->texture[0] = textures[0]; @@ -35,6 +35,7 @@ Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[ selectable->id = id; selectable->state = 0; selectable->groupID = groupID; + selectable->unselectAfterExecute = unselectAfterExecute; switch(selectable->id){ case SELECTABLE_ID_SPAWN_BUILDING: diff --git a/Ui/selectable.h b/Ui/selectable.h index a9b786e..393e7f8 100644 --- a/Ui/selectable.h +++ b/Ui/selectable.h @@ -17,6 +17,8 @@ typedef struct Selectable{ int state; // 0: not selected | 1: hovered | 2: selected int fontSize; // FontSize kann für jede Selectable Description individuell festgelegt werden + int unselectAfterExecute; // 0: will unselect | 1 won't unselect + // Notiz: Die Selectables können auch in einem 2-Dimensionalen Array im UiContainer gespeichert werden, worüber die Groups auch definiert werden könnten int groupID; // Selectables können gruppiert werden, man kann also mehrere Dinge gleichzeitig selected haben void (*onSelected)(Game *game, Selectable *selectable); @@ -26,7 +28,7 @@ typedef struct Selectable{ // hasBackground: 0: hat keine Background Textur | 1: hat Background Textur // showDescription 0: zeigt Description nicht | 1: zeigt Description // Max Description LEN 20 -Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[3], int hasBackground, Vector2 *position, char *description, int showDescripton, int descriptionLEN, int fontSize, int id, int groupID); +Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[3], int hasBackground, Vector2 *position, char *description, int showDescripton, int descriptionLEN, int fontSize, int id, int groupID, int unselectAfterExecute); void SelectableExecuteSelectable(Selectable *selectable, Game * game); diff --git a/Ui/uiContainer.c b/Ui/uiContainer.c index 705c2a2..877fe4c 100644 --- a/Ui/uiContainer.c +++ b/Ui/uiContainer.c @@ -96,15 +96,15 @@ static UiContainer * UiContainerInitGameUiContainer(){ int groupTwo = 1; int fontSize = 16; Selectable *selectable1 = SelectableInit(textures, backgroundTextures, hasBackground , &position, "Building", - showDescription, 9, fontSize, SELECTABLE_ID_SPAWN_BUILDING, groupOne); + showDescription, 9, fontSize, SELECTABLE_ID_SPAWN_BUILDING, groupOne, 1); position.y += 100; Selectable *selectable2 = SelectableInit(textures2, backgroundTextures, hasBackground,&position, "Worker", - showDescription, 7, fontSize, SELECTABLE_ID_SPAWN_WORKER, groupOne); + showDescription, 7, fontSize, SELECTABLE_ID_SPAWN_WORKER, groupOne, 1); position.y += 100; Selectable *selectable3 = SelectableInit(textures2, backgroundTextures, hasBackground,&position, "Lumberjack", - showDescription, 11, fontSize, SELECTABLE_ID_SPAWN_LUMBERJACK, groupOne); + showDescription, 11, fontSize, SELECTABLE_ID_SPAWN_LUMBERJACK, groupOne, 1); position.y += 100; int selectableCounter = 0; diff --git a/definitions.h b/definitions.h index 8f38632..d80d084 100644 --- a/definitions.h +++ b/definitions.h @@ -1,6 +1,12 @@ #ifndef DEFINITIONS_H_ #define DEFINITIONS_H_ + +//#define ACT_TIME 0.03333333333f// 30/s +#define ACT_TIME 0.01666666666f // 60/s +//#define ACT_TIME 0.00833333333f// 120/s + + #define N 0 #define NE 1 #define E 3 diff --git a/main.c b/main.c index aa20644..7ce9bd9 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ #include "Ui/button.h" #include "Ui/uiContainer.h" #include "Ui/debug.h" +#include "definitions.h" // TODO: Variable die den Maus Input freigibt solange nichts mit der Maus gemacht wurde // Sobald ein Button gedrückt wurde in diesem Frame oder rect gezogen wird oder so ist sind Maus Funktionen gesperrt @@ -26,7 +27,8 @@ int main(){ // Hides the operating systems own cursor HideCursor(); - //SetTargetFPS(60); + //SetTargetFPS(50); + double frame = 0; while(!WindowShouldClose()){ @@ -39,7 +41,6 @@ int main(){ game->cursorSprite->x = GetMousePosition().x; game->cursorSprite->y = GetMousePosition().y; - BeginDrawing(); // Drawing ist grundsätzlich immer aktiviert ClearBackground(RAYWHITE); // Screen wird in jedem Frame gecleared switch(game->screen){ // Screenspecific Code @@ -54,9 +55,11 @@ int main(){ return 0; case SCREEN_GAME: // Updating Entities - - EntityListActAllEntities(game); - + frame += GetFrameTime(); + if(frame >= ACT_TIME){ + EntityListActAllEntities(game); + frame = 0; + } // Drawing IsometricMap BeginMode2D(*(game->camera)); // Sorgt dafür, dass die Kameraposition beachtet wird @@ -69,13 +72,9 @@ int main(){ mouseInput(game); keyboardInput(game); - - if(IsKeyPressed(KEY_P)){ - game->screen = SCREEN_PAUSE; - } - break; case SCREEN_PAUSE: + int halfScreenHeight = GetScreenHeight()/2; // Still drawing isometric map, which is not updated atm BeginMode2D(*(game->camera)); // Sorgt dafür, dass die Kameraposition beachtet wird @@ -85,15 +84,15 @@ int main(){ UiContainerDrawUiContainer(game->UiContainers[SCREEN_GAME]); // darkened background + "Paused" Text DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (Color){0, 0, 0, 150}); - int textWidthHalf = MeasureText("Paused", 28) / 2; - DrawText("Paused", GetScreenWidth()/2 - textWidthHalf, GetScreenHeight()/4 - 14, 28, WHITE); + int textWidthHalf = (MeasureText("Paused", 28)/2); + DrawText("Paused", GetScreenWidth()/2 - textWidthHalf, (halfScreenHeight/2) - 14, 28, WHITE); // Controls lol - DrawText("I: Zoom in", 5, GetScreenHeight()/2, 16, WHITE); - DrawText("K: Zoom out", 5, GetScreenHeight()/2 + 16, 16, WHITE); - DrawText("P: Pause", 5, GetScreenHeight()/2 + 32, 16, WHITE); - DrawText("WASD: Move Camera", 5, GetScreenHeight()/2 + 48, 16, WHITE); - DrawText("ESC: Exit Game", 5, GetScreenHeight()/2 + 64, 16, WHITE); + DrawText("I: Zoom in", 5, halfScreenHeight, 16, WHITE); + DrawText("K: Zoom out", 5, halfScreenHeight + 16, 16, WHITE); + DrawText("P: Pause", 5, halfScreenHeight + 32, 16, WHITE); + DrawText("WASD: Move Camera", 5, halfScreenHeight + 48, 16, WHITE); + DrawText("ESC: Exit Game", 5, halfScreenHeight + 64, 16, WHITE); if(IsKeyPressed(KEY_P)){ game->screen = SCREEN_GAME; @@ -113,7 +112,6 @@ int main(){ // Dinge die grundsätzlich immer gedrawed werden sollen // Debug Menu, FPS anzeige, Cursor DebugDraw(game); - DrawFPS(GetScreenWidth() - 95, 10); DrawSpriteToScreen(game->cursorSprite); EndDrawing();