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,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