main
JanEhehalt 3 years ago
commit d837bc73fd

@ -1,10 +1,11 @@
{ {
"C_Cpp.errorSquiggles": "Disabled", "C_Cpp.errorSquiggles": "Enabled",
"files.associations": { "files.associations": {
"isometricrenderer.h": "c", "isometricrenderer.h": "c",
"sprite.h": "c", "sprite.h": "c",
"map": "c", "map": "c",
"isometricmap.h": "c", "isometricmap.h": "c",
"animationhandler.h": "c" "animationhandler.h": "c",
"textureids.h": "c"
} }
} }

@ -66,7 +66,7 @@ void mouseInput(Game *game){
// resetting last selected Tile to grass texture // resetting last selected Tile to grass texture
if(inputHandler->selectedLayer != -1){ if(inputHandler->selectedLayer != -1){
IsometricMapChangeTextureIdOfTile(layers[inputHandler->selectedLayer], (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, 0); IsometricMapChangeTextureIdOfTile(layers, (int) inputHandler->cursorWorldTile.x, (int) inputHandler->cursorWorldTile.y, inputHandler->selectedLayer, 0);
} }
/* /*
TODO: n=9 no good style, Segmentation fault when n > layerAmount TODO: n=9 no good style, Segmentation fault when n > layerAmount
@ -74,6 +74,7 @@ void mouseInput(Game *game){
-> Stash size in another variable. -> Stash size in another variable.
printf("%ld \n", sizeof(*layers) / sizeof(layers[0])); printf("%ld \n", sizeof(*layers) / sizeof(layers[0]));
*/ */
// hardcoded layer amount
int n = 9; int n = 9;
for(n = 9; n >= 0 ; n--){ for(n = 9; n >= 0 ; n--){
if(layers[n] != 0){ if(layers[n] != 0){
@ -84,14 +85,14 @@ void mouseInput(Game *game){
IsometricMapProject(layers[n], camera, inputHandler->cursorPos.x + mouseAdjustmentX, inputHandler->cursorPos.y + mouseAdjustmentY, &inputHandler->cursorWorldPos); IsometricMapProject(layers[n], camera, inputHandler->cursorPos.x + mouseAdjustmentX, inputHandler->cursorPos.y + mouseAdjustmentY, &inputHandler->cursorWorldPos);
Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers[n], inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y, n);
if(selectedTile != 0){ if(selectedTile != 0){
inputHandler->cursorWorldTile.x = selectedTile->x; inputHandler->cursorWorldTile.x = selectedTile->x;
inputHandler->cursorWorldTile.y = selectedTile->y; inputHandler->cursorWorldTile.y = selectedTile->y;
inputHandler->selectedLayer = n; inputHandler->selectedLayer = n;
// setting currently selected tile to tower // setting currently selected tile to tower
IsometricMapChangeTextureIdOfTile(layers[n], inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, 1); IsometricMapChangeTextureIdOfTile(layers, inputHandler->cursorWorldTile.x, inputHandler->cursorWorldTile.y, n, 1);
break; break;
} }
} }
@ -122,34 +123,33 @@ void mouseInput(Game *game){
float height = GetMousePosition().y - inputHandler->rectStart.y; float height = GetMousePosition().y - inputHandler->rectStart.y;
// Add Sprite // Add Sprite
if(width + height <= 1){ if(abs(width) + abs(height) < 20){
ListInsertBack(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, 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(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 {
ListInsertBack(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y));
}
} }
// Berechnung, welche Sprites ausgewählt wurden, scuffed // Berechnung, welche Sprites ausgewählt wurden
// TODO: ist wirklich sehr scuffed wegen negativen koordinaten etc
// Problem: Theoretisch einfach nur schauen ob unprojected Sprite coords im Rect liegen
// Muss aber unterschieden werden ob width negativ oder so ist, aber einfach abs funktioniert nicht
// So wie es aktuell ist, funktioniert die Auswahl, aber nur solange die Kamera nicht bewegt wird
Vector2 rect = GetRectangle(inputHandler->rectStart); Vector2 rect = GetRectangle(inputHandler->rectStart);
width = abs(width); width = abs(width);
height = abs(height); height = abs(height);
//printf("Auswahl: x: %f, y: %f, w: %f, h: %f\n", rect.x, rect.y, width, height);
// TODO: update to World Coordinates
float deltaX; float deltaX;
float deltaY; float deltaY;
Node *current = sprites->head; Node *current = sprites->head;
while (current != 0){ while (current != 0){
Vector2 currPos = {current->data.x, current->data.y}; Vector2 currPos = {current->data.x + current->data.texture->width, current->data.y + current->data.texture->height/2};
IsometricMapUnproject(layers[0], camera, currPos.x, currPos.y, &currPos); IsometricMapUnproject(layers, camera, currPos.x, currPos.y, current->data.z, &currPos);
deltaX = currPos.x - camera->target.x - (rect.x + camera->target.x); deltaX = currPos.x - camera->target.x - (rect.x + camera->target.x);
deltaY = currPos.y - camera->target.y - (rect.y + camera->target.y); deltaY = currPos.y - camera->target.y - (rect.y + camera->target.y);
//printf("deltaX: %f, deltaY: %f\n", deltaX, deltaY);
if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){ if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){
current->data.selected = 1; current->data.selected = 1;
} }
@ -169,18 +169,18 @@ void mouseInput(Game *game){
current->data.hasDestination = 1; current->data.hasDestination = 1;
float destX = inputHandler->cursorWorldPos.x; float destX = inputHandler->cursorWorldPos.x;
float destY = inputHandler->cursorWorldPos.y; float destY = inputHandler->cursorWorldPos.y;
if(destX < 0){ destX = 0; } int maxWidth = (game->layers[0]->width-1) * game->layers[0]->textureWidth;
if(destY < 0){ destY = 0; } int maxHeight = (game->layers[0]->height-1) * game->layers[0]->textureHeight;
int maxWidth = (game->layers[0]->widthBounds-1) * game->layers[0]->textureWidth; if(destX < 0){ printf("OutOfBoundsDestination\n"); goto skip; }
int maxHeight = (game->layers[0]->heightBounds-1) * game->layers[0]->textureHeight; if(destY < 0){ printf("OutOfBoundsDestination\n"); goto skip; }
if(destX > maxWidth){ destX = maxWidth; } if(destX > maxWidth){ printf("OutOfBoundsDestination\n"); goto skip; }
if(destY > maxHeight){ destY = maxHeight; } if(destY > maxHeight){ printf("OutOfBoundsDestination\n"); goto skip; }
current->data.destX = destX; current->data.destX = destX;
current->data.destY = destY; current->data.destY = destY;
} }
current = current->next; skip: current = current->next;
} }
} }
} }

@ -5,6 +5,7 @@
#include "raymath.h" #include "raymath.h"
#include "raylib.h" #include "raylib.h"
// returns pointer to IsometricMap Instance
IsometricMap * IsometricMapInit(int layer){ IsometricMap * IsometricMapInit(int layer){
IsometricMap* map = (IsometricMap *) malloc(sizeof(IsometricMap)); IsometricMap* map = (IsometricMap *) malloc(sizeof(IsometricMap));
@ -15,31 +16,32 @@ IsometricMap * IsometricMapInit(int layer){
//map->tileTextures[0] = LoadTexture("assets/desert.png"); //map->tileTextures[0] = LoadTexture("assets/desert.png");
//map->tileTextures[1] = LoadTexture("assets/bigtower.png"); //map->tileTextures[1] = LoadTexture("assets/bigtower.png");
map->originX = 0; map->width = 100;
map->originY = -layer * map->tileTextures[0].width / 4; map->height = 100;
map->width = 100 * map->tileTextures[0].width;
map->height = 100 * map->tileTextures[0].height;
map->widthBounds = 100;
map->heightBounds = 100;
map->layer = layer;
map->textureWidth = map->tileTextures[0].width; map->textureWidth = map->tileTextures[0].width;
map->textureHeight = map->tileTextures[0].height; map->textureHeight = map->tileTextures[0].height;
map->worldPixelWidth = map->width * map->textureWidth;
map->worldPixelWidth = map->height * map->textureHeight;
map->layer = layer;
Tile*** tiles = (Tile***)malloc(map->widthBounds*sizeof(Tile*));
// mallocating the twodimensional Tiles Array
Tile*** tiles = (Tile***)malloc(map->width*sizeof(Tile*));
int n = 0; int n = 0;
for(n=0; n < map->widthBounds; n++){ for(n=0; n < map->width; n++){
tiles[n] = (Tile**)malloc(map->heightBounds*sizeof(Tile*)); tiles[n] = (Tile**)malloc(map->height*sizeof(Tile*));
} }
int i = 0; int i = 0;
int j = 0; int j = 0;
for(i=0; i < map->widthBounds; i++){ for(i=0; i < map->width; i++){
for(j=0; j < map->heightBounds; j++){ for(j=0; j < map->height; j++){
Tile *tmp = (Tile *) malloc(sizeof(Tile)); Tile *tmp = (Tile *) malloc(sizeof(Tile));
// initially all the Tiles are "empty"
tmp->textureId = -1; tmp->textureId = -1;
tmp->x = i; tmp->x = i;
tmp->y = j; tmp->y = j;
tmp->z = layer;
tiles[i][j] = tmp; tiles[i][j] = tmp;
} }
} }
@ -49,6 +51,8 @@ IsometricMap * IsometricMapInit(int layer){
return map; return map;
} }
// For Rendering: calculates coordinate offset for a single tile at arrayPosition x y
// Only works for tiles with texture width == height (and for 22.5 degree?)
Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){ Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){
Vector2* offset = (Vector2 *)malloc(sizeof(Vector2)); Vector2* offset = (Vector2 *)malloc(sizeof(Vector2));
offset->x = x * textureSize/2 - y * textureSize/2; offset->x = x * textureSize/2 - y * textureSize/2;
@ -56,15 +60,16 @@ Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){
return offset; return offset;
} }
// returns Tile at x y on layer isometricMap
Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){ Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){
return map->tiles[x][y]; return map->tiles[x][y];
} }
// Project: Screen Coordinates -> World Coordinates // Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector
void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){
float tileWidthHalf = isometricMap->tileTextures[0].width / 2; float tileWidthHalf = isometricMap->textureWidth / 2;
float tileHeightQuarter = isometricMap->tileTextures[0].height / 4; float tileHeightQuarter = isometricMap->textureHeight / 4;
x += camera->target.x; x += camera->target.x;
y += camera->target.y; y += camera->target.y;
@ -72,7 +77,6 @@ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x,
float xPos = (float) x; float xPos = (float) x;
float yPos = (float) y; float yPos = (float) y;
float isoX = 0.5 * ( xPos / tileWidthHalf + yPos / tileHeightQuarter); float isoX = 0.5 * ( xPos / tileWidthHalf + yPos / tileHeightQuarter);
float isoY = 0.5 * ( -xPos / tileWidthHalf + yPos / tileHeightQuarter); float isoY = 0.5 * ( -xPos / tileWidthHalf + yPos / tileHeightQuarter);
@ -80,52 +84,70 @@ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x,
tmp->y = isoY * isometricMap->tileTextures[0].height; tmp->y = isoY * isometricMap->tileTextures[0].height;
} }
// Unproject: World Coordinates -> Screen Coordinates // Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector
void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, Vector2 *tmp){ void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp){
float xPos = (float) x; float xPos = (float) x;
float yPos = (float) y; float yPos = (float) y;
float worldX = (xPos - yPos) / 2; float screenX = (xPos - yPos) / 2;
float worldY = (xPos + yPos) / 4; float screenY = (xPos + yPos) / 4;
worldX += camera->target.x; screenX += camera->target.x;
worldY += camera->target.y; screenY += camera->target.y;
tmp->x = worldX; // z is currently implemented as z=1 equals 1 layer, z=2 would be two layers height (could be smoothed)
tmp->y = worldY; // hardcoded tile height
screenY -= z * 10;
tmp->x = screenX;
tmp->y = screenY;
} }
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y){ // returns Tile * -> tile at coordinates x y z=layer
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float z){
x = (int)(x / isometricMap->tileTextures->width); int layer = (int) z;
y = (int)(y / isometricMap->tileTextures->height);
Tile *ptr = (Tile *) malloc(sizeof(Tile *));
ptr->x = 0;
ptr->y = 0;
if( x < isometricMap->widthBounds && y < isometricMap->heightBounds && x = (int)(x / isometricMap[layer]->textureWidth);
x >= 0 && y >= 0 ){ y = (int)(y / isometricMap[layer]->textureHeight);
if(isometricMap->tiles[(int)x][(int)y]->textureId != -1){
ptr->x = isometricMap->tiles[(int)x][(int)y]->x; if( x < isometricMap[layer]->width && y < isometricMap[layer]->height && x >= 0 && y >= 0 ){
ptr->y = isometricMap->tiles[(int)x][(int)y]->y; if(isometricMap[layer]->tiles[(int)x][(int)y]->textureId != -1){
return ptr; return (isometricMap[layer]->tiles[(int)x][(int)y]);
} }
} }
ptr = 0; Tile *ptr = 0;
return ptr; return ptr;
} }
// IsometricMapAddTile and IsometricMapChangeTextureIdOfTile pretty much do the same by now... // Gives the most upper Tile above *tile
void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId){ Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){
isometricMap->tiles[x][y]->textureId = textureId; Tile *ptr = (Tile *) malloc(sizeof(Tile *));
isometricMap->tiles[x][y]->x = x; // hardcoded layer amount
isometricMap->tiles[x][y]->y = y; int n = 9;
for(n=9;n>=0;n--){
if( tile->x < isometricMap[n]->width && tile->y < isometricMap[n]->height &&
tile->x >= 0 && tile->y >= 0 ){
if(isometricMap[n]->tiles[tile->x][tile->y]->textureId != -1){
ptr->x = isometricMap[n]->tiles[tile->x][tile->y]->x;
ptr->y = isometricMap[n]->tiles[tile->x][tile->y]->y;
ptr->z = isometricMap[n]->tiles[tile->x][tile->y]->z;
return ptr;
}
}
}
ptr = 0;
return ptr;
} }
void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){
if( x < map->widthBounds && y < map->heightBounds && // changes to Texture ID of tile at x y on maplayer layer
void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id){
if( x < map[layer]->width && y < map[layer]->height &&
x >= 0 && y >= 0 ){ x >= 0 && y >= 0 ){
(map->tiles[x][y])->textureId = id; (map[layer]->tiles[x][y])->textureId = id;
} }
else{
printf("WARNING: trying to change Texture of Tile which is out of bounds!\n");
}
} }

@ -4,30 +4,59 @@
#include "tile.c" #include "tile.c"
typedef struct IsometricMap{ typedef struct IsometricMap{
// Array with all the needed textures for this layer
Texture2D tileTextures[10]; Texture2D tileTextures[10];
// twodimensional array of all the tiles in this layer
Tile ***tiles; Tile ***tiles;
int originX;
int originY; // amount of tiles in x-direction
// TODO: überprüfen, ob width/height und widthBounds/heightBounds überhaupt noch unterschiedlich sind. Wenn nicht eins der beiden löschen
int width; int width;
// amount of tiles in y-direction
int height; int height;
int widthBounds;
int heightBounds; // pixel width of a single tile texture
int textureWidth; int textureWidth;
// pixel height of a single tile texture
int textureHeight; int textureHeight;
// pixel width of the entire map
int worldPixelWidth;
// pixel height of the entire map
int worldPixelHeight;
// layer of the map
int layer; int layer;
} IsometricMap; } IsometricMap;
// returns pointer to IsometricMap Instance
IsometricMap * IsometricMapInit(int layer); IsometricMap * IsometricMapInit(int layer);
// For Rendering: calculates coordinate offset for a single tile at arrayPosition x y
// Only works for tiles with texture width == height (and for 22.5 degree?)
Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize); Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize);
// Gives the most upper Tile above *tile
Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile);
// returns Tile at x y on layer isometricMap
Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y); Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y);
// Project: Screen Coordinates -> World Coordinates
// Project: Screen Coordinates -> World Coordinates writes result in tmp Vector
// Currently only calcing coords on layer 0
void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp); void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp);
// Unproject: World Coordinates -> Screen Coordinates
void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, Vector2 *tmp); // Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector
void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId); void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp);
void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id);
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y); // changes to Texture ID of tile at x y on maplayer layer
void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id);
// returns Tile * -> tile at coordinates x y z=layer
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float layer);
#endif #endif

@ -4,6 +4,7 @@
#include "../Input/inputHandler.h" #include "../Input/inputHandler.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "../game.h"
// @param deprecated // @param deprecated
void IsometricRendererDrawMap(IsometricRenderer *renderer, int height){ void IsometricRendererDrawMap(IsometricRenderer *renderer, int height){
@ -38,23 +39,23 @@ void IsometricRendererDrawMap(IsometricRenderer *renderer, int height){
} }
} }
void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input){ // parameter could be changed to only IsometricMap[]
void IsometricRendererRenderIsometricMap(Game *game){
int n = 0; int n = 0;
int i = 0; int i = 0;
int j = 0; int j = 0;
for(n = 0; n < 10; n++){ for(n = 0; n < 10; n++){
for(i=0; i < map[n]->widthBounds; i++){ for(i=0; i < game->layers[n]->width; i++){
for(j=0; j < map[n]->heightBounds; j++){ for(j=0; j < game->layers[n]->height; j++){
if(map[n]->tiles[i][j]->textureId != -1){ // if tile had texture id -1, it would be empty tile
Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, map[n]->textureWidth); if(game->layers[n]->tiles[i][j]->textureId != -1){
Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, game->layers[n]->textureWidth);
// the higher the layer the higher it needs to be drawed
offset->y -= n * (game->layers[n]->textureHeight/4);
int textureId = game->layers[n]->tiles[i][j]->textureId;
int textureId = map[n]->tiles[i][j]->textureId; DrawTexture(game->layers[n]->tileTextures[textureId], offset->x, offset->y, WHITE);
float x = map[n]->originX + offset->x;
float y = map[n]->originY + offset->y + map[n]->textureHeight - map[n]->tileTextures[textureId].height;
DrawTexture(map[n]->tileTextures[textureId], x, y, WHITE);
free(offset); free(offset);
} }
} }

@ -3,6 +3,7 @@
#include "raylib.h" #include "raylib.h"
#include "isometricMap.h" #include "isometricMap.h"
#include "../Input/inputHandler.h" #include "../Input/inputHandler.h"
#include "../game.h"
typedef struct IsometricRenderer{ typedef struct IsometricRenderer{
Texture *texture; Texture *texture;
@ -11,6 +12,6 @@ typedef struct IsometricRenderer{
// @param deprecated // @param deprecated
void IsometricRendererDrawMap(IsometricRenderer *renderer, int height); void IsometricRendererDrawMap(IsometricRenderer *renderer, int height);
void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input); void IsometricRendererRenderIsometricMap(Game *game);
#endif #endif

@ -2,10 +2,12 @@
#define TILE_H_ #define TILE_H_
#include "raylib.h" #include "raylib.h"
// Tile with textureid = -1 wouldn't be drawed, would just be a placeholder tile
typedef struct Tile{ typedef struct Tile{
int textureId; int textureId;
int x; int x;
int y; int y;
int z;
} Tile; } Tile;
#endif #endif

@ -18,7 +18,7 @@ void ListPrintForward(List *list){
printf("\n[\n"); printf("\n[\n");
while(current != 0){ while(current != 0){
printf("(%f | %f),\n", current->data.x, current->data.y); //printf("(%f | %f),\n", current->data.x, current->data.y);
current = current->next; current = current->next;
} }
printf("]\n"); printf("]\n");
@ -69,7 +69,8 @@ List * ListInit(){
return newList; return newList;
} }
void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera){ // 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; Node *current = list->head;
while(current != 0){ while(current != 0){
@ -78,6 +79,7 @@ void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera){
} }
} }
// iterates over all Sprites in the list and does their acting (moving etc)
void ListActAllSprites(Game *game){ void ListActAllSprites(Game *game){
// Sprites move towards their destination // Sprites move towards their destination
@ -90,7 +92,6 @@ void ListActAllSprites(Game *game){
current->data.destX - current->data.x, current->data.destX - current->data.x,
current->data.destY - current->data.y current->data.destY - current->data.y
}; };
if(Vector2Length(movement) < movementSpeed){ if(Vector2Length(movement) < movementSpeed){
current->data.hasDestination = 0; current->data.hasDestination = 0;
current->data.x = current->data.destX; current->data.x = current->data.destX;
@ -105,7 +106,7 @@ void ListActAllSprites(Game *game){
// Change sprite according to direction // Change sprite according to direction
Vector2 nullvektor = {0,0}; Vector2 nullvektor = {0,0};
float f = Vector2Angle(movement, nullvektor); float f = Vector2Angle(movement, nullvektor);
printf("Angle: %f\n", f); //printf("Angle: %f\n", f);
f /= 3.14; f /= 3.14;
f *= 3.5; f *= 3.5;
f += 3.5; f += 3.5;
@ -115,6 +116,10 @@ void ListActAllSprites(Game *game){
} }
SpriteUpdateAnimation(&current->data); 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; current = current->next;
} }

@ -29,7 +29,7 @@ void ListPrintForward(List *list);
void ListInsertFront(List *list, Sprite *data); void ListInsertFront(List *list, Sprite *data);
void ListInsertBack(List *list, Sprite *data); void ListInsertBack(List *list, Sprite *data);
List * ListInit(); List * ListInit();
void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera); void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera);
void ListActAllSprites(Game *game); void ListActAllSprites(Game *game);
#endif #endif

@ -21,15 +21,13 @@ Fantasy Welt oder Realistisch?
+ Sprites Animationen etc improven + Sprites Animationen etc improven
+ Die Inputs sollten den Kamera Zoom beachten, aktuell geht noch alles kaputt wenn man den zoom umstellt + Die Inputs sollten den Kamera Zoom beachten, aktuell geht noch alles kaputt wenn man den zoom umstellt
+ Funktion, um die ganzen Sprites nach ihrer y-Koordinaten sortiert zu drawen + Funktion, um die ganzen Sprites nach ihrer y-Koordinaten sortiert zu drawen
+ Die Sprites brauchen evtl eine z-Koordinate, die anzeigt auf welchem Layer sie sind + Drawable Container machen, die sortiert werden können, dort kommen alle Tiles und Sprites rein, damit sie dann sortiert werden können
+ Die Sprites müssen auch entsprechend ihrer z-Koordinaten gedrawed werden
+ Wollen wir die Möglichkeit für Höhlen haben? -> Sprites könnten automatisch das höchste Tile als z nehmen (wenn keine Höhlen)
+ Maps in eigenen Dateien speichern + Maps in eigenen Dateien speichern
+ Parser für Map-Dateien + Parser für Map-Dateien
+ MapEditor + MapEditor
+ IsometricMap variablen auf Duplikate prüfen
* Rendering Reihenfolge: layer 0, Sprites auf layer 0, layer 1, Sprites auf layer 1; Theoretisch müssen die einzelnen Layer Reihenweise gedrawed werden mit den Sprites zwischendrin * Rendering Reihenfolge: layer 0, Sprites auf layer 0, layer 1, Sprites auf layer 1; Theoretisch müssen die einzelnen Layer Reihenweise gedrawed werden mit den Sprites zwischendrin
+ IsometricMap Refactoren? + IsometricMap struct erstellen, das den IsometricMap(+Layer) Array speichert ?
### WiP ### WiP

Binary file not shown.

Binary file not shown.

@ -8,6 +8,7 @@
#include "Textures/textureatlas.h" #include "Textures/textureatlas.h"
#include "stdio.h" #include "stdio.h"
// returns pointer to new Game instance
Game *GameInit() Game *GameInit()
{ {
Game *game = (Game *)malloc(sizeof(Game)); Game *game = (Game *)malloc(sizeof(Game));
@ -67,57 +68,57 @@ Game *GameInit()
((game->layers))[n] = IsometricMapInit(n); ((game->layers))[n] = IsometricMapInit(n);
} }
for (n = 0; n <= 10; n++) for (n = 0; n < 10; n++)
{ {
for (i = 0; i < 100; i++) for (i = 0; i < 100; i++)
{ {
for (j = 0; j < 100; j++) for (j = 0; j < 100; j++)
{ {
if(i > 50 && i < 70 && j == 50){
IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0);
}
switch (n) switch (n)
{ {
case 0: case 0:
IsometricMapAddTile(((game->layers))[n], i, j, 0); IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0);
break; break;
case 1: case 1:
if (i > 35 && i < 50 && j > 45 && j < 60) if (i > 35 && i < 50 && j > 45 && j < 60)
{ {
IsometricMapAddTile(((game->layers))[n], i, j, 0); IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0);
} }
break; break;
case 2: case 2:
if (i > 40 && i < 44 && j > 50 && j < 54) if (i > 40 && i < 44 && j > 50 && j < 54)
{ {
IsometricMapAddTile(((game->layers))[n], i, j, 2); IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 2);
} }
break; break;
} }
if(i == j && n == 1){ if(i == j && n == 1){
IsometricMapAddTile(((game->layers))[n], i, j, 0); IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0);
} }
if(i == j && n == 2){ if(i == j && n == 2){
IsometricMapAddTile(((game->layers))[n], i, j, 0); IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0);
} }
if(i == j-1 && n == 1){ if(i == j-1 && n == 1){
IsometricMapAddTile(((game->layers))[n], i, j, 0); IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0);
} }
if(i-1 == j && n == 1){ if(i-1 == j && n == 1){
IsometricMapAddTile(((game->layers))[n], i, j, 0); IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0);
} }
} }
} }
} }
for (n = 0; n <= 10; n++) for (n = 0; n < 10; n++)
{ {
for (i = 0; i < 20 - n * 2; i++) for (i = 0; i < 20 - n * 2; i++)
{ {
for (j = 0; j < 20 - n * 2; j++) for (j = 0; j < 20 - n * 2; j++)
{ {
IsometricMapAddTile(((game->layers))[n], i, j, 0); IsometricMapChangeTextureIdOfTile(game->layers, i, j, n, 0);
if (n == 9)
{
IsometricMapAddTile(((game->layers))[n], i, j, 2);
}
} }
} }
} }

@ -3,7 +3,6 @@
#include "raylib.h" #include "raylib.h"
// So kann man die includes umgehen, also keine Circular dependencies mehr :)
typedef struct Game{ typedef struct Game{
Texture2D worker[8]; Texture2D worker[8];
struct TextureAtlas *textures; struct TextureAtlas *textures;
@ -14,6 +13,7 @@ typedef struct Game{
struct IsometricMap **layers; struct IsometricMap **layers;
} Game; } Game;
// returns pointer to new Game instance
Game * GameInit(); Game * GameInit();
#endif #endif

BIN
game.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
list.o

Binary file not shown.

@ -31,9 +31,8 @@ int main(){
BeginMode2D(*(game->camera)); BeginMode2D(*(game->camera));
IsometricRendererRenderIsometricMap(game->layers, game->inputHandler); IsometricRendererRenderIsometricMap(game);
ListDrawAllSprites(game->sprites, game->layers, game->camera);
ListDrawAllSprites(game->sprites, game->layers[0], game->camera);
EndMode2D(); EndMode2D();
@ -45,11 +44,7 @@ int main(){
// User Input Handling // User Input Handling
mouseInput(game); mouseInput(game);
keyboardInput(game->inputHandler, game->camera); keyboardInput(game->inputHandler, game->camera);
//cursor Positions test
//printf("Cursor Pos: %f %f\n", game->inputHandler->cursorPos.x, game->inputHandler.cursorPos.y)->
//printf("Cursor World Pos: %f %f\n", game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y);
DrawFPS(GetScreenWidth() - 95, 10); DrawFPS(GetScreenWidth() - 95, 10);
EndDrawing(); EndDrawing();

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.

@ -7,13 +7,19 @@
#include "Textures/animationHandler.h" #include "Textures/animationHandler.h"
#include "Textures/animation.h" #include "Textures/animation.h"
#include "Textures/textureatlas.h" #include "Textures/textureatlas.h"
#include "IsometricMap/tile.h"
// @param deprecated
void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){
printf("WARNING: Using deprecated Function SpriteAdd!\n");
/* /*
if(*spriteAmount < 100){ if(*spriteAmount < 100){
(sprites + *spriteAmount) -> texture = texture; (sprites + *spriteAmount) -> texture = texture;
(sprites + *spriteAmount) -> x = x; (sprites + *spriteAmount) -> x = x;
(sprites + *spriteAmount) -> y = y; (sprites + *spriteAmount) -> y = y;
(sprites + *spriteAmount) -> z = 0;
(sprites + *spriteAmount) -> destX = x; (sprites + *spriteAmount) -> destX = x;
(sprites + *spriteAmount) -> destY = y; (sprites + *spriteAmount) -> destY = y;
(sprites + *spriteAmount) -> hasDestination = 0; (sprites + *spriteAmount) -> hasDestination = 0;
@ -26,31 +32,32 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in
*/ */
} }
void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){ void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera){
// TODO: Nach y sortieren, bevor sie gedrawed werden // TODO: Nach y sortieren, bevor sie gedrawed werden
// Wir müssen beachten, dass sie nach den unprojezierten Screen-Koordinaten sortiert werden müssen. // Wir müssen beachten, dass sie nach den unprojezierten Screen-Koordinaten sortiert werden müssen.
// Macht es vielleicht sinn den Sprites auch einen Vector mit ihren Screen Koordinaten zu geben? // Macht es vielleicht sinn den Sprites auch einen Vector mit ihren Screen Koordinaten zu geben?
Vector2 pos = {sprite->x, sprite->y}; Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2};
IsometricMapUnproject(map, camera, pos.x, pos.y, &pos); IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos);
// Also erst ab hier sortieren, mit den Werten aus dem pos Vector // Also erst ab hier sortieren, mit den Werten aus dem pos Vector
// IsometricMap muss auch mit reingerechnet werden -> mögliche Container Lösung steht in der README
pos.x -= camera->target.x; pos.x -= camera->target.x;
pos.y -= camera->target.y; pos.y -= camera->target.y;
if(sprite->selected){ if(sprite->selected){
DrawTexture(*sprite->texture, pos.x - sprite->texture->width/4, pos.y - sprite->texture->height/4, BLACK); DrawTexture(*sprite->texture, pos.x, pos.y, (Color){255, 255, 255, 200});
//DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK);
} }
else{ else{
DrawTexture(*sprite->texture, pos.x - sprite->texture->width/4, pos.y - sprite->texture->height/4, WHITE); DrawTexture(*sprite->texture, pos.x, pos.y, WHITE);
} }
printf("%f %f \n", sprite->x, sprite->y); //printf("%f %f \n", sprite->x, sprite->y);
} }
void DrawSpriteToScreen(Sprite *sprite){ void DrawSpriteToScreen(Sprite *sprite){
if(sprite->selected){ if(sprite->selected){
DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE); DrawTexture(*sprite->texture, sprite->x, sprite->y, (Color){255, 255, 255, 200});
//DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK);
} }
else{ else{

@ -10,15 +10,17 @@ typedef struct Sprite {
Texture2D *texture; Texture2D *texture;
float x; float x;
float y; float y;
float z;
float destX; float destX;
float destY; float destY;
int hasDestination; int hasDestination;
int selected; int selected;
} Sprite; } Sprite;
// @param deprecated
void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y); void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y);
void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera); void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera);
void DrawSpriteToScreen(Sprite *sprite); void DrawSpriteToScreen(Sprite *sprite);
void SpriteUpdateAnimation(Sprite *sprite); void SpriteUpdateAnimation(Sprite *sprite);

Binary file not shown.

Binary file not shown.

BIN
tile.o

Binary file not shown.
Loading…
Cancel
Save