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.

113 lines
5.0 KiB

#include "selectable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../definitions.h"
#include "../MapObject/building.h"
#include "../Sprite/sprite.h"
#include "../Input/inputHandler.h"
#include "../Entity/entity.h"
Selectable * SelectableInit(Texture2D textures[3], Texture2D backgroundTextures[3], int hasBackground, Vector2 *position, char *description,
int showDescripton, int descriptionLEN, int fontSize, int id, int groupID){
Selectable *selectable = malloc(sizeof(Selectable));
selectable->texture[0] = textures[0];
selectable->texture[1] = textures[1];
selectable->texture[2] = textures[2];
selectable->hasBackground = hasBackground;
if(selectable->hasBackground == 1){
selectable->backgroundTexture[0] = backgroundTextures[0];
selectable->backgroundTexture[1] = backgroundTextures[1];
selectable->backgroundTexture[2] = backgroundTextures[2];
}
//else{
// selectable->backgroundTexture[0] = 0;
// selectable->backgroundTexture[1] = 0;
// selectable->backgroundTexture[2] = 0;
//}
selectable->position.x = position->x;
selectable->position.y = position->y;
strncpy(selectable->description, description, descriptionLEN);
selectable->showDescription = showDescripton;
selectable->fontSize = fontSize;
selectable->id = id;
selectable->state = 0;
selectable->groupID = groupID;
return selectable;
}
void SelectableExecuteSelectable(Selectable *selectable, Game * game){
// Hier sollte eine Bedingung stehen die nur in einem Frame true sein kann
// Oder das selectable wird abgewählt indem seine ID auf default zurückgesetzt wird
// Sonst kann das hier jeden Frame passieren. ID wird nicht automatisch zurückgesetzt
switch(selectable->id){
case SELECTABLE_ID_SPAWN_BUILDING: // Test Case Platzhalter
// Hier sollte eine Bedingung stehen die nur in einem Frame true sein kann
// Oder das selectable wird abgewählt indem seine ID auf default zurückgesetzt wird
// Sonst kann das hier jeden Frame passieren. ID wird nicht automatisch zurückgesetzt
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){
Building *newObject = BuildingInit(game, BU_HOUSE, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y);
BuildingListInsert(game->buildings, newObject);
selectable->state = SELECTABLE_STATE_DEFAULT;
}
break;
case SELECTABLE_ID_SPAWN_WORKER:
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && game->mouseOnUI == 0){
Sprite *newSprite = SpriteCreate(game->textures, TE_WORKER, game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y);
Entity *entity = EntityInit(newSprite, PR_BUILDER, game->textures);
EntityListInsert(game->entities, entity);
SpriteListInsert(game->sprites, newSprite);
selectable->state = SELECTABLE_STATE_DEFAULT;
}
break;
default:
printf("\n\n\n\n\n\n WARNING: Unsupported SELECTABLE ID %d \n\n\n\n\n\n", selectable->id);
break;
}
}
int SelectableUpdateSelectableState(Selectable * selectable){
if(selectable->state == SELECTABLE_STATE_SELECTED){
if(IsMouseButtonDown(MOUSE_BUTTON_RIGHT)){
return selectable->state = SELECTABLE_STATE_DEFAULT;
}
return SELECTABLE_STATE_SELECTED;
}
if(GetMouseX() > selectable->position.x &&
GetMouseX() < selectable->position.x + selectable->backgroundTexture[selectable->state].width &&
GetMouseY() > selectable->position.y &&
GetMouseY() < selectable->position.y + selectable->backgroundTexture[selectable->state].height
){
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)){
return selectable->state = SELECTABLE_STATE_SELECTED;
}
return selectable->state = SELECTABLE_STATE_HOVERED;
}
return selectable->state = SELECTABLE_STATE_DEFAULT;
}
int SelectableUnselectSelectable(Selectable * selectable){
if(selectable->state == SELECTABLE_STATE_SELECTED){
return selectable->state = SELECTABLE_STATE_DEFAULT;
}
return selectable->state;
}
void SelectableDrawSelectable(Selectable * selectable){
int targetWidth = 0;
if(selectable->hasBackground == 1) {
targetWidth = selectable->backgroundTexture[selectable->state].width;
DrawTexture(selectable->backgroundTexture[selectable->state], selectable->position.x, selectable->position.y, WHITE);
}
//DrawTextureEx(selectable->texture[selectable->state], selectable->position, targetWidth / selectable->texture[selectable->state].width, 2, WHITE);
DrawTexture(selectable->texture[selectable->state], selectable->position.x, selectable->position.y, WHITE);
if(selectable->showDescription == 1){
DrawText(selectable->description, selectable->position.x, selectable->position.y, selectable->fontSize, WHITE);
}
}