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.

113 lines
2.6 KiB

#include "isometricMap.h"
#include <stdio.h>
#include <stdlib.h>
#include "tile.h"
#include "raymath.h"
#include "raylib.h"
IsometricMap * IsometricMapInit(int x, int y, 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 = x * map->tileTextures[0].width;
map->height = y * map->tileTextures[0].height;
Tile*** tiles = (Tile***)malloc(x*sizeof(Tile*));
int n = 0;
for(n=0; n<x; n++){
tiles[n] = (Tile**)malloc(y*sizeof(Tile*));
}
map->sizeX = x;
map->sizeY = y;
int i = 0;
int j = 0;
for(i=0; i < x; i++){
for(j=0; j < y; j++){
Tile *tmp = (Tile *) malloc(sizeof(Tile));
tmp->textureId = 0;
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, int x, int y, Vector2 *tmp){
int mouseAdjustmentX = -8;
int mouseAdjustmentY = -4;
float tileWidthHalf = isometricMap->tileTextures[0].width / 2;
float tileHeightHalf = isometricMap->tileTextures[0].height / 4;
x += camera->target.x + mouseAdjustmentX;
y += camera->target.y + mouseAdjustmentY;
float xPos = (float) x;
float yPos = (float) y;
int isoX = 0.5 * ( xPos / tileWidthHalf + yPos / tileHeightHalf);
int isoY = 0.5 * ( -xPos / tileWidthHalf + yPos / tileHeightHalf);
//Vector2 tmp = {isoX * tileWidthHalf * 2, isoY * tileHeightHalf * 2};
//Vector2* tmp = (Vector2 *) malloc(sizeof(Vector2));
tmp->x = isoX;
tmp->y = isoY;
//return tmp;
}
void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId){
//if(x < isometricMap->sizeX && y < isometricMap->sizeY){
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;
//}
}