Possibilité de pauser (P) + progrès UI

This commit is contained in:
attilavs2 2025-02-25 21:35:19 +01:00
parent 591977e84e
commit 5d4bccfd08
7 changed files with 191 additions and 37 deletions

View file

@ -1,6 +1,6 @@
OUTNAME = mtycoon
CFLAGS = -O2 -g -Wall -Wextra -I ./raylib/include -pipe
CFLAGS = -O0 -g -Wall -Wextra -I ./raylib/include -pipe
#linux
#LDFLAGS = -static -L./raylib/lib -lraylib -lm
#windows

View file

@ -10,6 +10,7 @@
#include "types.h"
#include "game.h"
#include "draw.h"
#include "ui.h"
#define TEX_N 9
@ -63,7 +64,12 @@ void draw_machines(Game *game){
}
void draw(Game *game){
BeginMode2D(game->camera);
draw_machines(game);
draw_workers(game);
EndMode2D();
draw_widgets(game);
DrawFPS(0,0);
}

View file

@ -1,5 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
@ -43,6 +44,7 @@ int init_game(Game *game){
srand(rand_seed);
game->paused = false;
game->ttime = 0;
game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1};
@ -133,23 +135,32 @@ int get_machine_localres(Machine *machine, int globalres){
return -1;
}
int mach_work_pos_cmpfunc(const void *a_, const void *b_){
const Worker *a = a_;
const Worker *b = b_;
return (int)(Vector2Distance(a->pos, b->pos)*1000);
}
Worker *get_worker_from_pos(Workers *workers, V2d pos){
Worker tmp = {.pos = pos};
Worker *match = bsearch(&tmp, workers->worker_stack, workers->worker_n,
sizeof(Worker), &mach_work_pos_cmpfunc);
Worker *match = NULL;
for(int i = 0; i < workers->worker_n; i++){
Worker *curr = &workers->worker_stack[i];
if(CheckCollisionPointRec(pos,
(Rectangle){.x=curr->pos.x,.y=curr->pos.y,.width=32,.height=32})){
match = curr;
break;
}
}
return match;
}
Machine *get_machine_from_pos(Machines *machines, V2d pos){
Machine tmp = {.pos = pos};
Machine *match = bsearch(&tmp, machines->machine_stack, machines->machine_n,
sizeof(Machine), &mach_work_pos_cmpfunc);
Machine *match = NULL;
for(int i = 0; i < machines->machine_n; i++){
Machine *curr = &machines->machine_stack[i];
if(CheckCollisionPointRec(pos,
(Rectangle){.x=curr->pos.x,.y=curr->pos.y,.width=64,.height=64})){
match = curr;
break;
}
}
return match;
}
@ -179,6 +190,9 @@ void update_worker(Game *game, Worker *worker){
if(ai->assigned_machine == MACH_NULL && ai->assigned_machine1 == MACH_NULL)
ai->status = S_Idle;
int localres;
Vector2 mov;
//TODO : Move !
switch(ai->status){
default:
@ -200,7 +214,7 @@ void update_worker(Game *game, Worker *worker){
}
break;
case S_Unloading:
int localres = get_machine_localres(mach[1], ai->carry_type);
localres = get_machine_localres(mach[1], ai->carry_type);
if(localres != (int)ai->carry_type){
ai->status = S_Idle;
break;
@ -224,7 +238,7 @@ void update_worker(Game *game, Worker *worker){
break;
case S_Transporting:
case S_Walking:
Vector2 mov = Vector2Scale(
mov = Vector2Scale(
Vector2Normalize(Vector2Subtract(ai->dest, worker->pos)),
2
);
@ -275,9 +289,13 @@ void update_machines(Game *game){
}
void update(Game *game){
game->ttime += GetFrameTime();
game->t_per_frame = 0;
if(game->paused)
return;
game->ttime += GetFrameTime();
while(game->ttime > TPS_TIME){
update_machines(game);
update_workers(game);

View file

@ -10,27 +10,11 @@
#include "types.h"
#include "game.h"
#include "draw.h"
#include "ui.h"
//Idées :
// - Drogues
void try_interface(Game *game, V2d pos){
DrawRectangleV(GetScreenToWorld2D(pos,game->camera), (V2d){16,16},PINK);
Machine *mach = get_machine_from_pos(&game->machines, pos);
if(mach){
return;
}
Worker *work = get_worker_from_pos(&game->workers, pos);
if(!work)
return;
return;
}
int init(Game *game){
int err = init_draw();
err |= init_game(game);
@ -50,7 +34,7 @@ bool keydown(int key){
void get_keys(Game *game){
V2d mouse = GetMousePosition();
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
try_interface(game, mouse);
try_interface(game, GetScreenToWorld2D(mouse,game->camera));
if(keydown(KEY_W))
game->camera.target.y -= 3;
@ -64,12 +48,18 @@ void get_keys(Game *game){
game->camera.zoom *= 1.1;
if(keydown(KEY_DOWN))
game->camera.zoom *= 1/1.1;
if(IsKeyPressed(KEY_P))
game->paused = !game->paused;
}
int main(){
Game game;
extern GUIInfo gui_info;
gui_info.zoom = &game.camera.zoom;
if(init(&game)){
printf("Failed to init game ! Exiting...\n");
clean(&game);
@ -79,7 +69,7 @@ int main(){
int c_worker = 0;
for(int k = 0; k < 42; k++){
for(int j = 0; j < 256; j++){
for(int j = 0; j < 1; j++){
Machine *coal_mine = add_machine(&game.machines, (V2d){k*250+40,j*400+40});
coal_mine->type = MT_CoalMine;
Machine *iron_mine = add_machine(&game.machines, (V2d){k*250+40,j*400+240});
@ -102,7 +92,6 @@ int main(){
while(!WindowShouldClose()){
BeginDrawing();
BeginMode2D(game.camera);
ClearBackground(BLACK);
@ -112,7 +101,6 @@ int main(){
draw(&game);
EndMode2D();
EndDrawing();
}

View file

@ -11,6 +11,8 @@
#define V2d Vector2
#define uint unsigned int
enum Status {
S_Idle = 0,
S_Transporting = 1,
@ -107,6 +109,8 @@ typedef struct {
Camera2D camera;
bool paused;
float ttime;
int t_per_frame;

74
src/ui.c Normal file
View file

@ -0,0 +1,74 @@
#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 "game.h"
#include "draw.h"
#include "ui.h"
void main_menu_draw(Widget *widget, Game *game){
printf("main mem draw\n");
widget->refresh = false;
}
void main_menu_do_key(Widget *widget, Game *game, int key){
}
Widget main_menu = {
.box = {.x = 0, .y = 0, .width = -1, .height = -1},
.draw = &main_menu_draw,
.do_key = &main_menu_do_key,
.refresh = true,
.capt_flags = CF_Mouse | CF_Keyboard,
.buttons = {}
};
GUIInfo gui_info = {{&main_menu}, 1, 0, NULL};
void draw_widgets(Game *game){
for(int i = 0; i < gui_info.widget_n; i++){
Widget *w = gui_info.widgets[i];
if(w->draw && w->refresh)
w->draw(w, game);
}
}
void widgets_treat_key(Game *game, int key){
for(int i = 0; i < WIDGET_N; i++){
Widget *w = gui_info.widgets[i];
if(!w->do_key)
continue;
w->do_key(w, game, key);
}
}
void try_interface(Game *game, V2d pos){
DrawRectangleV(pos, (V2d){16,16},PINK);
Machine *mach = get_machine_from_pos(&game->machines, pos);
if(mach){
printf("found machine %d\n",mach->id);
return;
}
Worker *work = get_worker_from_pos(&game->workers, pos);
if(!work)
return;
printf("found worker\n");
return;
}

64
src/ui.h Normal file
View file

@ -0,0 +1,64 @@
#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"
#pragma once
#define WIDGET_N 16
enum CaptureFlags {
CF_None = 0x0,
CF_Arrows = 0x2,
CF_Mouse = 0x4, //Mouse clicks
CF_Keyboard = 0x8
};
struct Widget;
typedef void (widget_filler_t)(struct Widget*, Game*);
typedef void (widget_dokey_t)(struct Widget*, Game*, int);
typedef void (button_handler_t)(void);
typedef struct {
Rectangle box;
Color color_0;
Color color_1;
int curr_select;
button_handler_t *handler;
} Button;
typedef struct Widget {
Rectangle box;
widget_filler_t *draw;
widget_dokey_t *do_key;
bool refresh;
int capt_flags;
Button buttons[10];
} Widget;
typedef struct {
Widget *widgets[WIDGET_N];
int widget_n;
int active_widget;
float *zoom;
} GUIInfo;
void draw_widgets(Game *game);
// World pos
void try_interface(Game *game, V2d pos);