movement now snaps to dest position

main
JanEhehalt 3 years ago
parent d573eba6d4
commit 0dc8dc83c6

@ -13,6 +13,6 @@ Fantasy Welt oder Realistisch?
## TODO
- Bug with movement of sprite
- Bug with movement of sprite after spawning a few other sprites
- Drawn Rectangle problems with negative width/height
- Selecting Sprites for moving all selected to the same destination

@ -1,7 +1,6 @@
struct InputHandler{
int pressed;
Vector2 rectStart;
int clicked;
Vector2 cursorPos;
} InputHandler;
@ -41,14 +40,13 @@ void mouseInput(struct InputHandler *inputHandler, struct Sprite *sprites, int *
float width = GetMousePosition().x - inputHandler->rectStart.x;
float height = GetMousePosition().y - inputHandler->rectStart.y;
if(width + height <= 1){
printf("Klick\n");
//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->hasDestination = 1;
sprites->destX = inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2;
sprites->destY = inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2;
}

@ -51,19 +51,22 @@ int main(){
// Sprites move towards their destination
float movementSpeed = 10.0f;
for(i=0; i < spriteAmount; i++){
if(sprites[i].clicked){
if(sprites[i].hasDestination == 1){
Vector2 movement = {sprites[i].destX - sprites->x, sprites[i].destY - sprites->y};
if(Vector2Length(movement) < 10.0f){
inputHandler.clicked = false;
sprites->clicked = false;
if(Vector2Length(movement) < movementSpeed){
sprites->hasDestination = 0;
sprites->x = sprites->destX;
sprites->y = sprites->destY;
}
else{
movement = Vector2Normalize(movement);
movement = Vector2Scale(movement, movementSpeed);
sprites->x += movement.x;
sprites->y += movement.y;
}
movement = Vector2Normalize(movement);
movement = Vector2Scale(movement, 10);
sprites->x += movement.x;
sprites->y += movement.y;
}
}

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.

@ -4,7 +4,7 @@ struct Sprite{
float y;
float destX;
float destY;
int clicked;
int hasDestination;
} Sprite;
void addSprite(struct Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){
@ -13,10 +13,10 @@ void addSprite(struct Sprite *sprites, int *spriteAmount, Texture2D *texture, in
(sprites + *spriteAmount) -> x = x;
(sprites + *spriteAmount) -> y = y;
(*spriteAmount)++;
printf("SPRITE\n");
//printf("SPRITE\n");
}
else{
printf("Voll\n");
printf("Sprites Voll\n");
}
}

Loading…
Cancel
Save