You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.6 KiB

#include "isometricMap.h"
#include <stdio.h>
#include <stdlib.h>
#include "tile.h"
#include "raymath.h"
#include "raylib.h"
IsometricMap * IsometricMapInit(int layer){
IsometricMap* map = (IsometricMap *) malloc(sizeof(IsometricMap));
map->tileTextures[0] = LoadTexture("assets/grass.png");
map->tileTextures[1] = LoadTexture("assets/tower.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;
Tile*** tiles = (Tile***)malloc(map->widthBounds*sizeof(Tile*));
int n = 0;
for(n=0; n < map->widthBounds; n++){
tiles[n] = (Tile**)malloc(map->heightBounds*sizeof(Tile*));
}
int i = 0;
int j = 0;
for(i=0; i < map->widthBounds; i++){
for(j=0; j < map->heightBounds; j++){
Tile *tmp = (Tile *) malloc(sizeof(Tile));
tmp->textureId = -1;
tmp->x = i;
tmp->y = j;
tiles[i][j] = tmp;
}
}
map->tiles = tiles;
return map;
}
Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){
Vector2* offset = (Vector2 *)malloc(sizeof(Vector2));
offset->x = x * textureSize/2;
offset->y = x * textureSize/4;
offset->x -= y * textureSize/2;
offset->y += y * textureSize/4;
return offset;
}
Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){
return map->tiles[x][y];
}
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;
x += camera->target.x;
y += camera->target.y;
float xPos = (float) x;
float yPos = (float) y;
float isoX = 0.5 * ( xPos / tileWidthHalf + yPos / tileHeightQuarter);
float isoY = 0.5 * ( -xPos / tileWidthHalf + yPos / tileHeightQuarter);
tmp->x = isoX * isometricMap->tileTextures[0].width;
tmp->y = isoY * isometricMap->tileTextures[0].height;
}
void IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y, Vector2 *tmp){
float xPos = (float) x;
float yPos = (float) y;
float worldX = (xPos - yPos) / 2;
float worldY = (xPos + yPos) / 4;
worldX += camera->target.x;
worldY += camera->target.y;
tmp->x = worldX;
tmp->y = worldY;
}
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y){
x = (int)(x / isometricMap->tileTextures->width);
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 >= 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;
}
}
ptr = 0;
return ptr;
}
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;
}
void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){
if( x < map->widthBounds && y < map->heightBounds &&
x >= 0 && y >= 0 ){
(map->tiles[x][y])->textureId = id;
}
}