commented, stepping works, ready for Merge

main
JanEhehalt 3 years ago
parent 38a78af8d0
commit 81a38c452f

@ -5,6 +5,7 @@
#include "raymath.h" #include "raymath.h"
#include "raylib.h" #include "raylib.h"
// returns pointer to IsometricMap Instance
IsometricMap * IsometricMapInit(int layer){ IsometricMap * IsometricMapInit(int layer){
IsometricMap* map = (IsometricMap *) malloc(sizeof(IsometricMap)); IsometricMap* map = (IsometricMap *) malloc(sizeof(IsometricMap));
@ -50,6 +51,7 @@ IsometricMap * IsometricMapInit(int layer){
return map; 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?) // Only works for tiles with texture width == height (and for 22.5 degree?)
Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){ Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){
Vector2* offset = (Vector2 *)malloc(sizeof(Vector2)); Vector2* offset = (Vector2 *)malloc(sizeof(Vector2));
@ -58,10 +60,12 @@ Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize){
return offset; return offset;
} }
// returns Tile at x y on layer isometricMap
Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){ Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){
return map->tiles[x][y]; return map->tiles[x][y];
} }
// Unproject: World Coordinates -> Screen Coordinates writes result in tmp Vector
void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){
float tileWidthHalf = isometricMap->textureWidth / 2; float tileWidthHalf = isometricMap->textureWidth / 2;
@ -92,13 +96,14 @@ void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x,
screenY += camera->target.y; screenY += camera->target.y;
// z is currently implemented as z=1 equals 1 layer, z=2 would be two layers height (could be smoothed) // z is currently implemented as z=1 equals 1 layer, z=2 would be two layers height (could be smoothed)
// TODO drift // hardcoded tile height
screenY -= z *(isometricMap[0]->textureHeight/2); screenY -= z * 10;
tmp->x = screenX; tmp->x = screenX;
tmp->y = screenY; tmp->y = screenY;
} }
// returns Tile * -> tile at coordinates x y z=layer
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float z){ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float z){
int layer = (int) z; int layer = (int) z;
@ -115,6 +120,7 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, floa
return ptr; return ptr;
} }
// Gives the most upper Tile above *tile
Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){ Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){
Tile *ptr = (Tile *) malloc(sizeof(Tile *)); Tile *ptr = (Tile *) malloc(sizeof(Tile *));
// hardcoded layer amount // hardcoded layer amount
@ -134,6 +140,7 @@ Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){
return ptr; return ptr;
} }
// changes to Texture ID of tile at x y on maplayer layer
void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id){ void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id){
if( x < map[layer]->width && y < map[layer]->height && if( x < map[layer]->width && y < map[layer]->height &&
x >= 0 && y >= 0 ){ x >= 0 && y >= 0 ){

@ -35,11 +35,14 @@ typedef struct IsometricMap{
// returns pointer to IsometricMap Instance // returns pointer to IsometricMap Instance
IsometricMap * IsometricMapInit(int layer); IsometricMap * IsometricMapInit(int layer);
// For Rendering: calculates // 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 textureSize); Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize);
// Gives the most upper Tile above *tile
Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile); Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile);
// returns Tile at x y on layer isometricMap
Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y); Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y);
// Project: Screen Coordinates -> World Coordinates writes result in tmp Vector // Project: Screen Coordinates -> World Coordinates writes result in tmp Vector
@ -52,6 +55,7 @@ void IsometricMapUnproject(IsometricMap **isometricMap, Camera2D *camera, int x,
// changes to Texture ID of tile at x y on maplayer layer // changes to Texture ID of tile at x y on maplayer layer
void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id); void IsometricMapChangeTextureIdOfTile(IsometricMap **map, int x, int y, int layer, int id);
// returns Tile * -> tile at coordinates x y z=layer
Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float layer); Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap **isometricMap, float x, float y, float layer);

@ -39,6 +39,7 @@ void IsometricRendererDrawMap(IsometricRenderer *renderer, int height){
} }
} }
// parameter could be changed to only IsometricMap[]
void IsometricRendererRenderIsometricMap(Game *game){ void IsometricRendererRenderIsometricMap(Game *game){
int n = 0; int n = 0;
int i = 0; int i = 0;
@ -46,8 +47,10 @@ void IsometricRendererRenderIsometricMap(Game *game){
for(n = 0; n < 10; n++){ for(n = 0; n < 10; n++){
for(i=0; i < game->layers[n]->width; i++){ for(i=0; i < game->layers[n]->width; i++){
for(j=0; j < game->layers[n]->height; j++){ for(j=0; j < game->layers[n]->height; j++){
// if tile had texture id -1, it would be empty tile
if(game->layers[n]->tiles[i][j]->textureId != -1){ if(game->layers[n]->tiles[i][j]->textureId != -1){
Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, game->layers[n]->textureWidth); Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, game->layers[n]->textureWidth);
// the higher the layer the higher it needs to be drawed
offset->y -= n * (game->layers[n]->textureHeight/4); offset->y -= n * (game->layers[n]->textureHeight/4);
int textureId = game->layers[n]->tiles[i][j]->textureId; int textureId = game->layers[n]->tiles[i][j]->textureId;

@ -2,6 +2,7 @@
#define TILE_H_ #define TILE_H_
#include "raylib.h" #include "raylib.h"
// Tile with textureid = -1 wouldn't be drawed, would just be a placeholder tile
typedef struct Tile{ typedef struct Tile{
int textureId; int textureId;
int x; int x;

@ -69,6 +69,7 @@ List * ListInit(){
return newList; return newList;
} }
// iterates over all Sprites in the list and draws them to the world
void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){ void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){
Node *current = list->head; Node *current = list->head;
@ -78,6 +79,7 @@ void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){
} }
} }
// iterates over all Sprites in the list and does their acting (moving etc)
void ListActAllSprites(Game *game){ void ListActAllSprites(Game *game){
// Sprites move towards their destination // Sprites move towards their destination
@ -90,7 +92,6 @@ void ListActAllSprites(Game *game){
current->data.destX - current->data.x, current->data.destX - current->data.x,
current->data.destY - current->data.y current->data.destY - current->data.y
}; };
if(Vector2Length(movement) < movementSpeed){ if(Vector2Length(movement) < movementSpeed){
current->data.hasDestination = 0; current->data.hasDestination = 0;
current->data.x = current->data.destX; current->data.x = current->data.destX;
@ -114,7 +115,7 @@ void ListActAllSprites(Game *game){
} }
} }
// updating z-position // updating z-position according to the tile the sprite stands on
Tile *floorTile = IsometricMapGetTileFromWorldCoordinates(game->layers, current->data.x, current->data.y, 0); Tile *floorTile = IsometricMapGetTileFromWorldCoordinates(game->layers, current->data.x, current->data.y, 0);
Tile *topTile = IsometricMapGetMostUpperTile(game->layers, floorTile); Tile *topTile = IsometricMapGetMostUpperTile(game->layers, floorTile);
current->data.z = topTile->z; current->data.z = topTile->z;

@ -19,16 +19,14 @@ Fantasy Welt oder Realistisch?
+ Sprites Animationen etc improven + Sprites Animationen etc improven
+ Die Inputs sollten den Kamera Zoom beachten, aktuell geht noch alles kaputt wenn man den zoom umstellt + Die Inputs sollten den Kamera Zoom beachten, aktuell geht noch alles kaputt wenn man den zoom umstellt
+ Funktion, um die ganzen Sprites nach ihrer y-Koordinaten sortiert zu drawen + Funktion, um die ganzen Sprites nach ihrer y-Koordinaten sortiert zu drawen
+ Die Sprites brauchen evtl eine z-Koordinate, die anzeigt auf welchem Layer sie sind + Drawable Container machen, die sortiert werden können, dort kommen alle Tiles und Sprites rein, damit sie dann sortiert werden können
+ Die Sprites müssen auch entsprechend ihrer z-Koordinaten gedrawed werden
+ Wollen wir die Möglichkeit für Höhlen haben? -> Sprites könnten automatisch das höchste Tile als z nehmen (wenn keine Höhlen)
+ Maps in eigenen Dateien speichern + Maps in eigenen Dateien speichern
+ Parser für Map-Dateien + Parser für Map-Dateien
+ MapEditor + MapEditor
+ IsometricMap variablen auf Duplikate prüfen
+ IsometricMap struct erstellen, das den IsometricMap(+Layer) Array speichert + IsometricMap struct erstellen, das den IsometricMap(+Layer) Array speichert
+ Sprites drift too high when going up mountain + Sprites drift too high when going up mountain
### WiP ### WiP
+ Dokumentation aufholen + Dokumentation aufholen

@ -7,6 +7,7 @@
#include "IsometricMap/isometricMap.h" #include "IsometricMap/isometricMap.h"
#include "stdio.h" #include "stdio.h"
// returns pointer to new Game instance
Game *GameInit() Game *GameInit()
{ {
Game *game = (Game *)malloc(sizeof(Game)); Game *game = (Game *)malloc(sizeof(Game));

@ -13,6 +13,7 @@ typedef struct Game{
struct IsometricMap **layers; struct IsometricMap **layers;
} Game; } Game;
// returns pointer to new Game instance
Game * GameInit(); Game * GameInit();
#endif #endif

@ -29,8 +29,6 @@ int main(){
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
BeginDrawing(); BeginDrawing();
//printf("ALARM\n");
BeginMode2D(*(game->camera)); BeginMode2D(*(game->camera));
IsometricRendererRenderIsometricMap(game); IsometricRendererRenderIsometricMap(game);
@ -47,10 +45,6 @@ int main(){
mouseInput(game); mouseInput(game);
keyboardInput(game->inputHandler, game->camera); keyboardInput(game->inputHandler, game->camera);
//cursor Positions test
//printf("Cursor Pos: %f %f\n", game->inputHandler->cursorPos.x, game->inputHandler.cursorPos.y)->
//printf("Cursor World Pos: %f %f\n", game->inputHandler->cursorWorldPos.x, game->inputHandler->cursorWorldPos.y);
DrawFPS(GetScreenWidth() - 95, 10); DrawFPS(GetScreenWidth() - 95, 10);
EndDrawing(); EndDrawing();

@ -5,7 +5,11 @@
#include "IsometricMap/isometricMap.h" #include "IsometricMap/isometricMap.h"
#include "IsometricMap/tile.h" #include "IsometricMap/tile.h"
// @param deprecated
void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){
printf("WARNING: Using deprecated Function SpriteAdd!\n");
if(*spriteAmount < 100){ if(*spriteAmount < 100){
(sprites + *spriteAmount) -> texture = texture; (sprites + *spriteAmount) -> texture = texture;
(sprites + *spriteAmount) -> x = x; (sprites + *spriteAmount) -> x = x;
@ -31,11 +35,12 @@ void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera){
Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2}; Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2};
IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos); IsometricMapUnproject(map, camera, pos.x, pos.y, sprite->z, &pos);
// Also erst ab hier sortieren, mit den Werten aus dem pos Vector // Also erst ab hier sortieren, mit den Werten aus dem pos Vector
// nvm, Isometric map muss mit reingerechnet werden // IsometricMap muss auch mit reingerechnet werden -> mögliche Container Lösung steht in der README
pos.x -= camera->target.x; pos.x -= camera->target.x;
pos.y -= camera->target.y; pos.y -= camera->target.y;
if(sprite->selected){ if(sprite->selected){
DrawTexture(*sprite->texture, pos.x, pos.y, BLACK); DrawTexture(*sprite->texture, pos.x, pos.y, (Color){255, 255, 255, 200});
//DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK);
} }
else{ else{
@ -45,9 +50,8 @@ void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera){
} }
void DrawSpriteToScreen(Sprite *sprite){ void DrawSpriteToScreen(Sprite *sprite){
if(sprite->selected){ if(sprite->selected){
DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE); DrawTexture(*sprite->texture, sprite->x, sprite->y, (Color){255, 255, 255, 200});
//DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); //DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK);
} }
else{ else{

@ -14,6 +14,7 @@ typedef struct Sprite {
int selected; int selected;
} Sprite; } Sprite;
// @param deprecated
void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y); void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y);
void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera); void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera);

Loading…
Cancel
Save