You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.6 KiB

#include "inputHandler.h"
#include "raylib.h"
#include "../sprite.h"
#include "../IsometricMap/isometricMap.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "../List/list.h"
#include "../IsometricMap/tile.h"
//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
void DrawRect(Vector2 rectStart, Vector2 *mousePosition){
float width = GetMousePosition().x - rectStart.x;
float height = GetMousePosition().y - rectStart.y;
if(width < 0 && height >= 0){
width *= -1;
rectStart.x -= width;
}
else if(height < 0 && width >= 0){
height *= -1;
rectStart.y -= height;
}
else if(height < 0 && width < 0){
height *= -1;
width *= -1;
rectStart.x -= width;
rectStart.y -= height;
}
DrawRectangleLines(rectStart.x, rectStart.y, width, height, GREEN);
}
Vector2 GetRectangle(Vector2 rectStart){
float width = GetMousePosition().x - rectStart.x;
float height = GetMousePosition().y - rectStart.y;
if(width < 0 && height >= 0){
width *= -1;
rectStart.x -= width;
}
else if(height < 0 && width >= 0){
height *= -1;
rectStart.y -= height;
}
else if(height < 0 && width < 0){
height *= -1;
width *= -1;
rectStart.x -= width;
rectStart.y -= height;
}
return rectStart;
}
void mouseInput(InputHandler *inputHandler, List *sprites, Texture2D *texture, Camera2D *camera, IsometricMap **layers){
inputHandler->cursorPos.x = GetMousePosition().x;
inputHandler->cursorPos.y = GetMousePosition().y;
// resetting last selected Tile to grass texture
int n = 9;
if(inputHandler->selectedLayer != -1){
IsometricMapChangeTextureIdOfTile(layers[inputHandler->selectedLayer], (int) inputHandler->cursorWorldPos.x, (int) inputHandler->cursorWorldPos.y, 0);
}
// TODO: n=2 no good style, but Segmentation fault when > layerAmount
for(n = 10; n >= 0 ; n--){
if(layers[n] != 0){
IsometricMapProject(layers[n], camera, inputHandler->cursorPos.x, inputHandler->cursorPos.y, &inputHandler->cursorWorldPos);
Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers[n], inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
if(selectedTile != 0){
inputHandler->cursorWorldPos.x = selectedTile->x;
inputHandler->cursorWorldPos.y = selectedTile->y;
// setting currently selected tile to tower
inputHandler->selectedLayer = n;
IsometricMapChangeTextureIdOfTile(layers[n], (int) inputHandler->cursorWorldPos.x, (int) inputHandler->cursorWorldPos.y, 1);
break;
}
}
}
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){
DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos));
}
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
inputHandler->pressed = 0;
float width = GetMousePosition().x - inputHandler->rectStart.x;
float height = GetMousePosition().y - inputHandler->rectStart.y;
// Add Sprite
if(width + height <= 1){
ListInsertBack(sprites, SpriteCreate(texture, inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2, inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2));
}
// Berechnung, welche Sprites ausgewählt wurden
Vector2 rect = GetRectangle(inputHandler->rectStart);
width = abs(width);
height = abs(height);
printf("Auswahl: x: %f, y: %f, w: %f, h: %f\n", rect.x, rect.y, width, height);
float deltaX;
float deltaY;
Node *current = sprites->head;
while (current != 0){
deltaX = current->data.x + current->data.texture->width/2 - (rect.x + camera->target.x);
deltaY = current->data.y + current->data.texture->height/2 - (rect.y + camera->target.y);
printf("deltaX: %f, deltaY: %f\n", deltaX, deltaY);
if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){
current->data.selected = 1;
}
else{
current->data.selected = 0;
}
current = current->next;
}
}
if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){
Node *current = sprites->head;
while (current != 0){
if(current->data.selected){
current->data.hasDestination = 1;
current->data.destX = inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2;
current->data.destY = inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2;
}
current = current->next;
}
}
}
void keyboardInput(InputHandler *inputHandler, Camera2D *camera){
if(IsKeyDown(KEY_W)){
(*camera).target.y -= 1000.0f * GetFrameTime();
}
if(IsKeyDown(KEY_S)){
(*camera).target.y += 1000.0f * GetFrameTime();
}
if(IsKeyDown(KEY_D)){
(*camera).target.x += 1000.0f * GetFrameTime();
}
if(IsKeyDown(KEY_A)){
(*camera).target.x -= 1000.0f * GetFrameTime();
}
}