Sprites cant leave world

main
JanEhehalt 3 years ago
parent 40a0a1b689
commit 58bb57eeb2

@ -167,8 +167,17 @@ void mouseInput(Game *game){
while (current != 0){
if(current->data.selected){
current->data.hasDestination = 1;
current->data.destX = inputHandler->cursorWorldPos.x;
current->data.destY = inputHandler->cursorWorldPos.y;
float destX = inputHandler->cursorWorldPos.x;
float destY = inputHandler->cursorWorldPos.y;
if(destX < 0){ destX = 0; }
if(destY < 0){ destY = 0; }
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; }
current->data.destX = destX;
current->data.destY = destY;
}
current = current->next;

@ -19,6 +19,9 @@ IsometricMap * IsometricMapInit(int layer){
map->heightBounds = 100;
map->layer = layer;
map->textureWidth = map->tileTextures[0].width;
map->textureHeight = map->tileTextures[0].height;
Tile*** tiles = (Tile***)malloc(map->widthBounds*sizeof(Tile*));
int n = 0;
for(n=0; n < map->widthBounds; n++){

@ -12,6 +12,8 @@ typedef struct IsometricMap{
int height;
int widthBounds;
int heightBounds;
int textureWidth;
int textureHeight;
int layer;
} IsometricMap;

@ -43,13 +43,10 @@ void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input
int i = 0;
int j = 0;
for(n = 0; n < 10; n++){
if(map[n] == 0){
continue;
}
for(i=0; i < map[n]->widthBounds; i++){
for(j=0; j < map[n]->heightBounds; j++){
if(map[n]->tiles[i][j]->textureId != -1){
Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, map[n]->tileTextures[0].width);
Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, map[n]->textureWidth);
float x = map[n]->originX + offset->x;
float y = map[n]->originY + offset->y;

@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "../sprite.h"
#include "raylib.h"
#include "raymath.h"
Node * ListCreateNode(Sprite *data){
Node *new = (Node *) malloc(sizeof(Node));
@ -75,3 +77,45 @@ void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera){
current = current->next;
}
}
void ListActAllSprites(Game *game){
// Sprites move towards their destination
float movementSpeed = 150.0f * GetFrameTime();
Node *current = game->sprites->head;
while (current != 0){
if(current->data.hasDestination == 1){
Vector2 movement = {
current->data.destX - current->data.x,
current->data.destY - current->data.y
};
if(Vector2Length(movement) < movementSpeed){
current->data.hasDestination = 0;
current->data.x = current->data.destX;
current->data.y = current->data.destY;
}
else{
movement = Vector2Normalize(movement);
movement = Vector2Scale(movement, movementSpeed);
current->data.x += movement.x;
current->data.y += movement.y;
// Change sprite according to direction
Vector2 nullvektor = {0,0};
float f = Vector2Angle(movement, nullvektor);
printf("Angle: %f\n", f);
f /= 3.14;
f *= 3.5;
f += 3.5;
int index = (int) f;
current->data.texture = game->worker + index;
}
}
current = current->next;
}
}

@ -3,6 +3,7 @@
#include "../sprite.h"
#include "../IsometricMap/isometricMap.h"
#include "raylib.h"
#include "../game.h"
typedef struct Node Node;
typedef struct List List;
@ -29,5 +30,6 @@ void ListInsertFront(List *list, Sprite *data);
void ListInsertBack(List *list, Sprite *data);
List * ListInit();
void ListDrawAllSprites(List *list, IsometricMap *map, Camera2D *camera);
void ListActAllSprites(Game *game);
#endif

@ -17,9 +17,16 @@ Fantasy Welt oder Realistisch?
+ LinkedList erweitern
+ Sprites Animationen etc improven
+ Die Sprites brauchen evtl eine z-Koordinate, die anzeigt auf welchem Layer sie sind, wird aber recht kompliziert. Macht es sinn das Spiel an sich auf ein Layer zu begrenzen und die höheren Layer nur für größere Gebäude zu nutzen, wie z.B. Türme?
+ 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
+ Movement speed der Sprites an delta time orientieren
+ Dokumentation anfangen
### WiP
+ Die Sprites brauchen evtl eine z-Koordinate, die anzeigt auf welchem Layer sie sind
### Done
+ Movement speed der Sprites an delta time orientieren

@ -93,6 +93,18 @@ Game *GameInit()
}
break;
}
if(i == j && n == 1){
IsometricMapAddTile(((game->layers))[n], i, j, 0);
}
if(i == j && n == 2){
IsometricMapAddTile(((game->layers))[n], i, j, 0);
}
if(i == j-1 && n == 1){
IsometricMapAddTile(((game->layers))[n], i, j, 0);
}
if(i-1 == j && n == 1){
IsometricMapAddTile(((game->layers))[n], i, j, 0);
}
}
}
}

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.

@ -13,23 +13,28 @@ int main(){
InitWindow(800, 450, "basic window");
Texture2D amulet = LoadTexture("assets/amulet.png");
//Texture2D amulet = LoadTexture("assets/amulet.png");
Game *game = GameInit();
// Hides the operating systems own cursor
HideCursor();
SetTargetFPS(60);
//SetTargetFPS(60);
// GAME MAIN ROUTINE
while(!WindowShouldClose()){
BeginDrawing();
ListActAllSprites(game);
ClearBackground(RAYWHITE);
BeginDrawing();
BeginMode2D(*(game->camera));
IsometricRendererRenderIsometricMap(game->layers, game->inputHandler);
ListDrawAllSprites(game->sprites, game->layers[0], game->camera);
DrawTexture(amulet, 400, 225, WHITE);
EndMode2D();
// Moving cursor Sprite to Mouse Pos and drawing it
@ -45,43 +50,6 @@ int main(){
//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);
// Sprites move towards their destination
float movementSpeed = 10.0f;
Node *current = game->sprites->head;
while (current != 0){
if(current->data.hasDestination == 1){
Vector2 movement = {
current->data.destX - current->data.x,
current->data.destY - current->data.y
};
if(Vector2Length(movement) < movementSpeed){
current->data.hasDestination = 0;
current->data.x = current->data.destX;
current->data.y = current->data.destY;
}
else{
movement = Vector2Normalize(movement);
movement = Vector2Scale(movement, movementSpeed);
current->data.x += movement.x;
current->data.y += movement.y;
// Change sprite according to direction
Vector2 nullvektor = {0,0};
float f = Vector2Angle(movement, nullvektor);
printf("Angle: %f\n", f);
f /= 3.14;
f *= 3.5;
f += 3.5;
int index = (int) f;
current->data.texture = game->worker + index;
}
}
current = current->next;
}
DrawFPS(GetScreenWidth() - 95, 10);
EndDrawing();

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.

@ -33,6 +33,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);
}
void DrawSpriteToScreen(Sprite *sprite){

Binary file not shown.

BIN
tile.o

Binary file not shown.
Loading…
Cancel
Save