From 471892d37483e9fad861c56c35b03565a9a21dd9 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Sun, 23 Oct 2022 06:52:24 -0700 Subject: [PATCH] Add gold as target --- gamestate.c | 33 ++++++++++++++++++++++++++++++- main.c | 13 +++++++++++-- types.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 95 insertions(+), 7 deletions(-) diff --git a/gamestate.c b/gamestate.c index ae7b260..16bd61c 100644 --- a/gamestate.c +++ b/gamestate.c @@ -343,6 +343,7 @@ void ser_player(char **out, struct Player *p) ser_V2(out, p->pos); ser_V2(out, p->vel); ser_float(out, p->spice_taken_away); + ser_float(out, p->goldness); // input ser_V2(out, p->movement); @@ -363,6 +364,7 @@ void des_player(char **in, struct Player *p, struct GameState *gs) des_V2(in, &p->pos); des_V2(in, &p->vel); des_float(in, &p->spice_taken_away); + des_float(in, &p->goldness); // input des_V2(in, &p->movement); @@ -387,6 +389,11 @@ void into_bytes(struct ServerToClient *msg, char *bytes, int *out_len, int max_l ser_int(&bytes, msg->your_player); LEN_CHECK(); + ser_float(&bytes, gs->time); + LEN_CHECK(); + + ser_V2(&bytes, gs->goldpos); + for (int i = 0; i < MAX_PLAYERS; i++) { ser_player(&bytes, &gs->players[i]); @@ -415,7 +422,6 @@ void from_bytes(struct ServerToClient *msg, char *bytes, int max_len) struct GameState *gs = msg->cur_gs; char *original_bytes = bytes; - // destroy and free all chipmunk destroy(gs); initialize(gs); @@ -423,6 +429,12 @@ void from_bytes(struct ServerToClient *msg, char *bytes, int max_len) des_int(&bytes, &msg->your_player); LEN_CHECK(); + des_float(&bytes, &gs->time); + LEN_CHECK(); + + des_V2(&bytes, &gs->goldpos); + LEN_CHECK(); + for (int i = 0; i < MAX_PLAYERS; i++) { des_player(&bytes, &gs->players[i], gs); @@ -478,10 +490,20 @@ struct Grid *closest_to_point_in_radius(struct GameState *gs, V2 point, float ra return NULL; } +static float hash11(float p) +{ + p = fract(p * .1031); + p *= p + 33.33; + p *= p + p; + return fract(p); +} + void process(struct GameState *gs, float dt) { assert(gs->space != NULL); + gs->time += dt; + // process input for (int i = 0; i < MAX_PLAYERS; i++) { @@ -489,6 +511,12 @@ void process(struct GameState *gs, float dt) if (!p->connected) continue; + if(V2length(V2sub(p->pos, gs->goldpos)) < GOLD_COLLECT_RADIUS) + { + p->goldness += 0.2; + gs->goldpos = (V2){.x = hash11(gs->time)*20.0f, .y = hash11(gs->time-13.6f)*20.0f}; + } + if (p->dobuild) { p->dobuild = false; // handle the input. if didn't do this, after destruction of hovered box, would try to build on its grid with grid_index... @@ -517,6 +545,7 @@ void process(struct GameState *gs, float dt) } p->spice_taken_away += 0.2f; grid_new(empty_grid, gs, p->build); + cpBodySetVelocity(empty_grid->body, v2_to_cp(p->vel)); box_new(&empty_grid->boxes[0], gs, empty_grid, (V2){0}); } else @@ -595,6 +624,8 @@ void process(struct GameState *gs, float dt) struct Grid *g = &gs->grids[p->currently_inhabiting_index]; p->pos = V2lerp(p->pos, grid_com(g), dt * 20.0f); cpBodyApplyForceAtWorldPoint(g->body, v2_to_cp(V2scale(p->movement, 5.0f)), v2_to_cp(grid_com(g))); + // bigger the ship, the more efficient the spice usage + p->spice_taken_away += dt*0.15f/(cpBodyGetMass(g->body)*2.0f)*V2length(p->movement); } if(p->spice_taken_away >= 1.0f) diff --git a/main.c b/main.c index 5ea38e0..7b8afa8 100644 --- a/main.c +++ b/main.c @@ -341,7 +341,7 @@ static void frame(void) if (myplayer != -1) { static float hand_reach_alpha = 1.0f; - hand_reach_alpha = lerp(hand_reach_alpha, hand_at_arms_length ? 1.0f : 0.0f, dt*5.0); + hand_reach_alpha = lerp(hand_reach_alpha, hand_at_arms_length ? 1.0f : 0.0f, dt * 5.0); sgp_set_color(1.0f, 1.0f, 1.0f, hand_reach_alpha); draw_circle(gs.players[myplayer].pos, MAX_HAND_REACH); } @@ -367,7 +367,10 @@ static void frame(void) continue; static float opacities[MAX_PLAYERS] = {1.0f}; opacities[i] = lerp(opacities[i], p->currently_inhabiting_index == -1 ? 1.0f : 0.1f, dt * 7.0f); - sgp_set_color(1.0f, 1.0f, 1.0f, opacities[i]); + Color col_to_draw = Collerp(WHITE, GOLD, p->goldness); + col_to_draw.a = opacities[i]; + + set_color(col_to_draw); sgp_push_transform(); float psize = 0.1f; sgp_draw_filled_rect(p->pos.x - psize / 2.0f, p->pos.y - psize / 2.0f, psize, psize); @@ -420,6 +423,12 @@ static void frame(void) } } + // gold target + set_color(GOLD); + sgp_draw_filled_rect(gs.goldpos.x, gs.goldpos.y,0.1f,0.1f); + + + sgp_set_color(1.0f, 1.0f, 1.0f, 1.0f); dbg_drawall(); diff --git a/types.h b/types.h index 8c7fd21..36a0c92 100644 --- a/types.h +++ b/types.h @@ -3,6 +3,7 @@ #define MAX_PLAYERS 4 #define BOX_SIZE 0.5f #define MAX_HAND_REACH 1.0f +#define GOLD_COLLECT_RADIUS 0.3f #define MAX_GRIDS 32 #define MAX_BOXES_PER_GRID 32 #define BOX_MASS 1.0f @@ -13,6 +14,8 @@ // including headers from headers bad #ifndef SOKOL_GP_INCLUDED +void sgp_set_color(float, float, float, float); + // @Robust use double precision for all vectors, when passed back to sokol // somehow automatically or easily cast to floats typedef struct sgp_vec2 @@ -51,14 +54,20 @@ typedef sgp_point P2; struct GameState { cpSpace *space; + + float time; + + V2 goldpos; + struct Player { bool connected; - + int currently_inhabiting_index; // is equal to -1 when not inhabiting a grid V2 pos; V2 vel; float spice_taken_away; // at 1.0, out of spice + float goldness; // how much the player is a winner // input V2 movement; @@ -68,7 +77,7 @@ struct GameState bool dobuild; int grid_index; } players[MAX_PLAYERS]; - + // if body or shape is null, then that grid/box has been freed // important that this memory does not move around, each box shape in it has a pointer to its grid struct, stored in the box's shapes user_data @@ -205,7 +214,12 @@ static V2 V2sub(V2 a, V2 b) static inline float clamp01(float f) { - return fmax(0.0f, fmin(f, 1.0f)); + return fmax(0.0f, fmin(f, 1.0f)); +} + +static float fract(float f) +{ + return f - floorf(f); } static float lerp(float a, float b, float f) @@ -220,4 +234,38 @@ static V2 V2lerp(V2 a, V2 b, float factor) to_return.y = lerp(a.y, b.y, factor); return to_return; -} \ No newline at end of file +} + +typedef struct Color +{ + float r, g, b, a; +} Color; + +static Color colhex(int r, int g, int b) +{ + return (Color){ + .r = (float)r / 255.0, + .g = (float)g / 255.0, + .b = (float)b / 255.0, + .a = 1.0f, + }; +} + +static Color Collerp(Color a, Color b, float factor) +{ + Color to_return = {0}; + to_return.r = lerp(a.r, b.r, factor); + to_return.g = lerp(a.g, b.g, factor); + to_return.b = lerp(a.b, b.b, factor); + to_return.a = lerp(a.a, b.a, factor); + + return to_return; +} + +static void set_color(Color c) +{ + sgp_set_color(c.r, c.g, c.b, c.a); +} + +#define WHITE (Color){.r=1.0f,.g=1.0f,.b=1.0f,.a=1.0f} +#define GOLD colhex(255, 215, 0) \ No newline at end of file