From 78c208d16aba126473b77f3fe068ce42b263be3a Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Sun, 6 Nov 2022 13:56:45 -0800 Subject: [PATCH] Add gold unlock --- gamestate.c | 31 +++++++++++++++++++++++-------- main.c | 24 +++++++++++++++--------- server.c | 21 ++++++++++++++------- types.h | 5 +++++ 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/gamestate.c b/gamestate.c index a7eaa9f..3abbba0 100644 --- a/gamestate.c +++ b/gamestate.c @@ -805,6 +805,7 @@ void ser_entity(SerState* ser, GameState* gs, Entity* e) if (e->is_box) { SER_VAR(&e->box_type); + SER_VAR(&e->is_explosion_unlock); ser_entityid(ser, &e->next_box); ser_entityid(ser, &e->prev_box); SER_VAR(&e->compass_rotation); @@ -1045,6 +1046,14 @@ bool possibly_use_energy(GameState* gs, Entity* grid, float wanted_energy) return false; } +void entity_ensure_in_orbit(Entity* e) +{ + cpVect pos = v2_to_cp(V2sub(entity_pos(e), SUN_POS)); + cpFloat r = cpvlength(pos); + cpFloat v = cpfsqrt(SUN_GRAVITY_STRENGTH / r) / r; + cpBodySetVelocity(e->body, cpvmult(cpvperp(pos), v)); +} + void process(GameState* gs, float dt) { assert(gs->space != NULL); @@ -1053,21 +1062,15 @@ void process(GameState* gs, float dt) gs->time += dt; // process input - for (int i = 0; i < MAX_PLAYERS; i++) + PLAYERS_ITER(gs->players, player) { - struct Player* player = &gs->players[i]; - if (!player->connected) - continue; Entity* p = get_entity(gs, player->entity); if (p == NULL) { p = new_entity(gs); create_player(gs, p); player->entity = get_id(gs, p); - cpVect pos = v2_to_cp(V2sub(entity_pos(p), SUN_POS)); - cpFloat r = cpvlength(pos); - cpFloat v = cpfsqrt(SUN_GRAVITY_STRENGTH / r) / r; - cpBodySetVelocity(p->body, cpvmult(cpvperp(pos), v)); + entity_ensure_in_orbit(p); } assert(p->is_player); @@ -1218,6 +1221,18 @@ void process(GameState* gs, float dt) if (!e->exists) continue; + if (e->is_explosion_unlock) + { + PLAYERS_ITER(gs->players, player) + { + Entity* player_entity = get_entity(gs, player->entity); + if (player_entity != NULL && V2length(V2sub(entity_pos(player_entity), entity_pos(e))) < GOLD_UNLOCK_RADIUS) + { + player->unlocked_bombs = true; + } + } + } + if (e->body != NULL) { cpVect p = cpvsub(cpBodyGetPosition(e->body), v2_to_cp(SUN_POS)); diff --git a/main.c b/main.c index 5082733..90684ba 100644 --- a/main.c +++ b/main.c @@ -303,10 +303,10 @@ static void ui(bool draw, float dt, float width, float height) { static float cur_opacity = 1.0f; - cur_opacity = lerp(cur_opacity, myentity() != NULL ? 1.0f : 0.0f, dt * 5.0f); - if (cur_opacity <= 0.01f) { - return; - } + cur_opacity = lerp(cur_opacity, myentity() != NULL ? 1.0f : 0.0f, dt * 5.0f); + if (cur_opacity <= 0.01f) { + return; + } if (draw) sgp_push_transform(); @@ -728,11 +728,17 @@ frame(void) Entity* e = &gs.entities[i]; if (!e->exists) continue; - if (e->is_grid) // grid drawing + // draw grid + if (e->is_grid) { Entity* g = e; BOXES_ITER(&gs, b, g) { + if (b->is_explosion_unlock) + { + set_color(colhexcode(0xfcba03)); + draw_circle(entity_pos(b), GOLD_UNLOCK_RADIUS); + } sgp_set_color(1.0f, 1.0f, 1.0f, 1.0f); // debug draw force vectors for thrusters #if 0 @@ -821,7 +827,7 @@ frame(void) { sgp_set_image(0, image_explosion); sgp_set_color(1.0f, 1.0f, 1.0f, 1.0f - (e->explosion_progresss / EXPLOSION_TIME)); - draw_texture_centered(e->explosion_pos, EXPLOSION_RADIUS*2.0f); + draw_texture_centered(e->explosion_pos, EXPLOSION_RADIUS * 2.0f); sgp_reset_image(0); } } @@ -846,9 +852,9 @@ frame(void) sgp_set_color(1.0f, 1.0f, 1.0f, 1.0f); dbg_drawall(); -} // world space transform end + } // world space transform end -} + } // UI drawn in screen space ui(true, dt, width, height); @@ -859,7 +865,7 @@ frame(void) sgp_end(); sg_end_pass(); sg_commit(); - } +} void cleanup(void) { diff --git a/server.c b/server.c index c6193ea..4b8317e 100644 --- a/server.c +++ b/server.c @@ -21,9 +21,17 @@ void server(void* data) initialize(&gs, entity_data, entities_size); Log("Allocated %zu bytes for entities\n", entities_size); - // unlock the explosive +#define BOX_AT(grid, pos) { Entity* box = new_entity(&gs); box_create(&gs, box, grid, pos); } + // space station with explosion unlock if (true) { + Entity* grid = new_entity(&gs); + grid_create(&gs, grid); + entity_set_pos(grid, (V2) { -10.0f, 0.0f }); + entity_ensure_in_orbit(grid); + Entity* explosion_box = new_entity(&gs); + box_create(&gs, explosion_box, grid, (V2) { 0 }); + explosion_box->is_explosion_unlock = true; } // one box policy @@ -46,12 +54,11 @@ void server(void* data) cpBodySetVelocity(grid->body, cpv(-0.1, 0.0)); cpBodySetAngularVelocity(grid->body, 1.0f); -#define BOX_AT(pos) { Entity* box = new_entity(&gs); box_create(&gs, box, grid, pos); } - BOX_AT(((V2) { 0 })); - BOX_AT(((V2) { BOX_SIZE, 0 })); - BOX_AT(((V2) { 2.0*BOX_SIZE, 0 })); - BOX_AT(((V2) { 2.0*BOX_SIZE, BOX_SIZE })); - BOX_AT(((V2) { 0.0*BOX_SIZE, -BOX_SIZE })); + BOX_AT(grid,((V2) { 0 })); + BOX_AT(grid,((V2) { BOX_SIZE, 0 })); + BOX_AT(grid,((V2) { 2.0*BOX_SIZE, 0 })); + BOX_AT(grid,((V2) { 2.0*BOX_SIZE, BOX_SIZE })); + BOX_AT(grid,((V2) { 0.0*BOX_SIZE, -BOX_SIZE })); } if (enet_initialize() != 0) diff --git a/types.h b/types.h index 3e8489e..8bbdee7 100644 --- a/types.h +++ b/types.h @@ -29,6 +29,7 @@ #define EXPLOSION_DAMAGE_PER_SEC 10.0f #define EXPLOSION_RADIUS 1.0f #define EXPLOSION_DAMAGE_THRESHOLD 0.2f // how much damage until it explodes +#define GOLD_UNLOCK_RADIUS 1.0f #define TIMESTEP (1.0f / 60.0f) // not required to simulate at this, but this defines what tick the game is on #define TIME_BETWEEN_INPUT_PACKETS (1.0f / 20.0f) @@ -169,6 +170,7 @@ typedef struct Entity // boxes bool is_box; enum BoxType box_type; + bool is_explosion_unlock; EntityID next_box; EntityID prev_box; // doubly linked so can remove in middle of chain enum CompassRotation compass_rotation; @@ -207,6 +209,8 @@ typedef struct GameState EntityID free_list; } GameState; +#define PLAYERS_ITER(players, cur) for(Player * cur = players; cur < players+MAX_PLAYERS; cur++) if(cur->connected) + #define PI 3.14159f // returns in radians @@ -264,6 +268,7 @@ V2 entity_pos(Entity* e); void entity_set_rotation(Entity* e, float rot); void entity_set_pos(Entity* e, V2 pos); float entity_rotation(Entity* e); +void entity_ensure_in_orbit(Entity* e); #define BOX_CHAIN_ITER(gs, cur, starting_box) for (Entity *cur = get_entity(gs, starting_box); cur != NULL; cur = get_entity(gs, cur->next_box)) #define BOXES_ITER(gs, cur, grid_entity_ptr) BOX_CHAIN_ITER(gs, cur, (grid_entity_ptr)->boxes)