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.
95 lines
2.0 KiB
95 lines
2.0 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){
|
|
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 = 0;
|
|
map->width = x * map->tileTextures[0].width;
|
|
map->height = y * map->tileTextures[0].height;
|
|
|
|
Tile* tiles[x];
|
|
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++){
|
|
if(i != j){
|
|
Tile tmp = {0, i, j};
|
|
tiles[i][j] = tmp;
|
|
}
|
|
else{
|
|
Tile tmp = {1, i, 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]);
|
|
}
|
|
|
|
Vector2 IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, int x, int y){
|
|
|
|
float tileWidthHalf = isometricMap->tileTextures[0].width / 2;
|
|
float tileHeightHalf = isometricMap->tileTextures[0].height / 2;
|
|
|
|
x += camera->target.x;
|
|
y += camera->target.y;
|
|
|
|
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 = {isoX, isoY};
|
|
|
|
return tmp;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|