Sprites no have a z-coordinate

main
JanEhehalt 3 years ago
parent 49848705f7
commit 26d3a2efca

@ -122,8 +122,16 @@ void mouseInput(Game *game){
float height = GetMousePosition().y - inputHandler->rectStart.y;
// Add Sprite
if(width + height <= 1){
ListInsertBack(sprites, SpriteCreate(texture, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y));
if(abs(width) + abs(height) < 20){
int maxWidth = (game->layers[0]->widthBounds-1) * game->layers[0]->textureWidth;
int maxHeight = (game->layers[0]->heightBounds-1) * game->layers[0]->textureHeight;
if(inputHandler->cursorWorldPos.x < 0){ printf("OutOfBoundsDestination Spawn\n");}
else if(inputHandler->cursorWorldPos.y < 0){ printf("OutOfBoundsDestination Spawn\n");}
else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");}
else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");}
else {
ListInsertBack(sprites, SpriteCreate(texture, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y));
}
}
// Berechnung, welche Sprites ausgewählt wurden, scuffed
@ -167,14 +175,14 @@ void mouseInput(Game *game){
while (current != 0){
if(current->data.selected){
current->data.hasDestination = 1;
float destX = inputHandler->cursorWorldPos.x;
float destY = inputHandler->cursorWorldPos.y;
if(destX < 0){ destX = 0; }
if(destY < 0){ destY = 0; }
float destX = inputHandler->cursorWorldTile.x * game->layers[0]->textureWidth;
float destY = inputHandler->cursorWorldTile.y * game->layers[0]->textureHeight;
int maxWidth = (game->layers[0]->widthBounds-1) * game->layers[0]->textureWidth;
int maxHeight = (game->layers[0]->heightBounds-1) * game->layers[0]->textureHeight;
if(destX > maxWidth){ destX = maxWidth; }
if(destY > maxHeight){ destY = maxHeight; }
if(destX < 0){ printf("OutOfBoundsDestination"); continue; }
if(destY < 0){ printf("OutOfBoundsDestination"); continue; }
if(destX > maxWidth){ printf("OutOfBoundsDestination"); continue; }
if(destY > maxHeight){ printf("OutOfBoundsDestination"); continue; }
current->data.destX = destX;
current->data.destY = destY;

@ -40,6 +40,7 @@ IsometricMap * IsometricMapInit(int layer){
tmp->textureId = -1;
tmp->x = i;
tmp->y = j;
tmp->z = layer;
tiles[i][j] = tmp;
}
}
@ -116,11 +117,30 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float
return ptr;
}
Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile){
Tile *ptr = (Tile *) malloc(sizeof(Tile *));
int n = 9;
for(n=9;n>=0;n--){
if( tile->x < isometricMap[n]->widthBounds && tile->y < isometricMap[n]->heightBounds &&
tile->x >= 0 && tile->y >= 0 ){
if(isometricMap[n]->tiles[tile->x][tile->y]->textureId != -1){
ptr->x = isometricMap[n]->tiles[tile->x][tile->y]->x;
ptr->y = isometricMap[n]->tiles[tile->x][tile->y]->y;
ptr->z = isometricMap[n]->tiles[tile->x][tile->y]->z;
return ptr;
}
}
}
ptr = 0;
return ptr;
}
// IsometricMapAddTile and IsometricMapChangeTextureIdOfTile pretty much do the same by now...
void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId){
isometricMap->tiles[x][y]->textureId = textureId;
isometricMap->tiles[x][y]->x = x;
isometricMap->tiles[x][y]->y = y;
isometricMap->tiles[x][y]->z = isometricMap->layer;
}
void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){
if( x < map->widthBounds && y < map->heightBounds &&

@ -20,6 +20,7 @@ typedef struct IsometricMap{
IsometricMap * IsometricMapInit(int layer);
Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize);
Tile * IsometricMapGetMostUpperTile(IsometricMap **isometricMap, Tile *tile);
Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y);
// Project: Screen Coordinates -> World Coordinates
void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp);

@ -51,7 +51,7 @@ void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input
int textureId = map[n]->tiles[i][j]->textureId;
float x = map[n]->originX + offset->x;
float y = map[n]->originY + offset->y + map[n]->textureHeight - map[n]->tileTextures[textureId].height;
float y = map[n]->originY + offset->y;// + map[n]->textureHeight - map[n]->tileTextures[textureId].height;
DrawTexture(map[n]->tileTextures[textureId], x, y, WHITE);

@ -6,6 +6,7 @@ typedef struct Tile{
int textureId;
int x;
int y;
int z;
} Tile;
#endif

@ -18,7 +18,7 @@ void ListPrintForward(List *list){
printf("\n[\n");
while(current != 0){
printf("(%f | %f),\n", current->data.x, current->data.y);
//printf("(%f | %f),\n", current->data.x, current->data.y);
current = current->next;
}
printf("]\n");
@ -69,7 +69,7 @@ List * ListInit(){
return newList;
}
void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera){
void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){
Node *current = list->head;
while(current != 0){
@ -105,7 +105,7 @@ void ListActAllSprites(Game *game){
// Change sprite according to direction
Vector2 nullvektor = {0,0};
float f = Vector2Angle(movement, nullvektor);
printf("Angle: %f\n", f);
//printf("Angle: %f\n", f);
f /= 3.14;
f *= 3.5;
f += 3.5;
@ -114,6 +114,11 @@ void ListActAllSprites(Game *game){
}
}
// updating z-position
Tile *floorTile = IsometricMapGetTileFromWorldCoordinates(game->layers[0], current->data.x, current->data.y);
floorTile = IsometricMapGetMostUpperTile(game->layers, floorTile);
current->data.z = floorTile->z;
current = current->next;
}

@ -29,7 +29,7 @@ void ListPrintForward(List *list);
void ListInsertFront(List *list, Sprite *data);
void ListInsertBack(List *list, Sprite *data);
List * ListInit();
void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera);
void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera);
void ListActAllSprites(Game *game);
#endif

BIN
game.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
list.o

Binary file not shown.

@ -33,7 +33,7 @@ int main(){
IsometricRendererRenderIsometricMap(game->layers, game->inputHandler);
ListDrawAllSprites(game->sprites, game->layers[0], game->camera);
ListDrawAllSprites(game->sprites, game->layers, game->camera);
EndMode2D();

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.

@ -3,12 +3,14 @@
#include <stdio.h>
#include <stdlib.h>
#include "IsometricMap/isometricMap.h"
#include "IsometricMap/tile.h"
void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){
if(*spriteAmount < 100){
(sprites + *spriteAmount) -> texture = texture;
(sprites + *spriteAmount) -> x = x;
(sprites + *spriteAmount) -> y = y;
(sprites + *spriteAmount) -> z = 0;
(sprites + *spriteAmount) -> destX = x;
(sprites + *spriteAmount) -> destY = y;
(sprites + *spriteAmount) -> hasDestination = 0;
@ -20,15 +22,18 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in
}
}
void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){
void DrawSpriteToWorld(Sprite *sprite, IsometricMap **map, Camera2D *camera){
// TODO: Nach y sortieren, bevor sie gedrawed werden
// Wir müssen beachten, dass sie nach den unprojezierten Screen-Koordinaten sortiert werden müssen.
// Macht es vielleicht sinn den Sprites auch einen Vector mit ihren Screen Koordinaten zu geben?
Vector2 pos = {sprite->x, sprite->y};
IsometricMapUnproject(map, camera, pos.x, pos.y, &pos);
IsometricMapUnproject(map[0], camera, pos.x, pos.y, &pos);
// Also erst ab hier sortieren, mit den Werten aus dem pos Vector
pos.y -= sprite->z * (map[0]->textureHeight/2);
pos.x -= camera->target.x;
pos.y -= camera->target.y;
if(sprite->selected){
@ -38,7 +43,7 @@ void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){
else{
DrawTexture(*sprite->texture, pos.x - sprite->texture->width/4, pos.y - sprite->texture->height/4, WHITE);
}
printf("%f %f \n", sprite->x, sprite->y);
//printf("%f %f \n", sprite->x, sprite->y);
}
void DrawSpriteToScreen(Sprite *sprite){

@ -7,6 +7,7 @@ typedef struct Sprite {
Texture2D *texture;
float x;
float y;
float z;
float destX;
float destY;
int hasDestination;
@ -15,7 +16,7 @@ typedef struct Sprite {
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);
void DrawSpriteToScreen(Sprite *sprite);
Sprite * SpriteCreate(Texture2D *texture, int x, int y);

Binary file not shown.

BIN
tile.o

Binary file not shown.
Loading…
Cancel
Save