Mineur_Tycoon/src/map.c
2025-04-15 16:14:21 +02:00

52 lines
1.2 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <raylib.h>
#include <raymath.h>
#include "types.h"
#include "draw.h"
#include "lua_inter.h"
extern MapCollec mapcollec;
int generate_coll(Game *game, int mapn){
LoadedMap *lmap = &game->map;
Map *map = mapcollec.maps[mapn];
Machines *mach = &game->machines;
memcpy(lmap->coll, map->layers[1], map->width*map->height);
for(int i = 0; i < mach->machine_n; i++){
V2d pos = mach->machine_stack[i].pos;
lmap->coll[(int)(pos.y * lmap->width + pos.x)] = 1;
lmap->coll[(int)(pos.y * lmap->width + pos.x+1)] = 1;
lmap->coll[(int)((pos.y+1) * lmap->width + pos.x)] = 1;
lmap->coll[(int)((pos.y+1) * lmap->width + pos.x+1)] = 1;
}
return 0;
}
int load_map(Game *game, int mapn){
LoadedMap *nmap = &game->map;
Map *map = mapcollec.maps[mapn];
nmap->maptex = mkmap_tex(map);
nmap->width = map->width;
nmap->height = map->height;
nmap->coll = realloc(nmap->coll, map->width*map->height);
if(!nmap->coll)
return 1;
if(generate_coll(game, mapn))
return 1;
lua_onmapload(game, mapn);
return 0;
}
void clean_map(Game *game){
free(game->map.coll);
game->map.coll = NULL;
}