parent
f29dee2553
commit
1976ce3f2e
@ -0,0 +1,111 @@
|
||||
#include "entity.h"
|
||||
#include "raymath.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../Sprite/sprite.h"
|
||||
|
||||
Entity * EntityInit(Sprite *sprite){
|
||||
Entity *new = malloc(sizeof(Entity));
|
||||
|
||||
new->sprite = sprite;
|
||||
new->destX = 0;
|
||||
new->destY = 0;
|
||||
new->hasDestination = 0;
|
||||
new->selected = 0;
|
||||
new->next = 0;
|
||||
new->prev = 0;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
EntityList * EntityListInit(){
|
||||
EntityList *new = malloc(sizeof(EntityList));
|
||||
new->head = 0;
|
||||
new->tail = 0;
|
||||
return new;
|
||||
}
|
||||
|
||||
void EntityListPrintForward(EntityList *entities){
|
||||
|
||||
}
|
||||
|
||||
void EntityListInsert(EntityList *entities, Entity *data){
|
||||
if(entities->head == 0){
|
||||
entities->head = data;
|
||||
entities->tail = data;
|
||||
}
|
||||
else{
|
||||
entities->tail->next = data;
|
||||
data->prev = entities->tail;
|
||||
entities->tail = data;
|
||||
}
|
||||
}
|
||||
void EntityListRemove(EntityList *entities, Entity *remove){
|
||||
if(remove == 0){
|
||||
printf("WARNING: TRIED TO REMOVE NULLPOINTER\n");
|
||||
}
|
||||
else if(entities->head == remove && entities->tail == remove){
|
||||
entities->head = 0;
|
||||
entities->tail = 0;
|
||||
}
|
||||
else if(entities->head == remove){
|
||||
remove->next->prev = 0;
|
||||
entities->head = remove->next;
|
||||
}
|
||||
else if(entities->tail == remove){
|
||||
remove->prev->next = 0;
|
||||
entities->tail = remove->prev;
|
||||
}
|
||||
else{
|
||||
remove->prev->next = remove->next;
|
||||
remove->next->prev = remove->prev;
|
||||
}
|
||||
|
||||
remove->next = 0;
|
||||
remove->prev = 0;
|
||||
}
|
||||
void EntityListActAllEntities(Game *game){
|
||||
|
||||
EntityList *entities = game->entities;
|
||||
//SpriteListPrintForward(game->sprites);
|
||||
// Sprites move towards their destination
|
||||
float movementSpeed = 150.0f * GetFrameTime();
|
||||
Entity *current = entities->head;
|
||||
|
||||
int counter = 0;
|
||||
while (current != 0){
|
||||
counter ++;
|
||||
if(current->hasDestination == 1){
|
||||
Vector2 movement = {
|
||||
current->destX - current->sprite->x,
|
||||
current->destY - current->sprite->y
|
||||
};
|
||||
if(Vector2Length(movement) < movementSpeed){
|
||||
current->hasDestination = 0;
|
||||
current->sprite->x = current->destX;
|
||||
current->sprite->y = current->destY;
|
||||
}
|
||||
else{
|
||||
movement = Vector2Normalize(movement);
|
||||
movement = Vector2Scale(movement, movementSpeed);
|
||||
current->sprite->x += movement.x;
|
||||
current->sprite->y += movement.y;
|
||||
|
||||
// Change sprite according to direction
|
||||
Vector2 nullvektor = {0,0};
|
||||
float angle = Vector2Angle(movement, nullvektor);
|
||||
angle = angle * RAD2DEG;
|
||||
angle -= 35.26;
|
||||
|
||||
current->sprite->angle = angle;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SpriteUpdate(current->sprite);
|
||||
|
||||
SpriteListSpriteChanged(game->sprites, current->sprite);
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
#ifndef ENTITY_H_
|
||||
#define ENTITY_H_
|
||||
#include "../Sprite/sprite.h"
|
||||
#include "../game.h"
|
||||
|
||||
typedef struct Entity Entity;
|
||||
|
||||
typedef struct Entity{
|
||||
Sprite *sprite;
|
||||
float destX;
|
||||
float destY;
|
||||
int hasDestination;
|
||||
int selected;
|
||||
|
||||
Entity *next;
|
||||
Entity *prev;
|
||||
} Entity;
|
||||
|
||||
typedef struct EntityList{
|
||||
Entity *head;
|
||||
Entity *tail;
|
||||
} EntityList;
|
||||
|
||||
Entity * EntityInit(Sprite *sprite);
|
||||
EntityList * EntityListInit();
|
||||
|
||||
void EntityListPrintForward(EntityList *entities);
|
||||
void EntityListInsert(EntityList *entities, Entity *data);
|
||||
void EntityListRemove(EntityList *entities, Entity *remove);
|
||||
void EntityListActAllEntities(Game *game);
|
||||
|
||||
#endif
|
||||
@ -1,204 +1,201 @@
|
||||
#include "inputHandler.h"
|
||||
#include "raylib.h"
|
||||
#include "../sprite.h"
|
||||
#include "../Sprite/sprite.h"
|
||||
#include "../IsometricMap/isometricMap.h"
|
||||
#include "../Entity/entity.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "../List/list.h"
|
||||
#include "../IsometricMap/tile.h"
|
||||
#include "../game.h"
|
||||
|
||||
void DrawRect(Vector2 rectStart, Vector2 *mousePosition){
|
||||
float width = GetMousePosition().x - rectStart.x;
|
||||
float height = GetMousePosition().y - rectStart.y;
|
||||
float width = GetMousePosition().x - rectStart.x;
|
||||
float height = GetMousePosition().y - rectStart.y;
|
||||
|
||||
rectStart = GetRectangle(rectStart);
|
||||
rectStart = GetRectangle(rectStart);
|
||||
|
||||
DrawRectangleLines(rectStart.x, rectStart.y, abs(width), abs(height), GREEN);
|
||||
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;
|
||||
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;
|
||||
List *sprites = game->sprites;
|
||||
Camera2D *camera = game->camera;
|
||||
IsometricMap **layers = game->layers;
|
||||
Texture2D *texture = game->worker +4;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
if(IsKeyPressed(KEY_K)){
|
||||
if(camera->zoom > minZoom){
|
||||
camera->zoom -= 0.2f;
|
||||
}
|
||||
// resetting last selected Tile to grass texture
|
||||
if(inputHandler->selectedLayer != -1){
|
||||
IsometricMapChangeTextureIdOfTile(layers, (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, inputHandler->selectedLayer, 0);
|
||||
}
|
||||
// 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(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"
|
||||
inputHandler->cursorSprite->texture = (inputHandler->cursorTextures) + 1;
|
||||
}
|
||||
|
||||
// hardcoded layer amount
|
||||
int n = 0;
|
||||
for(n = 0; n >= 0 ; n--){
|
||||
if(layers[n] != 0){
|
||||
float tileWidthHalf = layers[n]->tileTextures[0].width / 2;
|
||||
float tileHeightQuarter = layers[n]->tileTextures[0].height / 4;
|
||||
int mouseAdjustmentX = -tileWidthHalf;
|
||||
int mouseAdjustmentY = -tileHeightQuarter + (tileHeightQuarter * layers[n]->layer);
|
||||
|
||||
// Updating inputHandler->cursorWorldPos Vector2D
|
||||
IsometricMapProject(layers[n], camera,
|
||||
(inputHandler->cursorPos.x / camera->zoom) + mouseAdjustmentX,
|
||||
(inputHandler->cursorPos.y / camera->zoom) + mouseAdjustmentY,
|
||||
&inputHandler->cursorWorldPos);
|
||||
|
||||
/*N I C E*/ Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y, n);
|
||||
|
||||
if(selectedTile != 0){
|
||||
inputHandler->cursorWorldTile.x = selectedTile->x;
|
||||
inputHandler->cursorWorldTile.y = selectedTile->y;
|
||||
inputHandler->selectedLayer = n;
|
||||
// setting currently selected tile to tower
|
||||
IsometricMapChangeTextureIdOfTile(layers, inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, n, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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"
|
||||
inputHandler->cursorSprite->texture = (inputHandler->cursorTextures) + 1;
|
||||
}
|
||||
|
||||
if(inputHandler->pressed){
|
||||
DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos));
|
||||
}
|
||||
|
||||
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
|
||||
inputHandler->pressed = 0;
|
||||
// Cursorsprite is changed back to normal
|
||||
inputHandler->cursorSprite->texture = (inputHandler->cursorTextures);
|
||||
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, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
|
||||
Entity *entity = EntityInit(newSprite);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(inputHandler->pressed){
|
||||
DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos));
|
||||
}
|
||||
|
||||
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
|
||||
inputHandler->pressed = 0;
|
||||
// Cursorsprite is changed back to normal
|
||||
inputHandler->cursorSprite->texture = (inputHandler->cursorTextures);
|
||||
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->layers[0]->width) * game->layers[0]->textureWidth;
|
||||
int maxHeight = (game->layers[0]->height) * game->layers[0]->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 {
|
||||
ListInsert(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y));
|
||||
//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;
|
||||
Node *current = sprites->head;
|
||||
while (current != 0){
|
||||
Vector2 currPos = {current->data->x + current->data->texture->width, current->data->y + current->data->texture->height/2};
|
||||
IsometricMapUnproject(layers, camera, currPos.x, currPos.y, current->data->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->data->selected = 1;
|
||||
}
|
||||
else{
|
||||
current->data->selected = 0;
|
||||
}
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
else{
|
||||
current->selected = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){
|
||||
Node *current = sprites->head;
|
||||
|
||||
while (current != 0){
|
||||
if(current->data->selected){
|
||||
current->data->hasDestination = 1;
|
||||
float destX = inputHandler->cursorWorldPos.x;
|
||||
float destY = inputHandler->cursorWorldPos.y;
|
||||
int maxWidth = (game->layers[0]->width-1) * game->layers[0]->textureWidth;
|
||||
int maxHeight = (game->layers[0]->height-1) * game->layers[0]->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->data->destX = destX;
|
||||
current->data->destY = destY;
|
||||
}
|
||||
|
||||
skip: current = current->next;
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,257 +0,0 @@
|
||||
#include "list.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../sprite.h"
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include "../Textures/textureIDs.h"
|
||||
|
||||
Node * ListCreateNode(Sprite *data){
|
||||
Node *new = malloc(sizeof(Node));
|
||||
new->data = data;
|
||||
new->next = 0;
|
||||
new->prev = 0;
|
||||
return new;
|
||||
}
|
||||
|
||||
void ListPrintForward(List *list){
|
||||
Node *current = list->head;
|
||||
|
||||
printf("\n[\n");
|
||||
while(current != 0){
|
||||
printf("%f,\n", current->data->depth);
|
||||
current = current->next;
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
void ListInsertBefore(List *list, Node *new, Node *current){
|
||||
new->next = current;
|
||||
new->prev = current->prev;
|
||||
current->prev = new;
|
||||
|
||||
if(current == list->head){
|
||||
list->head = new;
|
||||
}
|
||||
else{
|
||||
new->prev->next = new;
|
||||
}
|
||||
}
|
||||
|
||||
void ListInsertAfter(List *list, Node *new, Node *current){
|
||||
new->prev = current;
|
||||
new->next = current->next;
|
||||
current->next = new;
|
||||
|
||||
if(current == list->tail){
|
||||
list->tail = new;
|
||||
}
|
||||
else{
|
||||
new->next->prev = new;
|
||||
}
|
||||
}
|
||||
|
||||
void ListInsert(List *list, Sprite *data){
|
||||
Node *new = ListCreateNode(data);
|
||||
|
||||
if(list->head == 0){
|
||||
list->head = new;
|
||||
list->tail = new;
|
||||
}
|
||||
else{
|
||||
Node *current = list->head;
|
||||
|
||||
while(current != 0){
|
||||
if(new->data->depth < current->data->depth){
|
||||
ListInsertBefore(list, new, current);
|
||||
return;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
ListInsertAfter(list, new, list->tail);
|
||||
}
|
||||
}
|
||||
|
||||
void ListSpriteChanged(List *list, Node *changed){
|
||||
if(changed != list->tail && changed->data->depth > changed->next->data->depth){
|
||||
//Nach rechts
|
||||
|
||||
Node *current = changed->next;
|
||||
ListRemove(list, changed);
|
||||
|
||||
while(current != 0){
|
||||
if(changed->data->depth < current->data->depth){
|
||||
ListInsertBefore(list, changed, current);
|
||||
return;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
ListInsertAfter(list, changed, list->tail);
|
||||
}
|
||||
else if(changed != list->head && changed->data->depth < changed->prev->data->depth){
|
||||
//Nach links
|
||||
|
||||
Node *current = changed->prev;
|
||||
ListRemove(list, changed);
|
||||
|
||||
while(current != 0){
|
||||
if(changed->data->depth > current->data->depth){
|
||||
ListInsertAfter(list, changed, current);
|
||||
return;
|
||||
}
|
||||
current = current->prev;
|
||||
}
|
||||
|
||||
ListInsertBefore(list, changed, list->head);
|
||||
}
|
||||
}
|
||||
|
||||
void ListRemove(List *list, Node *remove){
|
||||
if(remove == 0){
|
||||
printf("WARNING: TRIED TO REMOVE NULLPOINTER\n");
|
||||
}
|
||||
else if(list->head == remove && list->tail == remove){
|
||||
list->head = 0;
|
||||
list->tail = 0;
|
||||
}
|
||||
else if(list->head == remove){
|
||||
remove->next->prev = 0;
|
||||
list->head = remove->next;
|
||||
}
|
||||
else if(list->tail == remove){
|
||||
remove->prev->next = 0;
|
||||
list->tail = remove->prev;
|
||||
}
|
||||
else{
|
||||
remove->prev->next = remove->next;
|
||||
remove->next->prev = remove->prev;
|
||||
}
|
||||
|
||||
remove->next = 0;
|
||||
remove->prev = 0;
|
||||
}
|
||||
|
||||
List * ListInit(){
|
||||
List *newList = malloc(sizeof(List));
|
||||
newList->head = 0;
|
||||
newList->tail = 0;
|
||||
return newList;
|
||||
}
|
||||
|
||||
// iterates over all Sprites in the list and draws them to the world
|
||||
void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){
|
||||
Node *current = list->head;
|
||||
|
||||
// Only drawing the Sprites which are within Camera view
|
||||
Vector2 topleft = {0, 0};
|
||||
IsometricMapProject(map[0], camera, topleft.x, topleft.y, &topleft);
|
||||
Vector2 topright = {GetScreenWidth(), 0};
|
||||
IsometricMapProject(map[0], camera, topright.x, topright.y, &topright);
|
||||
Vector2 botleft = {0, GetScreenHeight()};
|
||||
IsometricMapProject(map[0], camera, botleft.x, botleft.y, &botleft);
|
||||
Vector2 botright = {GetScreenWidth(), GetScreenHeight()};
|
||||
IsometricMapProject(map[0], camera, botright.x, botright.y, &botright);
|
||||
int extraPixels = 0;
|
||||
int itmp, jtmp, maxI, maxJ;
|
||||
itmp = (int)(topleft.x) - extraPixels;
|
||||
jtmp = (int)(topright.y) - extraPixels;
|
||||
maxI = (int)(botright.x) + extraPixels;
|
||||
maxJ = (int)(botleft.y) + extraPixels;
|
||||
while(current != 0){
|
||||
// drawing some extra corner pixels
|
||||
// if extraPixels == 0 you can see flickering in the corners
|
||||
// Only drawing the Sprites which are within Camera view
|
||||
if( current->data->x > itmp &&
|
||||
current->data->y > jtmp &&
|
||||
current->data->x < maxI &&
|
||||
current->data->y < maxJ){
|
||||
DrawSpriteToWorld(current->data, map, camera);
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
// iterates over all Sprites in the list and does their acting (moving etc)
|
||||
void ListActAllSprites(Game *game){
|
||||
//ListPrintForward(game->sprites);
|
||||
// Sprites move towards their destination
|
||||
float movementSpeed = 150.0f * GetFrameTime();
|
||||
Node *current = game->sprites->head;
|
||||
|
||||
int counter = 0;
|
||||
while (current != 0){
|
||||
current->data->depth = current->data->x + current->data->y + current->data->z;
|
||||
ListSpriteChanged(game->sprites, current);
|
||||
counter ++;
|
||||
if(current->data->hasDestination == 1){
|
||||
Vector2 movement = {
|
||||
current->data->destX - current->data->x,
|
||||
current->data->destY - current->data->y
|
||||
};
|
||||
if(Vector2Length(movement) < movementSpeed){
|
||||
current->data->hasDestination = 0;
|
||||
current->data->x = current->data->destX;
|
||||
current->data->y = current->data->destY;
|
||||
}
|
||||
else{
|
||||
movement = Vector2Normalize(movement);
|
||||
movement = Vector2Scale(movement, movementSpeed);
|
||||
current->data->x += movement.x;
|
||||
current->data->y += movement.y;
|
||||
|
||||
// Change sprite according to direction
|
||||
Vector2 nullvektor = {0,0};
|
||||
float angle = Vector2Angle(movement, nullvektor);
|
||||
angle = angle * RAD2DEG;
|
||||
angle -= 35.26;
|
||||
|
||||
if(angle <= 22.5 && angle >= -22.5){
|
||||
// E
|
||||
AnimationChangeAnimation(current->data->animationHandler, E);
|
||||
}
|
||||
else if(angle > 0 && angle <= 67.5){
|
||||
// NE
|
||||
AnimationChangeAnimation(current->data->animationHandler, NE);
|
||||
}
|
||||
else if(angle > 0 && angle <= 112.5){
|
||||
// N
|
||||
AnimationChangeAnimation(current->data->animationHandler, N);
|
||||
}
|
||||
else if(angle > 0 && angle <= 157.5){
|
||||
// NW
|
||||
AnimationChangeAnimation(current->data->animationHandler, NW);
|
||||
}
|
||||
else if(angle < 0 && angle >= -67.5){
|
||||
// SE
|
||||
AnimationChangeAnimation(current->data->animationHandler, SE);
|
||||
}
|
||||
else if(angle < 0 && angle >= -112.5){
|
||||
// S
|
||||
AnimationChangeAnimation(current->data->animationHandler, S);
|
||||
}
|
||||
else if(angle < 0 && angle >= -157.5){
|
||||
// SW
|
||||
AnimationChangeAnimation(current->data->animationHandler, SW);
|
||||
}
|
||||
else{
|
||||
// W
|
||||
AnimationChangeAnimation(current->data->animationHandler, W);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SpriteUpdateAnimation(current->data);
|
||||
// updating z-position according to the tile the sprite stands on
|
||||
Tile *floorTile = IsometricMapGetTileFromWorldCoordinates(game->layers, current->data->x, current->data->y, 0);
|
||||
Tile *topTile = IsometricMapGetMostUpperTile(game->layers, floorTile);
|
||||
current->data->z = topTile->z;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
#ifndef LIST_H_
|
||||
#define LIST_H_
|
||||
#include "../sprite.h"
|
||||
#include "../IsometricMap/isometricMap.h"
|
||||
#include "raylib.h"
|
||||
#include "../game.h"
|
||||
|
||||
typedef struct Node Node;
|
||||
typedef struct List List;
|
||||
|
||||
typedef struct List {
|
||||
Node *head;
|
||||
Node *tail;
|
||||
} List;
|
||||
|
||||
typedef struct Node {
|
||||
Sprite *data;
|
||||
|
||||
Node *next;
|
||||
Node *prev;
|
||||
} Node;
|
||||
|
||||
//Only for internal purpose
|
||||
Node * ListCreateNode(Sprite *data);
|
||||
|
||||
//Print the list in order
|
||||
void ListPrintForward(List *list);
|
||||
|
||||
void ListInsertBefore(List *list, Node *new, Node *current);
|
||||
void ListInsertAfter(List *list, Node *new, Node *current);
|
||||
void ListInsert(List *list, Sprite *data);
|
||||
void ListRemove(List *list, Node *remove);
|
||||
void ListSpriteChanged(List *list, Node *changed);
|
||||
List * ListInit();
|
||||
void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera);
|
||||
void ListActAllSprites(Game *game);
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,59 @@
|
||||
#include "mapobject.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
MapObject * MapObjectInit(Sprite *sprite){
|
||||
MapObject *new = malloc(sizeof(MapObject));
|
||||
|
||||
new->sprite = sprite;
|
||||
new->next = 0;
|
||||
new->prev = 0;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
MapObjectList * MapObjectListInit(){
|
||||
MapObjectList *new = malloc(sizeof(MapObjectList));
|
||||
new->head = 0;
|
||||
new->tail = 0;
|
||||
return new;
|
||||
}
|
||||
|
||||
void MapObjectListPrintForward(MapObjectList *mapObjects){
|
||||
|
||||
}
|
||||
|
||||
void MapObjectListInsert(MapObjectList *mapObjects, MapObject *data){
|
||||
if(mapObjects->head == 0){
|
||||
mapObjects->head = data;
|
||||
mapObjects->tail = data;
|
||||
}
|
||||
else{
|
||||
mapObjects->tail->next = data;
|
||||
data->prev = mapObjects->tail;
|
||||
mapObjects->tail = data;
|
||||
}
|
||||
}
|
||||
void MapObjectListRemove(MapObjectList *mapObjects, MapObject *remove){
|
||||
if(remove == 0){
|
||||
printf("WARNING: TRIED TO REMOVE NULLPOINTER\n");
|
||||
}
|
||||
else if(mapObjects->head == remove && mapObjects->tail == remove){
|
||||
mapObjects->head = 0;
|
||||
mapObjects->tail = 0;
|
||||
}
|
||||
else if(mapObjects->head == remove){
|
||||
remove->next->prev = 0;
|
||||
mapObjects->head = remove->next;
|
||||
}
|
||||
else if(mapObjects->tail == remove){
|
||||
remove->prev->next = 0;
|
||||
mapObjects->tail = remove->prev;
|
||||
}
|
||||
else{
|
||||
remove->prev->next = remove->next;
|
||||
remove->next->prev = remove->prev;
|
||||
}
|
||||
|
||||
remove->next = 0;
|
||||
remove->prev = 0;
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
#ifndef MAPOBJECT_H_
|
||||
#define MAPOBJECT_H_
|
||||
#include "../Sprite/sprite.h"
|
||||
|
||||
typedef struct MapObject MapObject;
|
||||
typedef struct MapObjectList MapObjectList;
|
||||
|
||||
typedef struct MapObject{
|
||||
Sprite *sprite;
|
||||
|
||||
MapObject *next;
|
||||
MapObject *prev;
|
||||
} MapObject;
|
||||
|
||||
typedef struct MapObjectList{
|
||||
MapObject *head;
|
||||
MapObject *tail;
|
||||
} MapObjectList;
|
||||
|
||||
MapObject * MapObjectInit(Sprite *sprite);
|
||||
MapObjectList * MapObjectListInit();
|
||||
|
||||
void MapObjectListPrintForward(MapObjectList *mapObjects);
|
||||
void MapObjectListInsert(MapObjectList *mapObjects, MapObject *data);
|
||||
void MapObjectListRemove(MapObjectList *mapObjects, MapObject *remove);
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,270 @@
|
||||
#include "sprite.h"
|
||||
#include "raylib.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../IsometricMap/isometricMap.h"
|
||||
#include "../Textures/textureIDs.h"
|
||||
#include "../Textures/animationHandler.h"
|
||||
#include "../Textures/animation.h"
|
||||
#include "../Textures/textureatlas.h"
|
||||
#include "../IsometricMap/tile.h"
|
||||
|
||||
void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){
|
||||
|
||||
Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2};
|
||||
|
||||
IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos);
|
||||
|
||||
pos.x -= camera->target.x;
|
||||
pos.y -= camera->target.y;
|
||||
/*
|
||||
if(sprite->selected){
|
||||
DrawTexture(*sprite->texture, pos.x, pos.y, (Color){255, 255, 255, 200});
|
||||
//DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK);
|
||||
}
|
||||
else{
|
||||
*/
|
||||
DrawTexture(*sprite->texture, pos.x, pos.y, WHITE);
|
||||
//}
|
||||
}
|
||||
|
||||
void DrawSpriteToScreen(Sprite *sprite){
|
||||
/*if(sprite->selected){
|
||||
DrawTexture(*sprite->texture, sprite->x, sprite->y, (Color){255, 255, 255, 200});
|
||||
//DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK);
|
||||
}
|
||||
else{
|
||||
*/
|
||||
DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE);
|
||||
// }
|
||||
}
|
||||
|
||||
void SpriteUpdate(Sprite *sprite){
|
||||
|
||||
sprite->depth = sprite->x + sprite->y + sprite->z;
|
||||
|
||||
if(sprite->angle <= 22.5 && sprite->angle >= -22.5){
|
||||
// E
|
||||
AnimationChangeAnimation(sprite->animationHandler, E);
|
||||
}
|
||||
else if(sprite->angle > 0 && sprite->angle <= 67.5){
|
||||
// NE
|
||||
AnimationChangeAnimation(sprite->animationHandler, NE);
|
||||
}
|
||||
else if(sprite->angle > 0 && sprite->angle <= 112.5){
|
||||
// N
|
||||
AnimationChangeAnimation(sprite->animationHandler, N);
|
||||
}
|
||||
else if(sprite->angle > 0 && sprite->angle <= 157.5){
|
||||
// NW
|
||||
AnimationChangeAnimation(sprite->animationHandler, NW);
|
||||
}
|
||||
else if(sprite->angle < 0 && sprite->angle >= -67.5){
|
||||
// SE
|
||||
AnimationChangeAnimation(sprite->animationHandler, SE);
|
||||
}
|
||||
else if(sprite->angle < 0 && sprite->angle >= -112.5){
|
||||
// S
|
||||
AnimationChangeAnimation(sprite->animationHandler, S);
|
||||
}
|
||||
else if(sprite->angle < 0 && sprite->angle >= -157.5){
|
||||
// SW
|
||||
AnimationChangeAnimation(sprite->animationHandler, SW);
|
||||
}
|
||||
else{
|
||||
// W
|
||||
AnimationChangeAnimation(sprite->animationHandler, W);
|
||||
}
|
||||
|
||||
AnimationUpdate(sprite->animationHandler);
|
||||
sprite->texture = sprite->animationHandler->currentFrame->texture;
|
||||
}
|
||||
|
||||
Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){
|
||||
Sprite *newSprite = malloc(sizeof(Sprite));
|
||||
|
||||
//AnimationHandler create
|
||||
Animation **animations = 0;
|
||||
|
||||
if(textureID == worker){
|
||||
animations = atlas->workerAnimations;
|
||||
}
|
||||
else if(textureID == cursor){
|
||||
animations = atlas->cursorAnimation;
|
||||
}
|
||||
else{
|
||||
printf("\n\n\n\n\n\n\n\nSpriteCreate mit falscher ID (%d) aufgerufen oder ID nicht bekannt!!!\n\n\n\n\n\n\n\n", textureID);
|
||||
}
|
||||
|
||||
AnimationHandler *newHandler = AnimationHandlerInit(animations);
|
||||
|
||||
newSprite->animationHandler = newHandler;
|
||||
newSprite->texture = newSprite->animationHandler->currentFrame->texture;
|
||||
newSprite->x = x - newSprite->texture->width / 2;
|
||||
newSprite->y = y - newSprite->texture->height / 2;
|
||||
newSprite->z = 0;
|
||||
newSprite->depth = newSprite->x + newSprite->y;
|
||||
newSprite->angle = 0;
|
||||
newSprite->next = 0;
|
||||
newSprite->prev = 0;
|
||||
|
||||
return newSprite;
|
||||
}
|
||||
|
||||
void SpriteListPrintForward(SpriteList *list){
|
||||
Sprite *current = list->head;
|
||||
|
||||
printf("\n[\n");
|
||||
while(current != 0){
|
||||
printf("%f,\n", current->depth);
|
||||
current = current->next;
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
void SpriteListInsertBefore(SpriteList *list, Sprite *new, Sprite *current){
|
||||
new->next = current;
|
||||
new->prev = current->prev;
|
||||
current->prev = new;
|
||||
|
||||
if(current == list->head){
|
||||
list->head = new;
|
||||
}
|
||||
else{
|
||||
new->prev->next = new;
|
||||
}
|
||||
}
|
||||
|
||||
void SpriteListInsertAfter(SpriteList *list, Sprite *new, Sprite *current){
|
||||
new->prev = current;
|
||||
new->next = current->next;
|
||||
current->next = new;
|
||||
|
||||
if(current == list->tail){
|
||||
list->tail = new;
|
||||
}
|
||||
else{
|
||||
new->next->prev = new;
|
||||
}
|
||||
}
|
||||
|
||||
void SpriteListInsert(SpriteList *list, Sprite *new){
|
||||
|
||||
if(list->head == 0){
|
||||
list->head = new;
|
||||
list->tail = new;
|
||||
}
|
||||
else{
|
||||
Sprite *current = list->head;
|
||||
|
||||
while(current != 0){
|
||||
if(new->depth < current->depth){
|
||||
SpriteListInsertBefore(list, new, current);
|
||||
return;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
SpriteListInsertAfter(list, new, list->tail);
|
||||
}
|
||||
}
|
||||
|
||||
void SpriteListSpriteChanged(SpriteList *list, Sprite *changed){
|
||||
if(changed != list->tail && changed->depth > changed->next->depth){
|
||||
//Nach rechts
|
||||
|
||||
Sprite *current = changed->next;
|
||||
SpriteListRemove(list, changed);
|
||||
|
||||
while(current != 0){
|
||||
if(changed->depth < current->depth){
|
||||
SpriteListInsertBefore(list, changed, current);
|
||||
return;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
SpriteListInsertAfter(list, changed, list->tail);
|
||||
}
|
||||
else if(changed != list->head && changed->depth < changed->prev->depth){
|
||||
//Nach links
|
||||
|
||||
Sprite *current = changed->prev;
|
||||
SpriteListRemove(list, changed);
|
||||
|
||||
while(current != 0){
|
||||
if(changed->depth > current->depth){
|
||||
SpriteListInsertAfter(list, changed, current);
|
||||
return;
|
||||
}
|
||||
current = current->prev;
|
||||
}
|
||||
|
||||
SpriteListInsertBefore(list, changed, list->head);
|
||||
}
|
||||
}
|
||||
|
||||
void SpriteListRemove(SpriteList *list, Sprite *remove){
|
||||
if(remove == 0){
|
||||
printf("WARNING: TRIED TO REMOVE NULLPOINTER\n");
|
||||
}
|
||||
else if(list->head == remove && list->tail == remove){
|
||||
list->head = 0;
|
||||
list->tail = 0;
|
||||
}
|
||||
else if(list->head == remove){
|
||||
remove->next->prev = 0;
|
||||
list->head = remove->next;
|
||||
}
|
||||
else if(list->tail == remove){
|
||||
remove->prev->next = 0;
|
||||
list->tail = remove->prev;
|
||||
}
|
||||
else{
|
||||
remove->prev->next = remove->next;
|
||||
remove->next->prev = remove->prev;
|
||||
}
|
||||
|
||||
remove->next = 0;
|
||||
remove->prev = 0;
|
||||
}
|
||||
|
||||
SpriteList * SpriteListInit(){
|
||||
SpriteList *newSpriteList = malloc(sizeof(SpriteList));
|
||||
newSpriteList->head = 0;
|
||||
newSpriteList->tail = 0;
|
||||
return newSpriteList;
|
||||
}
|
||||
|
||||
// iterates over all Sprites in the list and draws them to the world
|
||||
void SpriteListDrawAllSprites(SpriteList *list, IsometricMap *map, Camera2D *camera){
|
||||
Sprite *current = list->head;
|
||||
|
||||
// Only drawing the Sprites which are within Camera view
|
||||
Vector2 topleft = {0, 0};
|
||||
IsometricMapProject(map, camera, topleft.x, topleft.y, &topleft);
|
||||
Vector2 topright = {GetScreenWidth(), 0};
|
||||
IsometricMapProject(map, camera, topright.x, topright.y, &topright);
|
||||
Vector2 botleft = {0, GetScreenHeight()};
|
||||
IsometricMapProject(map, camera, botleft.x, botleft.y, &botleft);
|
||||
Vector2 botright = {GetScreenWidth(), GetScreenHeight()};
|
||||
IsometricMapProject(map, camera, botright.x, botright.y, &botright);
|
||||
int extraPixels = 0;
|
||||
int itmp, jtmp, maxI, maxJ;
|
||||
itmp = (int)(topleft.x) - extraPixels;
|
||||
jtmp = (int)(topright.y) - extraPixels;
|
||||
maxI = (int)(botright.x) + extraPixels;
|
||||
maxJ = (int)(botleft.y) + extraPixels;
|
||||
while(current != 0){
|
||||
// drawing some extra corner pixels
|
||||
// if extraPixels == 0 you can see flickering in the corners
|
||||
// Only drawing the Sprites which are within Camera view
|
||||
if( current->x > itmp &&
|
||||
current->y > jtmp &&
|
||||
current->x < maxI &&
|
||||
current->y < maxJ){
|
||||
DrawSpriteToWorld(current, map, camera);
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
#ifndef SPRITE_H_
|
||||
#define SPRITE_H_
|
||||
#include "raylib.h"
|
||||
#include "../IsometricMap/isometricMap.h"
|
||||
#include "../Textures/animationHandler.h"
|
||||
#include "../Textures/textureatlas.h"
|
||||
|
||||
typedef struct Sprite Sprite;
|
||||
typedef struct SpriteList SpriteList;
|
||||
|
||||
typedef struct Sprite {
|
||||
AnimationHandler *animationHandler;
|
||||
Texture2D *texture;
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float depth;
|
||||
float angle;
|
||||
|
||||
Sprite *next;
|
||||
Sprite *prev;
|
||||
} Sprite;
|
||||
|
||||
typedef struct SpriteList {
|
||||
Sprite *head;
|
||||
Sprite *tail;
|
||||
} SpriteList;
|
||||
|
||||
void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera);
|
||||
void DrawSpriteToScreen(Sprite *sprite);
|
||||
void SpriteUpdate(Sprite *sprite);
|
||||
|
||||
Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y);
|
||||
|
||||
//Print the list in order
|
||||
void SpriteListPrintForward(SpriteList *list);
|
||||
void SpriteListInsertBefore(SpriteList *list, Sprite *new, Sprite *current);
|
||||
void SpriteListInsertAfter(SpriteList *list, Sprite *new, Sprite *current);
|
||||
void SpriteListInsert(SpriteList *list, Sprite *new);
|
||||
void SpriteListRemove(SpriteList *list, Sprite *remove);
|
||||
void SpriteListSpriteChanged(SpriteList *list, Sprite *changed);
|
||||
SpriteList * SpriteListInit();
|
||||
void SpriteListDrawAllSprites(SpriteList *list, IsometricMap *map, Camera2D *camera);
|
||||
|
||||
#endif
|
||||
|
After Width: | Height: | Size: 2.1 KiB |
@ -1,74 +0,0 @@
|
||||
#include "sprite.h"
|
||||
#include "raylib.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "IsometricMap/isometricMap.h"
|
||||
#include "Textures/textureIDs.h"
|
||||
#include "Textures/animationHandler.h"
|
||||
#include "Textures/animation.h"
|
||||
#include "Textures/textureatlas.h"
|
||||
#include "IsometricMap/tile.h"
|
||||
|
||||
void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera){
|
||||
|
||||
Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2};
|
||||
|
||||
IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos);
|
||||
|
||||
pos.x -= camera->target.x;
|
||||
pos.y -= camera->target.y;
|
||||
if(sprite->selected){
|
||||
DrawTexture(*sprite->texture, pos.x, pos.y, (Color){255, 255, 255, 200});
|
||||
//DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK);
|
||||
}
|
||||
else{
|
||||
DrawTexture(*sprite->texture, pos.x, pos.y, WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
void DrawSpriteToScreen(Sprite *sprite){
|
||||
if(sprite->selected){
|
||||
DrawTexture(*sprite->texture, sprite->x, sprite->y, (Color){255, 255, 255, 200});
|
||||
//DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK);
|
||||
}
|
||||
else{
|
||||
DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
void SpriteUpdateAnimation(Sprite *sprite){
|
||||
AnimationUpdate(sprite->animationHandler);
|
||||
sprite->texture = sprite->animationHandler->currentFrame->texture;
|
||||
}
|
||||
|
||||
Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){
|
||||
Sprite *newSprite = malloc(sizeof(Sprite));
|
||||
|
||||
//AnimationHandler create
|
||||
Animation **animations = 0;
|
||||
|
||||
if(textureID == worker){
|
||||
animations = atlas->workerAnimations;
|
||||
}
|
||||
else if(textureID == cursor){
|
||||
animations = atlas->cursorAnimation;
|
||||
}
|
||||
else{
|
||||
printf("\n\n\n\n\n\n\n\nSpriteCreate mit falscher ID (%d) aufgerufen oder ID nicht bekannt!!!\n\n\n\n\n\n\n\n", textureID);
|
||||
}
|
||||
|
||||
AnimationHandler *newHandler = AnimationHandlerInit(animations);
|
||||
|
||||
newSprite->animationHandler = newHandler;
|
||||
newSprite->texture = newSprite->animationHandler->currentFrame->texture;
|
||||
newSprite->x = x - newSprite->texture->width / 2;
|
||||
newSprite->y = y - newSprite->texture->height / 2;
|
||||
newSprite->z = 0;
|
||||
newSprite->destX = x;
|
||||
newSprite->destY = y;
|
||||
newSprite->hasDestination = 0;
|
||||
newSprite->selected = 0;
|
||||
newSprite->depth = newSprite->x + newSprite->y;
|
||||
|
||||
return newSprite;
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
#ifndef SPRITE_H_
|
||||
#define SPRITE_H_
|
||||
#include "raylib.h"
|
||||
#include "IsometricMap/isometricMap.h"
|
||||
#include "Textures/animationHandler.h"
|
||||
#include "Textures/textureatlas.h"
|
||||
|
||||
typedef struct Sprite {
|
||||
AnimationHandler *animationHandler;
|
||||
Texture2D *texture;
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float destX;
|
||||
float destY;
|
||||
int hasDestination;
|
||||
int selected;
|
||||
float depth;
|
||||
} Sprite;
|
||||
|
||||
void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera);
|
||||
void DrawSpriteToScreen(Sprite *sprite);
|
||||
void SpriteUpdateAnimation(Sprite *sprite);
|
||||
|
||||
Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y);
|
||||
|
||||
#endif
|
||||
Loading…
Reference in new issue