|
|
|
|
@ -15,28 +15,28 @@ IsometricMap * IsometricMapInit(int layer){
|
|
|
|
|
//map->tileTextures[0] = LoadTexture("assets/desert.png");
|
|
|
|
|
//map->tileTextures[1] = LoadTexture("assets/bigtower.png");
|
|
|
|
|
|
|
|
|
|
map->originX = 0;
|
|
|
|
|
map->originY = -layer * map->tileTextures[0].width / 4;
|
|
|
|
|
map->width = 100 * map->tileTextures[0].width;
|
|
|
|
|
map->height = 100 * map->tileTextures[0].height;
|
|
|
|
|
map->widthBounds = 100;
|
|
|
|
|
map->heightBounds = 100;
|
|
|
|
|
map->layer = layer;
|
|
|
|
|
|
|
|
|
|
map->width = 100;
|
|
|
|
|
map->height = 100;
|
|
|
|
|
map->textureWidth = map->tileTextures[0].width;
|
|
|
|
|
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;
|
|
|
|
|
for(n=0; n < map->widthBounds; n++){
|
|
|
|
|
tiles[n] = (Tile**)malloc(map->heightBounds*sizeof(Tile*));
|
|
|
|
|
for(n=0; n < map->width; n++){
|
|
|
|
|
tiles[n] = (Tile**)malloc(map->height*sizeof(Tile*));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
int j = 0;
|
|
|
|
|
for(i=0; i < map->widthBounds; i++){
|
|
|
|
|
for(j=0; j < map->heightBounds; j++){
|
|
|
|
|
for(i=0; i < map->width; i++){
|
|
|
|
|
for(j=0; j < map->height; j++){
|
|
|
|
|
Tile *tmp = (Tile *) malloc(sizeof(Tile));
|
|
|
|
|
// initially all the Tiles are "empty"
|
|
|
|
|
tmp->textureId = -1;
|
|
|
|
|
tmp->x = i;
|
|
|
|
|
tmp->y = j;
|
|
|
|
|
@ -50,6 +50,7 @@ IsometricMap * IsometricMapInit(int layer){
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only works for tiles with texture width == height (and for 22.5 degree?)
|
|
|
|
|
Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){
|
|
|
|
|
Vector2* offset = (Vector2 *)malloc(sizeof(Vector2));
|
|
|
|
|
offset->x = x * textureSize/2 - y * textureSize/2;
|
|
|
|
|
@ -61,11 +62,10 @@ Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){
|
|
|
|
|
return map->tiles[x][y];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Project: Screen Coordinates -> World Coordinates
|
|
|
|
|
void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){
|
|
|
|
|
|
|
|
|
|
float tileWidthHalf = isometricMap->tileTextures[0].width / 2;
|
|
|
|
|
float tileHeightQuarter = isometricMap->tileTextures[0].height / 4;
|
|
|
|
|
float tileWidthHalf = isometricMap->textureWidth / 2;
|
|
|
|
|
float tileHeightQuarter = isometricMap->textureHeight / 4;
|
|
|
|
|
|
|
|
|
|
x += camera->target.x;
|
|
|
|
|
y += camera->target.y;
|
|
|
|
|
@ -73,7 +73,6 @@ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x,
|
|
|
|
|
float xPos = (float) x;
|
|
|
|
|
float yPos = (float) y;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float isoX = 0.5 * ( xPos / tileWidthHalf + yPos / tileHeightQuarter);
|
|
|
|
|
float isoY = 0.5 * ( -xPos / tileWidthHalf + yPos / tileHeightQuarter);
|
|
|
|
|
|
|
|
|
|
@ -81,47 +80,47 @@ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x,
|
|
|
|
|
tmp->y = isoY * isometricMap->tileTextures[0].height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x, int y, float z, Vector2 *tmp){
|
|
|
|
|
float xPos = (float) x;
|
|
|
|
|
float yPos = (float) y;
|
|
|
|
|
|
|
|
|
|
float worldX = (xPos - yPos) / 2;
|
|
|
|
|
float worldY = (xPos + yPos) / 4;
|
|
|
|
|
float screenX = (xPos - yPos) / 2;
|
|
|
|
|
float screenY = (xPos + yPos) / 4;
|
|
|
|
|
|
|
|
|
|
screenX += camera->target.x;
|
|
|
|
|
screenY += camera->target.y;
|
|
|
|
|
|
|
|
|
|
worldX += camera->target.x;
|
|
|
|
|
worldY += camera->target.y;
|
|
|
|
|
// z is currently implemented as z=1 equals 1 layer, z=2 would be two layers height (could be smoothed)
|
|
|
|
|
// TODO drift
|
|
|
|
|
screenY -= z *(isometricMap[0]->textureHeight/2);
|
|
|
|
|
|
|
|
|
|
tmp->x = worldX;
|
|
|
|
|
tmp->y = worldY;
|
|
|
|
|
tmp->x = screenX;
|
|
|
|
|
tmp->y = screenY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y){
|
|
|
|
|
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float z){
|
|
|
|
|
|
|
|
|
|
x = (int)(x / isometricMap->tileTextures->width);
|
|
|
|
|
y = (int)(y / isometricMap->tileTextures->height);
|
|
|
|
|
int layer = (int) z;
|
|
|
|
|
|
|
|
|
|
Tile *ptr = (Tile *) malloc(sizeof(Tile *));
|
|
|
|
|
ptr->x = 0;
|
|
|
|
|
ptr->y = 0;
|
|
|
|
|
x = (int)(x / isometricMap[layer]->textureWidth);
|
|
|
|
|
y = (int)(y / isometricMap[layer]->textureHeight);
|
|
|
|
|
|
|
|
|
|
if( x < isometricMap->widthBounds && y < isometricMap->heightBounds &&
|
|
|
|
|
x >= 0 && y >= 0 ){
|
|
|
|
|
if(isometricMap->tiles[(int)x][(int)y]->textureId != -1){
|
|
|
|
|
ptr->x = isometricMap->tiles[(int)x][(int)y]->x;
|
|
|
|
|
ptr->y = isometricMap->tiles[(int)x][(int)y]->y;
|
|
|
|
|
return ptr;
|
|
|
|
|
if( x < isometricMap[layer]->width && y < isometricMap[layer]->height && x >= 0 && y >= 0 ){
|
|
|
|
|
if(isometricMap[layer]->tiles[(int)x][(int)y]->textureId != -1){
|
|
|
|
|
return (isometricMap[layer]->tiles[(int)x][(int)y]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ptr = 0;
|
|
|
|
|
Tile *ptr = 0;
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){
|
|
|
|
|
Tile *ptr = (Tile *) malloc(sizeof(Tile *));
|
|
|
|
|
// hardcoded layer amount
|
|
|
|
|
int n = 9;
|
|
|
|
|
for(n=9;n>=0;n--){
|
|
|
|
|
if( tile->x < isometricMap[n]->widthBounds && tile->y < isometricMap[n]->heightBounds &&
|
|
|
|
|
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;
|
|
|
|
|
@ -135,17 +134,13 @@ Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsometricMapAddTile and IsometricMapChangeTextureIdOfTile pretty much do the same by now...
|
|
|
|
|
void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId){
|
|
|
|
|
isometricMap->tiles[x][y]->textureId = textureId;
|
|
|
|
|
isometricMap->tiles[x][y]->x = x;
|
|
|
|
|
isometricMap->tiles[x][y]->y = y;
|
|
|
|
|
isometricMap->tiles[x][y]->z = isometricMap->layer;
|
|
|
|
|
}
|
|
|
|
|
void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){
|
|
|
|
|
if( x < map->widthBounds && y < map->heightBounds &&
|
|
|
|
|
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 ){
|
|
|
|
|
(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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|