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.
219 lines
8.4 KiB
219 lines
8.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 = GetMousePosition().x - rectStart.x;
|
|
float height = GetMousePosition().y - rectStart.y;
|
|
|
|
rectStart = GetRectangle(rectStart);
|
|
|
|
DrawRectangleLines(rectStart.x, rectStart.y, abs(width), abs(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(Game *game){
|
|
|
|
InputHandler *inputHandler = game->inputHandler;
|
|
EntityList *entities = game->entities;
|
|
Camera2D *camera = game->camera;
|
|
IsometricMap *map = game->map;
|
|
Texture2D *texture = game->worker +4;
|
|
|
|
inputHandler->cursorPos.x = GetMousePosition().x;
|
|
inputHandler->cursorPos.y = GetMousePosition().y;
|
|
|
|
|
|
// bissl Kamera Zoom
|
|
float maxZoom = 5.0f;
|
|
float minZoom = 0.2f;
|
|
if(IsKeyPressed(KEY_I)){
|
|
if(camera->zoom < maxZoom){
|
|
camera->zoom += 0.2f;
|
|
}
|
|
}
|
|
if(IsKeyPressed(KEY_K)){
|
|
if(camera->zoom > minZoom){
|
|
camera->zoom -= 0.2f;
|
|
}
|
|
}
|
|
|
|
// Temp code for testing HOLZHACKERLEUTE, weil ich net versteh wie die iButtons hmmpf. früher bescheid wisse!
|
|
if(IsKeyPressed(KEY_G)){
|
|
//Baum
|
|
StaticObject *tree = StaticObjectInit(game, SO_PINETREE, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
|
|
StaticObjectListInsert(game->objects, tree);
|
|
}
|
|
|
|
if(IsKeyPressed(KEY_H)){
|
|
//Holzkacker
|
|
Entity *lumberjack = EntityInit(game, PR_LUMBERJACK, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
|
|
EntityListInsert(game->entities, lumberjack);
|
|
}
|
|
|
|
// resetting last selected Tile to grass texture
|
|
if(inputHandler->selectedLayer != -1){
|
|
IsometricMapChangeTextureIdOfTile(map, (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, 0);
|
|
}
|
|
|
|
// hardcoded layer amount
|
|
float tileWidthHalf = map->tileTextures[0].width / 2;
|
|
float tileHeightQuarter = map->tileTextures[0].height / 4;
|
|
int mouseAdjustmentX = -tileWidthHalf;
|
|
int mouseAdjustmentY = -tileHeightQuarter;
|
|
|
|
// Updating inputHandler->cursorWorldPos Vector2D
|
|
IsometricMapProject(map, camera,
|
|
(inputHandler->cursorPos.x / camera->zoom) + mouseAdjustmentX,
|
|
(inputHandler->cursorPos.y / camera->zoom) + mouseAdjustmentY,
|
|
&inputHandler->cursorWorldPos);
|
|
|
|
Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(game->map, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
|
|
|
|
if(selectedTile != 0){
|
|
inputHandler->selectedLayer = 0;
|
|
inputHandler->cursorWorldTile.x = selectedTile->x;
|
|
inputHandler->cursorWorldTile.y = selectedTile->y;
|
|
// setting currently selected tile to tower
|
|
IsometricMapChangeTextureIdOfTile(map, inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, 1);
|
|
}
|
|
|
|
if(game->mouseOnUI == 0){
|
|
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
|
|
if(inputHandler->pressed == 0){
|
|
inputHandler->rectStart.x = GetMousePosition().x;
|
|
inputHandler->rectStart.y = GetMousePosition().y;
|
|
inputHandler->pressed = 1;
|
|
|
|
// Cursorsprite is changed to "down"
|
|
game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][1];
|
|
}
|
|
}
|
|
}
|
|
|
|
if(inputHandler->pressed){
|
|
DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos));
|
|
}
|
|
|
|
if(game->mouseOnUI == 0){
|
|
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
|
|
inputHandler->pressed = 0;
|
|
// Cursorsprite is changed back to normal
|
|
game->cursorSprite->texture = &game->textures->textures[TE_CURSOR][0];
|
|
float width = GetMousePosition().x - inputHandler->rectStart.x;
|
|
float height = GetMousePosition().y - inputHandler->rectStart.y;
|
|
|
|
|
|
// Add Sprite
|
|
if(abs(width) + abs(height) < 20){
|
|
int maxWidth = (game->map->width) * game->map->textureWidth;
|
|
int maxHeight = (game->map->height) * game->map->textureHeight;
|
|
if(inputHandler->cursorWorldPos.x < 0){ printf("OutOfBoundsDestination Spawn\n");}
|
|
else if(inputHandler->cursorWorldPos.y < 0){ printf("OutOfBoundsDestination Spawn\n");}
|
|
else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");}
|
|
else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");}
|
|
else {
|
|
//Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
|
|
//Entity *entity = EntityInit(newSprite, PR_BUILDER, game->textures);
|
|
//EntityListInsert(game->entities, entity);
|
|
//SpriteListInsert(game->sprites, newSprite);
|
|
//ListPrintForward(sprites);
|
|
//ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y));
|
|
}
|
|
} else{
|
|
// Berechnung, welche Sprites ausgewählt wurden
|
|
Vector2 rect = GetRectangle(inputHandler->rectStart);
|
|
width = abs(width);
|
|
height = abs(height);
|
|
|
|
float deltaX;
|
|
float deltaY;
|
|
Entity *current = game->entities->head;
|
|
while (current != 0){
|
|
Vector2 currPos = {current->sprite->x + current->sprite->texture->width, current->sprite->y + current->sprite->texture->height/2};
|
|
IsometricMapUnproject(map, camera, currPos.x, currPos.y, current->sprite->z, &currPos);
|
|
|
|
deltaX = currPos.x - camera->target.x - (rect.x + camera->target.x);
|
|
deltaY = currPos.y - camera->target.y - (rect.y + camera->target.y);
|
|
|
|
if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){
|
|
current->selected = 1;
|
|
}
|
|
else{
|
|
current->selected = 0;
|
|
}
|
|
current = current->next;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){
|
|
Entity *current = game->entities->head;
|
|
|
|
while (current != 0){
|
|
if(current->selected){
|
|
current->hasDestination = 1;
|
|
float destX = inputHandler->cursorWorldPos.x;
|
|
float destY = inputHandler->cursorWorldPos.y;
|
|
int maxWidth = (game->map->width-1) * game->map->textureWidth;
|
|
int maxHeight = (game->map->height-1) * game->map->textureHeight;
|
|
if(destX < 0){ printf("OutOfBoundsDestination\n"); goto skip; }
|
|
if(destY < 0){ printf("OutOfBoundsDestination\n"); goto skip; }
|
|
if(destX > maxWidth){ printf("OutOfBoundsDestination\n"); goto skip; }
|
|
if(destY > maxHeight){ printf("OutOfBoundsDestination\n"); goto skip; }
|
|
|
|
current->destX = destX;
|
|
current->destY = destY;
|
|
}
|
|
|
|
skip: current = current->next;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void keyboardInput(InputHandler *inputHandler, Camera2D *camera){
|
|
float camSpeed = 1000.0f;
|
|
if(IsKeyDown(KEY_W)){
|
|
(*camera).target.y -= camSpeed * GetFrameTime();
|
|
}
|
|
if(IsKeyDown(KEY_S)){
|
|
(*camera).target.y += camSpeed * GetFrameTime();
|
|
}
|
|
if(IsKeyDown(KEY_D)){
|
|
(*camera).target.x += camSpeed * GetFrameTime();
|
|
}
|
|
if(IsKeyDown(KEY_A)){
|
|
(*camera).target.x -= camSpeed * GetFrameTime();
|
|
}
|
|
}
|