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.

187 lines
6.4 KiB

#include "inputHandler.h"
#include "raylib.h"
#include "../Sprite/sprite.h"
#include "../IsometricMap/isometricMap.h"
#include "../Entity/entity.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "../IsometricMap/tile.h"
#include "../game.h"
#include "../definitions.h"
#include "../MapObject/staticobjects.h"
void DrawRect(Vector2 rectStart, Vector2 mousePosition){
float width = abs(mousePosition.x - rectStart.x);
float height = abs(mousePosition.y - rectStart.y);
rectStart = GetRectangle(rectStart, mousePosition);
DrawRectangleLines(rectStart.x, rectStart.y, width, height, GREEN);
}
// RETURNS UPPER LEFT CORNER OF THE RECTANGLE USING CURRENT MOUSE POS AND RECT START
Vector2 GetRectangle(Vector2 rectStart, Vector2 mousePos){
float width = mousePos.x - rectStart.x;
float height = mousePos.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;
}
static void SelectEntitiesInsideSelectionRect(Game *game){
// Vorher werden alle Entities abgewählt, außer User hält SHIFT
Entity *current = game->entities->head;
if(!IsKeyDown(KEY_LEFT_SHIFT)){
while (current != 0){
current->selected = 0;
current = current->next;
}
}
Vector2 rect = GetRectangle(game->inputHandler->rectStart, game->inputHandler->cursorPos);
float rectWidth = abs(GetMousePosition().x - game->inputHandler->rectStart.x);
float rectHeight = abs(GetMousePosition().y - game->inputHandler->rectStart.y);
current = game->entities->head;
float deltaX;
float deltaY;
// Jetzt neu anwählen
while (current != 0){
Vector2 currPos = {current->sprite->x + current->sprite->texture->width, current->sprite->y + current->sprite->texture->height/2};
IsometricMapUnproject(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);
if(deltaX > 0 && deltaX < rectWidth && deltaY > 0 && deltaY < rectHeight){
current->selected = 1;
}
current = current->next;
}
}
void mouseInput(Game *game){
/* --- UPDATING INPUT VECTOR VARIABLES --- */
game->inputHandler->cursorPos.x = GetMousePosition().x;
game->inputHandler->cursorPos.y = GetMousePosition().y;
// 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->cursorWorldPos);
// Updating cursorWorldTile
Tile *currentlySelectedTile = IsometricMapGetTileFromWorldCoordinates(game->map, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y);
if(currentlySelectedTile != 0){
game->inputHandler->cursorWorldTile.x = currentlySelectedTile->x;
game->inputHandler->cursorWorldTile.y = currentlySelectedTile->y;
}
/* --- CURSOR SPRITE --- */
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][1];
}
else if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][0];
}
/* --- SELECTION RECT --- */
// Creating new Selection Rect
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
if(game->mouseOnUI == 0){
if(game->inputHandler->selectionRectActive == 0){
game->inputHandler->rectStart.x = GetMousePosition().x;
game->inputHandler->rectStart.y = GetMousePosition().y;
game->inputHandler->selectionRectActive = 1;
}
}
}
// Drawing Rect while selectionRectActive
if(game->inputHandler->selectionRectActive == 1){
DrawRect(game->inputHandler->rectStart, game->inputHandler->cursorPos);
}
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
game->inputHandler->selectionRectActive = 0;
SelectEntitiesInsideSelectionRect(game);
}
/* --- SETTING DESTINATION FOR SELECTED ENTITIES --- */
if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){
float destX = game->inputHandler->cursorWorldPos.x;
float destY = game->inputHandler->cursorWorldPos.y;
if(destX < 0 || destY < 0 || destX > ((game->map->width) * game->map->textureWidth) || destY > ((game->map->height) * game->map->textureHeight)){
printf("OutOfBoundsDestination\n");
}
else{
Entity *current = game->entities->head;
while (current != 0){
if(current->selected){
current->hasDestination = 1;
current->destX = destX;
current->destY = destY;
}
current = current->next;
}
}
}
}
void keyboardInput(Game *game){
// Kamera Movement
float camSpeed = 1000.0f;
if(IsKeyDown(KEY_W)){
game->camera->target.y -= camSpeed * GetFrameTime();
}
if(IsKeyDown(KEY_S)){
game->camera->target.y += camSpeed * GetFrameTime();
}
if(IsKeyDown(KEY_D)){
game->camera->target.x += camSpeed * GetFrameTime();
}
if(IsKeyDown(KEY_A)){
game->camera->target.x -= camSpeed * GetFrameTime();
}
// bissl Kamera Zoom
float maxZoom = 5.0f;
float minZoom = 0.2f;
if(IsKeyPressed(KEY_I)){
if(game->camera->zoom < maxZoom){
game->camera->zoom += 0.2f;
}
}
if(IsKeyPressed(KEY_K)){
if(game->camera->zoom > minZoom){
game->camera->zoom -= 0.2f;
}
}
// Temp code for testing HOLZHACKERLEUTE, weil ich net versteh wie die iButtons hmmpf. früher bescheid wisse! (Jan: "noob")
if(IsKeyPressed(KEY_G)){
//Baum
StaticObject *tree = StaticObjectInit(game, SO_PINETREE, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y);
StaticObjectListInsert(game->objects, tree);
}
if(IsKeyPressed(KEY_P)){
game->currentScreen = SCREEN_PAUSE;
}
}