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.
170 lines
5.9 KiB
170 lines
5.9 KiB
#include "isometricMap.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "tile.h"
|
|
#include "raymath.h"
|
|
#include "raylib.h"
|
|
#include "../Sprite/sprite.h"
|
|
#include "../game.h"
|
|
|
|
// returns pointer to IsometricMap Instance
|
|
IsometricMap * IsometricMapInit(){
|
|
IsometricMap* map = malloc(sizeof(IsometricMap));
|
|
|
|
map->tileTextures[0] = LoadTexture("assets/grass.png");
|
|
map->tileTextures[1] = LoadTexture("assets/grass_selected.png");
|
|
map->tileTextures[2] = LoadTexture("assets/tower.png");
|
|
|
|
map->width = 500;
|
|
map->height = 500;
|
|
map->textureWidth = map->tileTextures[0].width;
|
|
map->textureHeight = map->tileTextures[0].height;
|
|
map->worldPixelWidth = map->width * map->textureWidth;
|
|
map->worldPixelWidth = map->height * map->textureHeight;
|
|
|
|
// mallocating the twodimensional Tiles Array
|
|
Tile*** tiles = malloc(map->width*sizeof(Tile*));
|
|
int n = 0;
|
|
for(n=0; n < map->width; n++){
|
|
tiles[n] = malloc(map->height*sizeof(Tile*));
|
|
}
|
|
|
|
int i = 0;
|
|
int j = 0;
|
|
int quarterTextureSize = map->textureWidth / 4;
|
|
int halfTextureSize = map->textureWidth / 2;
|
|
for(i=0; i < map->width; i++){
|
|
for(j=0; j < map->height; j++){
|
|
Tile *tmp = malloc(sizeof(Tile));
|
|
// initially all the Tiles are "empty"
|
|
tmp->textureId = -1;
|
|
tmp->x = i;
|
|
tmp->y = j;
|
|
|
|
Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, halfTextureSize, quarterTextureSize);
|
|
// the higher the layer the higher it needs to be drawed
|
|
tmp->offsetX = offset->x;
|
|
tmp->offsetY = offset->y;
|
|
free(offset);
|
|
tiles[i][j] = tmp;
|
|
}
|
|
}
|
|
|
|
map->tiles = tiles;
|
|
|
|
return map;
|
|
}
|
|
|
|
// For Rendering: calculates coordinate offset for a single tile at arrayPosition x y
|
|
// Only works for tiles with texture width == height (and for 22.5 degree?)
|
|
Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int halfTextureSize, int quarterTextureSize){
|
|
Vector2* offset = malloc(sizeof(Vector2));
|
|
offset->x = x * halfTextureSize - y * halfTextureSize;
|
|
offset->y = x * quarterTextureSize + y * quarterTextureSize;
|
|
return offset;
|
|
}
|
|
|
|
// Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector
|
|
void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){
|
|
|
|
float tileWidthHalf = isometricMap->textureWidth / 2;
|
|
float tileHeightQuarter = isometricMap->textureHeight / 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;
|
|
}
|
|
|
|
// 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 screenX = (xPos - yPos) / 2;
|
|
float screenY = (xPos + yPos) / 4;
|
|
|
|
screenX += camera->target.x;
|
|
screenY += camera->target.y;
|
|
|
|
// z is currently implemented as z=1 equals 1 layer, z=2 would be two layers height (could be smoothed)
|
|
// hardcoded tile height
|
|
//screenY -= z * 10;
|
|
|
|
tmp->x = screenX;
|
|
tmp->y = screenY;
|
|
}
|
|
|
|
// returns Tile * -> tile at coordinates x y z=layer
|
|
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float x, float y){
|
|
|
|
x = (int)(x / isometricMap->textureWidth);
|
|
y = (int)(y / isometricMap->textureHeight);
|
|
|
|
if( x < isometricMap->width && y < isometricMap->height && x >= 0 && y >= 0 ){
|
|
if(isometricMap->tiles[(int)x][(int)y]->textureId != -1){
|
|
return (isometricMap->tiles[(int)x][(int)y]);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// changes to Texture ID of tile at x y on maplayer layer
|
|
void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){
|
|
if( x < map->width && y < map->height &&
|
|
x >= 0 && y >= 0 ){
|
|
(map->tiles[x][y])->textureId = id;
|
|
}
|
|
else{
|
|
printf("WARNING: trying to change Texture of Tile which is out of bounds!\n");
|
|
}
|
|
}
|
|
|
|
void IsometricMapDraw(Game *game){
|
|
int windowWidth = GetScreenWidth();
|
|
int windowHeight = GetScreenHeight();
|
|
Vector2 topleft = {0, 0};
|
|
IsometricMapProject(game->map, game->camera, topleft.x, topleft.y, &topleft);
|
|
Vector2 topright = {windowWidth, 0};
|
|
IsometricMapProject(game->map, game->camera, topright.x, topright.y, &topright);
|
|
Vector2 botleft = {0, windowHeight};
|
|
IsometricMapProject(game->map, game->camera, botleft.x, botleft.y, &botleft);
|
|
Vector2 botright = {windowWidth, windowHeight};
|
|
IsometricMapProject(game->map, game->camera, botright.x, botright.y, &botright);
|
|
|
|
int extraTiles = 1;
|
|
int n = 0;
|
|
int itmp = (int)(topleft.x / game->map->textureWidth) - extraTiles;
|
|
int jtmp = (int)(topright.y / game->map->textureHeight) - extraTiles;
|
|
int maxI = (int)(botright.x / game->map->textureWidth) + extraTiles;
|
|
int maxJ = (int)(botleft.y / game->map->textureHeight) + extraTiles;
|
|
if (itmp < 0){ itmp = 0; }
|
|
if (jtmp < 0){ jtmp = 0; }
|
|
if (maxI > game->map->width){ maxI = game->map->width; }
|
|
if (maxJ > game->map->height){ maxJ = game->map->height; }
|
|
|
|
int i, j = 0;
|
|
for (j = jtmp; j < maxJ; j++){
|
|
for (i = itmp; i < maxI; i++){
|
|
if (game->map->tiles[i][j]->textureId == -1){
|
|
}
|
|
else{
|
|
DrawTexture(
|
|
game->map->tileTextures[game->map->tiles[i][j]->textureId],
|
|
game->map->tiles[i][j]->offsetX,
|
|
game->map->tiles[i][j]->offsetY,
|
|
WHITE);
|
|
}
|
|
}
|
|
}
|
|
|
|
SpriteListDrawAllSprites(game->sprites, game->map, game->camera);
|
|
}
|