merge conflict

main
Jonathan Hager 3 years ago
commit ac26ea6e60
Signed by: JonathanHager
GPG Key ID: 34881E488569708C

@ -0,0 +1,5 @@
{
"files.associations": {
"inputhandling.h": "c"
}
}

@ -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

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

@ -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();
}
}

@ -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();

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.

@ -6,6 +6,9 @@ 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);

Binary file not shown.
Loading…
Cancel
Save