multiple layers

main
JanEhehalt 3 years ago
parent 7fc4434836
commit f69445c5c7

@ -62,22 +62,22 @@ void mouseInput(InputHandler *inputHandler, List *sprites, Texture2D *texture, C
// resetting last selected Tile to grass texture // resetting last selected Tile to grass texture
int n = 9; int n = 9;
if(inputHandler->selectedLayer != -1){ if(inputHandler->selectedLayer != -1){
IsometricMapChangeTextureIdOfTile(layers[inputHandler->selectedLayer], (int) inputHandler->selectedTile.x, (int) inputHandler->selectedTile.y, 0); IsometricMapChangeTextureIdOfTile(layers[inputHandler->selectedLayer], (int) inputHandler->cursorWorldPos.x, (int) inputHandler->cursorWorldPos.y, 0);
} }
for(n = 2; n >= 0 ; n--){ // TODO: n=2 no good style, but Segmentation fault when > layerAmount
for(n = 10; n >= 0 ; n--){
if(layers[n] != 0){ if(layers[n] != 0){
IsometricMapProject(layers[n], camera, inputHandler->cursorPos.x, inputHandler->cursorPos.y, &inputHandler->selectedTile); IsometricMapProject(layers[n], camera, inputHandler->cursorPos.x, inputHandler->cursorPos.y, &inputHandler->cursorWorldPos);
Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers[n], inputHandler->selectedTile.x, inputHandler->selectedTile.y); Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers[n], inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y);
if(selectedTile != 0){ if(selectedTile != 0){
//printf("ALARM bei n = %d \n", n); inputHandler->cursorWorldPos.x = selectedTile->x;
inputHandler->selectedTile.x = selectedTile->x; inputHandler->cursorWorldPos.y = selectedTile->y;
inputHandler->selectedTile.y = selectedTile->y;
// setting currently selected tile to tower // setting currently selected tile to tower
inputHandler->selectedLayer = n; inputHandler->selectedLayer = n;
IsometricMapChangeTextureIdOfTile(layers[n], (int) inputHandler->selectedTile.x, (int) inputHandler->selectedTile.y, 1); IsometricMapChangeTextureIdOfTile(layers[n], (int) inputHandler->cursorWorldPos.x, (int) inputHandler->cursorWorldPos.y, 1);
break; break;
} }
} }

@ -9,7 +9,7 @@ typedef struct InputHandler{
int pressed; int pressed;
Vector2 rectStart; Vector2 rectStart;
Vector2 cursorPos; Vector2 cursorPos;
Vector2 selectedTile; Vector2 cursorWorldPos;
int selectedLayer; int selectedLayer;
} InputHandler; } InputHandler;

@ -5,39 +5,37 @@
#include "raymath.h" #include "raymath.h"
#include "raylib.h" #include "raylib.h"
IsometricMap * IsometricMapInit(int x, int y, int layer){ IsometricMap * IsometricMapInit(int layer){
IsometricMap* map = (IsometricMap *) malloc(sizeof(IsometricMap)); IsometricMap* map = (IsometricMap *) malloc(sizeof(IsometricMap));
map->tileTextures[0] = LoadTexture("assets/grass.png"); map->tileTextures[0] = LoadTexture("assets/grass.png");
map->tileTextures[1] = LoadTexture("assets/tower.png"); map->tileTextures[1] = LoadTexture("assets/tower.png");
map->originX = 0; map->originX = 0;
map->originY = -layer * map->tileTextures[0].width / 4; map->originY = -layer * map->tileTextures[0].width / 4;
map->width = x * map->tileTextures[0].width; map->width = 100 * map->tileTextures[0].width;
map->height = y * map->tileTextures[0].height; map->height = 100 * map->tileTextures[0].height;
map->widthBounds = 100; map->widthBounds = 100;
map->heightBounds = 100; map->heightBounds = 100;
map->layer = layer;
Tile*** tiles = (Tile***)malloc(x*sizeof(Tile*)); Tile*** tiles = (Tile***)malloc(map->widthBounds*sizeof(Tile*));
int n = 0; int n = 0;
for(n=0; n<x; n++){ for(n=0; n < map->widthBounds; n++){
tiles[n] = (Tile**)malloc(y*sizeof(Tile*)); tiles[n] = (Tile**)malloc(map->heightBounds*sizeof(Tile*));
} }
map->sizeX = x;
map->sizeY = y;
int i = 0; int i = 0;
int j = 0; int j = 0;
for(i=0; i < map->widthBounds; i++){
for(i=0; i < x; i++){ for(j=0; j < map->heightBounds; j++){
for(j=0; j < y; j++){
Tile *tmp = (Tile *) malloc(sizeof(Tile)); Tile *tmp = (Tile *) malloc(sizeof(Tile));
tmp->textureId = 0; tmp->textureId = -1;
tmp->x = i; tmp->x = i;
tmp->y = j; tmp->y = j;
tiles[i][j] = tmp; tiles[i][j] = tmp;
} }
} }
map->tiles = tiles; map->tiles = tiles;
return map; return map;
@ -63,12 +61,13 @@ Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){
void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){
int mouseAdjustmentX = -8;
int mouseAdjustmentY = -4;
float tileWidthHalf = isometricMap->tileTextures[0].width / 2; float tileWidthHalf = isometricMap->tileTextures[0].width / 2;
float tileHeightQuarter = isometricMap->tileTextures[0].height / 4; float tileHeightQuarter = isometricMap->tileTextures[0].height / 4;
int mouseAdjustmentX = -tileWidthHalf;
int mouseAdjustmentY = -tileHeightQuarter + (tileHeightQuarter * isometricMap->layer);
x += camera->target.x + mouseAdjustmentX; x += camera->target.x + mouseAdjustmentX;
y += camera->target.y + mouseAdjustmentY; y += camera->target.y + mouseAdjustmentY;
@ -92,11 +91,13 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float
ptr->x = 0; ptr->x = 0;
ptr->y = 0; ptr->y = 0;
if( x < isometricMap->sizeX && y < isometricMap->sizeY && if( x < isometricMap->widthBounds && y < isometricMap->heightBounds &&
x >= 0 && y >= 0 ){ x >= 0 && y >= 0 ){
ptr->x = isometricMap->tiles[(int)x][(int)y]->x; if(isometricMap->tiles[(int)x][(int)y]->textureId != -1){
ptr->y = isometricMap->tiles[(int)x][(int)y]->y; ptr->x = isometricMap->tiles[(int)x][(int)y]->x;
return ptr; ptr->y = isometricMap->tiles[(int)x][(int)y]->y;
return ptr;
}
} }
ptr = 0; ptr = 0;
return ptr; return ptr;
@ -105,23 +106,14 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float
void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId){ void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId){
isometricMap->tiles[x][y]->textureId = textureId;
//if(x < isometricMap->sizeX && y < isometricMap->sizeY){ isometricMap->tiles[x][y]->x = x;
isometricMap->tiles[x][y]->y = y;
Tile *tile = (Tile *) malloc(sizeof(Tile));
tile->textureId = textureId;
tile->x = x;
tile->y = y;
//Tile tile = {textureId, x, y};
isometricMap->tiles[x][y] = tile;
//}
} }
void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){ void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){
if( x < map->sizeX && y < map->sizeY && if( x < map->widthBounds && y < map->heightBounds &&
x >= 0 && y >= 0 ){ x >= 0 && y >= 0 ){
(map->tiles[x][y])->textureId = id; (map->tiles[x][y])->textureId = id;
} }

@ -6,14 +6,13 @@
typedef struct IsometricMap{ typedef struct IsometricMap{
Texture2D tileTextures[10]; Texture2D tileTextures[10];
Tile ***tiles; Tile ***tiles;
int sizeX;
int sizeY;
int originX; int originX;
int originY; int originY;
int width; int width;
int height; int height;
int widthBounds; int widthBounds;
int heightBounds; int heightBounds;
int layer;
} IsometricMap; } IsometricMap;
// TODO: // TODO:
@ -22,7 +21,7 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float
Vector2 IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y); Vector2 IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y);
// Working // Working
IsometricMap * IsometricMapInit(int x, int y, int layer); IsometricMap * IsometricMapInit(int layer);
Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize); Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize);
Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y); Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y);
// Screen Coordinates -> World Coordinates // Screen Coordinates -> World Coordinates

@ -40,20 +40,22 @@ void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input
int n = 0; int n = 0;
int i = 0; int i = 0;
int j = 0; int j = 0;
for(n = 0; n < 9; n++){ for(n = 0; n < 10; n++){
if(map[n] == 0){ if(map[n] == 0){
break; break;
} }
for(i=0; i < map[n]->sizeX; i++){ for(i=0; i < map[n]->widthBounds; i++){
for(j=0; j < map[n]->sizeY; j++){ for(j=0; j < map[n]->heightBounds; j++){
Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, map[n]->tileTextures[0].width); if(map[n]->tiles[i][j]->textureId != -1){
Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, map[n]->tileTextures[0].width);
float x = map[n]->originX + offset->x;
float y = map[n]->originY + offset->y; float x = map[n]->originX + offset->x;
float y = map[n]->originY + offset->y;
int textureId = map[n]->tiles[i][j]->textureId;
int textureId = map[n]->tiles[i][j]->textureId;
DrawTexture(map[n]->tileTextures[textureId], x, y, WHITE);
DrawTexture(map[n]->tileTextures[textureId], x, y, WHITE);
}
} }
} }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
list.o

Binary file not shown.

@ -27,8 +27,8 @@ int main(){
inputHandler.rectStart.y = 0; inputHandler.rectStart.y = 0;
inputHandler.cursorPos.x = 0; inputHandler.cursorPos.x = 0;
inputHandler.cursorPos.y = 0; inputHandler.cursorPos.y = 0;
inputHandler.selectedTile.x = 0; inputHandler.cursorWorldPos.x = 0;
inputHandler.selectedTile.y = 0; inputHandler.cursorWorldPos.y = 0;
inputHandler.selectedLayer = -1; inputHandler.selectedLayer = -1;
Camera2D camera = { 0 }; Camera2D camera = { 0 };
@ -47,10 +47,47 @@ int main(){
IsometricMap **layers = (IsometricMap **) malloc(10*sizeof(IsometricMap *)); IsometricMap **layers = (IsometricMap **) malloc(10*sizeof(IsometricMap *));
layers[0] = IsometricMapInit(50, 80, 0); int n = 0;
layers[1] = IsometricMapInit(20, 20, 1); int i = 0;
layers[2] = IsometricMapInit(15, 10, 2); int j = 0;
for(n = 0; n < 10; n++){
layers[n] = IsometricMapInit(n);
}
for(n = 0; n <= 10; n++){
for(i = 0; i < 100; i++){
for(j = 0; j < 100; j++){
switch(n){
case 0:
IsometricMapAddTile(layers[n], i, j, 0);
break;
case 1:
if(i > 35 && i < 50 && j > 45 && j < 60){
IsometricMapAddTile(layers[n], i, j, 0);
}
break;
case 2:
if(i > 40 && i < 44 && j > 50 && j < 54){
IsometricMapAddTile(layers[n], i, j, 1);
}
break;
}
}
}
}
for(n = 0; n <= 10; n++){
for(i = 0; i < 20-n*2; i++){
for(j = 0; j < 20-n*2; j++){
IsometricMapAddTile(layers[n], i, j, 0);
if(n == 9){
IsometricMapAddTile(layers[n], i, j, 1);
}
}
}
}
SetTargetFPS(60); SetTargetFPS(60);
while(!WindowShouldClose()){ while(!WindowShouldClose()){

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.

Binary file not shown.

BIN
tile.o

Binary file not shown.
Loading…
Cancel
Save