Compare commits

...

8 commits

Author SHA1 Message Date
attilavs2
bb34a2bc02 - fuite mémoire raylib, refactor de map 2025-07-02 14:31:35 +02:00
attilavs2
80688b4e26 Events clavier + resume avec echap 2025-07-01 15:36:01 +02:00
attilavs2
cd15a6802c draw_world_mini + Vieux trucs que j'ai oublié 2025-07-01 14:53:13 +02:00
attilavs2
ac266206bc Fin de refactor lua_inter - add_worker et Worker en lua 2025-05-20 13:31:16 +02:00
attilavs2
105095efbc Réecriture des types lua custom + fixs divers 2025-05-19 21:05:29 +02:00
attilavs2
ab44eac4ee Réparationement des problèmes de mémoire 2025-04-19 00:02:03 +02:00
attilavs2
c8c7fb1b52 Remove tmp file 2025-04-15 16:15:16 +02:00
attilavs2
081ceeb70e WIP (transfération d'un ordi à un autre) 2025-04-15 16:14:21 +02:00
17 changed files with 750 additions and 172 deletions

2
.gitignore vendored Normal file → Executable file
View file

@ -7,3 +7,5 @@ raylib/
*.swp *.swp
*.tiled-session *.tiled-session
map/maps_out.c map/maps_out.c
map/converter
log.txt

Binary file not shown.

View file

@ -1,24 +1,60 @@
mtprint("hello") mtprint("hello")
machine = add_machine(10, 10) coal = add_machine(320, 320)
mtprint(machine.id) coal.type = 0 -- charbon
machine.type = 2 work = add_worker(300,300)
mtprint(machine.type) work1 = add_worker(300,300)
pos = machine.pos work3 = add_worker(300,300)
machine.pos = pos work4 = add_worker(300,300)
mtprint(pos.x) assign_worker_machine(coal, work)
machine.pos.x = 50 assign_worker_machine(coal, work1)
machine.storage[0] = 1 assign_worker_machine(coal, work3)
assign_worker_machine(coal, work4)
iron = add_machine(416,320)
iron.type = 1 -- fer
work5 = add_worker(300,300)
work6 = add_worker(300,300)
work7 = add_worker(300,300)
work8 = add_worker(300,300)
assign_worker_machine(iron, work5)
assign_worker_machine(iron, work6)
assign_worker_machine(iron, work7)
assign_worker_machine(iron, work8)
furn = add_machine(320, 416)
furn.type = 2 -- fourneau
work9 = add_worker(300, 300)
work10 = add_worker(300, 300)
assign_worker_fetch(coal, furn, work9)
assign_worker_fetch(iron, furn, work10)
--mtprint(machine.id)
--machine.type = 2
--mtprint(machine.type)
--pos = machine.pos
--machine.pos = pos
--mtprint(pos.x)
--machine.pos.x = 50
--machine.storage[0] = 1
--mtprint(work.name)
--work2 = get_worker_from_pos(10,10)
--mtprint(work.name)
function on_tick(game) function on_tick(game)
-- Ceci va drainer la sortie de la machine 2 -- Ceci va drainer la sortie de la machine 2
-- (Le premier fourneau) -- (Le premier fourneau)
local mach = get_machine_from_id(2) local mach = get_machine_from_id(2)
if mach.storage[4] > 0 then if mach.storage[5] > 0 then
mach.storage[4] = mach.storage[4]-1 mach.storage[5] = mach.storage[5]-1
end end
end end
function on_frame(game) function on_frame(game)
end end
function on_load(game, map)
mtprint("loaded map :")
mtprint(map)
end

View file

@ -2,7 +2,14 @@
#define VERSION_MAJ 0 #define VERSION_MAJ 0
#define VERSION_MIN 1 #define VERSION_MIN 1
#define VERSION_PATCH 3 #define VERSION_PATCH 4
#define DEBUG 1
#define LOG_PATH "log.txt"
// Set in ui.c // Set in ui.c
extern char *VERSION_STR; extern char *VERSION_STR;
#define MAX_ZOOM 15.0f
#define MIN_ZOOM 0.27f

View file

@ -12,7 +12,7 @@
#include "draw.h" #include "draw.h"
#include "ui.h" #include "ui.h"
#define TEX_N 9 #define TEX_N 12
#define TILE_N 3 #define TILE_N 3
char *tex_files[TEX_N] = { char *tex_files[TEX_N] = {
@ -24,9 +24,29 @@ char *tex_files[TEX_N] = {
"assets/tex/blobrouge3.png", "assets/tex/blobrouge3.png",
"assets/tex/blobvert1.png", "assets/tex/blobvert1.png",
"assets/tex/blobvert2.png", "assets/tex/blobvert2.png",
"assets/tex/blobvert3.png" "assets/tex/blobvert3.png",
"assets/tex/coalmine.png",
"assets/tex/ironmine.png",
"assets/tex/furnace.png",
}; };
enum TexIds {
T_Blobb1,
T_Blobb2,
T_Blobb3,
T_Blobr1,
T_Blobr2,
T_Blobr3,
T_Blobv1,
T_Blobv2,
T_Blobv3,
T_Coalm,
T_Ironm,
T_Furna,
};
#define T_MBase T_Coalm
char *tile_files[TILE_N] = { char *tile_files[TILE_N] = {
"assets/tiles/tile_grass.png", "assets/tiles/tile_grass.png",
"assets/tiles/tile_grass.png", "assets/tiles/tile_grass.png",
@ -34,28 +54,35 @@ char *tile_files[TILE_N] = {
}; };
Texture2D tex_index[TEX_N]; Texture2D tex_index[TEX_N];
Image tile_index[TILE_N]; Texture2D tile_index[TILE_N];
extern GUIInfo gui_info; extern GUIInfo gui_info;
int init_draw(){ int init_draw(){
char tmpbuf[4096];
InitWindow(1280, 720, "Mineur Tycoon"); InitWindow(1280, 720, "Mineur Tycoon");
SetTargetFPS(60); SetTargetFPS(60);
const char *cwd = GetApplicationDirectory();
puts(cwd);
for(int i = 0; i < TEX_N; i++){ for(int i = 0; i < TEX_N; i++){
tex_index[i] = LoadTexture(tex_files[i]); snprintf(tmpbuf, 4096, "%s%s", cwd, tex_files[i]);
tex_index[i] = LoadTexture(tmpbuf);
GenTextureMipmaps(&tex_index[i]); GenTextureMipmaps(&tex_index[i]);
} }
for(int i = 0; i < TILE_N; i++){ for(int i = 0; i < TILE_N; i++){
tile_index[i] = LoadImage(tile_files[i]); snprintf(tmpbuf, 4096, "%s%s", cwd, tile_files[i]);
ImageFormat(&tile_index[i], 4); tile_index[i] = LoadTexture(tmpbuf);
GenTextureMipmaps(&tex_index[i]);
} }
return 0; return 0;
} }
/*
Texture2D mkmap_tex(Map *map){ Texture2D mkmap_tex(Map *map){
uint8_t *tiles = map->layers[0]; uint8_t *tiles = map->layers[0];
void *pixels = MemAlloc(3*map->width*32*map->height*32); void *pixels = MemAlloc(3*map->width*32*map->height*32);
@ -76,7 +103,8 @@ Texture2D mkmap_tex(Map *map){
Rectangle rectb = recta; Rectangle rectb = recta;
rectb.x = x*32; rectb.x = x*32;
rectb.y = y*32; rectb.y = y*32;
ImageDraw(&mapimg, tile_index[tiles[map->width*y + x]], recta, rectb, WHITE); ImageDraw(&mapimg, tile_index[tiles[map->width*y + x]], recta, rectb,
WHITE);
} }
} }
Texture2D maptex = LoadTextureFromImage(mapimg); Texture2D maptex = LoadTextureFromImage(mapimg);
@ -84,12 +112,21 @@ Texture2D mkmap_tex(Map *map){
MemFree(pixels); MemFree(pixels);
return maptex; return maptex;
} }
*/
void draw_workers(Game *game){ void draw_workers(Game *game){
Workers *workers = &game->workers; Workers *workers = &game->workers;
for(int i = 0; i < workers->worker_n; i++){ for(int i = 0; i < workers->worker_n; i++){
Worker *worker = &workers->worker_stack[i]; Worker *worker = &workers->worker_stack[i];
Camera2D *cam = &game->camera;
V2d pos = worker->pos; V2d pos = worker->pos;
float screenX = pos.x - cam->target.x;
float screenY = pos.y - cam->target.y;
float xmax = 1280/cam->zoom;
float ymax = 720/cam->zoom;
if(screenX < -64 || screenY < -64 || screenX > xmax || screenY > ymax)
continue;
if(worker->ai.status == S_Operating){ if(worker->ai.status == S_Operating){
pos = Vector2Add(pos, pos = Vector2Add(pos,
machine_dict[worker->ai.mtype].offsets[worker->assign_n]); machine_dict[worker->ai.mtype].offsets[worker->assign_n]);
@ -103,23 +140,65 @@ void draw_machines(Game *game){
Machines *machines = &game->machines; Machines *machines = &game->machines;
for(int i = 0; i < machines->machine_n; i++){ for(int i = 0; i < machines->machine_n; i++){
Machine *machine = &machines->machine_stack[i]; Machine *machine = &machines->machine_stack[i];
float active_per = Remap((float)machine->active,0,255,0.2,1); Camera2D *cam = &game->camera;
DrawRectangleV(machine->pos, (V2d){.x=64,.y=64}, Fade(BLUE,active_per)); float screenX = machine->pos.x - cam->target.x;
float screenY = machine->pos.y - cam->target.y;
float xmax = 1280/cam->zoom;
float ymax = 720/cam->zoom;
if(screenX < -64 || screenY < -64 || screenX > xmax || screenY > ymax)
continue;
float active_per = Remap((float)machine->active,0,255,0.5,1);
DrawTextureV(tex_index[T_MBase + machine->type], machine->pos, Fade(WHITE, active_per));
} }
} }
void draw_map(Game *game){ void draw_map(Game *game, Camera2D cam){
DrawTextureV(game->map.maptex, (V2d){0,0}, WHITE); int w = game->map.width;
int bx = (cam.target.x - cam.offset.x)/32;
int mx = bx + 1280.0/32.0/cam.zoom + 2;
bx = bx < 0 ? 0 : bx;
mx = mx >= w ? w : mx;
int by = (cam.target.y - cam.offset.y)/32;
int my = by + 720.0/32.0/cam.zoom + 2;
by = by < 0 ? 0 : by;
my = my >= game->map.height ? game->map.height : my;
uint8_t *layer = game->map.layers[0];
for(; by < my; by++){
for(int nbx = bx; nbx < mx; nbx++)
DrawTexture(tile_index[layer[by*w+nbx]], nbx*32, by*32, WHITE);
}
} }
void draw_game(Game *game){ void draw_game(Game *game){
ClearBackground(BLACK); ClearBackground((Color){144, 189, 209});
BeginMode2D(game->camera); BeginMode2D(game->camera);
draw_map(game); draw_map(game, game->camera);
draw_machines(game); draw_machines(game);
draw_workers(game); draw_workers(game);
EndMode2D(); EndMode2D();
printf("%f\n", game->camera.zoom);
}
void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box){
Camera2D ncam = {
.offset = {.x = screen_box.x, .y = screen_box.y},
.target = {
.x = world_box.x,
.y = world_box.y
},
.rotation = 0,
.zoom = screen_box.width/world_box.width
};
BeginMode2D(ncam);
BeginScissorMode(screen_box.x, screen_box.y, screen_box.width,
screen_box.height);
draw_map(game, ncam);
draw_machines(game);
draw_workers(game);
EndScissorMode();
EndMode2D();
} }
void draw(Game *game){ void draw(Game *game){

View file

@ -18,8 +18,8 @@
int init_draw(); int init_draw();
Texture2D mkmap_tex(Map *map);
void draw_game(Game *game); void draw_game(Game *game);
void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box);
void draw(Game *game); void draw(Game *game);

38
src/game.c Normal file → Executable file
View file

@ -8,17 +8,31 @@
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include "config.h"
#include "types.h" #include "types.h"
#include "map.h" #include "map.h"
#include "game.h" #include "game.h"
#include "lua_inter.h" #include "lua_inter.h"
MachineDef machine_dict[] = { MachineDef machine_dict[] = {
{{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Coal, 4}, // MT_CoalMine // MT_CoalMine
{{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Iron, 4}, // MT_IronMine {{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Coal, 4},
{{{0,0},{32,0},{0,32},{32,32}}, {R_Coal, R_Iron}, 2, R_Steel, 0}, // MT_Furnace // MT_IronMine
{{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Iron, 4},
// MT_Furnace
{{{0,0},{32,0},{0,32},{32,32}}, {R_Coal, R_Iron}, 2, R_Steel, 0},
}; };
char *worker_names[] = {
"Bob",
"Jill",
"Frank",
"Michel",
"Sophie"
};
_Static_assert(sizeof(worker_names)/sizeof(void*) >= WORKER_NAMES);
int init_machines(Machines *machines){ int init_machines(Machines *machines){
machines->machine_n = 0; machines->machine_n = 0;
machines->machine_id = 0; machines->machine_id = 0;
@ -58,29 +72,33 @@ int init_game(Game *game){
return 1; return 1;
} }
load_map(game, 0);
int c_worker = 0; int c_worker = 0;
for(int k = 0; k < 16; k++){ for(int k = 0; k < 20; k++){
for(int j = 0; j < 16; j++){ for(int j = 0; j < 40; j++){
Machine *coal_mine = add_machine(&game->machines, (V2d){k*250+40,j*400+40}); Machine *coal_mine = add_machine(&game->machines, (V2d){k*250+40,j*400+40});
coal_mine->type = MT_CoalMine; coal_mine->type = MT_CoalMine;
int coal_id = coal_mine->id;
Machine *iron_mine = add_machine(&game->machines, (V2d){k*250+40,j*400+240}); Machine *iron_mine = add_machine(&game->machines, (V2d){k*250+40,j*400+240});
iron_mine->type = MT_IronMine; iron_mine->type = MT_IronMine;
int iron_id = iron_mine->id;
Machine *furnace = add_machine(&game->machines, (V2d){k*250+140,j*400+140}); Machine *furnace = add_machine(&game->machines, (V2d){k*250+140,j*400+140});
furnace->type = MT_Furnace; furnace->type = MT_Furnace;
// The adresses may have changed
coal_mine = get_machine_from_id(&game->machines, coal_id);
iron_mine = get_machine_from_id(&game->machines, iron_id);
for(int i = 0; i < 10; i++) for(int i = 0; i < 10; i++)
add_worker(&game->workers, (V2d){.x=k*250+100,.y=j*400}); add_worker(&game->workers, (V2d){.x=k*250+100,.y=j*400});
for(int i = 0; i < 4; i++) for(int i = 0; i < 4; i++)
assign_worker_machine(coal_mine, &game->workers.worker_stack[c_worker++]); assign_worker_machine(coal_mine, &game->workers.worker_stack[c_worker++]);
for(int i = 4; i < 8; i++) for(int i = 4; i < 8; i++)
assign_worker_machine(iron_mine, &game->workers.worker_stack[c_worker++]); assign_worker_machine(iron_mine, &game->workers.worker_stack[c_worker++]);
game->workers.worker_stack[c_worker].satisf = rand() & 63;
assign_worker_fetch(iron_mine, furnace, &game->workers.worker_stack[c_worker++]); assign_worker_fetch(iron_mine, furnace, &game->workers.worker_stack[c_worker++]);
game->workers.worker_stack[c_worker].satisf = rand() & 63;
assign_worker_fetch(coal_mine, furnace, &game->workers.worker_stack[c_worker++]); assign_worker_fetch(coal_mine, furnace, &game->workers.worker_stack[c_worker++]);
if(!(furnace->id%1000))
printf("%d\n",furnace->id);
} }
} }
@ -115,6 +133,7 @@ Worker *add_worker(Workers *workers, V2d pos){
Worker *work = &workers->worker_stack[workers->worker_n++]; Worker *work = &workers->worker_stack[workers->worker_n++];
work->pos = pos; work->pos = pos;
work->name = rand()%WORKER_NAMES;
work->ai = (AiInternal){S_Idle, 0, 0, 0, MACH_NULL, MACH_NULL, {.x=0,.y=0}}; work->ai = (AiInternal){S_Idle, 0, 0, 0, MACH_NULL, MACH_NULL, {.x=0,.y=0}};
return work; return work;
@ -345,6 +364,7 @@ int assign_worker_machine(Machine *machine, Worker *worker){
ai->assigned_machine = machine->id; ai->assigned_machine = machine->id;
ai->assigned_machine1 = MACH_NULL; ai->assigned_machine1 = MACH_NULL;
ai->status = S_Operating; ai->status = S_Operating;
// TODO : Don't tp
worker->pos = machine->pos; worker->pos = machine->pos;
return 0; return 0;
} }

View file

@ -20,7 +20,10 @@
#define TPS_TIME (1.0/TPS) #define TPS_TIME (1.0/TPS)
#define WORKER_NAMES 5
extern MachineDef machine_dict[]; extern MachineDef machine_dict[];
extern char *worker_names[];
int init_game(Game *game); int init_game(Game *game);

View file

@ -13,6 +13,83 @@
Game *gl_game; Game *gl_game;
typedef void (lua_affect_t)(lua_State*, void *, int);
struct LuaAffect {
lua_affect_t *func;
int arg;
};
struct TypeMap {
char *type_name;
int n_fields;
char **field_names;
struct LuaAffect field_affect[];
};
void lua_index_custom(lua_State *L, struct TypeMap *map){
void **ptr = luaL_checkudata(L, 1, map->type_name);
const char *name = luaL_checkstring(L, 2);
for(int i = 0; i < map->n_fields; i++){
if(!strcmp(name,map->field_names[i])){
map->field_affect[i].func(L, *((char**)(ptr)), map->field_affect[i].arg);
return;
}
}
char tbuf[256];
snprintf(tbuf, 256, "Invalid index for %s : \"%s\"", map->type_name, name);
luaL_error(L, tbuf);
}
void lua_index_u8(lua_State *L, void *base, int offset){
uint8_t *ptr = (void*)(((char*)base) + offset);
lua_pushinteger(L, *ptr);
}
void lua_index_u16(lua_State *L, void *base, int offset){
uint16_t *ptr = (void*)(((char*)base) + offset);
lua_pushinteger(L, *ptr);
}
void lua_index_i32(lua_State *L, void *base, int offset){
int32_t *ptr = (void*)(((char*)base) + offset);
lua_pushinteger(L, *ptr);
}
// Index a V2d member of a struct != adressing into a V2d
void lua_index_V2d(lua_State *L, void *base, int offset){
V2d *ptr0 = (void*)(((char*)base) + offset);
V2d **ptr1 = lua_newuserdatauv(L, sizeof(void*), 0);
*ptr1 = ptr0;
luaL_newmetatable(L, "V2d");
lua_setmetatable(L, -2);
}
void lua_newindex_u8(lua_State *L, void *base, int offset){
uint8_t *ptr = (void*)(((char*)base) + offset);
*ptr = luaL_checkinteger(L, 3);
}
void lua_newindex_u16(lua_State *L, void *base, int offset){
uint16_t *ptr = (void*)(((char*)base) + offset);
*ptr = luaL_checkinteger(L, 3);
}
void lua_newindex_i32(lua_State *L, void *base, int offset){
uint16_t *ptr = (void*)(((char*)base) + offset);
*ptr = luaL_checkinteger(L, 3);
}
// newindex a V2d member of a struct != adressing into a V2d
void lua_newindex_V2d(lua_State *L, void *base, int offset){
V2d *dst = (void*)(((char*)base) + offset);
V2d **ptr = luaL_checkudata(L, 3, "V2d");
memmove(dst, *ptr, sizeof(V2d));
}
static int lua_V2d_index(lua_State *L){ static int lua_V2d_index(lua_State *L){
V2d **ptr = luaL_checkudata(L, 1, "V2d"); V2d **ptr = luaL_checkudata(L, 1, "V2d");
V2d *u = *ptr; V2d *u = *ptr;
@ -20,11 +97,11 @@ static int lua_V2d_index(lua_State *L){
if(name[1] != '\0') if(name[1] != '\0')
return luaL_error(L, "Invalid index for V2d"); return luaL_error(L, "Invalid index for V2d");
if(name[0] == 'x'){ if(name[0] == 'x'){
lua_pushinteger(L, u->x); lua_pushnumber(L, u->x);
return 1; return 1;
} }
else if(name[0] == 'y'){ else if(name[0] == 'y'){
lua_pushinteger(L, u->y); lua_pushnumber(L, u->y);
return 1; return 1;
} }
else { else {
@ -36,7 +113,7 @@ static int lua_V2d_newindex(lua_State *L){
V2d **ptr = luaL_checkudata(L, 1, "V2d"); V2d **ptr = luaL_checkudata(L, 1, "V2d");
V2d *u = *ptr; V2d *u = *ptr;
const char *name = luaL_checkstring(L, 2); const char *name = luaL_checkstring(L, 2);
float val = luaL_checknumber(L, 3); lua_Number val = luaL_checknumber(L, 3);
if(name[1] != '\0') if(name[1] != '\0')
return luaL_error(L, "Invalid index for V2d"); return luaL_error(L, "Invalid index for V2d");
if(name[0] == 'x'){ if(name[0] == 'x'){
@ -56,8 +133,8 @@ static int lua_storage_index(lua_State *L){
uint8_t **ptr = luaL_checkudata(L, 1, "Storage"); uint8_t **ptr = luaL_checkudata(L, 1, "Storage");
uint8_t *storage = *ptr; uint8_t *storage = *ptr;
int pos = luaL_checkinteger(L, 2); int pos = luaL_checkinteger(L, 2);
if(pos >= 0 && pos < 5){ if(pos > 0 && pos < 6){
lua_pushinteger(L, storage[pos]); lua_pushinteger(L, storage[pos-1]);
return 1; return 1;
} }
else else
@ -69,94 +146,215 @@ static int lua_storage_newindex(lua_State *L){
uint8_t *storage = *ptr; uint8_t *storage = *ptr;
int pos = luaL_checkinteger(L, 2); int pos = luaL_checkinteger(L, 2);
int val = luaL_checkinteger(L, 3); int val = luaL_checkinteger(L, 3);
if(pos >= 0 && pos < 5){ if(pos > 0 && pos < 6)
storage[pos] = val; storage[pos-1] = val;
}
else else
return luaL_error(L, "Invalid index for Storage"); return luaL_error(L, "Invalid index for Storage");
return 0; return 0;
} }
void lua_machine_storage_index(lua_State *L, void *mach_, int offset){
(void)offset;
Machine *mach = mach_;
uint8_t **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
*ptr = mach->storage;
luaL_newmetatable(L, "Storage");
lua_setmetatable(L, -2);
}
void lua_worker_aiint_index(lua_State *L, void *work_, int offset){
(void)offset;
Worker *work = work_;
AiInternal **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
*ptr = &work->ai;
luaL_newmetatable(L, "AiInternal");
lua_setmetatable(L, -2);
}
void lua_worker_aiint_newindex(lua_State *L, void *work_, int offset){
(void)offset;
Worker *work = work_;
AiInternal **src = luaL_checkudata(L, 3, "AiInternal");
memmove(&work->ai, *src, sizeof(AiInternal));
}
#define OFFSET(field) (size_t)((char*)(BASE.field)-(char*)(BASE))
Machine bmach;
#define BASE &bmach
char *mach_index_names[] = {
"pos",
"type",
"assign_workers",
"active",
"storage",
"id"
};
struct TypeMap machine_index_map = {
.type_name = "Machine",
.n_fields = 6,
.field_names = mach_index_names,
.field_affect = {
{lua_index_V2d, OFFSET(pos)},
{lua_index_u8, OFFSET(type)},
{lua_index_u8, OFFSET(assign_workers)},
{lua_index_u8, OFFSET(active)},
{lua_machine_storage_index, 0},
{lua_index_u16, OFFSET(id)}
}
};
struct TypeMap machine_newindex_map = {
.type_name = "Machine",
.n_fields = 6,
.field_names = mach_index_names,
.field_affect = {
{lua_newindex_V2d, OFFSET(pos)},
{lua_newindex_u8, OFFSET(type)},
{lua_newindex_u8, OFFSET(assign_workers)},
{lua_newindex_u8, OFFSET(active)},
{NULL, 0}, // Shouldn't happen - TODO : Fix when it happens
{lua_newindex_u16, OFFSET(id)}
}
};
#undef BASE
AiInternal baiint;
#define BASE &baiint
char *aiint_index_names[] = {
"status",
"carrying",
"carry_type",
"mtype",
"assigned_machine",
"assigned_machine1",
"dest"
};
struct TypeMap aiint_index_map = {
.type_name = "AiInternal",
.n_fields = 7,
.field_names = aiint_index_names,
.field_affect = {
{lua_index_u8, OFFSET(status)},
{lua_index_u8, OFFSET(carrying)},
{lua_index_u8, OFFSET(carry_type)},
{lua_index_u8, OFFSET(mtype)},
{lua_index_u16, OFFSET(assigned_machine)},
{lua_index_u16, OFFSET(assigned_machine1)},
{lua_index_V2d, OFFSET(dest)}
}
};
struct TypeMap aiint_newindex_map = {
.type_name = "AiInternal",
.n_fields = 7,
.field_names = aiint_index_names,
.field_affect = {
{lua_newindex_u8, OFFSET(status)},
{lua_newindex_u8, OFFSET(carrying)},
{lua_newindex_u8, OFFSET(carry_type)},
{lua_newindex_u8, OFFSET(mtype)},
{lua_newindex_u16, OFFSET(assigned_machine)},
{lua_newindex_u16, OFFSET(assigned_machine1)},
{lua_newindex_V2d, OFFSET(dest)}
}
};
#undef BASE
Worker bwork;
#define BASE &bwork
char *work_index_names[] = {
"pos",
"ai",
"traits",
"anim_frame",
"assign_n",
"satisf",
"fatigue",
"name"
};
struct TypeMap worker_index_map = {
.type_name = "Worker",
.n_fields = 8,
.field_names = work_index_names,
.field_affect = {
{lua_index_V2d, OFFSET(pos)},
{lua_worker_aiint_index, 0},
{lua_index_u16, OFFSET(traits)},
{lua_index_u8, OFFSET(anim_frame)},
{lua_index_u8, OFFSET(assign_n)},
{lua_index_u8, OFFSET(satisf)},
{lua_index_u8, OFFSET(fatigue)},
{lua_index_u8, OFFSET(name)}
}
};
struct TypeMap worker_newindex_map = {
.type_name = "Worker",
.n_fields = 8,
.field_names = work_index_names,
.field_affect = {
{lua_newindex_V2d, OFFSET(pos)},
{lua_worker_aiint_newindex, 0},
{lua_newindex_u16, OFFSET(traits)},
{lua_newindex_u8, OFFSET(anim_frame)},
{lua_newindex_u8, OFFSET(assign_n)},
{lua_newindex_u8, OFFSET(satisf)},
{lua_newindex_u8, OFFSET(fatigue)},
{lua_newindex_u8, OFFSET(name)}
}
};
#undef BASE
#undef OFFSET
static int lua_machine_index(lua_State *L){ static int lua_machine_index(lua_State *L){
void **ptr = luaL_checkudata(L, 1, "Machine"); lua_index_custom(L, &machine_index_map);
Machine *mach = *ptr;
const char *name = luaL_checkstring(L, 2);
int ret;
if(!strcmp(name, "pos")){
V2d **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
*ptr = &mach->pos;
luaL_newmetatable(L, "V2d");
lua_setmetatable(L, -2);
return 1;
}
else if(!strcmp(name, "type")){
ret = mach->type;
}
else if(!strcmp(name, "assign_workers")){
ret = mach->assign_workers;
}
else if(!strcmp(name, "active")){
ret = mach->active;
}
else if(!strcmp(name, "storage")){
uint8_t **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
*ptr = mach->storage;
luaL_newmetatable(L, "Storage");
lua_setmetatable(L, -2);
return 1;
}
else if(!strcmp(name, "id")){
ret = mach->id;
}
else {
return luaL_error(L, "Invalid index for Machine");
}
lua_pushinteger(L, ret);
return 1; return 1;
} }
static int lua_machine_newindex(lua_State *L){ static int lua_machine_newindex(lua_State *L){
void **ptr = luaL_checkudata(L, 1, "Machine"); lua_index_custom(L, &machine_newindex_map);
Machine *mach = *ptr;
const char *name = luaL_checkstring(L, 2);
int prm;
V2d *pos = NULL;
if(lua_isinteger(L, -1))
prm = luaL_checkinteger(L, 3);
else{
V2d **ptr = luaL_checkudata(L, 3, "V2d");
pos = *ptr;
}
if(!strcmp(name, "pos") && pos){
memmove(&mach->pos, pos, sizeof(V2d));
return 1;
}
else if(!strcmp(name, "type")){
mach->type = prm;
}
else if(!strcmp(name, "assign_workers")){
mach->assign_workers = prm;
}
else if(!strcmp(name, "active")){
mach->active = prm;
}
else if(!strcmp(name, "id")){
mach->id = prm;
}
else {
return luaL_error(L, "Invalid index for Machine");
}
return 0; return 0;
} }
// add_machine(x, y) static int lua_aiint_index(lua_State *L){
lua_index_custom(L, &aiint_index_map);
return 1;
}
static int lua_aiint_newindex(lua_State *L){
lua_index_custom(L, &aiint_newindex_map);
return 1;
}
static int lua_worker_index(lua_State *L){
lua_index_custom(L, &worker_index_map);
return 1;
}
static int lua_worker_newindex(lua_State *L){
lua_index_custom(L, &worker_newindex_map);
return 1;
}
// add_machine(x, y) -> Machine
static int lua_add_machine(lua_State *L){ static int lua_add_machine(lua_State *L){
int x = luaL_checkinteger(L, 1); lua_Number x = luaL_checknumber(L, 1);
int y = luaL_checkinteger(L, 2); lua_Number y = luaL_checknumber(L, 2);
Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0); Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0);
*buf = add_machine(&gl_game->machines, (V2d){.x=x,.y=y}); *buf = add_machine(&gl_game->machines, (V2d){.x=x,.y=y});
@ -169,12 +367,14 @@ static int lua_add_machine(lua_State *L){
return 1; return 1;
} }
// get_machine_from_id(id) // get_machine_from_id(id) -> Machine or nil
static int lua_get_machine_from_id(lua_State *L){ static int lua_get_machine_from_id(lua_State *L){
int id = luaL_checkinteger(L, 1) & 0xFFFF; int id = luaL_checkinteger(L, 1) & 0xFFFF;
Machine *mach = get_machine_from_id(&gl_game->machines, id); Machine *mach = get_machine_from_id(&gl_game->machines, id);
if(!mach) if(!mach){
return 0; lua_pushnil(L);
return 1;
}
Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0); Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0);
*buf = mach; *buf = mach;
luaL_newmetatable(L, "Machine"); luaL_newmetatable(L, "Machine");
@ -183,6 +383,67 @@ static int lua_get_machine_from_id(lua_State *L){
return 1; return 1;
} }
// add_worker(x, y) -> Worker
static int lua_add_worker(lua_State *L){
lua_Number x = luaL_checknumber(L, 1);
lua_Number y = luaL_checknumber(L, 2);
Worker *work = add_worker(&gl_game->workers, (V2d){.x=x,.y=y});
if(!work)
return luaL_error(L, "(lua) Failed to add a worker");
Worker **buf = lua_newuserdatauv(L, sizeof(void*), 0);
*buf = work;
luaL_newmetatable(L, "Worker");
lua_setmetatable(L, -2);
return 1;
}
// get_worker_from_pos(x, y) -> Machine or nil
static int lua_get_worker_from_pos(lua_State *L){
lua_Number x = luaL_checknumber(L, 1);
lua_Number y = luaL_checknumber(L, 2);
Worker *work = get_worker_from_pos(&gl_game->workers, (V2d){.x=x,.y=y});
if(!work){
lua_pushnil(L);
return 1;
}
Worker **buf = lua_newuserdatauv(L, sizeof(void*), 0);
*buf = work;
luaL_newmetatable(L, "Worker");
lua_setmetatable(L, -2);
return 1;
}
// assign_worker_machine(machine, worker) -> Int
static int lua_assign_worker_machine(lua_State *L){
Machine **buf0 = luaL_checkudata(L, 1, "Machine");
Machine *mach = *buf0;
Worker **buf1 = luaL_checkudata(L, 2, "Worker");
Worker *work = *buf1;
int res = assign_worker_machine(mach, work);
lua_pushinteger(L, res);
return 1;
}
// A vers B
// assign_worker_fetch(machine_a, machine_b, worker) -> Int
static int lua_assign_worker_fetch(lua_State *L){
Machine **buf0 = luaL_checkudata(L, 1, "Machine");
Machine *mach_a = *buf0;
buf0 = luaL_checkudata(L, 2, "Machine");
Machine *mach_b = *buf0;
Worker **buf1 = luaL_checkudata(L, 3, "Worker");
Worker *work = *buf1;
int res = assign_worker_fetch(mach_a, mach_b, work);
lua_pushinteger(L, res);
return 1;
}
// mtprint(str) // mtprint(str)
static int lua_mtprint(lua_State *L){ static int lua_mtprint(lua_State *L){
printf("(lua) %s\n", luaL_checkstring(L, 1)); printf("(lua) %s\n", luaL_checkstring(L, 1));
@ -199,6 +460,10 @@ struct LuaFn exported_funcs[] = {
{"mtprint", lua_mtprint}, {"mtprint", lua_mtprint},
{"add_machine", lua_add_machine}, {"add_machine", lua_add_machine},
{"get_machine_from_id", lua_get_machine_from_id}, {"get_machine_from_id", lua_get_machine_from_id},
{"add_worker", lua_add_worker},
{"get_worker_from_pos", lua_get_worker_from_pos},
{"assign_worker_machine", lua_assign_worker_machine},
{"assign_worker_fetch", lua_assign_worker_fetch},
}; };
int lstate_n; int lstate_n;
@ -258,38 +523,62 @@ void lua_onframe(Game *game){
lua_callloop(game, "on_frame"); lua_callloop(game, "on_frame");
} }
void lua_onmapload(Game *game, int map){
gl_game = game;
for(int i = 0; i < lstate_n; i++){
lua_State *L = lstates[i];
lua_mkgame(L, game);
if(lua_getglobal(L, "on_load") == LUA_TNIL){
lua_pop(L, 2);
continue;
}
lua_pushvalue(L, -2);
lua_pushinteger(L, map);
int ret = lua_pcall(L, 2, 0, 0);
if(ret)
puts(lua_tostring(L, -1));
else{
lua_pushliteral(L, "cash");
lua_gettable(L, -2);
if(lua_isinteger(L, -1))
game->cash = lua_tointeger(L, -1);
}
}
}
#define ADD_TYPE(name, index, newindex) \
luaL_newmetatable(L, (name)); \
lua_pushcfunction(L, (index)); \
lua_setfield(L, -2, "__index"); \
lua_pushcfunction(L, (newindex)); \
lua_setfield(L, -2, "__newindex")
void lua_mt_init(lua_State *L){ void lua_mt_init(lua_State *L){
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++) for(size_t i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
lua_register(L, exported_funcs[i].name, exported_funcs[i].fn); lua_register(L, exported_funcs[i].name, exported_funcs[i].fn);
luaL_newmetatable(L, "Machine"); ADD_TYPE("Machine", lua_machine_index, lua_machine_newindex);
lua_pushcfunction(L, lua_machine_index); ADD_TYPE("V2d", lua_V2d_index, lua_V2d_newindex);
lua_setfield(L, -2, "__index"); ADD_TYPE("Storage", lua_storage_index, lua_storage_newindex);
lua_pushcfunction(L, lua_machine_newindex); ADD_TYPE("AiInternal", lua_aiint_index, lua_aiint_newindex);
lua_setfield(L, -2, "__newindex"); ADD_TYPE("Worker", lua_worker_index, lua_worker_newindex);
luaL_newmetatable(L, "V2d");
lua_pushcfunction(L, lua_V2d_index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_V2d_newindex);
lua_setfield(L, -2, "__newindex");
luaL_newmetatable(L, "Storage");
lua_pushcfunction(L, lua_storage_index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_storage_newindex);
lua_setfield(L, -2, "__newindex");
} }
void script_init(Game *game){ void script_init(Game *game){
char tmpbuf[4096];
gl_game = game; gl_game = game;
FilePathList scripts = LoadDirectoryFilesEx("script",".lua",true);
const char *cwd = GetApplicationDirectory();
snprintf(tmpbuf, 4096, "%sscript", cwd);
FilePathList scripts = LoadDirectoryFilesEx(tmpbuf,".lua",true);
lstate_n = scripts.count; lstate_n = scripts.count;
lstates = malloc(sizeof(void*)*lstate_n); lstates = malloc(sizeof(void*)*lstate_n);
if(!lstates){ if(!lstates){
puts("Fail 318");
return; return;
} }
for(int i = 0; i < scripts.count; i++){ for(uint i = 0; i < scripts.count; i++){
lstates[i] = luaL_newstate(); lstates[i] = luaL_newstate();
lua_mt_init(lstates[i]); lua_mt_init(lstates[i]);
@ -298,6 +587,9 @@ void script_init(Game *game){
if(ret) if(ret)
puts(lua_tostring(lstates[i], -1)); puts(lua_tostring(lstates[i], -1));
} }
for(int i = 0; i < scripts.capacity; i++)
MemFree(scripts.paths[i]);
MemFree(scripts.paths);
} }
void script_clean(){ void script_clean(){

View file

@ -7,6 +7,8 @@ void lua_ontick(Game *game);
void lua_onframe(Game *game); void lua_onframe(Game *game);
void lua_onmapload(Game *game, int map);
void script_init(); void script_init();
void script_clean(); void script_clean();

View file

@ -7,6 +7,7 @@
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include "config.h"
#include "types.h" #include "types.h"
#include "game.h" #include "game.h"
#include "map.h" #include "map.h"
@ -20,16 +21,51 @@
/* TODO /* TODO
* UI worker/machine : * UI worker/machine :
* map : * map :
* - Textures : ~ * - Textures : x
* - Affichage : * - Génération :
* - convertisseur :
*/ */
#if DEBUG
FILE *log_file;
char *log_levels[] = {
"???",
"TRACE",
"DEBUG",
"INFO",
"WARNING",
"ERROR",
"FATAL",
"!!!"
};
void log_callback(int logLevel, const char *text, va_list args){
char tmpbuf[1024*16];
snprintf(tmpbuf, 1024*16, "%s : %s\n", log_levels[logLevel], text);
vfprintf(log_file, tmpbuf, args);
}
int init_log_file(){
char tmpbuf[4096];
snprintf(tmpbuf, 4096, "%s%s", GetApplicationDirectory(), LOG_PATH);
log_file = fopen(tmpbuf, "w");
if(!log_file)
return 1;
SetTraceLogCallback(log_callback);
return 0;
}
#endif
int init(Game *game){ int init(Game *game){
int err = init_draw(); int err;
#if DEBUG
err = init_log_file();
#endif
err |= init_draw();
err |= init_game(game); err |= init_game(game);
init_ui(); init_ui();
script_init(game); script_init(game);
load_map(game, 0);
return err; return err;
} }

View file

@ -10,6 +10,7 @@
#include "types.h" #include "types.h"
#include "draw.h" #include "draw.h"
#include "lua_inter.h"
extern MapCollec mapcollec; extern MapCollec mapcollec;
@ -20,10 +21,17 @@ int generate_coll(Game *game, int mapn){
memcpy(lmap->coll, map->layers[1], map->width*map->height); memcpy(lmap->coll, map->layers[1], map->width*map->height);
for(int i = 0; i < mach->machine_n; i++){ for(int i = 0; i < mach->machine_n; i++){
V2d pos = mach->machine_stack[i].pos; V2d pos = mach->machine_stack[i].pos;
pos.x /= 32;
pos.y /= 32;
if(pos.x >= lmap->width || pos.y >= lmap->height)
continue;
lmap->coll[(int)(pos.y * lmap->width + pos.x)] = 1; lmap->coll[(int)(pos.y * lmap->width + pos.x)] = 1;
lmap->coll[(int)(pos.y * lmap->width + pos.x+1)] = 1; if(pos.x+1 < lmap->width)
lmap->coll[(int)((pos.y+1) * 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)] = 1; if(pos.y+1 < lmap->height)
lmap->coll[(int)((pos.y+1) * lmap->width + pos.x)] = 1;
if(pos.x+1 < lmap->width && pos.y+1 < lmap->height)
lmap->coll[(int)((pos.y+1) * lmap->width + pos.x+1)] = 1;
} }
return 0; return 0;
} }
@ -31,14 +39,18 @@ int generate_coll(Game *game, int mapn){
int load_map(Game *game, int mapn){ int load_map(Game *game, int mapn){
LoadedMap *nmap = &game->map; LoadedMap *nmap = &game->map;
Map *map = mapcollec.maps[mapn]; Map *map = mapcollec.maps[mapn];
nmap->maptex = mkmap_tex(map);
nmap->width = map->width; nmap->width = map->width;
nmap->height = map->height; nmap->height = map->height;
nmap->coll = realloc(nmap->coll, map->width*map->height); nmap->coll = malloc(map->width*map->height);
nmap->layers[0] = map->layers[0];
nmap->layers[1] = map->layers[1];
if(!nmap->coll) if(!nmap->coll)
return 1; return 1;
if(generate_coll(game, mapn)) if(generate_coll(game, mapn))
return 1; return 1;
lua_onmapload(game, mapn);
return 0; return 0;
} }

16
src/mapgen.c Normal file
View file

@ -0,0 +1,16 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <raylib.h>
#include <raymath.h>
#include "config.h"
#include "types.h"
#include "mapgen.h"
int gen_map(Game *game){
}

13
src/mapgen.h Normal file
View file

@ -0,0 +1,13 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <raylib.h>
#include <raymath.h>
#include "config.h"
#include "types.h"
#pragma once

View file

@ -57,10 +57,11 @@ typedef struct {
AiInternal ai; AiInternal ai;
uint16_t traits; uint16_t traits;
uint8_t anim_frame; uint8_t anim_frame;
uint assign_n : 2; // To offset on machines uint8_t assign_n; // To offset on machines
uint satisf : 6; uint8_t satisf;
uint8_t fatigue; uint8_t fatigue;
uint8_t __padding[3]; uint8_t name;
uint8_t __padding[1];
} Worker; } Worker;
@ -122,10 +123,10 @@ typedef struct {
typedef struct { typedef struct {
uint8_t *layers[2];
uint8_t *coll; uint8_t *coll;
int32_t width; // * int32_t width; // *
int32_t height; // * int32_t height; // *
Texture2D maptex;
} LoadedMap; } LoadedMap;

View file

@ -27,6 +27,20 @@ void draw_text_centered(char *str, int y, int font_size, Color color){
DrawText(str, 640-width/2, y, font_size, color); DrawText(str, 640-width/2, y, font_size, color);
} }
void draw_print(int x, int y, int font_size, Color color, char *fmt, ...){
char tmpbuf[2048];
va_list vlist;
va_start(vlist, fmt);
vsnprintf(tmpbuf, 2048, fmt, vlist);
va_end(vlist);
DrawText(tmpbuf, x, y, font_size, color);
}
void draw_buttons(Widget *widget){ void draw_buttons(Widget *widget){
for(int i = 0; i < widget->button_n; i++){ for(int i = 0; i < widget->button_n; i++){
Button *butt = &widget->buttons[i]; Button *butt = &widget->buttons[i];
@ -134,10 +148,19 @@ void pause_menu_new_game(Game *game){
pause_menu_resume(game); pause_menu_resume(game);
} }
void pause_menu_do_event(Widget *widget, Game *game, MTEvent event){
if(event.type == EV_Mouse){
buttons_do_event(widget, game, event);
return;
}
if(event.key == KEY_ESCAPE)
pause_menu_resume(game);
}
Widget pause_menu = { Widget pause_menu = {
.box = {.x = 0, .y = 0, .width = -1, .height = -1}, .box = {.x = 0, .y = 0, .width = -1, .height = -1},
.draw = pause_menu_draw, .draw = pause_menu_draw,
.do_event = buttons_do_event, .do_event = pause_menu_do_event,
.capt_flags = CF_Mouse | CF_Keyb, .capt_flags = CF_Mouse | CF_Keyb,
.buttons = { .buttons = {
{.box = {520,170,240,40}, {.box = {520,170,240,40},
@ -188,33 +211,61 @@ Widget pause_menu = {
void worker_widget_draw(Widget *widget, Game *game){ void worker_widget_draw(Widget *widget, Game *game){
DrawRectangleRounded(widget->box, 0.1, 3, color_main_brown); DrawRectangleRounded(widget->box, 0.1, 3, color_main_brown);
draw_buttons(widget);
Worker *work = gui_info.widg_target;
Rectangle screen_box = {55,90,140,140};
Rectangle world_box = {work->pos.x-4, work->pos.y-4, 40, 40};
draw_game_mini(game, screen_box, world_box);
draw_print(205, 100, 17, BLACK, "Name : %s", worker_names[work->name]);
draw_print(205, 120, 17, BLACK, "Traits : TODO");
draw_print(205, 140, 17, BLACK, "Satisfaction : %.0f%%",
(float)work->satisf/64.0f*100.0f);
draw_print(205, 160, 17, BLACK, "Fatigue : %.0f%%",
(float)work->fatigue/256.0f*100.0f);
} }
void worker_widget_do_event(Widget *widget, Game *game, MTEvent event){ void worker_widget_close(Game *game){
if(event.click == MOUSE_BUTTON_LEFT) gui_info.widget_active[2] = false;
gui_info.widget_active[2] = false;
} }
Widget worker_widget = { Widget worker_widget = {
.box = {.x = 40, .y = 40, .width = 300, .height = 600}, .box = {.x = 40, .y = 40, .width = 350, .height = 600},
.draw = worker_widget_draw, .draw = worker_widget_draw,
.do_event = worker_widget_do_event, .do_event = buttons_do_event,
.capt_flags = CF_Mouse, .capt_flags = CF_Mouse,
.buttons = { .buttons = {
{.box = {350,50,30,30},
.color_0 = RED,
.color_1 = BLACK,
.color_txt = BLACK,
.txt = "X",
.handler = worker_widget_close
},
}, },
.button_n = 0 .button_n = 1
}; };
Widget machine_widget = { Widget machine_widget = {
}; };
void game_widget_do_event(Widget *widget, Game *game, MTEvent event){
if(event.key == KEY_P)
game->paused = !game->paused;
if(event.key == KEY_ESCAPE)
goto_pause_menu(game);
}
Widget game_widget = { Widget game_widget = {
.box = {0,0,0,0}, .box = {0,0,1280,720},
.draw = widget_draw_game, .draw = widget_draw_game,
.do_event = NULL, .do_event = game_widget_do_event,
.capt_flags = 0, .capt_flags = CF_Keyb,
.buttons = {}, .buttons = {},
.button_n = 0 .button_n = 0
}; };
@ -226,9 +277,12 @@ void init_ui(){
// Android exclusive key -> no exit key // Android exclusive key -> no exit key
SetExitKey(KEY_MENU); SetExitKey(KEY_MENU);
gui_info = (GUIInfo){{&game_widget, &main_menu, &worker_widget, &machine_widget, &pause_menu}, gui_info = (GUIInfo){
{false, true, false, false, false}, {&game_widget, &main_menu, &worker_widget, &machine_widget, &pause_menu},
NULL}; {false, true, false, false, false},
NULL,
NULL
};
} }
void draw_widgets(Game *game){ void draw_widgets(Game *game){
@ -306,6 +360,7 @@ void try_interface(Game *game, V2d pos){
printf("found worker\n"); printf("found worker\n");
gui_info.widget_active[2] = true; gui_info.widget_active[2] = true;
gui_info.widg_target = work;
return; return;
} }
@ -338,6 +393,12 @@ void get_keys(Game *game){
try_interface(game, GetScreenToWorld2D(mouse_pos,game->camera)); try_interface(game, GetScreenToWorld2D(mouse_pos,game->camera));
} }
int key = GetKeyPressed();
while(key){
ui_do_key(game, key);
key = GetKeyPressed();
}
if(keydown(KEY_W)) if(keydown(KEY_W))
game->camera.target.y -= 3; game->camera.target.y -= 3;
if(keydown(KEY_S)) if(keydown(KEY_S))
@ -346,12 +407,8 @@ void get_keys(Game *game){
game->camera.target.x -= 3; game->camera.target.x -= 3;
if(keydown(KEY_D)) if(keydown(KEY_D))
game->camera.target.x += 3; game->camera.target.x += 3;
if(keydown(KEY_UP)) if(keydown(KEY_UP) && game->camera.zoom < MAX_ZOOM)
game->camera.zoom *= 1.025; game->camera.zoom *= 1.025;
if(keydown(KEY_DOWN)) if(keydown(KEY_DOWN) && game->camera.zoom > MIN_ZOOM)
game->camera.zoom *= 1/1.025; game->camera.zoom *= 1/1.025;
if(IsKeyPressed(KEY_P))
game->paused = !game->paused;
if(IsKeyPressed(KEY_ESCAPE))
goto_pause_menu(game);
} }

View file

@ -72,6 +72,8 @@ typedef struct {
Widget *widgets[WIDGET_N]; Widget *widgets[WIDGET_N];
bool widget_active[WIDGET_N]; bool widget_active[WIDGET_N];
void *widg_target;
float *zoom; float *zoom;
} GUIInfo; } GUIInfo;