diff --git a/script/test.lua b/script/test.lua index 1020516..80b476d 100644 --- a/script/test.lua +++ b/script/test.lua @@ -1,18 +1,45 @@ mtprint("hello") -machine = add_machine(10, 10) -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 -worker = add_worker(30, 10) -mtprint(worker.name) -worker.pos.x = 25 -mtprint(worker.pos.x) +coal = add_machine(320, 320) +coal.type = 0 -- charbon +work = add_worker(300,300) +work1 = add_worker(300,300) +work3 = add_worker(300,300) +work4 = add_worker(300,300) +assign_worker_machine(coal, work) +assign_worker_machine(coal, work1) +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) -- Ceci va drainer la sortie de la machine 2 diff --git a/src/draw.c b/src/draw.c index c6d8b4b..d77e370 100644 --- a/src/draw.c +++ b/src/draw.c @@ -12,7 +12,7 @@ #include "draw.h" #include "ui.h" -#define TEX_N 9 +#define TEX_N 12 #define TILE_N 3 char *tex_files[TEX_N] = { @@ -24,9 +24,29 @@ char *tex_files[TEX_N] = { "assets/tex/blobrouge3.png", "assets/tex/blobvert1.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] = { "assets/tiles/tile_grass.png", "assets/tiles/tile_grass.png", @@ -45,7 +65,7 @@ int init_draw(){ SetTargetFPS(60); - char *cwd = GetApplicationDirectory(); + const char *cwd = GetApplicationDirectory(); puts(cwd); for(int i = 0; i < TEX_N; i++){ @@ -96,7 +116,15 @@ void draw_workers(Game *game){ Workers *workers = &game->workers; for(int i = 0; i < workers->worker_n; i++){ Worker *worker = &workers->worker_stack[i]; + Camera2D *cam = &game->camera; 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){ pos = Vector2Add(pos, machine_dict[worker->ai.mtype].offsets[worker->assign_n]); @@ -110,8 +138,15 @@ void draw_machines(Game *game){ Machines *machines = &game->machines; for(int i = 0; i < machines->machine_n; i++){ Machine *machine = &machines->machine_stack[i]; - float active_per = Remap((float)machine->active,0,255,0.2,1); - DrawRectangleV(machine->pos, (V2d){.x=64,.y=64}, Fade(BLUE,active_per)); + Camera2D *cam = &game->camera; + 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)); } } @@ -129,6 +164,27 @@ void draw_game(Game *game){ EndMode2D(); } +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); + draw_machines(game); + draw_workers(game); + EndScissorMode(); + EndMode2D(); +} + void draw(Game *game){ draw_widgets(game); diff --git a/src/draw.h b/src/draw.h index 9d3acbd..a69ecd7 100644 --- a/src/draw.h +++ b/src/draw.h @@ -22,4 +22,6 @@ Texture2D mkmap_tex(Map *map); void draw_game(Game *game); +void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box); + void draw(Game *game); diff --git a/src/game.c b/src/game.c index f93cdfd..8869685 100755 --- a/src/game.c +++ b/src/game.c @@ -15,9 +15,12 @@ #include "lua_inter.h" MachineDef machine_dict[] = { - {{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Coal, 4}, // MT_CoalMine - {{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Iron, 4}, // MT_IronMine - {{{0,0},{32,0},{0,32},{32,32}}, {R_Coal, R_Iron}, 2, R_Steel, 0}, // MT_Furnace + // MT_CoalMine + {{{0,0},{32,0},{0,32},{32,32}}, {}, 0, R_Coal, 4}, + // 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[] = { @@ -71,8 +74,8 @@ int init_game(Game *game){ int c_worker = 0; - for(int k = 0; k < 16; k++){ - for(int j = 0; j < 16; j++){ + for(int k = 0; k < 20; k++){ + for(int j = 0; j < 40; j++){ Machine *coal_mine = add_machine(&game->machines, (V2d){k*250+40,j*400+40}); coal_mine->type = MT_CoalMine; int coal_id = coal_mine->id; @@ -84,7 +87,7 @@ int init_game(Game *game){ // The adresses may have changed coal_mine = get_machine_from_id(&game->machines, coal_id); - iron_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++) add_worker(&game->workers, (V2d){.x=k*250+100,.y=j*400}); @@ -96,8 +99,6 @@ int init_game(Game *game){ 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++]); - if(!(furnace->id%1000)) - printf("%d\n",furnace->id); } } diff --git a/src/lua_inter.c b/src/lua_inter.c index cdf4a5c..988d4e1 100644 --- a/src/lua_inter.c +++ b/src/lua_inter.c @@ -16,10 +16,8 @@ Game *gl_game; typedef void (lua_affect_t)(lua_State*, void *, int); struct LuaAffect { - lua_affect_t *func; int arg; - }; struct TypeMap { @@ -353,7 +351,7 @@ static int lua_worker_newindex(lua_State *L){ return 1; } -// add_machine(x, y) +// add_machine(x, y) -> Machine static int lua_add_machine(lua_State *L){ lua_Number x = luaL_checknumber(L, 1); lua_Number y = luaL_checknumber(L, 2); @@ -369,12 +367,14 @@ static int lua_add_machine(lua_State *L){ 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){ int id = luaL_checkinteger(L, 1) & 0xFFFF; Machine *mach = get_machine_from_id(&gl_game->machines, id); - if(!mach) - return 0; + if(!mach){ + lua_pushnil(L); + return 1; + } Machine **buf = lua_newuserdatauv(L, sizeof(void*), 0); *buf = mach; luaL_newmetatable(L, "Machine"); @@ -383,7 +383,7 @@ static int lua_get_machine_from_id(lua_State *L){ return 1; } -// add_worker(x, y) +// add_worker(x, y) -> Machine static int lua_add_worker(lua_State *L){ lua_Number x = luaL_checknumber(L, 1); lua_Number y = luaL_checknumber(L, 2); @@ -394,6 +394,54 @@ static int lua_add_worker(lua_State *L){ *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) @@ -413,6 +461,9 @@ struct LuaFn exported_funcs[] = { {"add_machine", lua_add_machine}, {"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; diff --git a/src/main.c b/src/main.c index 5727f71..b216247 100644 --- a/src/main.c +++ b/src/main.c @@ -21,8 +21,8 @@ /* TODO * UI worker/machine : * map : -* - Textures : ~ -* - Affichage : +* - Textures : x +* - Génération : */ #if DEBUG diff --git a/src/map.c b/src/map.c index 11fd919..85149b1 100644 --- a/src/map.c +++ b/src/map.c @@ -23,10 +23,15 @@ int generate_coll(Game *game, int mapn){ 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)] = 1; - lmap->coll[(int)((pos.y+1) * lmap->width + pos.x)] = 1; - lmap->coll[(int)((pos.y+1) * lmap->width + pos.x+1)] = 1; + if(pos.x+1 < lmap->width) + lmap->coll[(int)(pos.y * 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; } diff --git a/src/mapgen.c b/src/mapgen.c new file mode 100644 index 0000000..f501547 --- /dev/null +++ b/src/mapgen.c @@ -0,0 +1,12 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include "config.h" +#include "types.h" +#include "mapgen.h" diff --git a/src/mapgen.h b/src/mapgen.h new file mode 100644 index 0000000..768e1a0 --- /dev/null +++ b/src/mapgen.h @@ -0,0 +1,13 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include "config.h" +#include "types.h" + +#pragma once diff --git a/src/ui.c b/src/ui.c index 05cf52b..689725d 100644 --- a/src/ui.c +++ b/src/ui.c @@ -206,6 +206,12 @@ void worker_widget_draw(Widget *widget, Game *game){ 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%%", @@ -232,15 +238,8 @@ Widget worker_widget = { .txt = "X", .handler = NULL }, - {.box = {55,90,140,140}, - .color_0 = GRAY, - .color_1 = BLACK, - .color_txt = WHITE, - .txt = "", - .handler = NULL - }, }, - .button_n = 2 + .button_n = 1 }; Widget machine_widget = {