diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..23106ef --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "inputhandling.h": "c" + } +} \ No newline at end of file diff --git a/README.md b/README.md index 9be82e5..ef20ff1 100644 --- a/README.md +++ b/README.md @@ -1 +1,18 @@ # Aufbauspiel jaja + +[[_TOC_]] + +## Ackermatch gegen die Natur? + +KI Gegner ist erstmal zu aufwändig, ein wenig Ackern muss man aber immer! + +Fantasy Welt oder Realistisch? + +## Isometrie in RayLib +[Projection Orthogonal -> Isometric](https://gamedevelopment.tutsplus.com/tutorials/creating-isometric-worlds-a-primer-for-game-developers--gamedev-6511) + +## TODO + +- Bug with movement of sprite +- Drawn Rectangle problems with negative width/height +- Selecting Sprites for moving all selected to the same destination \ No newline at end of file diff --git a/amulet.png b/assets/amulet.png similarity index 100% rename from amulet.png rename to assets/amulet.png diff --git a/assets/grass.png b/assets/grass.png new file mode 100644 index 0000000..adae751 Binary files /dev/null and b/assets/grass.png differ diff --git a/assets/tower.png b/assets/tower.png new file mode 100644 index 0000000..1fa634b Binary files /dev/null and b/assets/tower.png differ diff --git a/inputHandling.h b/inputHandling.h new file mode 100644 index 0000000..fdd3359 --- /dev/null +++ b/inputHandling.h @@ -0,0 +1,72 @@ +struct InputHandler{ + int pressed; + Vector2 rectStart; + int clicked; + Vector2 cursorPos; +} InputHandler; + +//TODO: Macht es Sinn ein einzelnes "Game" struct zu haben, das alle möglichen Pointer hat zu allen arrays, camera, textures etc? +// Man hat einen Übergabeparameter mit dem man dann alles verändern kann, man muss nicht alles was man verändern will einzeln übergeben + +/* + cursor.x = GetMousePosition().x - texture.width / 2; + cursor.y = GetMousePosition().y - texture.height / 2; +*/ + +void mouseInput(struct InputHandler *inputHandler, struct Sprite *sprites, int *spriteAmount, Texture2D *texture, Camera2D *camera){ + + inputHandler->cursorPos.x = GetMousePosition().x; + inputHandler->cursorPos.y = GetMousePosition().y; + + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ + if(inputHandler->pressed == 0){ + inputHandler->rectStart.x = GetMousePosition().x; + inputHandler->rectStart.y = GetMousePosition().y; + inputHandler->pressed = 1; + } + } + + if(inputHandler->pressed){ + float width = GetMousePosition().x - inputHandler->rectStart.x; + float height = GetMousePosition().y - inputHandler->rectStart.y; + if(width + height > 1){ + //TODO: Fallunterscheidung für negative width / height? + // Auslagern in eigene Funktion + DrawRectangleLines(inputHandler->rectStart.x, inputHandler->rectStart.y, width, height, GREEN); + } + } + + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + inputHandler->pressed = 0; + float width = GetMousePosition().x - inputHandler->rectStart.x; + float height = GetMousePosition().y - inputHandler->rectStart.y; + if(width + height <= 1){ + printf("Klick\n"); + addSprite(sprites, spriteAmount, texture, inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2, inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2); + } + } + + if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){ + inputHandler->clicked = 1; + sprites->clicked = 1; + sprites->destX = inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2; + sprites->destY = inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2; + } + +} + + +void keyboardInput(struct InputHandler *inputHandler, Camera2D *camera){ + if(IsKeyDown(KEY_W)){ + (*camera).target.y -= 100.0f * GetFrameTime(); + } + if(IsKeyDown(KEY_S)){ + (*camera).target.y += 100.0f * GetFrameTime(); + } + if(IsKeyDown(KEY_D)){ + (*camera).target.x += 100.0f * GetFrameTime(); + } + if(IsKeyDown(KEY_A)){ + (*camera).target.x -= 100.0f * GetFrameTime(); + } +} \ No newline at end of file diff --git a/main.c b/main.c index e37e162..a94017d 100644 --- a/main.c +++ b/main.c @@ -1,28 +1,26 @@ #include "raylib.h" #include "stdio.h" #include "sprite.h" +#include "inputHandling.h" #include "raymath.h" int main(){ - - InitWindow(800, 450, "basic window"); Texture2D texture; Sprite sprites[100]; - int destX = 0; - int destY = 0; - int clicked = 0; + texture = LoadTexture("assets/amulet.png"); - int pressed = 0; - Vector2 rectStart = {0,0}; + int spriteAmount = 0; + struct Sprite cursorSprite = {&texture, 450, 225}; texture = LoadTexture("amulet.png"); int j = 0; Sprite cursor = {&texture, 450, 225}; + struct InputHandler inputHandler; Camera2D camera = { 0 }; camera.target = (Vector2){400, 225}; @@ -31,12 +29,9 @@ int main(){ SpriteAdd(sprites, &j, &texture, cursor.x + camera.target.x, cursor.y + camera.target.y); - - SetTargetFPS(60); while(!WindowShouldClose()){ - BeginDrawing(); ClearBackground(RAYWHITE); @@ -49,10 +44,14 @@ int main(){ } EndMode2D(); - DrawTexture(*cursor.texture, cursor.x, cursor.y, WHITE); + // Moving cursor Sprite to Mouse Pos and drawing it + cursorSprite.x = inputHandler.cursorPos.x - texture.width / 2; + cursorSprite.y = inputHandler.cursorPos.y - texture.height / 2; + DrawSprite(&cursorSprite); - cursor.x = GetMousePosition().x - texture.width / 2; - cursor.y = GetMousePosition().y - texture.height / 2; + // User Input Handling + mouseInput(&inputHandler, sprites, &spriteAmount, &texture, &camera); + keyboardInput(&inputHandler, &camera); /* if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ @@ -61,62 +60,26 @@ int main(){ } */ - if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ - if(pressed == 0){ - rectStart.x = cursor.x; - rectStart.y = cursor.y; - pressed = 1; - } - } - - if(pressed){ - float width = GetMousePosition().x - rectStart.x; - float height = GetMousePosition().y - rectStart.y; - - DrawRectangleLines(rectStart.x, rectStart.y, width, height, GREEN); - } - - + // Sprites move towards their destination + for(i=0; i < spriteAmount; i++){ + if(sprites[i].clicked){ + Vector2 movement = {sprites[i].destX - sprites->x, sprites[i].destY - sprites->y}; - if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ - pressed = 0; - } - - if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){ - clicked = 1; - destX = cursor.x + camera.target.x; - destY = cursor.y + camera.target.y; - } - - if(IsKeyDown(KEY_W)){ - camera.target.y -= 100.0f * GetFrameTime(); - } - if(IsKeyDown(KEY_S)){ - camera.target.y += 100.0f * GetFrameTime(); - } - if(IsKeyDown(KEY_D)){ - camera.target.x += 100.0f * GetFrameTime(); - } - if(IsKeyDown(KEY_A)){ - camera.target.x -= 100.0f * GetFrameTime(); - } - - if(clicked){ - Vector2 movement = {destX - sprites->x, destY - sprites->y}; + if(Vector2Length(movement) < 10.0f){ + inputHandler.clicked = false; + sprites->clicked = false; + } - if(Vector2Length(movement) < 10.0f){ - clicked = false; + movement = Vector2Normalize(movement); + movement = Vector2Scale(movement, 10); + sprites->x += movement.x; + sprites->y += movement.y; } - - movement = Vector2Normalize(movement); - movement = Vector2Scale(movement, 10); - sprites->x += movement.x; - sprites->y += movement.y; } - EndDrawing(); + EndDrawing(); } CloseWindow(); diff --git a/main.o b/main.o index 8506279..4fb5d89 100644 Binary files a/main.o and b/main.o differ diff --git a/spiel b/spiel deleted file mode 100755 index ad1e946..0000000 Binary files a/spiel and /dev/null differ diff --git a/sprite.c b/sprite.c index 42cd1d5..894a25f 100644 --- a/sprite.c +++ b/sprite.c @@ -11,4 +11,4 @@ void SpriteAdd(struct Sprite *cursors, int *j, Texture2D *texture, int x, int y) else{ printf("Voll\n"); } -} \ No newline at end of file +} diff --git a/sprite.h b/sprite.h index cf7d6f5..9f2f7c4 100644 --- a/sprite.h +++ b/sprite.h @@ -6,8 +6,11 @@ typedef struct Sprite { Texture2D *texture; float x; float y; + float destX; + float destY; + int clicked; } Sprite; void SpriteAdd(struct Sprite *cursors, int *j, Texture2D *texture, int x, int y); -#endif \ No newline at end of file +#endif diff --git a/sprite.o b/sprite.o index 8da654d..459c435 100644 Binary files a/sprite.o and b/sprite.o differ