I'm using Raylib, and I'm attempting to place tiles on a level, and the algorithm for placing the tiles is working.
The problem is that the player will display over the walls on the bottom right hand side of the isometric grid.
What am I doing wrong, and does this involve the painter's algorithm?
Here is the code:
#include "raylib.h"
#include <stdint.h>
//Defines
#define COLUMNS 10
#define ROWS 10
//Player
typedef struct Player
{
int dirX;
int dirY;
Texture2D playerTexture;
Vector2 position;
struct Player* player;
}Player;
//Change from 2D to Isometric
Vector2 twoDtoIso(Vector2 tilePosition);
void movePlayer(Player* player, int8_t newX, int8_t newY);
int main()
{
// Initialization
//-------------------------------------------------------------------------
const uint8_t TILE_WIDTH = 32;
const uint8_t TILE_HEIGHT = 32;
const uint8_t PLAYER_OFFSET = 32;
const float SCREEN_WIDTH = 640.0f;
const float SCREEN_HEIGHT = 352.0f;
const float SCREEN_CENTER_X = 288.0f;
const float SCREEN_CENTER_Y = 0.0f;
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Isometric Tile placement");
//These images are loaded from a folder named "images" with
//isometric cubes that are 64 pixels by 64 pixels
Texture2D wallTexture = LoadTexture("images/wallTile.png");
Texture2D groundTexture = LoadTexture("images/groundTile.png");
Player player = {0, 0,LoadTexture("images/player.png"),(Vector2){ SCREEN_CENTER_X, PLAYER_OFFSET }};
uint8_t levelData[COLUMNS][ROWS] =
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,2,1}, //<-----The player is designated as 2, ground as 0, and wall tiles as 1
{1,1,1,1,1,1,1,1,1,1}
};
static uint8_t tileType;
const uint8_t NUM_COLUMNS = 10;
const uint8_t NUM_ROWS = 10;
const int8_t newX = 32;
const int8_t newY = 16;
Vector2 coords;
Vector2 tilePosition;
SetTargetFPS(30);
//------------------------End Initialization-------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//---------------------------------------------------------------------
//Player input/movement
movePlayer(&player, newX, newY);
//--------------------------End Update---------------------------------
// Draw
//---------------------------------------------------------------------
BeginDrawing();
ClearBackground(BLACK);
//place tiles
for (uint8_t i = 0; i < NUM_ROWS; i++)
{
for (uint8_t j = 0; j < NUM_COLUMNS ; j++)
{
tilePosition = (Vector2){(float)j * TILE_WIDTH, (float)i * TILE_HEIGHT};
tileType = levelData[i][j];
coords = twoDtoIso(tilePosition);
if(tileType == 0) //place ground tile
{
DrawTexture(groundTexture, coords.x + SCREEN_CENTER_X, coords.y + SCREEN_CENTER_Y, WHITE);
}
else if(tileType == 1) //place wall tile
{
DrawTexture(wallTexture, coords.x + SCREEN_CENTER_X, coords.y + SCREEN_CENTER_Y, WHITE);
}
else //otherwise place the player
{
DrawTexture(player.playerTexture, player.position.x, player.position.y, WHITE);
}
}
}//end for loop
EndDrawing();
//---------------------------------------------------------------------
}
// De-Initialization
//-------------------------------------------------------------------------
UnloadTexture(groundTexture);
UnloadTexture(wallTexture);
UnloadTexture(player.playerTexture);
CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------
return 0;
}
Vector2 twoDtoIso(Vector2 tilePosition)
{
Vector2 tempPoint;
tempPoint.x = tilePosition.x - tilePosition.y;
tempPoint.y = (tilePosition.x + tilePosition.y)/ 2;
return(tempPoint);
}
void movePlayer(Player* player, int8_t newX, int8_t newY)
{
if (IsKeyPressed(KEY_LEFT))
{
player->position = (Vector2){ player->position.x - newX, player->position.y + newY };
}
if (IsKeyPressed(KEY_RIGHT))
{
player->position = (Vector2){ player->position.x + newX, player->position.y - newY };
}
if (IsKeyPressed(KEY_UP))
{
player->position = (Vector2){ player->position.x - newX, player->position.y - newY };
}
if (IsKeyPressed(KEY_DOWN))
{
player->position = (Vector2){ player->position.x + newX, player->position.y + newY };
}
}
```